html - 我可以在前一个元素旁边浮动一个元素吗?
问题描述
考虑以下带有侧边栏的沼泽标准段落布局。布局有两个问题:首先,浏览器首先会看到侧边栏(在我的例子中是导航的东西),导致屏幕阅读器和纯文本浏览器出现一些可访问性问题。所以元素应该在文章之后。但在那种情况下,我不能依赖久经考验的真实float:right
机制;所有的绝对定位都会导致问题。第二个问题与第一个有关;对于狭窄的视口,我不希望它漂浮在文本旁边并希望它放在后面。
我怎样才能获得相同的外观,但使用两个元素#main
和#sidebar
交换位置?
#main {
width: 100%;
}
#sidebar {
width: 150px;
height: 200px;
background-color: #abc;
float: right;
}
<div id="sidebar"></div>
<div id="main">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
解决方案
如果你可以使用 FlexBox,你可以使用order
它来左右交换。
我已经为你做了:
.wrapper {
display: flex;
}
#main {
width: 85%;
}
#sidebar {
width: 150px;
background-color: #abc;
order: 1;
}
<div class="wrapper">
<div id="main">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div id="sidebar"></div>
</div>
预览
推荐阅读
- go - 在golang中结合时间和日期?
- python - kivy 导入有什么问题?
- linux - 在附加扩展名并检查它是否存在后,如何解析块中的 mod-rewrite url 变量
- maven - Spring-boot with maven - 排除测试范围依赖
- snmp - 如何使用 SNMPv3 从思科交换机提取每个 VLAN 的 MAC 地址?
- firebase - 如何使用测试模式而不是锁定模式重新创建 Firebase Firestore 数据库
- javascript - 在给定 CSS 选择器的情况下查找 DOM 元素的 Vanilla JavaScript 函数
- matplotlib-venn - matplotlib venn 安装在 Windows 上
- python - 按相关模型的属性排序查询集,另一个属性等于某物
- r - 图表上不需要的边界线