html - 为什么 html 元素在我的代码中仍然有偏移量?

标签 html css

<分区>

我将 divbody 元素的 marginpadding 设置为 0 但我发现我的 div 之间仍然存在一些偏移。我也使用内联 block 。

这是我的例子:

.background{
    padding:0;
    margin:0;
    background-color: yellow;
}
.page{
    background-color: darkolivegreen;
    width:80%;
    padding:0;
    margin:0;
}

.title-boss-offset{
    background-color: antiquewhite;
    width:70%;
}

.title-boss {
    width: 29%;
    background-color: aqua;
}

.title-boss-offset, .title-boss{
    border-width:0;
    display:inline-block;
    margin:0;
    padding:0;
    height: 15em;
}
<body class="background">
<div class="page">
    <div class="title-boss-offset"></div>
    <div class="title-boss"></div>
</div>
</body>

enter image description here

为什么会这样?我该如何解决?

最佳答案

请务必在使用display: inline-block 时移除元素之间的空格。或者查看纯 CSS 解决方案。

剥离空格

但这不会(请注意,我向父级添加了一个 line-height:0 以去除底部边距):

.background{
    padding:0;
    margin:0;
    background-color: yellow;
}
.page{
    background-color: darkolivegreen;
    width:80%;
    padding:0;
    margin:0;
    line-height:0;
}

.title-boss-offset{
    background-color: antiquewhite;
    width:70%;
}

.title-boss {
    width: 30%;
    background-color: aqua;
}

.title-boss-offset, .title-boss{
    border-width:0;
    display:inline-block;
    margin:0;
    padding:0;
    height: 15em;
    word-spacing: normal
    
}
<body class="background">
  <div class="page">
    <div class="title-boss-offset"></div><!--
    --><div class="title-boss"></div>
  </div>
</body>

纯 CSS:

您可以通过将父样式设置为 display: table; 来利用表格显示去除单元格之间的空间 重要的部分是:

.parent{
  display: table; 
  width: 100%; 
  table-layout: fixed; 
  line-height: 0;
  word-spacing: -2em; /*cross-browser quirks*/
}
.children{
  display: inline-block; 
  word-spacing: normal; /*cross-browser quirks*/
}

.background{
    padding:0;
    margin:0;
    background-color: yellow;
}
.page{
    background-color: darkolivegreen;
    width:80%;
    padding:0;
    margin:0;
    display:table;
    table-layout:fixed;
    word-spacing: -2em;
    line-height:0;
}

.title-boss-offset{
    background-color: antiquewhite;
    width:70%;
}

.title-boss {
    width: 30%;
    background-color: aqua;
}

.title-boss-offset, .title-boss{
    border-width:0;
    display:inline-block;
    margin:0;
    padding:0;
    height: 15em;
    word-spacing: normal
    
}
<body class="background">
  <div class="page">
    <div class="title-boss-offset"></div>
    <div class="title-boss"></div>
  </div>
</body>

关于html - 为什么 html 元素在我的代码中仍然有偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44543470/

上一篇:html - 使 div 使用滚动条而不是变大

下一篇:html - 如何以固定高度居中图像位置

相关文章:

javascript - 使用 HTML/JS 将输入保存到文本文件?

javascript - 在功能性(无状态)组件中读取 &lt;input&gt;

html - 为什么高度会随着较小的字体大小而增加?

html - 覆盖 Rails 中的 Bootstrap custom.css.scss 未按预期工作

css - 如何在 less 中循环遍历一组变量?

css - CSS 选择器中的特殊字符

javascript - 当用户在不同的电台/专辑上点击播放时,我想改变封面图片

javascript - 使用 javascript 在 HTML 表格中显示 firebase 数据

jquery - 自动为缩略图添加放大图标

css - 使用 4 个 Bootstrap-3 Cols @270 设置 30px 间距