首页 > 解决方案 > 我可以在前一个元素旁边浮动一个元素吗?

问题描述

考虑以下带有侧边栏的沼泽标准段落布局。布局有两个问题:首先,浏览器首先会看到侧边栏(在我的例子中是导航的东西),导致屏幕阅读器和纯文本浏览器出现一些可访问性问题。所以元素应该在文章之后。但在那种情况下,我不能依赖久经考验的真实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>

标签: htmlcssresponsive-design

解决方案


如果你可以使用 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>

预览

预览


推荐阅读