【问题标题】:Why is the 4px margin of a fixed div not showing?为什么固定 div 的 4px 边距不显示?
【发布时间】:2016-11-04 17:58:00
【问题描述】:
我有一个简单的固定位置<div>,它有 100% width。它周围有4px 边距。但是,它没有显示右侧的边距。为什么?
代码如下:
<html>
<body>
<div style="position:fixed;height:50px;width:100%;margin:4px;background-color:black;">
</div>
</body>
</html>
【问题讨论】:
标签:
html
css
position
margin
【解决方案1】:
它在右侧确实有一个边距,但您看不到它,因为它被推到了屏幕边缘。这是因为宽度是 100%,即身体的宽度。解决它的最佳方法是使用top、left 和right。
div {
position: fixed;
height: 50px;
left: 4px;
right: 4px;
top: 4px;
background-color: black;
}
body {
margin: 0;
}
<div></div>
这样你还有余量,只是top、left和right的形式。
【解决方案2】:
你应该像下面这样:
<div style="position:fixed; left:0; height:50px;width:98%;margin:1%; background-color:black;">
test
</div>
然后用下面的方法重置正文
<style>body{margin:0;}</style>