简体   繁体   English

CSS的最小宽度和边距/填充与绝对定位

[英]css min-width and margin/padding with absolute positioning

I'm using absolute positioning to have 100% height of the screen. 我使用绝对定位来获得100% height的屏幕100% height I also want to limit the resizing of my page so I add min-width in my header and content divs. 我还想限制页面的resizing ,因此我在标题和内容div中添加了min-width The problem I'm facing is that once resizing reaches the min-width boundary, right side margin disappears . 我面临的问题是,一旦调整大小达到最小宽度边界,右侧margin disappears Any suggestions how to make it work? 有什么建议如何使其工作?

HTML: HTML:

<div id="header">Header</div>
<div id="content">Content</div>

CSS: CSS:

html, body{
    background: blue;
    padding:0px;
    margin:0px;

}

#header{
    height:30px;
    background: yellow;
    position: absolute;
    top:20px;
    right:20px;
    left:20px;
    min-width:500px;
}

#content{
    background:green;
     position: absolute;
    top:50px;
    right:20px;
    left:20px;
    bottom:20px;
    min-width:500px;
}

jsfiddle 的jsfiddle

您可以使用CSS box-sizing属性在现代浏览器(IE8 +)中完成此操作

I found a solution my self. 我找到了解决方案。 The problem is that if you apply min-width on the element which has padding or margin , that padding or margin collapses when you resize the window. 问题是,如果对具有paddingmargin的元素应用min-width ,则在调整窗口大小时该padding或margin会折叠。 So the solution was to wrap that element inside root element and set min-width on the root element. 因此,解决方案是将该元素包装在根元素内,并在根元素上设置最小宽度。

PS I also tried box-sizing approach suggested by Sam but effect was almost the same: margin shrinks but doesn't disappear all. PS我也尝试了Sam提出的盒大小调整方法,但效果几乎相同:边际收缩但并没有消失。 Applying min-width on outer element solves the issue as well. 在外部元素上应用最小宽度也可以解决此问题。

Here is an example 这是一个例子

updated fiddle 更新的小提琴

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

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