gpt4 book ai didi

html - 如何在 CSS 中将框及其内容居中?

转载 作者:太空宇宙 更新时间:2023-11-04 05:12:27 24 4
gpt4 key购买 nike

我看过其他一些答案,但无法让它们适用于我的特定情况。我已经简化并包含了下面的整个代码块。基本上它只是在蓝色框中显示文本。我想知道如何使框居中。

<!DOCTYPE html>

<html>
<head>
<style type="text/css"><!--
#content {
width:500px;
padding: 10px 10px 10px;
background-color: #D1ECFF;
position: relative;
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
}
--></style>
</head>
<body>

<div id="content">

<h2>Create</h2>

</div>
</body>
</html>

最佳答案

因为#content有宽度,所以可以将左右边距设置为auto。这将使元素在其父元素中居中。一小段代码 margin: 0 auto; 将上/下边距设置为零,将左/右边距设置为自动。

http://jsfiddle.net/vL5r2/

#content {
margin: 0 auto;
width:500px;
...

关于html - 如何在 CSS 中将框及其内容居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958905/

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