如何对齐文本中心-在CSS中不起作用

阿比拉姆

我正在尝试使用以下代码对齐文本中心,但无法正常工作。我的link代码和代码:

body {
  padding: 0;
  margin: 0;
}

.container {
  width: 1200px;
  overflow: auto;
  margin: 240px auto;
  background: transparent;
  position: relative;
}

.container .box {
  position: relative;
  float: left;
  width: calc(400px - 30px);
  height: calc(300px - 30px);
  background: yellow;
  overflow: hidden;
  margin: 15px;
  border-radius: 10px;
  box-sizing: border-box;
}

.container .box .icon {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: red;
  transition: 0.5s;
  z-index: 1;
}

.container .box:hover .icon {
  top: 20px;
  left: calc(50% - 40px);
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.container .box .icon .fa {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 80px;
  color: #fff;
  transition: 0.5s;
}

.container .box:hover .icon .fa {
  font-size: 40px;
}

.container .box .content {
  position: absolute;
  top: 100px;
  height: calc(100% - 100px);
  padding: 20px;
  box-sizing: border-box;
  transition: 0.5s;
  text-align: center;
}
<div class="container">
  <div class="box">
    <div class="icon">
      <i class="fa fa-search" aria-hidden="true"></i>
    </div>
    <div class="content">

      <h3>Search</h3>
      <p>sadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfen</p>

    </div>
  </div>
</div>

小羊羔

加入width: 100%content到元素适合绝对定位的元素在其容器-见下面的演示:

body {
  padding: 0;
  margin: 0;
}

.container {
  width: 1200px;
  overflow: auto;
  margin: 240px auto;
  background: transparent;
  position: relative;
}

.container .box {
  position: relative;
  float: left;
  width: calc(400px - 30px);
  height: calc(300px - 30px);
  background: yellow;
  overflow: hidden;
  margin: 15px;
  border-radius: 10px;
  box-sizing: border-box;
}

.container .box .icon {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: red;
  transition: 0.5s;
  z-index: 1;
}

.container .box:hover .icon {
  top: 20px;
  left: calc(50% - 40px);
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.container .box .icon .fa {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 80px;
  color: #fff;
  transition: 0.5s;
}

.container .box:hover .icon .fa {
  font-size: 40px;
}

.container .box .content {
  position: absolute;
  top: 100px;
  height: calc(100% - 100px);
  padding: 20px;
  box-sizing: border-box;
  transition: 0.5s;
  text-align: center;
  width: 100%; /* ADDED */
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <div class="box">
    <div class="icon">
      <i class="fa fa-search" aria-hidden="true"></i>
    </div>
    <div class="content">
      <h3>Search</h3>
      <p>sadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfen</p>
    </div>
  </div>
</div>

实际上不需要在这里定位-使用flexbox并将其与底部对齐

  1. 取出height时,绝对 positiontopcontent
  2. 制作box一个flexbox并添加align-items: flex-end以将其推到底部。

请参见下面的演示:

body {
  padding: 0;
  margin: 0;
}

.container {
  width: 1200px;
  overflow: auto;
  margin: 240px auto;
  background: transparent;
  position: relative;
}

.container .box {
  position: relative;
  float: left;
  width: calc(400px - 30px);
  height: calc(300px - 30px);
  background: yellow;
  overflow: hidden;
  margin: 15px;
  border-radius: 10px;
  box-sizing: border-box;
  display: flex; /* ADDED */
  align-items: flex-end; /* ADDED */
}

.container .box .icon {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: red;
  transition: 0.5s;
  z-index: 1;
}

.container .box:hover .icon {
  top: 20px;
  left: calc(50% - 40px);
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.container .box .icon .fa {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 80px;
  color: #fff;
  transition: 0.5s;
}

.container .box:hover .icon .fa {
  font-size: 40px;
}

.container .box .content {
  /*position: absolute;
  top: 100px;
  height: calc(100% - 100px);*/
  padding: 20px;
  box-sizing: border-box;
  transition: 0.5s;
  text-align: center;
  width: 100%; /* ADDED */
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <div class="box">
    <div class="icon">
      <i class="fa fa-search" aria-hidden="true"></i>
    </div>
    <div class="content">
      <h3>Search</h3>
      <p>sadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfensadfdfoiljwkjfen</p>
    </div>
  </div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章