Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

html - CSS section layout with shaped divs

I would like to have a section with 3 divs, with layout like this: Layout

How can i do this with HTML and CSS?

My html:

<section class="hero">
    <div class="div1">
    <div class="div2">
    <div class="div3">
</section>

My css:

section.hero {
    width: 100vw;
    height: 100vh;
}
.div1 {
    background: url(img1.png);
}
.div3 {
    background: url(img2.png);
}
.div2 {
    background: url(img3.png);
}

Thanks for all answers...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

3 divs and shape-outside can help you to fill them only on those parts seen ;)

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

The shape-outside CSS property defines a shape—which may be non-rectangular—around which adjacent inline content should wrap. By default, inline content wraps around its margin box; shape-outside provides a way to customize this wrapping, making it possible to wrap text around complex objects rather than simple boxes.

body {
  margin: 0;
}

section.hero {
  width: 100vw;
  height: 100vh;
  display: grid;
}

section.hero>div[class] {
  grid-row: 1;
  grid-column: 1;
  height: 100%;
  width: 100%;
  text-shadow: 0 0 2px white;
}

div[class]:before {
  content: "";
  height: 100%;
  width: 100%;
}

.div1 {
  background: url(https://picsum.photos/id/1039/1920/1080) 0 0 / cover;
  clip-path: polygon(0% 0%, 0% 66%, 0% 66%, 80% 0%, 80% 0%);
  font-size: calc(1.5vmin + 1vmax);  
  color:purple;
}

.div1:before {
  float: right;
  shape-outside: polygon(0 66%, 80% 0%, 100% 0, 100% 100%, 0 100%);
}

.div2 {
  background: url(https://picsum.photos/id/1001/1920/1080) 100% 100% / cover;
  clip-path: polygon(80% 0%, 43% 30%, 95% 100%, 100% 100%, 100% 0%);
  font-size: calc(1.5vmin + 1vmax);
  color:turquoise;
}

.div2:before {
  float: left;
  shape-outside: polygon(80% 0%, 44% 30%, 96% 100%, 0% 100%, 0% 0%);
}

.div3 {
  background: url(https://picsum.photos/id/1006/1920/1080) 100% 100% / cover;
  clip-path: polygon(0% 66%, 43% 30%, 95% 100%, 0 100%);
  font-size: calc(1.5vmin + 1vmax);
  color:blue;
}

.shape {
  height: 100vh;
  width: 57%;
  float: right;
  shape-outside: polygon(0% 31%, 0% 0%, 100% 0%, 100% 100%, 90% 100%);
}

.shape+.shape {
  float: left;
  height: 100vh;
  width: 43%;
  shape-outside: polygon(0% 0%, 0% 66%, 100% 31%, 100% 0);
}
<section class="hero">
  <div class="div1">
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
      Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus
      lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor,
      facilisis luctus, metus</p>
  </div>

  <div class="div2">
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
      Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus
      lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor,
      facilisis luctus, metus</p>
  </div>
  <div class="div3">
    <!-- because pseudo or a single element  cannot handle that shape at once -->
    <div class="shape"></div>
    <div class="shape"></div>
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
      Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus
      lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor,
      facilisis luctus, metus</p>
  </div>
</section>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...