为什么固定div的4px余量没有显示?

时间:2016-07-02 12:52:11

标签: html css position margin

我有一个简单的固定位置<div>,其中有100%width。它周围有4px个边距。但是,它没有显示右侧的边距。为什么呢?

以下是代码:

<html>

<body>
  <div style="position:fixed;height:50px;width:100%;margin:4px;background-color:black;">
  </div>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

它右边有一个边距,但你看不到它,因为它被推到了屏幕的边缘。这是因为宽度是身体宽度的100%。解决问题的最佳方法是使用topleftright

&#13;
&#13;
div {
  position: fixed;
  height: 50px;
  left: 4px;
  right: 4px;
  top: 4px;
  background-color: black;
}

body {
  margin: 0;
}
&#13;
<div></div>
&#13;
&#13;
&#13;

这样您仍然可以获得保证金,只有topleftright的形式。

答案 1 :(得分:0)

你应该像下面这样:

<div style="position:fixed; left:0; height:50px;width:98%;margin:1%; background-color:black;">
test
</div>

使用以下

重置身体
<style>body{margin:0;}</style>