简体   繁体   English

等宽高flexbox

[英]equal width and height flexbox

I've two question's about flexboxes but can't find the answer to this.我有两个关于弹性盒的问题,但找不到答案。

  1. I want to create 4 boxes that have all the same width and the same height without using the viewport.我想在不使用视口的情况下创建 4 个具有相同宽度和相同高度的框。 for example: I create one box with a 50% width and the height needs to be equal to the width.例如:我创建了一个宽度为 50% 的盒子,高度需要等于宽度。 All the other boxes get the same properties.所有其他框都具有相同的属性。

  2. If I have those boxes I would like to set div's inside one of those square's, one that takes a height of 20% and the whole width of that box.如果我有这些盒子,我想将 div 设置在其中一个正方形内,一个需要 20% 的高度和该盒子的整个宽度的盒子。 how can I set the height to 20% and make sure the box doesn't extend?如何将高度设置为 20% 并确保框不会延伸?

my code so far:到目前为止我的代码:

 .flex-container { float: none; width: 50%; padding: 0; margin: 0; list-style: none; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -moz-flex; display: -webkit-flex; display: flex; -webkit-flex-wrap: wrap; flex-wrap: wrap; }.flex-item { width: 50%; color: white; }.flex1 { background-color: green; float: left; }.flex2 { background-color: red; float: right; }.flex3 { background-color: purple; float: left; }.flex4 { background-color: orange; float: right; }.inlineBox { width: 100%; height: 20%; background-color: yellow; }
 <ul class="flex-container"> <li class="flex-item flex1">this boxes that I created here all need to have the same height and width. They need to be responsive and dont use a vieuwport</li> <li class="flex-item flex2">2</li> <li class="flex-item flex3">3</li> <li class="flex-item flex4">4</li> </ul> <br> <br> <br> these yellow box need to take a width of 100% and a height of 20% inside the flex box<br> <ul class="flex-container"> <li class="flex-item flex1"> <div class="inlineBox"> </div> <div class="inlineBox"> </div> <div class="inlineBox"> </div> </li> <li class="flex-item flex2">2</li> <li class="flex-item flex3">3</li> <li class="flex-item flex4">4</li> </ul>

The answer is NO.答案是不。 The reason is provided in the flexbox specification: https://www.w3.org/TR/css-flexbox-1/#flex-lines原因在flexbox规范中提供: https://www.w3.org/TR/css-flexbox-1/#flex-lines

You can accomplish that now with display: grid:您现在可以使用 display: grid:

https://codepen.io/pgurav/pen/eYpeOEp https://codepen.io/pgurav/pen/eYpeOEp

Although the grid itself is not flexbox, it behaves very similar to a flexbox container, and the items inside the grid can be flex.虽然网格本身不是 flexbox,但它的行为与 flexbox 容器非常相似,并且网格内的项目可以是 flex 的。

The grid layout is also very handy in the case you want responsive grids.如果您需要响应式网格,网格布局也非常方便。 That is, if you want the grid to have a different number of columns per row you can then just change grid-template-columns:也就是说,如果您希望网格每行具有不同数量的列,则只需更改 grid-template-columns:

grid-template-columns: repeat(1, 1fr); // 1 column
grid-template-columns: repeat(2, 1fr); // 2 columns
grid-template-columns: repeat(3, 1fr); // 3 columns

and so on...等等...

You can mix it with media queries and change according to the size of the page.您可以将其与媒体查询混合使用,并根据页面大小进行更改。

Sadly there is still no support for container queries / element queries in the browsers (out of the box) to make it work well with changing the number of columns according to the container size, not to the page size (this would be great to use with reusable webcomponents).遗憾的是,仍然不支持浏览器中的容器查询/元素查询(开箱即用),以使其能够很好地根据容器大小而不是页面大小更改列数(这会很好用带有可重用的网络组件)。

More information about the grid layout:有关网格布局的更多信息:

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout

Support of the Grid Layout accross browsers:跨浏览器支持网格布局:

https://caniuse.com/#feat=css-grid https://caniuse.com/#feat=css-grid

html code: html 代码:

<ul class="flex-container">
    <li class="flex-item flex1">
        <span>this boxes that I created here all need to have the same height and width. They need to be responsive and dont use a vieuwport</span>
    </li>
    <li class="flex-item flex2"></li>
    <li class="flex-item flex3"></li>
    <li class="flex-item flex4"></li>



  </ul>

  <br>
  <br>
  <br>


  these yellow box need to take a width of 100% and a height of 20% inside the flex box<br>


  <ul class="flex-container">
    <li class="flex-item flex1">
        <div class="item-wrap">

            <div class="inlineBox"> </div>
            <div class="inlineBox"> </div>
            <div class="inlineBox"> </div>
        </div>

    </li>
    <li class="flex-item flex2"></li>
    <li class="flex-item flex3"></li>
    <li class="flex-item flex4"></li>


  </ul>

css code: css 代码:

.flex-container {
    width: 50%;
    padding: 0;
    margin: 0;
    list-style: none;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -moz-flex;
    display: -webkit-flex;
    display: flex;  
     -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
  }

  .flex-item { 
      position: relative;
    width: 50%;
    padding-top: 50%;
    color: white;
  }
  .flex-item > span {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
  }

  .flex1 {
    background-color: green;
     float: left;
  }

  .flex2 {
    background-color: red;
    float: right;
  }

  .flex3 {
    background-color: purple;
     float: left;
  }

  .flex4 {
    background-color: orange;
    float: right;
  }

  .inlineBox {
    width: 100%;
    padding-top: 20%;
    background-color: yellow;
  }
  .item-wrap {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
  }

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

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