如何修复 WordPress 管理栏破坏 100% 高度

问题描述 投票:0回答:5

我正在使用 Wordpress 创建一个适合屏幕的网站。

当网站所有者登录时,会出现管理栏,但它添加了以下样式:

html{ margin-top: 28px !important; }

这会导致出现垂直滚动条。有没有什么方法可以只使用 CSS 来解决这个问题?

有人有类似的问题但他没有得到答案。

我的相关html结构:

<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>

以及相关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%;
}

对于管理栏:

#wpadminbar {
  height: 28px;
  left: 0;
  min-width: 600px;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 99999;
}

我尝试过使用(负)边距和填充,还将管理栏的

position
设置为
absolute
而不是
fixed
,但没有运气。

html css wordpress layout viewport
5个回答
5
投票

2022在这里。

我最近注意到,当以管理员身份登录时,WordPress在前端设置了相关的CSS变量:

html {
  --wp-admin--admin-bar--height: 32px;
  scroll-padding-top: var(--wp-admin--admin-bar--height);
}

@media screen and (max-width: 782px)
  html {
    --wp-admin--admin-bar--height: 46px;
  }
}

这非常方便,因为它允许您执行类似的操作(使英雄元素与视口一样高,但减去管理栏的高度):

.hero {
  min-height: calc(100vh - var(--wp-admin--admin-bar--height));
}

不过,这有一个小问题:如果您没有以管理员身份登录,则不会设置 CSS 变量,这可能会破坏计算等内容。

这可以通过检查 WordPress 也设置的主体类来修复:

.hero {
  min-height: 100vh;
}

body.admin-bar .hero {
  min-height: calc(100vh - var(--wp-admin--admin-bar--height));
}

…但这很快就会变得混乱,所以我想出了一个通用的解决方案,我可能会经常使用它:

body:not(.admin-bar) {
  --wp-admin--admin-bar--height: 0px;
}

这允许我做这样的事情,而不必担心用户是否登录:

.site-header {
  top: var(--wp-admin--admin-bar--height);
}

3
投票

查看开头的

wordpress/wp-includes/class-wp-admin-bar.php
,你会发现这一点。仔细看评论才能得到真正的答案:

if ( current_theme_supports( 'admin-bar' ) ) {
  /**
   * To remove the default padding styles
   * from WordPress for the Toolbar,
   * use the following code:
   * add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );
   */
  $admin_bar_args = get_theme_support( 'admin-bar' );
  $header_callback = $admin_bar_args[0]['callback'];
}

if ( empty($header_callback) )
  $header_callback = '_admin_bar_bump_cb';

add_action('wp_head', $header_callback);

wordpress/wp-includes/admin-bar.php
包含
_admin_bar_bump_cb
的默认实现:

/**
 * Default admin bar callback.
 *
 * @since 3.1.0
 */
function _admin_bar_bump_cb() { ?>
<style type="text/css" media="screen">
  html { margin-top: 28px !important; }
  * html body { margin-top: 28px !important; }
</style>
<?php
}

1
投票

在您的 php 代码中(在您不希望出现管理栏的页面上),只需添加以下内容:

add_filter('show_admin_bar', '__return_false');

请参阅此处:http://davidwalsh.name/hide-admin-bar-wordpress


0
投票

尝试以下操作:

add_action('get_header', 'fix_adminbar');

function fix_adminbar()
{
    if (is_admin_bar_showing()) {
        remove_action('wp_head', '_admin_bar_bump_cb');
        add_action(
            'wp_head', function () {
            ob_start();
            _admin_bar_bump_cb();
            $code = ob_get_clean();
            $code = str_replace('margin', 'padding', $code);
            $code = preg_replace('/{/', '{ box-sizing: border-box;', $code, 1);
            echo $code;
        }
        );
    }
}```

0
投票

请尝试此解决方案。它在我的 WP 主题上运行正常。

#home-slider {
    height: 100vh;
    min-height: 500px; /* You can adjust the minimum height */
    display: flex;
    align-items: center; /* If you want to vertically center the content */
    justify-content: center; /* If you want to horizontally center the content */
}

/* For desktop devices */
@media (min-width: 768px) {
    body.logged-in #home-slider {
        height: calc(100vh - 32px);
    }
}

/* For devices with a width up to 600px */
@media (max-width: 600px) {
    body.logged-in #home-slider {
        height: calc(100vh - 46px);
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.