将 div 居中对齐
我想将 div
浮动到中心。是否可以? text-align: center
在 IE 中不起作用。
I want to float a div
to center. Is it possible? text-align: center
is not working in IE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
本身没有浮动到中心。如果您想将一个块元素置于另一个块元素的中心,请执行以下操作:
with:
现在这不会使文本环绕它(就像向左或向右浮动一样),但就像我说的:没有浮动中心。
There is no float to center per se. If you want to center a block element inside another do this:
with:
Now that won't make the text wrap around it (like it would with a float left or right) but like I said: there is no float center.
这一直对我有用。
如果您为 DIV 设置了固定宽度和正确的 DOCTYPE,请尝试此
希望这会有所帮助。
This has always worked for me.
Provided you set a fixed width for your DIV, and the proper DOCTYPE, try this
Hope this helps.
通常的技术是
margin:auto
但是,旧版 IE 不理解这一点,因此通常将
text-align: center
添加到外部包含元素。您不会认为这行得通,但忽略auto
的 IE 也会错误地将文本对齐中心应用于块级内部元素,这样事情就解决了。这实际上并没有实现真正的浮动。
The usual technique for this is
margin:auto
However, old IE doesn't grok this so one usually adds
text-align: center
to an outer containing element. You wouldn't think that would work but the same IE's that ignoreauto
also incorrectly apply the text align center to block level inner elements so things work out.And this doesn't actually do a real float.
浮动div居中“工作”与display:inline-block和text-align:center的组合。
的窗口大小来更改外部 div 的宽度:
尝试通过调整 jsfiddle和 css
floating divs to center "works" with the combination of display:inline-block and text-align:center.
Try changing width of the outer div by resizing the window of this jsfiddle
and the css:
以下解决方案对我有用。
Following solution worked for me.
我的一个网站涉及一个大小可变的 div,您不会提前知道它。它是一个带有 2 个嵌套 div 的外部 div,外部 div 与第一个嵌套 div 的宽度相同,即内容,内容正下方的第二个嵌套 div 是标题,必须居中。因为宽度未知,所以我使用 jQuery 进行相应调整。
所以我的 html 是这样的
,然后我将标题放在 jQuery 中,如下所示
One of my websites involves a div whose size is variable and you won't know it ahead of time. it is an outer div with 2 nested divs, the outer div is the same width as the first nested div, which is the content, and the second nested div right below the content is the caption, which must be centered. Because the width is not known, I use jQuery to adjust accordingly.
so my html is this
and then I center the captions in jQuery like this
使用“spacer”div 包围要居中的 div。最适合流体设计。一定要给垫片高度,否则它们将不起作用。
Use "spacer" divs to surround the div you want to center. Works best with a fluid design. Be sure to give the spacers height, or else they will not work.
这对我有用..
This worked for me..
这可以帮助你..:D
this could help you..:D
不,不是。
您可以将内容冒泡到元素的右侧 (
float: left
) 或元素的左侧 (float: right
),但没有规定让内容在两侧冒泡。No, it isn't.
You can either have content bubble up to the right of an element (
float: left
) or to the left of an element (float: right
), there is no provision for having content bubble up on both sides.将背景中的 div 浮动到最大宽度,在其中设置一个不透明的 div 并使用 margin auto 将其居中。
Float the div in the background to the max width, set a div inside that that's not transparent and center it using margin auto.
这工作得很好
这也可以很好地调整自适应布局的大小..
CSS 示例是:
this works nicely
This resizes nicely with adaptive layouts also..
CSS example would be:
这对我有用。
我在页面上添加了两次无序列表。
一个 div class="menu" id="vertical" 另一个居中的是 div class="menu" id="horizontal"。由于列表向左浮动,我需要一个内部 div 将其居中。见下文。
This worked for me.
I included an unordered list on my page twice.
One div class="menu" id="vertical" the other to be centered was div class="menu" id="horizontal". Since the list was floated left, I needed an inner div to center it. See below.
试试这个,它帮助了我:将 div 包装在标签中,问题是它也会将 div 的内容居中(如果没有以其他方式编码)。希望有帮助:)
Try this, it helped me: wrap the div in tags, the problem is that it will center the content of the div also (if not coded otherwise). Hope that helps :)