首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用html和css在淡入覆盖图中居中显示文本?

如何使用html和css在淡入覆盖图中居中显示文本?
EN

Stack Overflow用户
提问于 2018-07-04 02:40:42
回答 1查看 680关注 0票数 0

我偶然发现了这个tutorial,它为我提供了有关如何创建图像的说明,当图像悬停在图像上时,该图像将转换为文本

但是,我的文本对于图像来说太大了,所以我试图减少悬停覆盖的边距。

我更改了.text类,使顶部和左侧的值为20%,还更改了transform属性和ms-transform属性。但现在的问题是,文本不再居中

请参阅下面的代码和codepen here

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #008CBA;
}

.container:hover .overlay {
  opacity: 1;
}

.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 20%;
  left: 20%;
  transform: translate(-20%, -20%);
  -ms-transform: translate(-20%, -20%);
  text-align: center;
}
</style>
</head>
<body>

<h2>Fade in Overlay</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="https://s4827.pcdn.co/wp-content/uploads/2016/03/stack_overflow_logo.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.


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

</body>
</html>

问题是文本没有居中(如下所示):

EN

回答 1

Stack Overflow用户

发布于 2018-07-04 02:54:23

克里斯

您只需编辑.text转换即可。

您已修复的代码在此处

代码语言:javascript
复制
    .text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  text-align: center;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51161097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档