【问题标题】:Div spacing offset using calc()使用 calc() 的 div 间距偏移
【发布时间】:2015-01-19 14:46:12
【问题描述】:

我有 3 个垂直行,使用 calc() 在我的屏幕上划分 我正在使用 JS 来设置每个盒子类型的高度

width: calc(1/3 * 100%);

这是设置每个块高度的javascript,它设置高度等于宽度 如果是长盒子,它会将高度设置为宽度的一半。

$('.box1').height( $('.box1').width() );
$('.box2').height( $('.box2').width() / 2 );
$('.box4').height( $('.box4').width() );

我在行中的列之间有一个奇怪的偏移量(见截图)

您可以在这里查看页面http://cloudlabme.webhosting.be/4sr

这是垂直两行的 HTML

<div class="container">
    <div class="row">
        <div class="box box4 box-event" style="background-image: url(build/img/events/2.jpg)"><h1>II</h1></div>
        <div class="box box2 box-medium"></div>
    </div>

    <div class="row">
        <div class="box box2 box-light"></div>
        <div class="box box1 box-dark"><h3>Hier</h3></div>
        <div class="box box1 box-medium"></div>
        <div class="box box2"></div>
    </div>
</div>

这是我的 CSS

.container {
    position: relative;
    width: 100%;
    max-width: $breakpoint;
    margin: 0 auto;
    z-index: 0;
}

.row {
    float: left;
    width: calc(1/3 * 100%);
    background: #f2f2f2;
}

/* BOX */
.box {
    &.box-light {background: #fff;}
    &.box-medium {background: #666;}
    &.box-dark {background: #111;}
}

.box1 {
    float: left;
    width:50%;
    background: #ff4444;
}

.box2 {
    clear: left;
    width: 100%;
    background: #ff6666;
}

.box4 {
    clear: left;
    width: 100%;
    background: #ff8888;
}

谢谢!这简直是​​在扼杀我的大脑!

【问题讨论】:

  • 我可以使用一些 CSS。不仅 HMTL
  • 33.33% 正在四舍五入!
  • 这可能是真的!所以这就是为什么大块比左边那个高?知道可能的解决方案是什么吗?
  • 使用Math.round($('.box1').width()) 看看结果是否满足int 的高度值。尽管有针对此类问题的 css 技术,但它们确实存在一些缺陷,这也取决于。用css或其他东西搜索正方形

标签: html css css-calc


【解决方案1】:

很可能,一个像素的差距是由舍入误差引起的。我能想到的唯一解决方案是使用 JavaScript 强制容器宽度为 3 的倍数。

更好的解决方案是使用 CSS 表格显示。在前两个“单元格”上设置 33.33333333% 宽度,并让第三个单元格自动调整大小。高度仍会相差一到两个像素,但这比使用calc() 和解决舍入问题要好。

$(window).on("load resize", function() {
  $('.box1').height($('.box1').width());
  $('.box2').height($('.box2').width() / 2);
  $('.box4').height($('.box4').width());
});
.container {
  display: table;
  width: 100%;
}
.row {
  display: table-cell;
  vertical-align: top;
  background: #CCC;
}
.row:first-child, .row:first-child + .row {
  width: 33.33333333%;
}
.box1 {
  float: left;
  width: 50%;
  background: #C99;
}
.box2 {
  clear: left;
  background: #C66;
}
.box4 {
  clear: left;
  background: #C33;
}
h1, h3 {
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="container">
  <div class="row">
    <div class="box box4">
      <h1>II</h1>
    </div>
    <div class="box box2"></div>
  </div>
  <div class="row">
    <div class="box box2"></div>
    <div class="box box1">
      <h3>Hier</h3>
    </div>
    <div class="box box1"></div>
    <div class="box box2"></div>
  </div>
  <div class="row">
    <div class="box box2"></div>
    <div class="box box4"></div>
  </div>
</div>

【讨论】:

  • 真聪明!我现在只有一个 1pixel 的问题!截图:prntscr.com/58l7f8谢谢!
  • 我将尝试您的第二个解决方案,任何已知的代码笔或有关如何实现此目的的教程?谢谢!
  • 我通过使用 chrome devtools 编辑 CSS 创建了一个模型。不幸的是,我无法 grab 编辑后的 ​​CSS。我会尝试添加一个 sn-p 来回答。
  • 非常感谢!你帮了大忙
  • 查看修改后的答案。请注意,您可以放弃 JavaScript 并使用“css padding top hack”(谷歌)设置 50% 的高度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
相关资源
最近更新 更多