html - 使用 css 将 div 移动到其上方的行中
问题描述
我的代码连续有 3 个 div(图库、侧边栏、描述)。HTML 需要保持不变,但我需要使用 CSS 将 .description 放在 .sidebar 下方(.gallery 旁边)而不是 .gallery 下方。 我想像这样移动那个 div 代码:
<div class="product">
<div class="gallery">
<img src="https://via.placeholder.com/300" alt="item" />
</div>
<div class="sidebar">
<h3>
Sidebar
</h3>
<p>
Product price, etc.
</p>
</div>
<div class="description">
<h3>
Details
</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer sed eros sem. Aliquam erat volutpat. Phasellus auctor lorem dolor, vitae egestas neque vestibulum sed. Proin sapien purus, faucibus ut elementum eget, consequat sed arcu. Morbi nisl libero,
molestie eget ligula quis, feugiat iaculis felis. Donec condimentum, felis eu sodales interdum, ex purus convallis augue, quis sollicitudin nibh ex vel lorem. Sed eget semper ipsum, vel dictum lorem. Proin ornare massa elit, non aliquam erat ultricies
at.
</p>
</div>
</div>
.product {
box-sizing: border-box;
position: static;
}
.gallery {
box-sizing: border-box;
float: left;
position: relative;
}
.sidebar {
box-sizing: border-box;
float: right;
position: static;
}
.description {
box-sizing: border-box;
clear: left;
float: left;
position: static;
}
解决方案
如果您愿意使用浮动,请设置元素的宽度。这是小提琴http://jsfiddle.net/y6g4p7u8/1/ 我已经设置了视觉显示的背景颜色。
.product {
box-sizing: border-box;
background: green;
}
.product:before,
.product:after {
content: " ";
display: table;
}
.product:after {
clear: both;
}
.gallery {
box-sizing: border-box;
width: 35%;
float: left;
background: red;
}
.sidebar {
box-sizing: border-box;
float: left;
width: 65%;
background: lightblue;
}
.description {
box-sizing: border-box;
float: left;
width: 65%;
background: yellow;
}
推荐阅读
- desktop-bridge - 为什么我打包的 WinForms App 需要 60 秒才能启动?
- amazon-cognito - AWS Cognito - 包括 OTP 过期时间戳
- javascript - 如何在 Discord.js 中将一组成员角色分成十个
- regex - Powershell重命名包含方括号的动态文件名,来自目录中的文件类型扫描
- javascript - JavaScript 普通函数与箭头函数 - 如何设置新范围
- python - Python:有效地创建 datetime.date 类型的列表,如 R 中的“lubridate”
- javascript - 订阅两个可观察对象,即在另一个可观察 HTTP 中可观察
- javascript - 你能帮助初学者解决为 javscript 设置 firebase 数据库的问题吗?
- reactjs - 嵌套块是多余的警告 ESLint
- regex - 如何更改此正则表达式以支持空格和连字符