【问题标题】:How to build a responsive slanted layout?如何构建响应式倾斜布局?
【发布时间】:2023-03-23 05:24:01
【问题描述】:

我正在准备一个带有倾斜元素的新布局,应该以任何方式响应。 我目前在默认桌面和移动设备上看起来不错,但如果屏幕更大,布局就会被破坏:例如在我的 32" 屏幕上,文本从容器元素之外开始,就在倾斜元素之前。这不应该发生。

main {
  font-family: Arial;
}

.content_nocolor {
  padding: 60px 40px 40px 40px;
}

.content__slant1 {
  position: relative;
  padding: 40px 40px 120px 40px;
  overflow: hidden;
  filter: drop-shadow(0px 40px 0px #f2f2f2);
}
.content__slant1:before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #004B4F;
  -webkit-transform: skewY(-3deg);
  -moz-transform: skewY(-3deg);
  -ms-transform: skewY(-3deg);
  -o-transform: skewY(-3deg);
  transform: skewY(-3deg);
  transform-origin: top left;
}
.content__slant1 h1, .content__slant1 p {
  position: relative;
  color: #ffffff;
}
.content__slant1 h1 a, .content__slant1 p a {
  color: inherit;
}

.content__slant2 {
  position: relative;
  padding: 120px 40px 40px 40px;
  overflow: hidden;
  filter: drop-shadow(0px calc(-1*40px) 0px #f2f2f2);
}
.content__slant2:before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #F89300;
  /* background-image: linear-gradient(#ff9d2f, #ff6126); */
  -webkit-transform: skewY(-3deg);
  -moz-transform: skewY(-3deg);
  -ms-transform: skewY(-3deg);
  -o-transform: skewY(-3deg);
  transform: skewY(-3deg);
  transform-origin: bottom right;
}
.content__slant2 h1, .content__slant2 h3, .content__slant2 p {
  position: relative;
  color: #000000;
}
.content__slant2 h1 a, .content__slant2 h3 a, .content__slant2 p a {
  color: inherit;
}

.content__slant3 {
  position: relative;
  padding: 0 40px 140px 40px;
  overflow: hidden;
  filter: drop-shadow(0px 40px 0px #f2f2f2);
}
.content__slant3:before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #F89300;
  /* background-image: linear-gradient(#ff9d2f, #ff6126); */
  -webkit-transform: skewY(3deg);
  -moz-transform: skewY(3deg);
  -ms-transform: skewY(3deg);
  -o-transform: skewY(3deg);
  transform: skewY(3deg);
  transform-origin: bottom right;
}
.content__slant3 h1, .content__slant3 h3, .content__slant3 p {
  position: relative;
  color: #000000;
}
.content__slant3 h1 a, .content__slant3 h3 a, .content__slant3 p a {
  color: inherit;
}
<main class="content">
  <div class="content__slant1">
    <h1>Office ipsum</h1>
    <p>That's great, but we need to add this 2000 line essay make it original, for can it handle a million in one go, yet it needs to be the same, but totally different this looks perfect. Just Photoshop out the dog, add a baby, and make the curtains blue we exceed the clients' expectations this is just a 5 minutes job.</p>
    <p>
      What you've given us is texty, we want sexy something summery; colourful make it look like Apple. Can you make it faster? the animation does not work, when i print the page or can you punch up the fun level on these icons give us a complimentary logo along with the website, nor thanks for taking the time to make the website, but i already made it in wix.
    </p>
  </div>
  
  <div class="content_nocolor">
  <p>There is too much white space why is a 15mb gif on the startpage a bad idea?!, yet you can get my logo from facebook.     </p>
  <p>
    That's great, but we need to add this 2000 line essay it needs to be the same, but totally different so there are more projects lined up charge extra the next time.
  </p>
  </div>
  
  <div class="content__slant2">
    <h1>... continues ...</h1>
    <p>
      We try your eye, but can you change everything? submit your meaningless business jargon to be used on the site!, but that will be a conversation piece this red is too red can you pimp this powerpoint, need more geometry patterns I have an awesome idea for a startup, i need you to build it for me. The flier should feel like a warm handshake. The animation does not work, when i print the page you might wanna give it another shot, or can you turn it around in photoshop so we can see more of the front can you make the blue bluer? jazz it up a little, can you please send me the design specs again?
    </p>
    <p>
        I know somebody who can do this for a reasonable cost. Anyway, you are the designer, you know what to do can you make the logo bigger yes bigger bigger still the logo is too big can you use a high definition screenshot, in an ideal world. Remember, everything is the same or better in an ideal world can you please send me the design specs again?
    </p>
    <h3>Thats not what i saw in my head at all!!</h3>
  </div>
  
  
  <div class="content__slant3">
    <h1>... and so it ends.</h1>
  </div>  
    
</main>

【问题讨论】:

  • 顺便说一句,你有没有更好的选择来构建这样的布局?
  • 您可以使用 clip-path 并最终引入 CSS var() 和 calc() 来匹配剪辑坐标和填充,而不是布置伪。快速示例jsfiddle.net/hz7y30pu
  • clamp() 也是一个选项codepen.io/gc-nomade/pen/YzVvRLG
  • 很好的例子,谢谢。对于所有给定的版本,我确实对彩色元素的不透明度有疑问。 v1 有双透明的“边框”,其他的根本就不是半透明的。必须详细检查并检查不同的浏览器。

标签: html css layout


【解决方案1】:

根据之前的评论做出的回答让任何人都可以看到:

它使用clip-path 代替转换,并从CSS var() 获取值以同时更新pathpadding

描述想法的实时示例:

html {/* set here your min and max value for the slanted clip-path and padding values */
  --pad:clamp( 15px, calc(10vw - 30px) ,60px);
  /*           min       if valid       max  */
}

body {
  padding: 0 0 200px 0;
  margin: 0;
  background: transparent url("https://images.unsplash.com/photo-1622495546323-5dac33dedb01?ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2167&q=80") no-repeat fixed;
  background-size: cover;
}

main {
  font-family: Arial;
    filter:
      drop-shadow(0px 20px 0px #f2f2f2) 
      drop-shadow(0px -20px 0px #f2f2f2)
      drop-shadow(20px 0px 1px #f2f2f2)
      drop-shadow(-20px 0px 1px #f2f2f2)  ;
}

.content_bgWhite {
  background-color: #ffffff;
}

.content_bgSemitrans {
  background-color: rgba(255, 255, 255, 0.25);
  display: inline-block;
  padding: 0px 20px;
 text-align:left;
}

.content_bgTransparent {
}

.content_noslant {
  padding: 60px ;
  text-align:center
}

.content__slant1 {
  --pad: calc(20px + 5vw);
  background:#004B4F;
  padding: 40px 40px var(--pad) 40px;
  clip-path: polygon(0 0, 100% 0, 100% calc(100% - var(--pad) ), 0% 100%);

}

.content__slant1 h1, .content__slant1 p {
  position: relative;
  color: #ffffff;
}
.content__slant1 h1 a, .content__slant1 p a {
  color: inherit;
}

.content__slant2 {  
  background:#F89300;
  position: relative;
  padding: var(--pad) 40px 1px 40px;
  clip-path: polygon(0 0, 100% var(--pad), 100% 100%, 0% 100%);

}
.content__slant2 h1, .content__slant2 h3, .content__slant2 p {
  position: relative;
  color: #000000;
}
.content__slant2 h1 a, .content__slant2 h3 a, .content__slant2 p a {
  color: inherit;
}

.content__slant3 {
  background:#F89300;
  padding: 1px 40px var(--pad) 40px;
  clip-path: polygon(0 0, 100% 0, 100% calc(100% - var(--pad) ), 0% 100%);
}
.content__slant3 h1, .content__slant3 h3, .content__slant3 p {
  position: relative;
  color: #000000;
}
.content__slant3 h1 a, .content__slant3 h3 a, .content__slant3 p a {
  color: inherit;
}
<main class="content">
  <div class="content__slant1">
    <h1>Office ipsum</h1>
    <p>That's great, but we need to add this 2000 line essay make it original, for can it handle a million in one go, yet it needs to be the same, but totally different this looks perfect. Just Photoshop out the dog, add a baby, and make the curtains blue we exceed the clients' expectations this is just a 5 minutes job.</p>
    <p>
      What you've given us is texty, we want sexy something summery; colourful make it look like Apple. Can you make it faster? the animation does not work, when i print the page or can you punch up the fun level on these icons give us a complimentary logo along with the website, nor thanks for taking the time to make the website, but i already made it in wix.
    </p>
  </div>
  
  <div class="content_noslant content_bgTransparent">
        <div class="content_bgSemitrans">
              <p>
                <b> I'm so sensible, i'm getting touch by that filter too.</b>
              </p>
              <p>
                  That's great, but we need to add this 2000 line essay it needs to be the same, but totally different so there are more projects lined up charge extra the next time.
              </p>
         </div>
  </div>
  
  <div class="content__slant2">
    <h1>... continues ...</h1>
    <p>
      We try your eye, but can you change everything? submit your meaningless business jargon to be used on the site!, but that will be a conversation piece this red is too red can you pimp this powerpoint, need more geometry patterns I have an awesome idea for a startup, i need you to build it for me. The flier should feel like a warm handshake. The animation does not work, when i print the page you might wanna give it another shot, or can you turn it around in photoshop so we can see more of the front can you make the blue bluer? jazz it up a little, can you please send me the design specs again?
    </p>
    <p>
        I know somebody who can do this for a reasonable cost. Anyway, you are the designer, you know what to do can you make the logo bigger yes bigger bigger still the logo is too big can you use a high definition screenshot, in an ideal world. Remember, everything is the same or better in an ideal world can you please send me the design specs again?
    </p>
    <h3>Thats not what i saw in my head at all!!</h3>
  </div>
  
  
  <div class="content__slant3">
    <h1>... and so it ends.</h1>
  </div>  
    
</main>

【讨论】:

  • 是否可以用一个 div 而不是两个 div 构建橙色元素?
  • 是的,当然,您需要更新剪辑路径和填充以使其适用于两个边缘。我不敢这么说尽量适合你的 HTML。 @JonSnow
  • 我明白了 :-D 好吧,我是用两个 div 的 bauce 做的,我没有让它像一个 div 单独的飞人一样工作。你能告诉我如何采用剪辑路径吗?我以前从来没有用过这个..
  • 我找到了一个在线工具,它展示了如何使用剪辑路径制作它。我会试一试。非常感谢!
【解决方案2】:
@media screen and (min-width: 1200px) {
  .content__slant1 {
    padding: 40px 40px 120px 40px;
  }
  .content__slant2 {
      padding: 180px 40px 0px 40px;
  }
  .content__slant3 {
      padding: 0 40px 180px 40px;
  }
}

成功了

【讨论】:

    【解决方案3】:

    使用媒体查询,使用最小宽度和最大宽度。

    * {
      box-sizing: border-box;
    }
    /* Responsive layout - when the screen is less than 700px wide, make the two columns stack on top of each other instead of next to each other */
    @media screen and (max-width: 700px) {
        .row {   
            flex-direction: column;
            opacity:0.90; filter:alpha(opacity=90);
        }
    }
    
    /* Responsive layout - when the screen is less than 400px wide, make the navigation links stack on top of each other instead of next to each other */
    @media screen and (max-width: 400px) {
        .navbar a {
            float: none;
            width: 100%;
        }
    ´´´
    
    Or something like that, my code does not correspond to your example but try.
    

    【讨论】:

      【解决方案4】:

      在 css 中使用媒体查询 => @media。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2018-01-23
        • 2020-09-25
        相关资源
        最近更新 更多