简体   繁体   English

使用宽度百分比

[英]Using percent on width

I'm trying to float two divs within an outer div, but my problem is that when using percent for width, the space isn't fully occupied 我正在尝试在外部div中浮动两个div,但是我的问题是,当使用百分比表示宽度时,该空间没有被完全占用

You can check the html here: http://codepen.io/vincentccw/pen/DBEot 您可以在此处检查html: http//codepen.io/vincentccw/pen/DBEot

<div class="outer">
    <div class="d1">a</div>
    <div class="d2">b</div>
</div>

.outer{
  background:red;
  width:1024px;
  margin:0 auto;
  height:400px;
}

.d1{
  background:green;
  width:20%;
  float:left;
}

.d2{background:blue;
    width:80%;
    float:left;
}

Notice the 'd2' class isnt fully occupied( you can still see the red colour on right), unless I changed convert the percent back to pixel value. 请注意,'d2'类并未完全被占用(您仍然可以在右侧看到红色),除非我更改将百分比转换回像素值。

.d2{
  background:blue;
    width:80%;
    float:right;
}

Works for me 为我工作

One float left, the other one float right. 一个漂浮在左边,另一个漂浮在右边。

Check this http://codepen.io/anon/pen/qyDEe . 检查此http://codepen.io/anon/pen/qyDEe As you are trying to float both the div's to left you are not able occupy full width. 当您尝试将两个div都向左浮动时,您将无法占据整个宽度。

It's strange and looks like some bug in browser renderer. 这很奇怪,看起来像浏览器渲染器中的一些错误。 Browser reports that d1 has width 205px and d2 - 819px and the sum of them is precisely 1024px. 浏览器报告d1的宽度为205px,d2的宽度为819px,它们的总和恰好为1024px。 But 20% of 1024px is 204.8px and 80% of 1024px is 819.2px (not precise value). 但是20%的1024px是204.8px,而80%的1024px是819.2px(非精确值)。 If you set width of parent div to 1000px then you got precise fit of the inner divs. 如果将父div的宽度设置为1000px,则可以精确匹配内部div。

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

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