gpt4 book ai didi

css - HTML5 用 CSS 替换 block div 的 "center"?

转载 作者:太空宇宙 更新时间:2023-11-04 16:20:51 25 4
gpt4 key购买 nike

我正在尝试编写一个符合 HTML-5 标准的文档,该文档通过了W3C 验证器:http://validator.w3.org/check .
验证器提示每次使用

标签,并建议我应该使用 CSS。不幸的是,CSS 似乎无法使 BLOCK 居中水平地。它可以使用 'text-align: center' 属性使文本居中,但尝试居中表格和图像失败。
我已经阅读了“CSS居中指南”: http://www.w3.org/Style/Examples/007/center.en.html但它的所有示例也无法使表格或图像水平居中(至少在 Firefox 36.0 上)。谁能建议如何使这张 table 居中?:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>CSS centering bug demo
</title>
<style type="text/css">
.centered{ display: block;
margin-left:auto;
margin-right:auto;
align: center;
text-align: center;
vertical-align: center;
horizontal-align: center;
}
</style>
</head>
<body>
<div class="centered">
<table width="60%">
<tr>
<td>Example Table</td>
<td>which should be horizontally centered on the page.</td>
</tr>
</table>
</div>
</body>
</html>

最佳答案

如果您希望表格居中,请将 centered 类添加到 table 中。

<table width="60%" class="centered">

您的 css 类中也有一些无效属性。你只需要像这样使用边距

.centered {
margin-left:auto;
margin-right:auto;
}

所以底线是将左边距和右边距作为 auto 放置到您想要居中的元素。

Js Fiddle Example

关于css - HTML5 用 CSS 替换 block div 的 "center"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29411609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com