每列上都有flexbox和空白的结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每列上都有flexbox和空白的结构相关的知识,希望对你有一定的参考价值。

我需要创建此结构:https://i.imgur.com/7YPURIy.png

我有这个html,我不能修改它,只能添加类。我的问题是列的边距,如果我有50%+ 25%+ 25,并且每列都有边距,则最后一列会导致溢出。我认为我可以使用calc(),但我认为这不是最佳解决方案...

我正在尝试使用flexbox,但是我可以使用任何东西(如果可能的话,最好的解决方案)

The Codepen:https://codepen.io/visualit/pen/WNvpOmX

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/responsive.css">
</head>

<body>
    <div class="overlay"></div>
    <section id="wrapper">
        <div class="grid-items row">
            <div class="item item-50 col-xs-6">
            <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
        </div>
        <div class="item col-xs-3">
            <h2>Lorem Ipsum is simply dummy text</h2>
        </div>
        <div class="item item-image col-xs-3 ">
            <h2>Lorem Ipsum has been the industry standar</h2>
        </div>
        <div class="item col-xs-3">
            <h2>Lorem Ipsum has been the industry standar</h2>
        </div>
        <div class="item-grid2 row col-xs-9">
            <div class="item audi-logo col-xs-4">
                <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
            </div>
            <div class="item item-news col-xs-4">
                <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
            </div>
            <div class="item item-news col-xs-4">
                <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
            </div>
            <div class="item item-50 col-xs-8">
                <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
            </div>
            <div class="item item-image col-xs-4">
                <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
            </div>
        </div>
    </div>
</section>
</body>

</html>

这是我正在使用的active.css

:root {
  --gutter-width: 1rem;
  --outer-margin: 2rem;
  --gutter-compensation: calc((var(--gutter-width) * 0.5) * -1);
  --half-gutter-width: calc((var(--gutter-width) * 0.5));
  --xs-min: 30;
  --sm-min: 48;
  --md-min: 64;
  --lg-min: 75;
  --screen-xs-min: var(--xs-min)em;
  --screen-sm-min: var(--sm-min)em;
  --screen-md-min: var(--md-min)em;
  --screen-lg-min: var(--lg-min)em;
  --container-sm: calc(var(--sm-min) + var(--gutter-width));
  --container-md: calc(var(--md-min) + var(--gutter-width));
  --container-lg: calc(var(--lg-min) + var(--gutter-width));
}

@custom-media --sm-viewport only screen and (min-width: 48em);
@custom-media --md-viewport only screen and (min-width: 64em);
@custom-media --lg-viewport only screen and (min-width: 75em);


.img-responsive {
  width: 100%;
  height: auto;
}

.item{
    border:1px solid;
}

.container-fluid, .container {
  margin-right: auto;
  margin-left: auto;
}

.container-fluid {
  padding-right: var(--outer-margin, 2rem);
  padding-left: var(--outer-margin, 2rem);
}

.row {
  box-sizing: border-box;
  display: flex;
  flex: 0 1 auto;
  flex-direction: row;
  flex-wrap: wrap;
  margin-right: var(--gutter-compensation, -0.5rem);
  margin-left: var(--gutter-compensation, -0.5rem);
}

.row.reverse {
  flex-direction: row-reverse;
}

.col.reverse {
  flex-direction: column-reverse;
}

.col-xs,
.col-xs-1,
.col-xs-2,
.col-xs-3,
.col-xs-4,
.col-xs-5,
.col-xs-6,
.col-xs-7,
.col-xs-8,
.col-xs-9,
.col-xs-10,
.col-xs-11,
.col-xs-12,
.col-xs-offset-0,
.col-xs-offset-1,
.col-xs-offset-2,
.col-xs-offset-3,
.col-xs-offset-4,
.col-xs-offset-5,
.col-xs-offset-6,
.col-xs-offset-7,
.col-xs-offset-8,
.col-xs-offset-9,
.col-xs-offset-10,
.col-xs-offset-11,
.col-xs-offset-12 {
  box-sizing: border-box;
  flex: 0 0 auto;
  padding-right: var(--half-gutter-width, 0.5rem);
  padding-left: var(--half-gutter-width, 0.5rem);
}

.col-xs {
  flex-grow: 1;
  flex-basis: 0;
  max-width: 100%;
}

.col-xs-1 {
  flex-basis: 8.33333333%;
  max-width: 8.33333333%;
}

.col-xs-2 {
  flex-basis: 16.66666667%;
  max-width: 16.66666667%;
}

.col-xs-3 {
  flex-basis: 25%;
  max-width: 25%;
}

.col-xs-4 {
  flex-basis: 33.33333333%;
  max-width: 33.33333333%;
}

.col-xs-5 {
  flex-basis: 41.66666667%;
  max-width: 41.66666667%;
}

.col-xs-6 {
  flex-basis: 50%;
  max-width: 50%;
}

.col-xs-7 {
  flex-basis: 58.33333333%;
  max-width: 58.33333333%;
}

.col-xs-8 {
  flex-basis: 66.66666667%;
  max-width: 66.66666667%;
}

.col-xs-9 {
  flex-basis: 75%;
  max-width: 75%;
}

.col-xs-10 {
  flex-basis: 83.33333333%;
  max-width: 83.33333333%;
}

.col-xs-11 {
  flex-basis: 91.66666667%;
  max-width: 91.66666667%;
}

.col-xs-12 {
  flex-basis: 100%;
  max-width: 100%;
}

.col-xs-offset-0 {
  margin-left: 0;
}

.col-xs-offset-1 {
  margin-left: 8.33333333%;
}

.col-xs-offset-2 {
  margin-left: 16.66666667%;
}

.col-xs-offset-3 {
  margin-left: 25%;
}

.col-xs-offset-4 {
  margin-left: 33.33333333%;
}

.col-xs-offset-5 {
  margin-left: 41.66666667%;
}

.col-xs-offset-6 {
  margin-left: 50%;
}

.col-xs-offset-7 {
  margin-left: 58.33333333%;
}

.col-xs-offset-8 {
  margin-left: 66.66666667%;
}

.col-xs-offset-9 {
  margin-left: 75%;
}

.col-xs-offset-10 {
  margin-left: 83.33333333%;
}

.col-xs-offset-11 {
  margin-left: 91.66666667%;
}

.start-xs {
  justify-content: flex-start;
  text-align: start;
}

.center-xs {
  justify-content: center;
  text-align: center;
}

.end-xs {
  justify-content: flex-end;
  text-align: end;
}

.top-xs {
  align-items: flex-start;
}

.middle-xs {
  align-items: center;
}

.bottom-xs {
  align-items: flex-end;
}

.around-xs {
  justify-content: space-around;
}

.between-xs {
  justify-content: space-between;
}

.first-xs {
  order: -1;
}

.last-xs {
  order: 1;
}

@media (--sm-viewport) {
  .container {
    width: var(--container-sm, 46rem);
  }

  .col-sm,
  .col-sm-1,
  .col-sm-2,
  .col-sm-3,
  .col-sm-4,
  .col-sm-5,
  .col-sm-6,
  .col-sm-7,
  .col-sm-8,
  .col-sm-9,
  .col-sm-10,
  .col-sm-11,
  .col-sm-12,
  .col-sm-offset-0,
  .col-sm-offset-1,
  .col-sm-offset-2,
  .col-sm-offset-3,
  .col-sm-offset-4,
  .col-sm-offset-5,
  .col-sm-offset-6,
  .col-sm-offset-7,
  .col-sm-offset-8,
  .col-sm-offset-9,
  .col-sm-offset-10,
  .col-sm-offset-11,
  .col-sm-offset-12 {
    box-sizing: border-box;
    flex: 0 0 auto;
    padding-right: var(--half-gutter-width, 0.5rem);
    padding-left: var(--half-gutter-width, 0.5rem);
  }

  .col-sm {
    flex-grow: 1;
    flex-basis: 0;
    max-width: 100%;
  }

  .col-sm-1 {
    flex-basis: 8.33333333%;
    max-width: 8.33333333%;
  }

  .col-sm-2 {
    flex-basis: 16.66666667%;
    max-width: 16.66666667%;
  }

  .col-sm-3 {
    flex-basis: 25%;
    max-width: 25%;
  }

  .col-sm-4 {
    flex-basis: 33.33333333%;
    max-width: 33.33333333%;
  }

  .col-sm-5 {
    flex-basis: 41.66666667%;
    max-width: 41.66666667%;
  }

  .col-sm-6 {
    flex-basis: 50%;
    max-width: 50%;
  }

  .col-sm-7 {
    flex-basis: 58.33333333%;
    max-width: 58.33333333%;
  }

  .col-sm-8 {
    flex-basis: 66.66666667%;
    max-width: 66.66666667%;
  }

  .col-sm-9 {
    flex-basis: 75%;
    max-width: 75%;
  }

  .col-sm-10 {
    flex-basis: 83.33333333%;
    max-width: 83.33333333%;
  }

  .col-sm-11 {
    flex-basis: 91.66666667%;
    max-width: 91.66666667%;
  }

  .col-sm-12 {
    flex-basis: 100%;
    max-width: 100%;
  }

  .col-sm-offset-0 {
    margin-left: 0;
  }

  .col-sm-offset-1 {
    margin-left: 8.33333333%;
  }

  .col-sm-offset-2 {
    margin-left: 16.66666667%;
  }

  .col-sm-offset-3 {
    margin-left: 25%;
  }

  .col-sm-offset-4 {
    margin-left: 33.33333333%;
  }

  .col-sm-offset-5 {
    margin-left: 41.66666667%;
  }

  .col-sm-offset-6 {
    margin-left: 50%;
  }

  .col-sm-offset-7 {
    margin-left: 58.33333333%;
  }

  .col-sm-offset-8 {
    margin-left: 66.66666667%;
  }

  .col-sm-offset-9 {
    margin-left: 75%;
  }

  .col-sm-offset-10 {
    margin-left: 83.33333333%;
  }

  .col-sm-offset-11 {
    margin-left: 91.66666667%;
  }

  .start-sm {
    justify-content: flex-start;
    text-align: start;
  }

  .center-sm {
    justify-content: center;
    text-align: center;
  }

  .end-sm {
    justify-content: flex-end;
    text-align: end;
  }

  .top-sm {
    align-items: flex-start;
  }

  .middle-sm {
    align-items: center;
  }

  .bottom-sm {
    align-items: flex-end;
  }

  .around-sm {
    justify-content: space-around;
  }

  .between-sm {
    justify-content: space-between;
  }

  .first-sm {
    order: -1;
  }

  .last-sm {
    order: 1;
  }
}

@media (--md-viewport) {
  .container {
    width: var(--container-md, 61rem);
  }

  .col-md,
  .col-md-1,
  .col-md-2,
  .col-md-3,
  .col-md-4,
  .col-md-5,
  .col-md-6,
  .col-md-7,
  .col-md-8,
  .col-md-9,
  .col-md-10,
  .col-md-11,
  .col-md-12,
  .col-md-offset-0,
  .col-md-offset-1,
  .col-md-offset-2,
  .col-md-offset-3,
  .col-md-offset-4,
  .col-md-offset-5,
  .col-md-offset-6,
  .col-md-offset-7,
  .col-md-offset-8,
  .col-md-offset-9,
  .col-md-offset-10,
  .col-md-offset-11,
  .col-md-offset-12 {
    box-sizing: border-box;
    flex: 0 0 auto;
    padding-right: var(--half-gutter-width, 0.5rem);
    padding-left: var(--half-gutter-width, 0.5rem);
  }

  .col-md {
    flex-grow: 1;
    flex-basis: 0;
    max-width: 100%;
  }

  .col-md-1 {
    flex-basis: 8.33333333%;
    max-width: 8.33333333%;
  }

  .col-md-2 {
    flex-basis: 16.66666667%;
    max-width: 16.66666667%;
  }

  .col-md-3 {
    flex-basis: 25%;
    max-width: 25%;
  }

  .col-md-4 {
    flex-basis: 33.33333333%;
    max-width: 33.33333333%;
  }

  .col-md-5 {
    flex-basis: 41.66666667%;
    max-width: 41.66666667%;
  }

  .col-md-6 {
    flex-basis: 50%;
    max-width: 50%;
  }

  .col-md-7 {
    flex-basis: 58.33333333%;
    max-width: 58.33333333%;
  }

  .col-md-8 {
    flex-basis: 66.66666667%;
    max-width: 66.66666667%;
  }

  .col-md-9 {
    flex-basis: 75%;
    max-width: 75%;
  }

  .col-md-10 {
    flex-basis: 83.33333333%;
    max-width: 83.33333333%;
  }

  .col-md-11 {
    flex-basis: 91.66666667%;
    max-width: 91.66666667%;
  }

  .col-md-12 {
    flex-basis: 100%;
    max-width: 100%;
  }

  .col-md-offset-0 {
    margin-left: 0;
  }

  .col-md-offset-1 {
    margin-left: 8.33333333%;
  }

  .col-md-offset-2 {
    margin-left: 16.66666667%;
  }

  .col-md-offset-3 {
    margin-left: 25%;
  }

  .col-md-offset-4 {
    margin-left: 33.33333333%;
  }

  .col-md-offset-5 {
    margin-left: 41.66666667%;
  }

  .col-md-offset-6 {
    margin-left: 50%;
  }

  .col-md-offset-7 {
    margin-left: 58.33333333%;
  }

  .col-md-offset-8 {
    margin-left: 66.66666667%;
  }

  .col-md-offset-9 {
    margin-left: 75%;
  }

  .col-md-offset-10 {
    margin-left: 83.33333333%;
  }

  .col-md-offset-11 {
    margin-left: 91.66666667%;
  }

  .start-md {
    justify-content: flex-start;
    text-align: start;
  }

  .center-md {
    justify-content: center;
    text-align: center;
  }

  .end-md {
    justify-content: flex-end;
    text-align: end;
  }

  .top-md {
    align-items: flex-start;
  }

  .middle-md {
    align-items: center;
  }

  .bottom-md {
    align-items: flex-end;
  }

  .around-md {
    justify-content: space-around;
  }

  .between-md {
    justify-content: space-between;
  }

  .first-md {
    order: -1;
  }

  .last-md {
    order: 1;
  }
}

@media (--lg-viewport) {
  .container {
    width: var(--container-lg, 71rem);
  }

  .col-lg,
  .col-lg-1,
  .col-lg-2,
  .col-lg-3,
  .col-lg-4,
  .col-lg-5,
  .col-lg-6,
  .col-lg-7,
  .col-lg-8,
  .col-lg-9,
  .col-lg-10,
  .col-lg-11,
  .col-lg-12,
  .col-lg-offset-0,
  .col-lg-offset-1,
  .col-lg-offset-2,
  .col-lg-offset-3,
  .col-lg-offset-4,
  .col-lg-offset-5,
  .col-lg-offset-6,
  .col-lg-offset-7,
  .col-lg-offset-8,
  .col-lg-offset-9,
  .col-lg-offset-10,
  .col-lg-offset-11,
  .col-lg-offset-12 {
    box-sizing: border-box;
    flex: 0 0 auto;
    padding-right: var(--half-gutter-width, 0.5rem);
    padding-left: var(--half-gutter-width, 0.5rem);
  }

  .col-lg {
    flex-grow: 1;
    flex-basis: 0;
    max-width: 100%;
  }

  .col-lg-1 {
    flex-basis: 8.33333333%;
    max-width: 8.33333333%;
  }

  .col-lg-2 {
    flex-basis: 16.66666667%;
    max-width: 16.66666667%;
  }

  .col-lg-3 {
    flex-basis: 25%;
    max-width: 25%;
  }

  .col-lg-4 {
    flex-basis: 33.33333333%;
    max-width: 33.33333333%;
  }

  .col-lg-5 {
    flex-basis: 41.66666667%;
    max-width: 41.66666667%;
  }

  .col-lg-6 {
    flex-basis: 50%;
    max-width: 50%;
  }

  .col-lg-7 {
    flex-basis: 58.33333333%;
    max-width: 58.33333333%;
  }

  .col-lg-8 {
    flex-basis: 66.66666667%;
    max-width: 66.66666667%;
  }

  .col-lg-9 {
    flex-basis: 75%;
    max-width: 75%;
  }

  .col-lg-10 {
    flex-basis: 83.33333333%;
    max-width: 83.33333333%;
  }

  .col-lg-11 {
    flex-basis: 91.66666667%;
    max-width: 91.66666667%;
  }

  .col-lg-12 {
    flex-basis: 100%;
    max-width: 100%;
  }

  .col-lg-offset-0 {
    margin-left: 0;
  }

  .col-lg-offset-1 {
    margin-left: 8.33333333%;
  }

  .col-lg-offset-2 {
    margin-left: 16.66666667%;
  }

  .col-lg-offset-3 {
    margin-left: 25%;
  }

  .col-lg-offset-4 {
    margin-left: 33.33333333%;
  }

  .col-lg-offset-5 {
    margin-left: 41.66666667%;
  }

  .col-lg-offset-6 {
    margin-left: 50%;
  }

  .col-lg-offset-7 {
    margin-left: 58.33333333%;
  }

  .col-lg-offset-8 {
    margin-left: 66.66666667%;
  }

  .col-lg-offset-9 {
    margin-left: 75%;
  }

  .col-lg-offset-10 {
    margin-left: 83.33333333%;
  }

  .col-lg-offset-11 {
    margin-left: 91.66666667%;
  }

  .start-lg {
    justify-content: flex-start;
    text-align: start;
  }

  .center-lg {
    justify-content: center;
    text-align: center;
  }

  .end-lg {
    justify-content: flex-end;
    text-align: end;
  }

  .top-lg {
    align-items: flex-start;
  }

  .middle-lg {
    align-items: center;
  }

  .bottom-lg {
    align-items: flex-end;
  }

  .around-lg {
    justify-content: space-around;
  }

  .between-lg {
    justify-content: space-between;
  }

  .first-lg {
    order: -1;
  }

  .last-lg {
    order: 1;
  }
}
答案

您可以使用https://i.imgur.com/7YPURIy.png来创建此Grid Layouts

下面是一个例子。

.item1{grid-area:header}
.item1_1{grid-area:header1}
.item1_2{grid-area:header2}
.item2{grid-area:menu}
.item3{grid-area:main}
.item3_1{grid-area:main1}
.item4{grid-area:right}
.item5{grid-area:footer}
.item5_1{grid-area:footer1}

.grid-container {
  display: grid;
  grid-template-areas: 'header header header1 header2' 'menu main main1 right' 'menu footer footer footer1';
  grid-gap: 10px;
  background-color: transparent;
  padding: 10px;
}

.grid-container > div {
  background-color: #00A2E8;
  text-align: center;
  padding: 20px 0;
  font-size: 24px;
  color: #fff;
  max-height: 140px;
  min-height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: sans-serif;
  border: 4px solid #000000;
}

.grid-container > div.item2 {
  height: auto;
  max-height: initial;
}

@media (max-width:479.98px) {
  .grid-container {
    display: block;
  }
  .grid-container > div {
    margin-bottom: 10px;
  }
}

@media (min-width:480px) and (max-width:575.98px) {
  .grid-container > div {
    max-height: 70px;
    min-height: 50px;
  }
}

@media (min-width:576px) and (max-width:767.98px) {
  .grid-container > div {
    max-height: 100px;
    min-height: 80px;
  }
}

@media (min-width:768px) and (max-width:991.98px) {
  .grid-container > div {
    max-height: 130px;
    min-height: 110px;
  }
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>

    <body>
        <div class="grid-container">
            <div class="item1">Header</div>
            <div class="item1_1">Header1</div>
            <div class="item1_2">Header2</div>
            <div class="item2">Menu</div>
            <div class="item3">Main</div>
            <div class="item3_1">Main1</div>
            <div class="item4">Right</div>
            <div class="item5">Footer</div>
            <div class="item5_1">Footer1</div>
        </div>
    </body>
</html>
另一答案

JS小提琴:https://jsfiddle.net/prak_mtl/nmhpug4j/20/

<!DOCTYPE html>
<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/responsive.css">
  </head>

  <body>
    <div class="overlay"></div>
    <section class="wrapper">
      <div class="d-flex" id="1">
        <div class="box2">
          <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
        </div>
        <div class="box1">
          <h2>Lorem Ipsum is simply dummy text</h2>
        </div>
        <div class="box1">
          <h2>Lorem Ipsum has been the industry standar</h2>
        </div>
      </div>

      <div class="d-flex" id="2">
        <div class="box3">
          <h2>Lorem Ipsum has been the industry standar</h2>
        </div>

        <div>
          <div class="d-flex">
            <div class="box1">
              <h2>Lorem Ipsum has been the industry standar</h2>
            </div>
            <div class="box1">
              <h2>Lorem Ipsum has been the industry standar</h2>
            </div>
          </div>
          <div class="ml-20 box2">
            <h2>Lorem Ipsum has been the industry standar</h2>
          </div>
        </div>

        <div>
          <div class="box1">
            <h2>Lorem Ipsum has been the industry standar</h2>
          </div>
          <div class="box1">
            <h2>Lorem Ipsum has been the industry standar</h2>
          </div>
        </div>
      </div>

    </section>
  </body>

</html>
body {
  background: grey;
  padding: 20px;
  font-family: Helvetica;
}

.wrapper {
  background-color: white;
  padding: 20px;
}
.d-flex{
  display: flex;
}

.box1 {
  border: 3px solid black;
  background-color: #00A2E8;
  color: white;
  text-align: center;
  padding: 20px;
  width: 150px;
  height: 150px;
  margin-left: 20px;
  margin-bottom: 20px;
}

.box2{
  border: 3px solid black;
  background-color: #00A2E8;
  color: white;
  text-align: center;
  padding: 20px;
  width: 366px;
  height: 150px;
  margin-bottom: 20px;
}

.box3{
  border: 3px solid black;
  background-color: #00A2E8;
  color: white;
  text-align: center;
  padding: 20px;
  width: 150px;
  height: 367px;
}

.ml-20{  
  margin-left: 20px;
}

签出。

以上是关于每列上都有flexbox和空白的结构的主要内容,如果未能解决你的问题,请参考以下文章

如何控制 flexbox 中每列的项目数?

Flexbox:如何在列上组合百分比、像素和剩余高度?

ASP.NET MVC 图表:如何在每列上显示值

经典容斥+排列组合——cf1342E

MySQL计算两列上的唯一值并为每列加入这些计数

使用 BottomNav 和 TabMenu 导航后的空白片段