调整浏览器大小时,图像上方的文本不起作用
Text over image does not work when browser resized
背景
我需要一些基本的文字图片为我的网站。我首先使用rgb和rgba函数给出了一个透明的黑盒。然后通过将图像声明为"相对",将文本声明为"绝对"来放置文本。
问题
当我调整浏览器的大小时,文本会向下超出图像。如所示https://jsfiddle.net/Lheg26kw/这是html-
<div class="hero-container">
<div class="hero-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/0/05/Old-Style_Balinese_Cat.png" width=100% height=80%>
<div class="hero-text">
<p>This is text<br>
<span>this is some more text for trial of this problem</span></p>
</div>
</div>
</div>
这里是CSS:
.hero-image{
position: relative;
width: 100%;
}
.hero-text p{
position: absolute;
text-align: center;
bottom: 0px;
top: 0px;
width: 100%;
background: rgb(0, 0, 0); /* fallback color */
background: rgba(0, 0, 0, 0.4);
padding-top: 80px;
color: white;
font-weight: bolder;
}
.hero-text span{
font-weight: normal;
}
需要
我需要文本在调整到最小数量后保持在图像上,即如果网站是通过手机访问的。
您可以将内容克隆到英雄下方的另一个div,然后隐藏。hero-text
$(window).on('resize', function() {
var $mobileContent = $('.mobile-content');
var $bodyWidth = $('body').width();
if ($bodyWidth < 620) {
var $innerHero = $('.hero-text').html();
$mobileContent.html($innerHero);
} else {
$mobileContent.empty();
}
}).trigger('resize');
body {margin: 0;}
.hero-image {
position: relative;
width: 100%;
}
.hero-text p {
position: absolute;
text-align: center;
bottom: 0px;
top: 0px;
width: 100%;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.4);
padding-top: 80px;
color: white;
font-weight: bolder;
}
.hero-text span {
font-weight: normal;
}
.mobile-content {
color: #000;
font-weight: 700;
text-align: center;
}
@media(max-width: 620px) {
.hero-text {
display: none;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="hero-container">
<div class="hero-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/0/05/Old-Style_Balinese_Cat.png" width=100% height=80%>
<div class="hero-text">
<p>This is text
<br>
<span>this is some more text for trial of this problem</span>
</p>
</div>
</div>
</div>
<div class="mobile-content"></div>
如果不在图像上设置最小大小,那么无论怎样,最终文本都不适合。但我建议您将padding-top: 80px;
从恒定值80px更改为类似padding-top: 25%;
的百分比。这将有助于随着图像大小的变化而更好地缩放。
这将起作用:(用这些替换你的css)
.hero-image{
position: relative;
width: 100%;
}
.hero-image img{
width:100%;
display:block;
}
.hero-text{
width:100%;
height:100%;
background: rgb(0, 0, 0); /* fallback color */
background: rgba(0, 0, 0, 0.4);
position: absolute;
top:0;
left:0;
}
.hero-text p{
position: absolute;
text-align: center;
top: 50%;
transform:translateY(-50%);
-webkit-transform:translateY(-50%);
-moz-transform:translateY(-50%);
-ms-transform:translateY(-50%);
width:100%;
padding: 5px 0;
margin:0;
color: white;
font-weight: bolder;
}
.hero-text span{
font-weight: normal;
}
相关文章:
- html/jquery输入类型:单选框中的文本不起作用
- AngularJS按钮文本不起作用
- 单选插件的占位符文本不起作用
- Javascript - 在将标签环绕后重新选择文本不起作用?所见即所得
- 动态更改选项文本不起作用
- 在 Javascript 中使用 setTimeout 移动文本不起作用
- 填充文本不起作用
- 返回了JS对象,但响应文本不起作用
- 使用 javascript 更改按钮文本不起作用
- 在画布上呈现文本不起作用
- 在 javascript 中更改标签的文本不起作用
- OrbitControls 和 dat.gui 文本不起作用
- 在当前卡雷位置插入文本不起作用(多个文本框)
- 使用 JavaScript 复制模型的输入文本不起作用
- 多选插件中的占位符文本不起作用
- 使用 jQuery .data() 设置元素的文本不起作用
- 我每隔几个小时使用 JavaScript 的自定义文本不起作用
- Javascript多个突出显示文本不起作用
- 更新文本不起作用
- 调整浏览器大小时,图像上方的文本不起作用