十种现代css布局方法

原文: 1-Line Layouts
比较多的用到了grid,注意IE不支持

1. 超级居中

place-items: center

<div class="parent blue">
  <div class="box coral"> :)</div>
</div>
.ex1 .parent {
  display: grid;
  place-items: center;
}

2. 自适应布局

flex: 0 1 <baseWidth>

<div class="parent white">
    <div class="box green">1</div>
    <div class="box green">2</div>
    <div class="box green">3</div>
</div>
.parent {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.box {
  flex: 1 1 150px; /*  弹性伸展100%宽度  */
  flex: 0 1 150px; /* 不伸展 */
  margin: 5px;
}

3. Sidebar

grid-template-columns: minmax(<min>, <max>) ...

<div class="parent">
    <div class="section yellow">
    Min: 150px / Max: 25%
    </div>
    <div class="section purple">
      This element takes the second grid position (1fr), meaning
      it takes up the rest of the remaining space.
    </div>
</div>
.parent {
    display: grid;
    grid-template-columns: minmax(150px, 25%) 1fr;
}

4. 固定头部和底部

grid-template-rows: auto 1fr auto

<div class="parent">
    <header class="blue section">Header</header>
    <main class="coral section">Main</main>
    <footer class="purple section">Footer Content</footer>
</div>
.parent {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

5. 经典圣杯布局

<div class="parent">
    <header class="pink section">Header</header>
    <div class="left-side blue section">Left Sidebar</div>
    <main class="section coral"> Main Content</main>
    <div class="right-side yellow section">Right Sidebar</div>
    <footer class="green section">Footer</footer>
</div>
.parent {
    display: grid;
    grid-template: auto 1fr auto / auto 1fr auto;
}

.parent header {
    padding: 2rem;
    grid-column: 1 / 4;
  }

.parent .left-side {
    grid-column: 1 / 2;
  }

.parent main {
    grid-column: 2 / 3;
  }

.parent .right-side {
    grid-column: 3 / 4;
  }

.parent footer {
    grid-column: 1 / 4;
}

6. 栅格布局

grid-template-columns: repeat(12, 1fr)

<div class="parent white">
  <div class="span-12 green section">Span 12</div>
  <div class="span-6 coral section">Span 6</div>
  <div class="span-4 blue section">Span 4</div>
  <div class="span-2 yellow section">Span 2</div>
</div>
.parent {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
}

.span-12 {
  grid-column: 1 / span 12;
}

.span-6 {
  grid-column: 1 / span 6;
}

.span-4 {
  grid-column: 4 / span 4;
}

.span-2 {
  grid-column: 3 / span 2;
}

.section {
  display: grid;
  place-items: center;
  text-align: center
}

7. RAM (Repeat, Auto, Minmax)

grid-template-columns: repeat(auto-fit, minmax(<base>, 1fr))

<div class="parent white">
  <div class="box pink">1</div>
  <div class="box purple">2</div>
  <div class="box blue">3</div>
  <div class="box green">4</div>
</div>
.parent {
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}

8. 卡片

justify-content: space-between

<div class="parent white">
  <div class="card yellow">
    <h3>Title - Card 1</h3>
    <p>Medium length description with a few more words here.</p>
    <div class="visual pink"></div>
  </div>
  <div class="card yellow">
    <h3>Title - Card 2</h3>
    <p>Long Description. Lorem ipsum dolor sit, amet consectetur adipisicing elit.</p>
    <div class="visual blue"></div>
  </div>
  <div class="card yellow">
    <h3>Title - Card 3</h3>
    <p>Short Description.</p>
    <div class="visual green"></div>
  </div>
</div>

这里.card不设置justify-content: space-between; 给.visualmargin-top: auto有类似效果

.parent {
  height: auto;
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(3, 1fr);
}

.visual {
  height: 100px;
  width: 100%;
}

.card {
  display: flex;
  flex-direction: column;
  padding: 1rem;
  justify-content: space-between;
}

h3 {
  margin: 0
}

9. clamp弹性排版

<div class="parent white">
  <div class="card purple">
    <h1>Title Here</h1>
    <div class="visual yellow"></div>
    <p>Descriptive Text. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Sed est error repellat veritatis.</p>
  </div>
</div>
.parent {
  display: grid;
  place-items: center;
}

.card {
  width: clamp(23ch, 50%, 46ch);
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

.visual {
  height: 125px;
  width: 100%;
}

10. 指定比例

目前这个属性支持性还不是很好

aspect-ratio: <width> / <height>

<div class="parent white">
    <div class="card blue">
      <h1>Video Title</h1>
      <div class="visual green"></div>
      <p>Descriptive Text. This demo works in Chromium 84+.</p>
    </div>
</div>
.parent {
  display: grid;
  place-items: center;
}

.visual {
  aspect-ratio: 16 / 9;
}

.card {
  width: 50%;
  display: flex;
  flex-direction: column;
  padding: 1rem;
}
Comments
Write a Comment