I’m creating a web site that fits to the screen with WordPress.
When the site owner logs in, the admin bar will appear but it adds the following style:
html{ margin-top: 28px !important; }
This causes a vertical scroll bar to appear. Is there any way to fix this using just CSS?
Someone had a similar issue but he got no answer.
My relevant html structure:
<html>
<body>
<div id="page">
<div class="site-main" id="main">
<div class="content-area" id="primary">
<div role="main" class="site-content" id="content">
</div><!-- #content .site-content -->
</div><!-- #primary .content-area -->
</div><!-- #main .site-main -->
</div><!-- #page -->
<div id="wpadminbar">
</div>
</body>
</html>
And relevant CSS:
html, body, #page {
width: 100%;
height: 100%;
min-width: 350px;
margin: 0;
padding: 0;
}
#main {
height: 100%;
}
#primary {
float: right;
width: 100%;
margin-left: -200px;
height: 100%;
}
#content {
margin-left: 250px;
height: 100%;
}
For the admin bar:
#wpadminbar {
height: 28px;
left: 0;
min-width: 600px;
position: fixed;
top: 0;
width: 100%;
z-index: 99999;
}
I’ve tried using (negative) margins and paddings, also setting the admin bar’s position
to absolute
instead of fixed
but no luck.
2022 here.
I recently noticed that WordPress has been setting a relevant CSS variable on the front-end when logged in as an admin:
This is very convenient, as it allows you to do things like this (making a hero element as high as the viewport, but subtracting the height of the admin bar):
There is a slight issue with that, though: If youâre not signed in as an admin, the CSS variable is not set, and this can break things like calculations.
This can be fixed by checking for a body class that WordPress also sets:
â¦but this can get messy rather quickly, so I came up with a little one-size-fits-all solution that Iâll probably be using a lot:
This allows me to do things like this, and not having to worry about whether the user is logged in or not:
Look into
wordpress/wp-includes/class-wp-admin-bar.php
at the beginning, and you will find this. Watch closely the comments for the actual answer:wordpress/wp-includes/admin-bar.php
contains the default implementation of_admin_bar_bump_cb
:In your php code (on the page where you DON’T want the admin bar to appear), just add the following:
See here: http://davidwalsh.name/hide-admin-bar-wordpress
Try following: