html - 鉴于它是响应式的,我如何才能将我的文本置于图像之上?

标签 html css image responsive-design

文本已经在图像的顶部,但是当您调整页面大小时图像的大小会发生变化。你怎么能让文本调整它的中心到图像的高度?

这是我得到的

HTML

<section id="eyeCatcher">
   <div id="catchContainer" >
       <div id="eyeCatchQuote">
        <h1>text</h1>
       </div>

       <span id="eyeCatchPhoto" role="img" >
           <span class="inner">
           </span>
       </span>
   </div>
</section>

CSS

#eyeCatcher{
   max-width: 1366px; 
   margin: 0 auto; 
}

#catchContainer {
   max-width: 1366px; 
}

#catchContainer #eyeCatchPhoto {
   width: 100%;
   display: inline-block;
   vertical-align: middle;
   font: 0/0 serif;
   text-shadow: none;
   color: transparent;
   background-size: 100%;
   background-position: 50% 50%;
   background-repeat: no-repeat;
}

#catchContainer #eyeCatchPhoto .inner{
   padding-top: 60%;
   display: block; 
   height: 0; 
}

#catchContainer #eyeCatchPhoto{
   background-image: url("../img/overview.jpeg");
}

#eyeCatchQuote{
   margin-top: -26px; 
   position: relative; 
   top: 300px; 
}

这就是它的样子

Big

Resize to small

最佳答案

您可以使用实际图像,而不是使用带背景图像的 span。然后可以使用绝对定位使文本居中。

HTML:

<section id="eyeCatcher">
 <div id="catchContainer">
    <div id="eyeCatchQuote">
      <h1>TEXT ELEMENT</h1>
    </div>
    <img src="YOURIMage" id="eyeCatchPhoto" role="img">
  </div>
</section>

CSS:

#eyeCatcher {
  max-width: 1366px;
  margin: 0 auto;
}
#catchContainer {
  max-width: 1366px;
  position: relative;
}
#catchContainer #eyeCatchPhoto {
 width:100%;
}
#eyeCatchQuote {
 position: absolute;
 top: 50%;
 left:50%;
 margin:0;
 transform:translateX(-50%)translateY(-50%);
}

这是一个fiddle

关于html - 鉴于它是响应式的,我如何才能将我的文本置于图像之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32999358/

相关文章:

javascript - rcarousel 如何合并 jquery 语句

css - RTL bulma CSS 导航栏

CSS居中给出不同的结果

java - 显示图像数据的直方图

image - 图像的步幅和填充

javascript - asp.net 使用 javascript 打印页面

javascript - jQuery可变宽度到高度调整与自定义滚动条

html - 我的复选框或单选按钮没有正确对齐?

html - 除非我明确设置容器宽度,否则嵌套的 UL 列不会向左浮动

image - Kubernetes kubectl 设置镜像部署无法--记录历史记录?