简体   繁体   English

CSS居中的div

[英]css centered div

I have a centered div on my site, using a fixed width and margin:0 auto; 我的网站上有一个居中的div,使用固定宽度和margin:0自动;

All looks fine in IE, but on FF, for the pages with long content, only the top part of the div has the proper div color, and the rest has the body background color. 在IE中,一切看起来都不错,但是在FF上,对于内容较长的页面,只有div的顶部具有适当的div颜色,其余部分具有主体背景颜色。

what I'm doing wrong? 我做错了什么?

many thx 许多

Without seeing your code it's hard to tell, but my bet is that you've set the div height to %100, which means 100% of the viewport. 没有看到您的代码很难说,但是我敢打赌,您已经将div高度设置为%100,这意味着100%的视口。

It will not stretch beyond that, even if the content is long enough. 即使内容足够长,它也不会超出此范围。 This is the correct behaviour. 这是正确的行为。

To make it the full scree height when there's not enough content, and go beyond the viewport height when there's more than enough content, you'll need to use two divs. 要在没有足够内容的情况下将其设置为完整的卵石高度,而在没有足够内容的情况下使其超过视口的高度,则需要使用两个div。

Here's an example I've hosted: 这是我主持的一个示例:

Div height 100% fix div高度100%修复

If you know the width (ie:600px) and height of the div you can use the following. 如果您知道div的宽度(即:600px)和高度,则可以使用以下内容。 I center divs in one direction using 3 parameters: 我使用3个参数在一个方向上使div居中:

Horizontal: 水平:

<div class='hcnt'>Some H Centered Text</div>

CSS: CSS:

.hcnt{
 left-margin:50%;
 width:600px;
 left:-300px;
}

Vertical: 垂直:

<div class='vcnt'>Some V Centered Text</div>

CSS: CSS:

.vcnt{
     top-margin:50%;
     height:400px;
     top:-200px;
    }

Both: 都:

<div class='hcnt vcnt'>Some completely Centered Text</div>

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

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