【问题标题】:How to Overlap Div with Hero Image for Material-Design Look?如何将 Div 与英雄图像重叠以实现 Material Design 外观?
【发布时间】:2019-01-30 22:30:51
【问题描述】:

我试图让这个布局看起来更有材料(like this site),其中第一个 div 与英雄图像重叠。我尝试将.about-inner 中的边距设置为-100px 以尝试将其调高,但是当缩小到移动尺寸时它会变得有些混乱。有没有我想念的简单方法?

小提琴:https://jsfiddle.net/kq0x48fc/

<section class="hero">
      <div class="hero-inner">
        <h1>Lorem ipsum<br>dolor sit amet.</h1>
      </div>
</section>

<div class="main-outer">

        <div class="about-inner">
          <h3>「 About 」</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. </p>
        </div>

         <div class="clients-inner">
    <h3>「 Clients 」</h3>
    <div class="flex-container">
      <div>
        <img src="./Assets/Clients/logo.jpg" alt="1" />
      </div>
      <div>
        <img src="./Assets/Clients/logo.jpg" alt="2"  />
      </div>
      <div>
        <img src="./Assets/Clients/logo.jpg" alt="3"  />
      </div>
      <div>
        <img src="./Assets/Clients/logo.jpg" alt="4"  />
      </div>
      <div>
        <img src="./Assets/Clients/logo.jpg" alt="5"  />
      </div>
    </div>
  </div>

【问题讨论】:

    标签: html css material-design


    【解决方案1】:

    首先,您将负边距应用于错误的元素。您应该将其应用于.hero 之后的第一个孩子。

    致.about-inner。

    另外,您在第二部分中使用的垂直居中技术(将两个元素居中在整个视口高度的空间中)在更宽(和更高)的屏幕上会影响这一点。要禁用它,只需从.main-outer 中删除min-height:

    body {
      margin: 0;
      font-family: 'Montserrat', serif;
    }
    
    h1 {
      font-size: 1.5em;
      font-weight: 600;
    }
    
    h6 {
      font-family: 'Montserrat', serif;
      font-size: 0.8em;
      font-weight: 200;
      display: block;
      text-align: center;
    }
    
    .header {
      height: 80px;
      display: flex;
      justify-content: left;
      /* align horizontal */
      align-items: center;
      /* align vertical */
    }
    
    .hero {
      /* Sizing */
      width: 100%;
      min-height: 60vh;
      /* Flexbox Stuff */
      display: flex;
      justify-content: center;
      align-items: center;
      padding-bottom: 50px;
      /* Text styles */
      text-align: center;
      /* Background styles */
      background-color: red;
      background-size: cover;
      background-position: center center;
      background-repeat: no-repeat;
      background-attachment: fixed;
    }
    
    .flex-container {
      display: flex;
      flex-flow: row wrap;
      justify-content: center;
      background: white;
      padding: 5px;
    }
    
    .flex-container>* {
      margin: auto;
    }
    
    .flex-container img {
      display: block;
      width: 150px;
      height: auto;
      margin: 5px;
    }
    
    .main-outer {
      display: flex;
      justify-content: center;
      flex-direction: column;
      justify-content: safe center;
      align-items: center;
      background-color: #eeeeee;
      position: relative;
      box-sizing: border-box;
      padding-bottom: 50px;
    }
    
    .main-outer>* {
      width: 80vw;
      box-shadow: 0 3px 5px -1px rgba(0, 0, 0, .1), 0 5px 8px 0 rgba(0, 0, 0, .07), 0 1px 14px 0 rgba(0, 0, 0, .06)
    }
    
    .about-inner {
      margin-top: -50px;
      margin-bottom: 10px;
      width: 80vw;
      background-color: white;
      display: inline-block;
      justify-content: center;
      align-items: center;
      text-align: center;
    }
    
    .clients-inner {
      background-color: white;
      display: inline-block;
      justify-content: center;
      align-items: center;
      text-align: center;
    }
    
    .about-inner h3,
    .clients-inner h3 {
      font-size: 1.5em;
      font-weight: 600;
      padding-top: 10px;
      letter-spacing: 0.1em;
    }
    
    .about-inner p,
    .clients-inner p {
      font-size: 1em;
      font-weight: 300;
      text-align: left;
      padding: 0 10vw 1em;
      line-height: 2em;
    }
    
    .hero h1 {
      /* Text styles */
      font-size: 3em;
      color: white;
      /* Margins */
      margin-bottom: 0.5em;
      padding: 20px;
    }
    <section class="hero">
      <div class="hero-inner">
        <h1>Lorem ipsum<br>dolor sit amet.</h1>
      </div>
    </section>
    
    <div class="main-outer">
    
      <div class="about-inner">
        <h3>「 About 」</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. </p>
      </div>
    
      <div class="clients-inner">
        <h3>「 Clients 」</h3>
        <div class="flex-container">
          <div>
            <img src="./Assets/Clients/logo.jpg" alt="1" />
          </div>
          <div>
            <img src="./Assets/Clients/logo.jpg" alt="2" />
          </div>
          <div>
            <img src="./Assets/Clients/logo.jpg" alt="3" />
          </div>
          <div>
            <img src="./Assets/Clients/logo.jpg" alt="4" />
          </div>
          <div>
            <img src="./Assets/Clients/logo.jpg" alt="5" />
          </div>
        </div>
      </div>
    </div>

    我还对其进行了一些调整:

    • 将100px 水平填充更改为10vw
    • 在hero 中添加了一些bottom-padding 以抵消以下元素的负边距
    • 使用100% 作为.hero 的宽度而不是100vw 来消除水平条(这是由于body 在 Stack Overflow sn-ps 上有一个边距)。
    • 将软box-shadow 应用于卡片式元素

    【讨论】:

    • 这是完美的。谢谢!
    【解决方案2】:

    您可以使用负边距将内容拉到上面的元素上...

    .main-outer {
        margin-top: -100px;
    }
    

    或者您可以使用no-repeat 的主体图像作为主体背景,并将内容放置在您想要的页面下方。

    这是一个更简单的页面结构示例 .page div 代表你的身体。

    .page {
      height: 500px;
      width: 100%;
      position: relative;
      background: tan;
      }
      
    .hero {
      width: 100%;
      height: 40%;
      background: blue url(https://images.unsplash.com/photo-1523575518836-9166d367179f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2534&q=80) top center no-repeat;
      background-size: cover;
    }
    .content {
      background-color: white;
      min-height: 600px;
      margin: -100px auto 0 auto;
      width: 80%;
      border-radius: 10px;
      padding: 20px;
    }
    <div class="page">
      <div class="hero"></div>
      <div class="content">
        Some content
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2018-10-15
      • 2021-08-30
      相关资源
      最近更新 更多