简体   繁体   English

Flexbox 问题 - 高度不相等

[英]Flexbox issue - heights not equal

I have created a flex box, please see codepen link here:- https://codepen.io/scottYg55/pen/zYYWbJG .我创建了一个弹性盒子,请在此处查看 codepen 链接:- https://codepen.io/scottYg55/pen/zYYWbJG

I want the pink backgrounds of this flexbox to be 50% height and the background image 50% height too, so it looks more of a grid system.我希望这个 flexbox 的粉红色背景为 50% 高度,背景图像也为 50% 高度,所以它看起来更像是一个网格系统。

For some reason the pink BG is not expanding its width no matter what I try, I just want the images and Pink BG to all be the exact same height.出于某种原因,无论我尝试什么,粉红色 BG 都不会扩大其宽度,我只希望图像和粉红色 BG 的高度完全相同。 Maybe CSS grid would be better?也许 CSS 网格会更好?

If someone could help that would be great!如果有人可以提供帮助,那就太好了!

HTML HTML

<div class="blog-contl row">



        <div class="col">
          <div class="blog-half">
          <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog6.jpg')">
          </div>
          <div class="blog-half-cont">
            <h4>Test Post 12</h4>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            <div class="main-button">
              <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-12/" title="Read More" class="btn-default">Read More</a>
            </div>
          </div>
      </div>
      </div>



        <div class="col">
          <div class="blog-half">
          <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog3.jpg')">
          </div>
          <div class="blog-half-cont">
            <h4>Test Post 9</h4>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            <div class="main-button">
              <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-9/" title="Read More" class="btn-default">Read More</a>
            </div>
          </div>
      </div>
      </div>



        <div class="col">
          <div class="blog-half">
          <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog6.jpg')">
          </div>
          <div class="blog-half-cont">
            <h4>Test Post 6</h4>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
            <div class="main-button">
              <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-6/" title="Read More" class="btn-default">Read More</a>
            </div>
          </div>
      </div>
      </div>



</div>

CSS CSS

.row {display:flex;}

.blog-contl img {width:auto;}

.blog-contl .row {
    display: flex;
    flex-wrap: wrap;
}

.blog-contl .col {padding:0;}

.blog-half {
}

.blog-half-img {
    background-size: cover;
    background-repeat: no-repeat;
    width: 100%;
    padding-bottom: 50%;
}

.blog-half-cont {
    background: #da55c9;
    margin: 0;
    padding: 40px;
    box-sizing: border-box;
    text-align: center;
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.blog-half {display:flex;flex-wrap:wrap;}

.col:nth-of-type(2) .blog-half .blog-half-cont {
  order:1;
}

.col:nth-of-type(2) .blog-half .blog-half-img {
  order:2;
}

Thank you!谢谢!

It is better to you use grid instead of flex.最好使用grid而不是flex。 Please use the below code.请使用以下代码。 Note: We have used grid to adjust the height.注意:我们使用网格来调整高度。

 .row { display: grid; grid: auto /auto auto auto; }.blog-half { display: grid; grid-auto-rows: 1fr; }.blog-contl img {width:auto;}.blog-half-img { background-size: cover; background-repeat: no-repeat; width: 100%; padding-bottom: 50%; }.blog-half-cont { background: #da55c9; margin: 0; padding: 40px; box-sizing: border-box; text-align: center; color: #fff; display: flex; flex-direction: column; justify-content: center; }.col:nth-of-type(2).blog-half.blog-half-cont { order:1; }.col:nth-of-type(2).blog-half.blog-half-img { order:2; }
 <div class="blog-contl row"> <div class="col"> <div class="blog-half"> <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog6.jpg')"> </div> <div class="blog-half-cont"> <h4>Test Post 12</h4> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p> <div class="main-button"> <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-12/" title="Read More" class="btn-default">Read More</a> </div> </div> </div> </div> <div class="col"> <div class="blog-half"> <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog3.jpg')"> </div> <div class="blog-half-cont"> <h4>Test Post 9</h4> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p> <div class="main-button"> <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-9/" title="Read More" class="btn-default">Read More</a> </div> </div> </div> </div> <div class="col "> <div class="blog-half"> <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog6.jpg')"> </div> <div class="blog-half-cont"> <h4>Test Post 6</h4> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p> <div class="main-button"> <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-6/" title="Read More" class="btn-default">Read More</a> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM