html - 如何让我的 div 在窗口/屏幕大小更改时换行?
问题描述
我知道如何包装物品(理论上),但我一定是做错了什么。这些都是拒绝的。
我只想要两个并排的配置文件,当窗口宽度缩小时,它们会堆叠成一列。此刻,它们有点挤在一起并重叠。
#profile-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: center;
padding-bottom: 5%;
}
.profile-desc {
width: 35%;
margin: 5% 5%;
text-align: center;
}
#profileM {
height: 230px;
width: 219px;
}
#profileF {
height: 230px;
width: 219px;
}
<div id="profile-container">
<div class="profile-desc" style="float:left;width:35%;padding: 5px;">
<img src="images/male.jpg" id="profileM">
<h3 style="color:white;background-color:rgb(244,212,69);">
Name
</h3>
<h4 style="color: rgb(244,212,69);">
Occupation
</h4>
<p>
Description
</p>
</div>
<div class="profile-desc" style="float:right;width:35%; padding: 5px;">
<img src="images/female.jpg" id="profileF">
<h3 style="color:white;background-color:rgb(244,212,69);">
Name
</h3>
<h4 style="color: rgb(244,212,69);">
Occupation
</h4>
<p>
Description
</p>
</div>
</div>
解决方案
您正在寻找的是CSS 媒体查询,它允许您在满足有关设备的某些条件(例如宽度或方向)时应用 CSS。
我调整了您的代码,添加了一个媒体查询行(我还删除了一些不需要的 flex 属性并删除了一些多余的内联样式.profile-desc
)
#profile-container {
padding-bottom: 5%;
}
.profile-desc {
float: left;
width: 35%;
margin: 5% 5%;
text-align: center;
}
#profileM {
height: 230px;
width: 219px;
}
#profileF {
height: 230px;
width: 219px;
}
@media(max-width: 580px) {
.profile-desc {
width: 100%;
margin: 0;
}
}
<div id="profile-container">
<div class="profile-desc">
<img src="images/male.jpg" id="profileM">
<h3 style="color:white;background-color:rgb(244,212,69);">
Name
</h3>
<h4 style="color: rgb(244,212,69);">
Occupation
</h4>
<p>
Description
</p>
</div>
<div class="profile-desc">
<img src="images/female.jpg" id="profileF">
<h3 style="color:white;background-color:rgb(244,212,69);">
Name
</h3>
<h4 style="color: rgb(244,212,69);">
Occupation
</h4>
<p>
Description
</p>
</div>
</div>
推荐阅读
- javascript - Ajax 在 Localhost 上工作,但不在 Live Server 上
- java - 引起:java.lang.SecurityException:用户10110和当前进程都没有android.permission.CLEAR_APP_CACHE
- r - Complete() r 中时间序列的空列 (Tidyr)
- javascript - 为什么 javascript const 关键字在数组中不起作用?
- javascript - javascript在一行之后运行行
- ruby-on-rails - Sidekiq 守护进程在几秒钟后死亡
- unicode - UTF-8 编码标准中的第一位(即二进制 0)是什么意思?
- php - 使用mysql将新记录插入另一个表中的最高值的表中?
- c# - 从文件中检索字符串,当多行包含完全相同的字符串时使用 Linq 进行过滤
- c# - c# ADO 为 SQL INSERT TRIGGER 设置 context_info