【问题标题】:How to center text within a fade in overlay using html and css?如何使用 html 和 css 在淡入淡出的叠加层中居中文本?
【发布时间】:2018-07-03 18:40:41
【问题描述】:

我遇到了这个tutorial,它为我提供了有关如何创建当图像悬停在其上时变成文本的图像的说明

但是,我的文字对于图像来说太大了,所以我正在尝试减少悬停叠加层的边距。

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

在下面查看我的代码和codepen here

<!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>

问题是文本没有居中(见下文):

【问题讨论】:

    标签: html css hover media-queries mousehover


    【解决方案1】:

    因此,根据您希望它的外观,我找到了两种不同的方式,很可能可以找到更多方式。

    1. 删除转换并添加right: 20%;,如下所示: https://codepen.io/anon/pen/YvMNje

    2. 去掉变换和左边:20%;它看起来像这样: https://codepen.io/anon/pen/xzegaq

    【讨论】:

      【解决方案2】:

      克里斯

      您只需要编辑您的 .text 转换。

      你的固定代码在这里

          .text {
        color: white;
        font-size: 20px;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        text-align: center;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-24
        • 2016-10-21
        • 2016-04-02
        • 1970-01-01
        • 2019-03-22
        • 2016-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多