【问题标题】:Display table-cell vertical-align middle create 1px weird spacing in chrome显示表格单元格垂直对齐中间在 chrome 中创建 1px 奇怪的间距
【发布时间】:2017-07-04 12:06:02
【问题描述】:

我需要将我的内容放在 div 中,所以我使用 table-cell 和 vertical-align 来实现它。但是,它只会在 Chrome 中创建一个奇怪的间距,例如 1px。我真的找不到解决办法。请帮帮我。

body {
  background: red;
}
.img-wrapper, .txt-wrapper {
  height: 300px;
  width: 50%;
  float: left;
  position: relative;
}
.img-wrapper {
  background: yellow;
}
.txt-wrapper {
  background: green;
  display: table;
}
.txt {
  display: table-cell;
  vertical-align: middle;
}
.col1 {
  vertical-align: middle !important;
  background: yellow;
  width: 50%;
}
.col2 {
  background: green;
  width: 50%;
}
.pushtoright {
  left: 50%;
}
.pulltoleft {
  right: 50%;
}
<div class="therow">
  <div class="img-wrapper">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper">
    <div class="txt">
      <h2>Title 1</h2>
      <p>Description 1 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

<div class="therow">
  <div class="img-wrapper pushtoright">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper pulltoleft">
    <div class="txt">
      <h2>Title 2</h2>
      <p>Description 2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

<div class="therow">
  <div class="img-wrapper">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper">
    <div class="txt">
      <h2>Title 3</h2>
      <p>Description 3 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

<div class="therow">
  <div class="img-wrapper pushtoright">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper pulltoleft">
    <div class="txt">
      <h2>Title 4</h2>
      <p>Description 4 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

演示:https://codepen.io/rae0724/pen/jwxJqX

【问题讨论】:

  • 你在哪里看到空间? Mac 上的 Chrome 59.0.3071.115,看不到任何奇怪的空格。
  • 我的也是 mac chrome:版本 59.0.3071.115(官方构建)(64 位)。也许可以尝试调整窗口大小?然后会看到红色的 1px 线
  • 当您使用 Chrome 开发者工具并转到右侧的“计算”选项卡时,您会看到左侧 div 的宽度为 531px,右侧的 div 为 531.5。太空中似乎损失了半个像素。
  • @Gerard 是的,我也意识到了。但我为每列应用了 50% 的宽度。我该如何解决?看起来很烦人。

标签: jquery html css google-chrome css-tables


【解决方案1】:

如果您可以在 .txt-wrapper 中不使用 display: table ,如果这对其他东西没有问题...奇怪的效果消失了

body {
  background: red;
}
.img-wrapper, .txt-wrapper {
  height: 300px;
  width: 50%;
  float: left;
  position: relative;
}
.img-wrapper {
  background: yellow;
}
.txt-wrapper {
  background: green;
  display: flex;
  align-items:center;

  /*display: table;*/
}
.txt {
  display: inline-block;
  padding: 20px;
}
.col1 {
  vertical-align: middle !important;
  background: yellow;
  width: 50%;
}
.col2 {
  background: green;
  width: 50%;
}
.pushtoright {
  left: 50%;
}
.pulltoleft {
  right: 50%;
}
<div class="therow">
  <div class="img-wrapper">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper">
    <div class="txt">
      <h2>Title 1</h2>
      <p>Description 1 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

<div class="therow">
  <div class="img-wrapper pushtoright">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper pulltoleft">
    <div class="txt">
      <h2>Title 2</h2>
      <p>Description 2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

<div class="therow">
  <div class="img-wrapper">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper">
    <div class="txt">
      <h2>Title 3</h2>
      <p>Description 3 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

<div class="therow">
  <div class="img-wrapper pushtoright">
    <div class="img"></div>
  </div>
  <div class="txt-wrapper pulltoleft">
    <div class="txt">
      <h2>Title 4</h2>
      <p>Description 4 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

【讨论】:

  • 是的,但我需要在盒子的中间/中心写字。有没有办法解决这个问题?没有创建奇怪的 1px?
  • 我已经编辑了我的答案。垂直和水平居中
  • 如果你也想让

    居中,你可以使用 text-align:center for h2 或整个 .txt 类来居中

猜你喜欢
  • 1970-01-01
  • 2017-10-18
  • 2016-09-19
  • 2014-01-02
  • 1970-01-01
  • 2013-10-11
  • 2023-03-08
  • 1970-01-01
  • 2012-07-06
相关资源
最近更新 更多