html - margin-right 不适用于相对元素? (宽度 100%)

标签 html css

我目前的问题是,margin-right 不再适用于 width: 100%

我已经尝试过关闭 width: 100%,更改位置等。 但没有任何效果。我在互联网上看到的所有问题和答案都没有帮助我。我试了很多。如果没有 width: 95% 之类的东西,它真的能工作吗?我错过了什么吗?

<body>
    <div class="content" id="id_content">
      <div class="topbar-header">
        <input type="text" class="topbar-field" style="min-width: 300px; max-width: 600px; width: 30%;">
      </div>
    </div>
  </body>
/* The header to search and navigate on. */
.topbar-header {
  position: relative;
  margin: 20px;
  width: 100%;
  height: var(--val-head-height);
  background-color: var(--color-menu-background);
}

这发生了 here, 但我想要右边的空间。

最佳答案

或者,您可以使用 width: calc(100% - value) 或仅设置父元素的填充 (.content { padding-right: 32px; })

/* The header to search and navigate on. */
.topbar-header {
  box-sizing: border-box;
  position: relative;
  width: calc(100% - 64px);
  height: 48px;
  margin-right: 256px;
  background-color: tomato;
}
<body>
  <div class="content" id="id_content">
    <div class="topbar-header">
      <input type="text" class="topbar-field" style="min-width: 300px; max-width: 600px; width: 30%;">
    </div>
  </div>
</body>

关于html - margin-right 不适用于相对元素? (宽度 100%),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57135462/

相关文章:

javascript - XHTML5 中的 SVG : setting attributes with proper namespace

html - 监控新用户

javascript - 使用其中的文本更改 svg-image 中的背景颜色

html - 多列 CSS 使第一列更宽

html - CSS Firefox - 顶部填充更改宽度

javascript - Datalist 箭头不在 ie 和 firefox 中

html - 使三列 HTML 响应

html - 目前让网站图标在所有支持网站图标的浏览器中显示的最佳方法是什么?

html - 如何将页脚保留在我的图片库下方

css - 无法使用 float :left and float:right 在父 div 内对齐 2 个子 div