嗯,这是因为你
text-block
div已定位
absolute
到
container
div,因此只有当该元素发生变化时
文本块
div将作出响应。您可以简单地通过更改的
container
元素,因为它是控制文本块位置的父级
这是代码:
.container {
position: relative;
font-family: Arial;
width: 50%; /* only line added. Change wight here instead of in img*/
}
.text-block {
position: absolute;
bottom: 20px;
right: 20px;
background-color: black;
color: white;
padding-left: 20px;
padding-right: 20px;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<img src="https://xofarragio.com/resources/graphics/placeholder_1920x1280_01.png" alt="Nature" style="width:100%;">
<div class="text-block">
<h4>Testing</h4>
<p>This is a test</p>
</div>
</div>
</body>
</html>