绝对定位的孩子没有得到父母的背景颜色

时间:2016-09-15 18:28:09

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

我有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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

由于您已经在使用flexbox,为什么不使用flex属性将“Read more”按钮固定到底部。你不需要绝对定位。

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

.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将按钮固定在底部。