简体   繁体   English

浮动DIV中具有左右边距的IE7间距问题

[英]IE7 Spacing issue in floating DIVs with margin left and right

I wanna create some columns with width and margins in percentage in DIVs can fit, inside any container with dynamic width. 我想在任何具有动态宽度的容器内创建一些列,这些列具有可容纳DIV的宽度和边距的百分比。

example

<div class="gridFluid">
    <div class="col-1-4"></div>
    <div class="col-1-4"></div>
    <div class="col-1-4"></div>
    <div class="col-1-4"></div>
</div>

 * {
     margin: 0;
     padding: 0;
 }
 body {
     font-size: 1%
 }
 .gridFluid {
     width: 960px;
     display: table;
     background: #eee;
     font-size: 0px;
     overflow: auto;
     zoom: 1;
 }
 .col-1-4 {
     background: #ddd;
     height: 200px;
     width: 23%;
     display: table-cell;
     *display: inline;
     zoom:1;
     margin-left: 1%;
     margin-right: 1%;
     float: left;
 }

It's working as expected everywhere, but in IE7 last column is not able to fit properly in single line. 它在所有地方都按预期工作,但是在IE7中,最后一列无法正确地放在一行中。 May be because it's adding extra space between DIVs other then margin. 可能是因为它在除保证金之外的DIV之间增加了额外的空间。

I don't want to use different width or margin for IE7 (width: 24.5% instead 25). 我不想为IE7使用不同的宽度或边距(宽度:24.5%而不是25)。 Because it's not a proper solution. 因为这不是一个适当的解决方案。 I'm looking for a good solution for it. 我正在寻找一个好的解决方案。

May be it's Known issue of IE7, I find lot of issue related to it when i'm searching for solution but not get any proper way to solve my problem. 可能是IE7的已知问题,我在寻找解决方案时发现了很多与其相关的问题,但没有找到解决问题的适当方法。

Let's do some quick calculations based off of your CSS. 让我们根据您的CSS进行一些快速计算。

You want each column to be 23% of the width of the container. 您希望每一列都占容器宽度的23%。 So each column should be: 因此,每列应为:

.23 * 960 = 220.8 .23 * 960 = 220.8

So each column should be 220.8 pixels. 因此,每列应为220.8像素。

You want the margins to each be 1% wide. 您希望每个边距为1%宽。

.01 * 960 = 9.6 0.01 * 960 = 9.6

So each margin should be 9.6 pixels wide. 因此,每个边距应为9.6像素宽。

Wait a minute. 等一下。 We've got some partial pixels in there. 我们那里有一些局部像素。 How do you handle a .8 or a .6 of a pixel? 如何处理.8或.6像素? Well you have to do some rounding. 好吧,你必须做一些舍入。 But which way do you round... up or down? 但是,您采用哪种方式向上或向下取整? Well if you round up you end up with 4 columns sized 221 pixels. 好吧,如果四舍五入,您最终会得到4列大小为221像素的列。

221 * 4 = 884 221 * 4 = 884

So the total width of the columns is an estimated 884 pixels. 因此,列的总宽度估计为884像素。

What about the margins? 利润率呢?

8 * 10 = 80 8 * 10 = 80

So the total width of the margins is an estimated 80 pixels. 因此,页边距的总宽度估计为80像素。

Now let's add those numbers together. 现在,让我们将这些数字加在一起。

80 + 884 = 964 80 + 884 = 964

Hrmm... looks like we've got 4 extra pixels here... better push the content down a line to fit in the 960px wide box. 嗯...看起来我们这里有4个额外的像素...最好将内容向下推到一行以适合960px宽的框。

That's why your columns are being pushed down. 这就是为什么您的列被压低的原因。 Also, check out: http://ejohn.org/blog/sub-pixel-problems-in-css/ 另外,请查看: http : //ejohn.org/blog/sub-pixel-problems-in-css/

Just make a small change to your css... 只需对您的CSS进行一点改动...

Change:- 更改:-

.col-1-4 {
     width: 23%;     
 }

To this:- 为此:

.col-1-4 {
     *width: 22.9%; 
     width: 23%;    
 }

How about double divs's. 怎么样的双股利。 That will obviously solve the problem. 那显然可以解决问题。

 .col-1-4 {
     position:relative;
     background: #eee;
     height: 200px;
     width: 25%;
    /*******************************/
    /* Don't use this properties, not necessary here */
     display: table-cell;
     *display: inline;
    /*******************************/
     zoom:1;
     float: left;
 }
 .col-1-4>div {
     background: #ddd;
     position:absolute;
     left:4%;
     right:4%;  /* 100/25 = 4 */
     top:0;
     bottom:0;
 }

Working Fiddle 工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM