CSS适当的方式来中心内容 [英] CSS Appropriate Way to Center Content

查看:87
本文介绍了CSS适当的方式来中心内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更喜欢使用基于CSS的设计,但随着更多的后端编码器我的CSS技能有点弱。当我涉足布局时,我倾向于回退基于表格的格式,因为我的头脑已经扭曲了多年的基于表的滥用。有一个特殊的问题,我总是跳过。什么是最好的CSS替代品:

I prefer working with CSS based design, but as more of a back end coder my CSS skills are a bit weak. When I get involved with layout, I tend to fall back on table based formatting because my mind has been warped by years of table based abuse. There's one particular problem that I always trip over. What is the best CSS alternative to:

<table width="100%">
    <tr>
        <td align="center">
            content goes here
        </td>
    </tr>
</table>

我有时使用:

<div style="width:100%; text-align:center">content</div>

但这似乎不太对。我不是试图对齐文本,我试图对齐内容。此外,这似乎对封闭元素的文本对齐有影响,这需要调整以进行修复。我没有得到的一件事是:为什么没有一个浮点:中心风格?它似乎是最好的解决方案。希望我缺少一些东西,有一个完美的CSS方法来做到这一点。

But this doesn't seem quite right. I'm not trying to align text, I'm trying to align content. Also, this seems to have an effect on the text alignment of enclosed elements, which requires tweaking to fix. One thing I don't get is: why isn't there a float:center style? It seems like that would be the best solution. Hopefully, I'm missing something and there is a perfect CSS way to do this.

推荐答案

$ c> text-align 用于对齐文本。它实际上只有Internet Explorer,它允许您集中任何东西,而不是文本。任何其他浏览器都会正确处理此问题,并且不会让块元素受到 text-align 的影响。

You are right that text-align is intended for aligning text. It's actually only Internet Explorer that lets you center anything other than text with it. Any other browser handles this correctly and doesn't let block elements be affected by text-align.

块元素使用css你使用 margin:0 auto; 正如Joe Philllips建议的。

To center block elements using css you use margin: 0 auto; just as Joe Philllips suggested. Although you don't need to keep the table at all.

因为没有 float:center; 是浮动的目的是将元素(通常是图像)放在左边或右边,并在它们周围有文本流。在中心浮动东西在这个上下文中没有意义,因为这意味着文本必须在元素的两侧流动。

The reason that there is no float: center; is that floating is intended to place elements (typically images) either to the left or the right and have text flow around them. Floating something in the center doesn't make sense in this context as that would mean that the text would have to flow on both sides of the element.

这篇关于CSS适当的方式来中心内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆