【问题标题】: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>

【问题讨论】:

  • 使用这个 css body{margin:0;}

标签: html css position margin


【解决方案1】:

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

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

body {
  margin: 0;
}
&lt;div&gt;&lt;/div&gt;

这样你还有余量,只是topleftright的形式。

【讨论】:

    【解决方案2】:

    你应该像下面这样:

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

    然后用下面的方法重置正文

    <style>body{margin:0;}</style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2013-04-14
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多