css - 我在粘性标题下有 div,但如果它们浮动,标题就不再粘住了
问题描述
我发现浮动元素是导致问题的原因。
这是我的代码
#header {
position: sticky;
}
#main {
width: 100%;
}
#content {
width: 50%;
float: left;
height: 800px;
box-sizing: border-box;
border: solid red;
}
<body>
<h1 id="header">Header</h1>
<div id="main">
<p id="content">words</p>
<p id="content">words</p>
</div>
</body>
在我的网站上,如果相关,我正在使用 W3 的 RWD 列设计。它只是格式化宽度并使用特定类浮动任何内容。
解决方案
使用后float
,您必须使用clear
body {
height: 100%;
}
#header {
position: sticky;
top: 0;
}
#main {
width: 100%;
}
#content {
width: 50%;
float: left;
height: 800px;
box-sizing: border-box;
border: solid red;
}
.clear-fix {
clear: both;
}
<body>
<h1 id="header">Header</h1>
<div id="main">
<p id="content">words</p>
<p id="content">words</p>
<div class="clear-fix"></div>
</div>
</body>
推荐阅读
- python - python:计算数据框的列并将它们添加到新列
- javascript - 销毁对象时如何设置默认值?
- reactjs - 如何拆分状态以避免在反应应用程序中重新呈现
- c# - UltraTree infragistics 如何更改 UltraTreeNode 的背景色?
- angular - 在 Angular 中动态生成选项卡和表格
- kubernetes - 控制 Openshift 出口
- html - @page margin 导致在打印时隐藏固定位置的页眉和页脚
- ffmpeg - 为什么ffmpeg生成的psnr值是inf?
- module - 模块之间的USES关系是传递的吗?
- vue.js - Vitepress 自定义主题,不同页面的多种布局