html - Bootstrap - 2列 - 在窄屏幕上使图像全宽
问题描述
使用下面的简单 HTML,640 像素正方形的图像出现在左栏中,其他内容出现在右栏中。
有没有一种方法可以控制 HTML / CSS 的响应特性,以便当屏幕宽度 < 375 像素时,左侧栏中的图像不会越来越小,非图像内容会下降到图像下方并且图像占据了视图屏幕的整个宽度?
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row">
<div class="col">
<img src="https://via.placeholder.com/640" class="img-fluid">
</div>
<div class="col">
<h2>Section 1</h2>
<p>Something happened and something else happened and then it was the end.</p>
</div>
</div>
<hr />
<div class="row">
<div class="col">
<img src="https://via.placeholder.com/640" class="img-fluid">
</div>
<div class="col">
<h2>Section 2</h2>
<p>Nothing happens without blue cheese sandwiches being served first.</p>
</div>
</div>
</div>
解决方案
只需添加一个媒体查询 css 实体,如下面的代码片段所示。如果屏幕尺寸为 375 像素或更小,这将覆盖引导网格并将其替换为单列网格。
强烈推荐 480 像素,因为它是智能手机纵向的最高宽度。
@media only screen
and (max-width: 375px) {
.container-fluid .row {
display: grid;
grid-template-columns: 1fr;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row">
<div class="col">
<img src="https://via.placeholder.com/640" class="img-fluid">
</div>
<div class="col">
<h2>Section 1</h2>
<p>Something happened and something else happened and then it was the end.</p>
</div>
</div>
<hr />
<div class="row">
<div class="col">
<img src="https://via.placeholder.com/640" class="img-fluid">
</div>
<div class="col">
<h2>Section 2</h2>
<p>Nothing happens without blue cheese sandwiches being served first.</p>
</div>
</div>
</div>
推荐阅读
- java - Hibernate 级联持久性:子对象被保存但 ID 未分配(仅在一种情况下)
- deep-learning - 用于图像识别的预训练卷积神经网络的硬件要求
- yii2 - Yii2 relationship hasOne
- javascript - 如果文本溢出,如何在列中添加水平滚动条,否则它将被隐藏
- vue.js - vue:如何使传递给组件的对象反应?
- android - 如何保护我的 SQLite 数据库
- ios - AVAudioEngine 初始化错误
- ionic-framework - 如何从 ionic 3 中的推送通知中检测应用程序打开?
- angular - 使用角度将隐藏参数传递给外部链接
- python - tkinter 中的新窗口标题