【问题标题】:How to position flexbox elements [duplicate]如何定位flexbox元素[重复]
【发布时间】:2019-02-21 22:46:44
【问题描述】:

我正在制作一个使用 flex 类构建的网格系统。这是我第一次使用 flex,所以我不太确定它是如何运作的。

到目前为止我制作的网格 is looking like this.

我希望 flexbox item 6 看起来像下图:

目前第 5 项和第 6 项各占 50% 的宽度

item5 和 item6 的宽度为 50%。我似乎无法改变这一点。我希望第 5 项占 66% 左右,第 6 项占 33%,但元素周围仍有余量。

谁能看出我做错了什么?

<style>

.d-flex {
  display: -ms-flexbox!important;
  display: flex!important
}

.d-inline-flex {
    display: -ms-inline-flexbox!important;
    display: inline-flex!important
}
.flex-wrap {
    -ms-flex-wrap: wrap!important;
    flex-wrap: wrap!important
}
.bg-img {
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat; }

/* CTA Buttons */

.btn-top-left {
  position: absolute;
  left: 10%;
  top: 10%
}

.btn-top-right {
  
}

.btn-center {
  position: absolute;
  left: 50%;
  top: 50%
}

.btn-bottom-left {
  
}

.btn-bottom-right {
  
}


/* :: Set overlay over image and placement of text */
.single-welcome-post {
  position: relative;
  z-index: 1; }
  .single-welcome-post::after {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.4);
    content: '';
    z-index: 5; }
  .single-welcome-post .welcome-post-content {
    position: absolute;
    z-index: 100;
    left: 50px;
    bottom: 50px;
    right: 50px; }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .single-welcome-post .welcome-post-content {
        left: 20px;
        bottom: 20px;
        right: 20px; } }
    @media only screen and (max-width: 767px) {
      .single-welcome-post .welcome-post-content {
        left: 15px;
        bottom: 15px;
        right: 15px; } }
  .single-welcome-post.style-2 {
    height: 645px; }
 

 

.hero-area {
  position: relative;
  z-index: 0;
  background-color: #ffffff;
  padding: 5px; }
  .hero-area .hero-single-section {
    position: relative;
    z-index: 0;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 50%;
    flex: 0 0 50%;
    max-width: 50%;
    width: 50%; }
    @media only screen and (max-width: 767px) {
      .hero-area .hero-single-section {
        -webkit-box-flex: 0;
        -ms-flex: 0 0 100%;
        flex: 0 0 100%;
        max-width: 100%;
        width: 100%; } }
  .hero-area .item1 {
    height: 700px;
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item1 {
        height: 600px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item1 {
        height: 500px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item1 {
        height: 350px; } }
  .hero-area .item2 {
    height: 340px;
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item2 {
        height: 290px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item2 {
        height: 240px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item2 {
        height: 200px; } }
  .hero-area .item3 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(50% - 10px);
    width: calc(50% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item3 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item3 {
        height: 250px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item3 {
        height: 200px; } }
  .hero-area .item4 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(50% - 10px);
    width: calc(50% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item4 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item4 {
        height: 250px; } }
    @media only screen and (max-width: 767px) {
      .hero-area .item4 {
        height: 200px; } }

  .hero-area .item5 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(130% - 10px);
    width: calc(130% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item5 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item5 {
        height: 250px;
        max-width: calc(100% - 10px); } }
    @media only screen and (max-width: 767px) {
      .hero-area .item5 {
        height: 200px; 
        max-width: calc(100% - 10px); } }

.hero-area .item6 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(100% - 10px);
    width: calc(100% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item6 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item6 {
        height: 250px;
        max-width: calc(100% - 10px); } }
    @media only screen and (max-width: 767px) {
      .hero-area .item6 {
        height: 200px; 
        max-width: calc(100% - 10px); } }

.hero-area .item7 {
    height: 350px;
    -webkit-box-flex: 0;
    -ms-flex: 0 0 calc(50% - 10px);
    flex: 0 0 calc(50% - 10px);
    max-width: calc(100% - 10px);
    width: calc(100% - 10px);
    margin: 5px; }
    @media only screen and (min-width: 992px) and (max-width: 1199px) {
      .hero-area .item7 {
        height: 300px; } }
    @media only screen and (min-width: 768px) and (max-width: 991px) {
      .hero-area .item7 {
        height: 250px;
        max-width: calc(100% - 10px); } }
    @media only screen and (max-width: 767px) {
      .hero-area .item7 {
        height: 200px; 
        max-width: calc(100% - 10px); } }



.single-blog-post {
  position: relative;
  z-index: 1; }
  .single-blog-post .blog-thumb {
    position: relative;
    z-index: 1; }
    .single-blog-post .blog-thumb img {
      width: 100%; }
  .single-blog-post .blog-content .post-tag {
    color: #a6a6a6;
    text-transform: uppercase;
    font-size: 12px;
    display: block;
    font-weight: 700;
    margin-bottom: 5px; }
    .single-blog-post .blog-content .post-tag:hover, .single-blog-post .blog-content .post-tag:focus {
      color: #ff1662; }

    /* Set font-size */
    @media only screen and (max-width: 767px) {
      .single-blog-post .blog-content .post-tag {
        font-size: 10px; } }
  .single-blog-post .blog-content .post-title {
    font-weight: 600;
    font-size: 30px;
    display: block;
    color: #282828;
    margin-bottom: 10px; }
    
  

    /* Align text */
    @media only screen and (max-width: 767px) {
      .single-blog-post .blog-content .post-title {
        font-size: 16px; } }
    .single-blog-post .blog-content .post-title:hover, .single-blog-post .blog-content .post-title:focus {
      color: #ff1662; }
  .single-blog-post .blog-content .post-meta {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex; }

    /* Set space between text elements*/
    .single-blog-post .blog-content .post-meta a {
      color: #ffffff;
      margin-right: 15px;
      font-size: 11px;
      line-height: 1;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: end;
      -ms-flex-align: end;
      -ms-grid-row-align: flex-end;
      align-items: flex-end; }
    

      /* Set color and placement on text*/
      @media only screen and (max-width: 767px) {
        .single-blog-post .blog-content .post-meta a {
          font-size: 8px;
          margin-right: 5px; } }
      .single-blog-post .blog-content .post-meta a img {
        margin-right: 10px; }
      .single-blog-post .blog-content .post-meta a:last-child {
        margin-right: 0; }
      .single-blog-post .blog-content .post-meta a:hover, .single-blog-post .blog-content .post-meta a:focus {
        color: #ff1662; }
  .single-blog-post.white .blog-content .post-title,
  .single-blog-post.white .blog-content .post-tag,
  .single-blog-post.white .blog-content .post-meta a {
    color: #ffffff; }
    .single-blog-post.white .blog-content .post-title:hover, .single-blog-post.white .blog-content .post-title:focus,
    .single-blog-post.white .blog-content .post-tag:hover,
    .single-blog-post.white .blog-content .post-tag:focus,
    .single-blog-post.white .blog-content .post-meta a:hover,
    .single-blog-post.white .blog-content .post-meta a:focus {
      color: #008ebe; }
  .single-blog-post.style2 .blog-content .post-title {
    font-size: 24px; }



/* Sidebar Nyhedsbrev */
.sidebar-area {
  position: relative;
  z-index: 1; }
  @media only screen and (min-width: 768px) and (max-width: 991px) {
    .sidebar-area {
      margin-top: 100px; } }
  @media only screen and (max-width: 767px) {
    .sidebar-area {
      margin-top: 100px; } }

.single-widget-area {
  position: relative;
  z-index: 1; }
  .single-widget-area.newsletter-widget {
    background-color: #f0f4f8;
    padding: 50px 20px;
    text-align: center; }
    .single-widget-area.newsletter-widget h4 {
      margin-bottom: 25px; }
    .single-widget-area.newsletter-widget form input {
      text-align: center;
      width: 100%;
      background-color: #d4dfe3;
      height: 54px;
      font-size: 12px;
      font-style: italic;
      color: #4c4c4c;
      border: none;
      margin-bottom: 15px; }
    .single-widget-area.newsletter-widget p {
      font-size: 12px;
      font-style: italic; }
  .single-widget-area.news-widget h4 {
    margin-bottom: 60px; }
</style>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<!-- ##### Hero Area Start ##### -->
    <div class="hero-area d-flex flex-wrap">

        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item1" style="background-image: url(http://vouzalis.dk/stack/1.jpg);">
                <!-- Content -->
                <a href="https://www.youtube.com/watch?v=0aNNYEUARAk" class="video-play-btn"><i class="fa fa-play"></i></a>
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Watch a tiny cat taking a bath</a>
                            <div class="post-meta">
                                <a href="#">Here is a text about the picture</a>
                            </div>
                            <button href="#" class="btn btn-success btn-center">Read More</button>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>

        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item2" style="background-image: url(http://vouzalis.dk/stack/2.jpg);">
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Spain: Take a virtual tour</a>
                            <div class="post-meta">
                                <a href="#"> Her er en tekst</a>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>

            <div class="hero-second-section d-flex flex-wrap">
                <!-- Single Welcome Post -->
                <div class="single-welcome-post bg-img item3" style="background-image: url(http://vouzalis.dk/stack/3.jpg);">
                    <!-- Content -->
                    <div class="welcome-post-content">
                        <!-- Single Blog Post -->
                        <div class="single-blog-post style2 white">
                            <div class="blog-content">
                                <a href="#" class="post-tag">Featured</a>
                                <a href="#" class="post-title">5 Tips to create your garden</a>
                            </div>
                        </div>
                    </div>
                    
                </div>

                <!-- Single Welcome Post -->
                <div class="single-welcome-post bg-img item4" style="background-image: url(http://vouzalis.dk/stack/4.jpg);">
                    <!-- Content -->
                    <div class="welcome-post-content">
                        <!-- Single Blog Post -->
                        <div class="welcome-post-content">
                        <!-- Single Blog Post -->
                            <div class="single-blog-post style2 white">
                                <div class="blog-content">
                                    <a href="#" class="post-tag">Featured</a>
                                    <a href="#" class="post-title">10 Movies you need to see</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item5" style="background-image: url(http://vouzalis.dk/stack/1.jpg);">
                <!-- Content -->
                <a href="https://www.youtube.com/watch?v=0aNNYEUARAk" class="video-play-btn"><i class="fa fa-play"></i></a>
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Watch a tiny cat taking a bath</a>
                            <div class="post-meta">
                                <a href="#">Here is a text about the picture</a>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
        <div class="hero-single-section">
            <!-- Single Welcome Post -->
            <div class="single-welcome-post bg-img item6" style="background-image: url(http://vouzalis.dk/stack/2.jpg);">
                <!-- Content -->
                <a href="https://www.youtube.com/watch?v=0aNNYEUARAk" class="video-play-btn"><i class="fa fa-play"></i></a>
                <!-- Content -->
                <div class="welcome-post-content">
                    <!-- Single Blog Post -->
                    <div class="single-blog-post white">
                        <div class="blog-content">
                            <a href="#" class="post-tag">Featured</a>
                            <a href="#" class="post-title">Watch a tiny cat taking a bath</a>
                            <div class="post-meta">
                                <a href="#">Here is a text about the picture</a>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
    </div>
<!-- ##### Hero Area end ##### -->

【问题讨论】:

  • 我正在使用 flex 类构建一个网格系统 为什么不直接使用网格属性呢?无论哪种方式,您都将 .hero-areamax-width 设置为 50%,那么它和该容器中的项目将如何达到 66%?
  • 但是无论你改变50%,item5的位置还是一样的,所以它们是重叠的。
  • 如果您想要网格布局,请使用 css 网格,这将使您的工作比使用 flexbox 创建您想要的网格更容易。
  • 谢谢。我可以看到其他人也建议这样做。

标签: html twitter-bootstrap css flexbox


【解决方案1】:

我不会使用宽度百分比。对于这个特定的屏幕宽度,我会将最后 2 个元素包装到另一个 div 中,使其具有弹性,然后将 flex:1; 添加到第一个元素,将 flex:2; 添加到第二个元素。

这也将使宽度响应。底线是,避免百分比,将flex: 属性与弹性框项目一起使用。

.wrapper {
  width: 100%;
  min-height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.one {
  min-height: 150px;
  flex: 2;
  background: red;
}

.two {
  min-height: 150px;
  flex: 1;
  background: green;
}
<div class="wrapper">
  
  <div class="one"></div>
  <div class="two"></div>
  
</div>

【讨论】:

  • 正是我的回答,jsfiddle.net/a1k358ud
  • 感谢您的回答。我之前没有使用过 CSS 网格,所以我需要开始学习它。我很高兴,因为我设法制作了 flexbox,而不是一直使用 Bootstrap。
  • Css Grid 和 FlexBox 是两个不同的东西。 Grid 是最近才添加到 CSS 中的,但我个人更喜欢 FlexBox 并且在任何地方都使用它。一开始有点棘手,但是一旦你抓住了,你就不会回去了。如果你曾经尝试过 React-Native,你会很高兴知道它默认使用 FlexBox。
  • @Varin "recently" 是一个相对术语... CSS Grid 布局已经在 since 2012 左右。
  • @TylerH 是的,你是对的。它直到最近才被浏览器广泛支持,这在大多数情况下意味着由于兼容性,可以在生产中使用它。
【解决方案2】:

你可以像这样在引导程序中解决这个问题

.vh-100 {
height:100vh
}

.vh-50 {
height:50vh
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
    <div class="col-6 bg-dark vh-100">
    </div>

    <div class="col-6 vh-100 bg-danger">
        <div class="row">
            <div class="col-6 vh-50 bg-primary">
            </div>
            <div class="col-6 vh-50 bg-warning">
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-8 bg-success vh-100">
    </div>

    <div class="col-4 vh-100 bg-info">
    </div>
</div>

【讨论】:

  • 您好,欢迎来到 Stack Overflow;请添加一些解释,说明此代码为何/如何解决问题;不要让 OP 猜测有什么不同以及这些更改为何起作用。
猜你喜欢
  • 1970-01-01
  • 2017-12-17
  • 2019-05-18
  • 1970-01-01
  • 2017-08-20
  • 2018-05-27
  • 1970-01-01
  • 2021-07-23
  • 2016-11-08
相关资源
最近更新 更多