我如何设置HTML/CSS,使我的DIV跟随屏幕大小的宽度,但一旦它适合内容就停止扩展(当DIV不能完全包含内容时,它应该向左/向右滚动)。
伪代码:
HTML:
<div class="image-container">
<img width="1000">
</div>
CSS:
.image-container {
/* ??? */
display: inline-block; /* ??? */
overflow: auto;
}
使用CSS媒体查询设置各种屏幕大小。查看此页得源代码,以查看如何使用媒体查询。
为此,将父div width设置为
下面是Codepen演示
null
.parent {
background-color: green;
width: fit-content;
max-width: 100%;
overflow: scroll;
}
.child {
padding: 30px;
width: 700px;
background-color: red;
color: #fff;
}
<div class="parent">
<div class="child">
test string
</div>
</div>