browser zoom out breaks page layout

2019-06-09 02:20发布

I am having trouble when i zoom out my page in browser.

this is a simple page, body has a width of 970px, and devided into 2 parts, left and right. Left one is 300px and float to left with margin-right 10px, right one is 660px, and float right. This is ok when i zoom in or zoom out.

but when i change left width to 298px, and add right width to 658px, add 1px border to both parts, though it is ok in normal size, but when i zoom out(ctrl+ mouse wheel down) to 90%, the right part drop down and breaks the layout. You can see here for detail:jsfiddle

<body>
    <style>
    </style>
    <div id='left'>left</div>
    <div id='right'>right</div>
</body>

2条回答
Rolldiameter
2楼-- · 2019-06-09 02:41

To give your layout some flexibility, use percentage widths for your divs and float left and right as suggested in previous answers, but do not set a margin. Allow the margin to be the space left over. Using exact widths or percentage values that add up exactly 100% there is a risk the layout will break in one or more browser for reasons explained by Shomz.

#left{
    float:left;
    width:30%;
}

#right{
    float:right;
    width:65%;

The space between the two elements will be a the same as a 5% margin, and less likely to break.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-06-09 02:54

HTML doesn't like to work with decimal pixel numbers and that's exactly what happens when you zoom - all the pixel sizes get internally divided or multiplied (though multiplication isn't really a problem).

Think about it, how should it represent 268.2 pixels? It gets rounded. So if you have many of those not-divisible elements, you're bound to lose some pixels and that causes the layout to break. Also, that's why some zoom percentages work better than the others.

查看更多
登录 后发表回答