簡體   English   中英

如何將框及其內容集中在CSS中?

[英]How do I center a box and its contents in CSS?

我已經看過其他一些答案,但無法讓他們為我的特定案例工作。 我已經簡化並包含了下面的整個代碼塊。 基本上它只是在藍色框中顯示文本。 我想知道如何使盒子居中。

<!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;的短位margin: 0 auto; 將top / bottom margin設置為零,將left / right設置為auto。

http://jsfiddle.net/vL5r2/

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

如果要垂直居中的框它的內容的水平,你可以用絕對定位和負利潤率中可以看到做這個的jsfiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM