css 将内容扩展到顶部

jtoj6r0c  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(194)

如何将内容从底部扩展到顶部?我尝试将文本放到顶部,但它总是放到底部。下面是我的代码=〉

.box {
  width: 200px;
  height: 200px;
  background-color: red;
  position: absolute;
}

.div {
  position: relative;
  transition: .35s ease-in-out 0s;
  height: 0;
  width: 70%;
  left: 50%;  
  top: 100px;
  transform: translateX(-50%);
  text-align: center;
  z-index: 3;
}

.text {
  position: relative;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  box-shadow: 0 0 0 1px black;
  line-height: 155%;
  padding: 0.2em 0.3em;
  color: white;
  background-color: black;
  background-color: rgba(0, 0, 0, .8);
  border-radius: 3px;
  font-size: 1.125rem;
}
<div class="box">
  <div class="div">
    <p class="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </div>
</div>

我的预期输出为=〉

我知道我在.div中使用了top: 100px,但这只是为了创建一个示例,我的目标是无论配置如何,文本总是位于顶部。
我尝试操作height, top and bottom,但没有成功,我该怎么办?谢谢。

uurv41yg

uurv41yg1#

您可能需要交换.box类和.div类中的position: relative;position: absolute;,如下所示:

.box {
  width: 200px;
  height: 200px;
  background-color: red;
  position: relative;
}

.div {
  position: absolute;
  transition: .35s ease-in-out 0s;
  height: 0;
  width: 70%;
  left: 50%;  
  transform: translateX(-50%);
  text-align: center;
  z-index: 3;
}

.text {
  position: relative;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  box-shadow: 0 0 0 1px black;
  line-height: 155%;
  padding: 0.2em 0.3em;
  color: white;
  background-color: black;
  background-color: rgba(0, 0, 0, .8);
  border-radius: 3px;
  font-size: 1.125rem;
}
<div class="box">
  <div class="div">
    <p class="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </div>
</div>
goqiplq2

goqiplq22#

你有很多不必要的代码。和混乱的位置相对和绝对:d在这里它是工作良好https://jsfiddle.net/ue6s3ap5/5/

* {
  margin: 0;
  padding: 0;
}

.box {
  width: 200px;
  height: 200px;
  background-color: red;
  position: relative;
}

.div {
  position: absolute;
  margin: 20px
}

.text {
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  box-shadow: 0 0 0 1px black;
  line-height: 155%;
  padding: 0.2em 0.3em;
  color: white;
  background-color: rgba(0, 0, 0, .8);
  border-radius: 3px;
  font-size: 1.125rem;
}
<div class="box">
  <div class="div">
    <p class="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </div>
</div>

相关问题