CSS 高级 Div 定位。中心及中间

发布于 2024-10-08 22:28:47 字数 225 浏览 0 评论 0原文

我想将

定位

  • 到以 wrt body 宽度居中
  • 到以 wrt body 高度居中

div 将具有以下属性:

  • div 的宽度将为 90%
  • 高度div 自动,最小值将会被知道

I want to position a <div>

  • To Center with w.r.t body width
  • To Middle with w.r.t body height

The div will have these properties:

  • width of div will be 90%
  • height of div auto, min. will be known

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瀟灑尐姊 2024-10-15 22:28:47

我已经在 IE8、Firefox、Chrome 中对此进行了测试。它在 IE7 中不起作用。

如果您需要它在 <= IE7 中工作,我想最简单的解决方案是放弃纯 CSS 并使用 javascript 进行垂直定位。

现场演示

HTML:

<div id="container"> 
    <div id="content"> 
        <input type="button" value="click me" onclick="javascript:document.getElementById('content').innerHTML += 'i i i i i i i i i i i i i i i i i i i i i i i i i<br />';" /><br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
    </div> 
</div>

CSS:

html, body {
 margin: 0;
 padding: 0;
 border: 0;

 width: 100%;
 height: 100%
}

body {
 display: table
}

#container {
 width: 100%;
 display: table-cell;
 vertical-align: middle
}

#content {
 background: red;
 width: 90%;
 margin: 0 auto;
 min-height: 150px
}

I've tested this in IE8, Firefox, Chrome. It does not work in IE7.

If you need this to work in <= IE7, I imagine the simplest solution would be to give up on pure CSS and use javascript for the vertical positioning.

Live Demo

HTML:

<div id="container"> 
    <div id="content"> 
        <input type="button" value="click me" onclick="javascript:document.getElementById('content').innerHTML += 'i i i i i i i i i i i i i i i i i i i i i i i i i<br />';" /><br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
        i i i i i i i i i i i i i i i i i i i i i i i i i<br />
    </div> 
</div>

CSS:

html, body {
 margin: 0;
 padding: 0;
 border: 0;

 width: 100%;
 height: 100%
}

body {
 display: table
}

#container {
 width: 100%;
 display: table-cell;
 vertical-align: middle
}

#content {
 background: red;
 width: 90%;
 margin: 0 auto;
 min-height: 150px
}
独﹏钓一江月 2024-10-15 22:28:47

您可以这样居中高度(注意浏览器兼容性)。

对于居中宽度:您可以使用 position:absolute;left: 50%;,以及 margin: -num;。在边距中,将 num 替换为 div 宽度的一半。

You can center height like this (beware browser compatibility).

For centering width: you can use position: absolute; and left: 50%;, and them margin: -num;. In the margin, replace num with half of the width of the div.

千笙结 2024-10-15 22:28:47

用这个设置div。我相信它在 IE 7 及更低版本中不起作用。可怕的IE....

边距:自动;
上:0;右:0;左:0;下:0;
位置:绝对;

Set the div with this. I believe it doesn't work in IE 7 and lower. Terrible IE....

margin:auto;
top:0;right:0;left:0;bottom:0;
position:absolute;

水水月牙 2024-10-15 22:28:47

您可以使用 margin(left,right) 5% 来模仿居中宽度的效果。

关于高度居中,我不知道如何单独使用 css 来做到这一点(因为垂直尺寸未知)。但你可以尝试使用 javascript 来做到这一点。

这是一个示例: http://www.demtron.com/blog/post/2009/01/14/Centering-a-DIV-Window-with-Cross-Browser-JavaScript.aspx(您可以忽略检测窗口大小部分)。

You may use margin(left,right) 5% to imitate the effect of centering width.

About the height centering, I don't know the way that you can do it with css alone (because of the unknown vertical size). But you may try using javascript to do that.

Here's an example: http://www.demtron.com/blog/post/2009/01/14/Centering-a-DIV-Window-with-Cross-Browser-JavaScript.aspx (you can ignore the detecting window size part).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文