简体   繁体   English

响应文本覆盖在响应图像之上

[英]Responsive text overlay on top of a responsive image

Utilizing bootstrap, I have a responsive image and some overlay text/button that I would like placed in the center of the image.利用引导程序,我有一个响应式图像和一些我希望放置在图像中心的覆盖文本/按钮。 However, despite the text being w/in the div of the img tag, the text doesn't stay in the center when the screen size is shrunk.然而,尽管文本在 img 标签的 div 中,但当屏幕尺寸缩小时,文本不会留在中心。 What it's supposed to look like:它应该是什么样子: 全屏模式

What it looks like in responsive:响应式中的样子: 响应模式

HTML code: HTML代码:

 <div id="container-a" class="img-content">
        <img src="../img/file.large.jpg" class="img-fluid" alt="workout world img" width="1440" height="600">
            <span class="text-box-a container px-4 text-center z-2 align-items-center">
            <div class="text-box-b">
                <h1 class="main-text f-30 f-sm-50 f-md-75 f-xl-75 text-center font-weight-bold animated fadeInUpSmooth" style="font-size:6vw;">Gains are simply one click away!</h1>
                <button type="button" class="btn btn-info" style="font-size:3vw;">Ready?</button>
            </div>
            </span>
 </div>

CSS code: CSS代码:

#container-a img {
  position: relative;
  display: block;
  z-index: 1;
  top: 0px;
  left: 0px; 
  padding-bottom: 20px;
}
.img-content {
  display: inline;
}

@media screen and (min-width: 601px) {
 .text-box-a {
   font-size: 80px;
 }
}
  @media screen and (max-width: 600px) {
.text-box-a {
  font-size: 30px;
 }
}

.text-box-a {
  z-index: 20; 
  top: 50%; 
  left: 50%;
  overflow: hidden;
  position: absolute;
  align-items: center !important;
  text-align: center !important;
  transform: translate(-50%, -50%); 
  text-shadow: 2px 2px #585858;
}
.text-box-b {
  background: rgb(204, 204, 204);
  background: rgba(204, 204,204, .1);  
}

.img-content .main-text {
   z-index: 2;
}

Your problem is .text-box-a using % measurement for top , also unsure what the overflow property is for on .text-box-a if there is no specified width / height .您的问题是.text-box-a使用%测量top ,如果没有指定的width / height ,也不确定.text-box-a上的overflow属性是什么。

Also I would advise a to use the banner image as a background-image:url();另外我建议使用横幅图像作为background-image:url(); instead of using a img tag而不是使用img标签

<div class="container">
<div class="text-box-a"></div>
</div>

.container{
display:flex;
justify-content: center
width:100%;
background-image:url(image.jpg);
background-size:contain;
height:/*height of banner img*/
}
.text-box-a{
padding:;
;
}

Try to structure your top banner like this.尝试像这样构建顶部横幅。 Use padding to position the children inside your banner image.使用padding将子padding定位在横幅图像内。 And use display:flex;并使用display:flex; and justify-content:center;justify-content:center; to center your items.将您的项目居中。

There's a variety of ways to solve your issue, but here are solutions I use.有多种方法可以解决您的问题,但这里是我使用的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM