【问题标题】:How can I create a full-width background inside a fixed container with bootstrap?如何使用引导程序在固定容器内创建全角背景?
【发布时间】:2018-08-25 18:46:40
【问题描述】:

我想创建一个全角背景颜色来显示我网站上一个页面的子部分。类似于此设计中的“我们的价值观”部分,其中 BG 为灰色:https://www.w3schools.com/bootstrap/trybs_theme_company_full.htm

我的主题在页眉中声明 .container 并在页脚中关闭该 .container。

我要编辑的页面是这样的

<div class="content-wrap">
<div class="blog-top">test text - no bg color</div>
<!--This is where I want the full-width background color to start-->
    <div id="primary" class="content-area content-area-arch<?php echo $zillah_sidebar_show !== true ? ' content-area-with-sidebar' : ''; ?>">

        <main id="main" class="site-main" role="main">

            <?php zillah_hook_index_top(); ?>

        <?php
            /*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
            query_posts( array ( 'category_name' => 'featured', 'posts_per_page' => 24, 'paged' => $paged) );


            ?>

            <?php
            if ( have_posts() ) :
            while ( have_posts() ) :

                the_post();

                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */

                $alternative = $zillah_alternative_layout == true ? $zillah_alternative_layout : '-alternativeblog';
                get_template_part( 'template-parts/content' . $alternative , get_post_format() );

            endwhile;

            the_posts_navigation();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif;
        ?>

            <?php zillah_hook_index_bottom(); ?>
        </main><!-- #main -->

    </div><!-- #primary -->
    <!--This is where I want the full-width background color to start-->


</div><!-- .content-wrap -->
<?php zillah_hook_index_after(); ?>

我已经注明了我想从哪里开始全角背景颜色。有没有办法在我的 .container 中实现下一个 .container-fluid ,或者是否有关于如何在 WordPress / bootstrap 中自定义页面的这个特定子部分完成这个全角背景的建议?

感谢您的帮助!

【问题讨论】:

    标签: css wordpress twitter-bootstrap


    【解决方案1】:

    这是设置全宽背景颜色的一种方法。使用:before 伪元素通过设置height:autowidth: 100vw 创建绝对定位的全宽背景。您可以设置图像或颜色背景。我会将背景代码放在一个类中,这样您就可以将其附加到任何居中的块级元素上,而无需修改 dom 或 Bootstrap 网格结构。

    --添加html { overflow-x: hidden; }以避免100vw背景元素导致水平滚动条。

    .bg-full {
      position: relative;
    }
    
    .bg-full:before {
      content: '';
      position: absolute;
      top: 0;
      left: 50%;
      transform: translateX( -50%);
      height: 100%;
      width: 100vw;
      background: red;
    }
    
    html {
      overflow-x: hidden;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <div class="container">
      <div class="row">
        <div class="col">
          Row 1
        </div>
      </div>
      <div class="row bg-full">
        <div class="col">
          Row 2
        </div>
      </div>
      <div class="row">
        <div class="col">
          Row 3
        </div>
      </div>
    </div>
    <div class="container bg-full">
      <div class="row">
        <div class="col">
          Row 4
        </div>
      </div>
    </div>

    【讨论】:

    • 小心width: 100vw - 我自己最近也遇到了这个问题。如果有垂直滚动条,100vw 会假装它不存在,从而导致出现水平滚动条。
    • 感谢@Scoots,添加html { overflow-x: hidden; } 以避免由100vw 背景元素引起的水平滚动条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多