【问题标题】:Bootstrap 4: Make an object inside of a container column extend to the viewport edgeBootstrap 4:使容器列内的对象延伸到视口边缘
【发布时间】:2019-02-22 01:05:22
【问题描述】:

任何使列内的 div 能够扩展到右(或左)边缘的技巧?我需要坚持使用固定容器而不是流体容器。此外,由于 CMS,“扩展块”需要保留在标记中(不能是 CSS 伪元素)。

https://codepen.io/mwmd/pen/eLPxOX

<div class="container">
  <div class="row">
    <div class="col-sm-7">
      <p>How can I get that div to extend to viewport right while maintaining its left edge to the Bootstrap column?</p>
    </div>
    <div class="col-sm-5">
      <div class="extend-me">
        <img src="https://via.placeholder.com/350x150"/>
      </div>
    </div>
  </div>
</div>

.container {
  background-color: #999;
}
.extend-me {
  background-color: darkred;
  height: 300px;
}
.extend-me img {
  object-fit: cover;
  width: 100%;
  height: 300px;
}

没有回答 Extend an element beyond the bootstrap container 因为这个解决方案使用了伪元素。

没有回答 Extend bootstrap row outside the container 因为此解决方案仅适用于 50% 50% 的拆分。

【问题讨论】:

    标签: css bootstrap-4


    【解决方案1】:

    HTML:在带有“extend-me”类的 div 上,还添加“row”类 CSS:要将内部 div 向右扩展,请将 margin-left 添加到 0 .extend-me { margin-left: 0;}

    sample

    <div class="container yourOverallContainer">
        <div class="row">
            <div class="col-sm-7">
                <h1> Stack overflow question </h1>
            </div>
        </div>
    </div>
    
    <div class="container-fluid">
            <div class="row">
              <div class="col-sm-7 thisToBeAlignedToContainer">
                <p>How can I get that div to extend to viewport right while maintaining its left edge to the Bootstrap column?</p>
              </div>
              <div class="col-sm-5">
                <div class="extend-me row">
                  <img src="https://via.placeholder.com/350x150"/>
                </div>
              </div>
            </div>
          </div>
    
    
    <div class="container">
        <div class="row">
            <footer>
                <p id='feedback'> without jQuery and .container-fluid, yet completely responsive will be interesting... </p>
              </footer>
        </div>
      </div>
    
    
    .container {
      background-color:#c9efb1;
    }
    
    .container-fluid {
      background-color: #fff;
    }
    
    .extend-me {
      background-color: darkred;
      height: 300px;
    }
    .extend-me img {
      object-fit: cover;
      width: 100%;
      height: 300px;
    }
    
    
    
    mWilson();
    
        $(window).resize(function(){
            mWilson();
        });
    
        function mWilson(){
             $('.thisToBeAlignedToContainer').css('padding-left', $('.yourOverallContainer').css('margin-left'));
        } 
    

    【讨论】:

    • 谢谢@Alqbal,但这不太正确。我的错是没有更清楚。我刚刚添加了一个参考图像来阐明我想要实现的目标。看看“extend-me”需要如何到达视口边缘?
    • @MattWilson 立即查看
    【解决方案2】:

    试试这个codepen:https://codepen.io/anon/pen/EedrQV

    我使用以下 css 调整了容器元素:

    .extend-container {
      margin-right: 0;
      margin-left: auto;
    }
    

    这会将整个容器向右移动,因此您可能需要根据您希望它们的宽度调整 col-sm 类。

    【讨论】:

    • Thx @souzan 但是让整个容器向右移动是行不通的——它需要保持居中。我刚刚添加了一张参考图片以帮助使问题更加清晰。
    【解决方案3】:

    这是一个有效的代码笔:https://codepen.io/migli/pen/QWvqOeM

    不使用绝对定位,只使用 css --root 变量和 css 计算:

    /* HTML
    -------------------------------------------------- */
    
    <div class="container">
        <div class="row">
            <div class="col">
                <h4>This is a column</h4>
            </div>
        </div>
        <div class="col full-width">
            <h4>This column is 100vw</h4>
        </div>
    </div>
    
    /* CSS
    -------------------------------------------------- */
    
    :root {
        --gutter: 15px;
        --container-width: 100%;
        --full-width-margin: calc(
            (-100vw + var(--container-width)) / 2 - var(--gutter)
        );
    }
    
    @media (min-width: 576px) {
        :root {
            --container-width: 540px;
            --full-width-margin: calc(
                (-100vw + var(--container-width)) / 2 - var(--gutter)
            );
        }
    }
    
    @media (min-width: 768px) {
        :root {
            --container-width: 720px;
            --full-width-margin: calc(
                (-100vw + var(--container-width)) / 2 - var(--gutter)
            );
        }
    }
    
    @media (min-width: 992px) {
        :root {
            --container-width: 960px;
            --full-width-margin: calc(
                (-100vw + var(--container-width)) / 2 - var(--gutter)
            );
        }
    }
    
    @media (min-width: 1200px) {
        :root {
            --container-width: 1140px;
            --full-width-margin: calc(
                (-100vw + var(--container-width)) / 2 - var(--gutter)
            );
        }
    }
    
    .full-width {
        width: 100vw;
        max-width: none;
        margin-left: var(--full-width-margin);
        margin-right: var(--full-width-margin);
    }
    

    【讨论】:

    • 感谢您的贡献。我不得不对此投反对票,因为它没有解决最初的问题/问题。
    • 也感谢您的贡献,但我不明白。最初的问题是如何将容器内的对象延伸到视口边缘,这正是我的答案。容器内的 100vw 行。
    • 当我查看您的 Codepen 时,您的“扩展”对象变为全 VW,但上面的问题是寻找可以在一侧对齐网格然后延伸到另一侧的视口边缘的东西边...所以只能向一个方向延伸。
    猜你喜欢
    • 2022-06-15
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2020-06-02
    相关资源
    最近更新 更多