如何水平和垂直居中
中?

发布于 2024-10-08 09:21:54 字数 106 浏览 0 评论 0原文

我不想将 div 居中,我希望 div 的内容居中。我想要水平和垂直对齐。

抱歉,如果这太简单了或者其他什么,但这有点麻烦。

格雷

我正在使用 IE7

I don't want to center a div, I want the contents of a div to be centered. I would like horizontal and vertical alignment.

Sorry, if this is too easy or whatever, but this is kind of a hassle to get it right.

Grae

I am using IE7

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

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

发布评论

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

评论(5

邮友 2024-10-15 09:21:55

如果您知道图像的高度和宽度,请将其绝对定位,将顶部/左侧设置为 50%,并将顶部/左侧边距设置为图像高度/宽度的一半。

#foo {
    position:relative; /* Ensure that this is a positioned parent */
}
#foo img {
    width:240px;
    height:200px;
    position:absolute;
    left:50%;
    top:50%;
    margin-left:-120px;
    margin-top:-100px;
}

实例:http://jsfiddle.net/Zd2pz/

If you know the height and width of your image, position it absolutely, set top/left to 50% and margin-top/left to negative half the height/width of your image.

#foo {
    position:relative; /* Ensure that this is a positioned parent */
}
#foo img {
    width:240px;
    height:200px;
    position:absolute;
    left:50%;
    top:50%;
    margin-left:-120px;
    margin-top:-100px;
}

Live example: http://jsfiddle.net/Zd2pz/

_蜘蛛 2024-10-15 09:21:55

我知道您说过不想将 div 居中,但使用 jquery 插件和包含要居中的元素的假 div 以跨浏览器方式实现您的要求会更容易。

我已经使用这个可以居中任何块元素的非常小的插件成功地将几乎所有内容居中。

我知道的唯一其他方法是您已经从 @simshaun & 收到的答案。 @Prhogz

编辑:根据评论请求

将脚本包含在您的头标记中

<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/jquery.center.min.js" )%>"></script>

现在,如果您有一个想要在标记内居中的 DIV,只需将其用作

$(document).ready(function() {
    $("#myDIV").center({ vertical: false });
});

I know you've said that dont want to center a div but to achieve your requirement in a cross browser way would be easier using a jquery plugin and a fake div that contains your element to be centered.

I have successfully centered almost anything using this very small plugin that can center any block element.

The only other way I know are the answer that you already received from @simshaun & @Prhogz

EDIT: As per comment request

Include the script in your head tag

<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/jquery.center.min.js" )%>"></script>

Now if you have a DIV that you want to center inside your markup simply use it as

$(document).ready(function() {
    $("#myDIV").center({ vertical: false });
});
无妨# 2024-10-15 09:21:55

尽管以下内容已过时,但它仍然适用于几乎所有浏览器

<center>
<div>
your html
</div>
</center>

,但请访问此链接

http://www.110mb.com/forum/vertical-horizo​​ntal-alignment-of-image-within-div-t31709.0.html< /a>

although the following is obsolete, it still works for almost all browsers

<center>
<div>
your html
</div>
</center>

however, visit this link

http://www.110mb.com/forum/vertical-horizontal-alignment-of-image-within-div-t31709.0.html

反话 2024-10-15 09:21:55

对于水平对齐,请使用 text-align:center;

对于垂直对齐,请参阅 W3 风格指南

For horizontal alignment, use text-align:center;

For vertical alignment, see for example the W3 style guide

¢蛋碎的人ぎ生 2024-10-15 09:21:55

如果您事先知道内部元素的高度,

CSS:

.container {
    text-align: center; /* Center horizontally. */

    /* For demo only */
    border: 1px solid #000;
    height: 500px;
    margin: 20px;
    width: 700px;
}

.container img {
    margin-top: -167px;
    position: relative;
    top: 50%;
}

HTML:

<div class="container">
    <img src="http://farm6.static.flickr.com/5004/5270561847_7223069d5e.jpg" width="500" height="334" alt="">
</div>

示例

If you know the inner element's height beforehand,

CSS:

.container {
    text-align: center; /* Center horizontally. */

    /* For demo only */
    border: 1px solid #000;
    height: 500px;
    margin: 20px;
    width: 700px;
}

.container img {
    margin-top: -167px;
    position: relative;
    top: 50%;
}

HTML:

<div class="container">
    <img src="http://farm6.static.flickr.com/5004/5270561847_7223069d5e.jpg" width="500" height="334" alt="">
</div>

Example

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