【问题标题】:Absolutely positioned child not getting parent's background color绝对定位的孩子没有得到父母的背景颜色
【发布时间】:2016-09-16 03:13:42
【问题描述】:

我有 4 个按钮(“阅读更多”)在它们父母底部的同一级别对齐。

每一个都有不同的内容,所以为了让它们都具有相同的大小,我使用了 flexbox 和绝对定位。

现在的问题是我希望每个父母都有一个背景颜色和按钮留在里面,但他们留在外面,因为我告诉他们在每列的底部。

我有点卡住了,因为如果我删除按钮的绝对定位,父母将再次有不同的尺寸,我不希望他们有一个确定的响应式设计的高度。

Here's the working fiddle

.col-3 {
  width: 25%;
  background-color:red
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

*::before,
*::after {
  box-sizing: border-box;
}

[class*="col-"] {
  float: left;
}

.row::after {
  content: "";
  clear: both;
  display: block;
}

#services {
  background-color: rgb(265, 265, 265);
  color: #424242;
}

#services .col-3 {
  padding: 0 20px 20px;
  text-align: left;
}

#services .row .col-3:first-child {
  margin-left: 0
}

#services .right-align {
  text-align: right;
}

#services [class^="flaticon-"]:before,
#services [class^="flaticon-"]:after,
#services [class*="flaticon-"]:before,
#services [class*="flaticon-"]:after {
  font-size: 60px;
  margin-left: 0;
  -webkit-transition: color 0.8s;
  transition: color 0.8s;
}

#services [class*="flaticon"]:hover {
  color: #1AC4F8
}

#sevices h3 {
  margin: 10px 0
}

.btn {
  display: inline-block;
  font-family: 'Oswald', sans-serif;
  font-weight: 400px;
  padding: 5px 10px;
  border: 2px solid #1AC4F8;
  color: #1AC4F8;
  cursor: pointer;
  -webkit-transition: color 0.8s, background-color 0.8s;
  transition: color 0.8s, background-color 0.8s;
}
.row {
  display: flex;
  flex-direction: row;
  padding-bottom:40px;
}
.column {
  flex: 0 0 auto;
  position: relative;
}
.btn-bottom {
  position: absolute;
  bottom: -40px;
  left: 0px;
  right: 0px;
  text-align:center;
}
<section id="services">
  <!-- Services Starts Here -->
  <div class="wrapper">
    <h2>SERVICES</h2>
    <div class="divider"></div>
    <div class="row">
      <div class="col-3 column">
        <h3>Consulting and audit</h3>
        <p>Get help to accelerate your company online from our highly experienced specialists. It is as good as it sounds!</p>
        <a href="#" class="btn btn-bottom">Read more</a>
      </div>
      <div class="col-3 column">
        <h3>Web Development</h3>
        <p>Professional custom e-commerce and web design to let your business grow at a rapid pace. See how we do that.</p>
        <a href="#" class="btn btn-bottom">Read more</a>
      </div>
      <div class="col-3 right-align column">
        <h3>Search Engine Optimization</h3>
        <p>Want to be on Page 1 of Google and Bing? Read about our SEO services that drive more potential customers.</p>
        <a href="#" class="btn btn-bottom">Read more</a>
      </div>
      <div class="col-3 right-align column">
        <h3>Pay Per Click Management</h3>
        <p>Attract highly relevant audience with Google Adwords, Display, Retargeting, Facebook, YouTube, Twitter.</p>
        <a href="#" class="btn btn-bottom">Read more</a>
      </div>
    </div>
  </div>
</section>

【问题讨论】:

    标签: html css flexbox css-position background-color


    【解决方案1】:

    既然您已经在使用 flexbox,为什么不使用 flex 属性将“阅读更多”按钮固定在底部。你不需要绝对定位。

    尝试将.col-3 设为嵌套的弹性容器:

    .col-3 {
        display: flex;
        flex-direction: column;
        width: 25%;
        background-color: red
    }
    
    .btn {
        margin-top: auto;
    }
    

    .row {
      display: flex;
      flex-direction: row;
      padding-bottom: 40px;
    }
    .col-3 {
      display: flex;
      flex-direction: column;
      width: 25%;
      background-color: red
    }
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    *::before,
    *::after {
      box-sizing: border-box;
    }
    #services {
      background-color: rgb(265, 265, 265);
      color: #424242;
    }
    #services .col-3 {
      padding: 0 20px;
    }
    #services .row .col-3:first-child {
      margin-left: 0
    }
    #services .right-align {
      text-align: right;
    }
    #services [class^="flaticon-"]:before,
    #services [class^="flaticon-"]:after,
    #services [class*="flaticon-"]:before,
    #services [class*="flaticon-"]:after {
      font-size: 60px;
      margin-left: 0;
      -webkit-transition: color 0.8s;
      transition: color 0.8s;
    }
    #services [class*="flaticon"]:hover {
      color: #1AC4F8
    }
    #sevices h3 {
      margin: 10px 0
    }
    .btn {
      margin-top: auto;
      font-family: 'Oswald', sans-serif;
      font-weight: 400px;
      padding: 5px 10px;
      border: 2px solid #1AC4F8;
      color: #1AC4F8;
      cursor: pointer;
      transition: color 0.8s, background-color 0.8s;
    }
    <section id="services">
      <!-- Services Starts Here -->
      <div class="wrapper">
        <h2>SERVICES</h2>
        <div class="divider"></div>
        <div class="row">
          <div class="col-3 column">
            <h3>Consulting and audit</h3>
            <p>Get help to accelerate your company online from our highly experienced specialists. It is as good as it sounds!</p>
            <a href="#" class="btn btn-bottom">Read more</a>
          </div>
          <div class="col-3 column">
            <h3>Web Development</h3>
            <p>Professional custom e-commerce and web design to let your business grow at a rapid pace. See how we do that.</p>
            <a href="#" class="btn btn-bottom">Read more</a>
          </div>
          <div class="col-3 right-align column">
            <h3>Search Engine Optimization</h3>
            <p>Want to be on Page 1 of Google and Bing? Read about our SEO services that drive more potential customers.</p>
            <a href="#" class="btn btn-bottom">Read more</a>
          </div>
          <div class="col-3 right-align column">
            <h3>Pay Per Click Management</h3>
            <p>Attract highly relevant audience with Google Adwords, Display, Retargeting, Facebook, YouTube, Twitter.</p>
            <a href="#" class="btn btn-bottom">Read more</a>
          </div>
        </div>
      </div>
    </section>

    revised fiddle

    此解决方案将所有弹性项目放置在容器开头的列中(justify-content: flex-start 是默认值)。然后按钮用flex auto margin固定在底部。

    【讨论】:

    • 就是这样!谢谢!
    • 最后一件事,我一直试图弄清楚“margin-top:auto”如何在这里工作,但无法猜测。我对 flexbox 不是很熟悉。
    • 我在回答中提供了一个链接。单击文本“flex auto margin”。又来了:stackoverflow.com/q/32551291/3597276
    • 太棒了!谢谢!
    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2014-08-27
    • 2019-06-20
    • 2022-01-12
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多