重叠式div中的文本格式

时间:2018-07-25 15:05:01

标签: html css overlay

我正在尝试在图像上创建悬停叠加层。我从那里得到了代码; https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade

我的文字有问题,我想在其下放一个小标题和一个句子说明。我似乎不太适合它,因为新行将上述文本进一步推了上去,而将其放在一个段落标签上会使它泛滥。

我正在使用wordpress,custom-css插件和WYSIWYG编辑器。

.container {
  float: left;
  position: relative;
  width: 25%;
}

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

.overlay {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  background-color: #edede7;
  overflow: hidden;
  width: 100%;
  height: 0;
  transition: .5s ease;
}

.container:hover .overlay {
  bottom: 0;
  height: 100%;
}

.text {
  white-space: pre;
  color: #000000;
  font-size: 20px;
  position: absolute;
  overflow: auto;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}
<div class="container">
  <img class="image" src="https://www.ultraframehomeimprovements.co.uk/wp-content/uploads/2018/07/Shakletons.png" alt="logo" width="733" height="494" />
  <div class="overlay">
    <div class="text">
      <p><span style="font-size: small;">Inspiring products of indoor and outdoor furniture</span>
      </p>
    </div>
  </div>
</div>

如何使文本“适合”到悬停框中?

3 个答案:

答案 0 :(得分:1)

您需要删除<p><span>标签并删除以下行:

   white-space: pre;

并替换为:

   font-size: 20px;

与此:

   font-size: small;

这就是您要解决的全部,但是我也有自由地简化您的代码。

.container {
  float: left;
  position: relative;
  width: 25%;
}

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

.overlay {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  background-color: #edede7;
  width: 100%;
  height: 0;
  transition: .5s ease;
  overflow: hidden;
}

.container:hover .overlay {
  bottom: 0;
  height: 100%;
  overflow: visible;
}

.text {
  position: absolute;
  font-size: small;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="container">
  <img class="image" src="https://via.placeholder.com/500x500" alt="logo"/>
  <div class="overlay">
    <div class="text">
      Inspiring products of indoor and outdoor furniture
    </div>
  </div>
</div>

答案 1 :(得分:0)

看看这个:

我删除了white-space: pre;,这阻碍了文本换行,我也删除了overflow属性,该属性或多或少地放在了所有地方,然后我更改了图像而没有白色帆布,并更改了一些高度和宽度以适合容器。

.container {
  float: left;
  position: relative;
  width: 25%;
  overflow: hidden;
}
p {margin:0}
.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  background-color: #edede7;
  width: 100%;
  transition: .5s ease;
}

.container:hover .overlay {
  bottom: 0;
  height: 100%;
}

.text {
  color: #000000;
  font-size: 20px;
  text-align:center;
  float: left;
  text-align: justify;
  padding: 0 10px;
  margin-top: 50%;
  transform: translateY(-50%);
}
<div class="container">
  <img class="image" src="http://via.placeholder.com/400x400" alt="logo" width="100%" />
  <div class="overlay">
    <div class="text">
      <p><span style="font-size: small;">Inspiring products of indoor and outdoor furniture</span>
      </p>
    </div>
  </div>
</div>

答案 2 :(得分:0)

你是这个意思吗?

.container {
  float: left;
  position: relative;
  width: 25%;
}

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

.overlay {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  background-color: #edede7;
  overflow: hidden;
  width: 100%;
  height: 0;
  transition: .5s ease;
}

.container:hover .overlay {
  bottom: 0;
  height: 100%;
}

.text {
  color: #000000;
  position: absolute;
  font-size: small;
  text-align: justify;
  padding: 0 10px;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
}
<div class="container">
  <img class="image" src="http://via.placeholder.com/500x350" alt="logo" width="733" height="494" />
  <div class="overlay">
    <div class="text">Inspiring products of indoor and outdoor furniture
    </div>
  </div>
</div>
<div class="container">
  <img class="image" src="http://via.placeholder.com/300x150" alt="logo" width="733" height="494" />
  <div class="overlay">
    <div class="text">Inspiring products of indoor and outdoor furniture
    </div>
  </div>
</div>
<div class="container">
  <img class="image" src="http://via.placeholder.com/600x550" alt="logo" width="733" height="494" />
  <div class="overlay">
    <div class="text">Inspiring products of indoor and outdoor furniture
    </div>
  </div>
</div>