[英]How to align entire html body to the center?
如何將整個 html body 與中心對齊?
我只是偶然發現了這篇舊帖子,雖然我確信 user01 早就找到了他的答案,但我發現目前的答案並不完全有效。 在使用其他人提供的信息玩了一會兒之后,我找到了一個適用於 IE、Firefox 和 Chrome 的解決方案。 在 CSS 中:
html, body {
height: 100%;
}
html {
display: table;
margin: auto;
}
body {
display: table-cell;
vertical-align: middle;
}
這幾乎與 abernier 的答案相同,但我發現包括寬度會破壞居中,就像省略自動邊距一樣。 我希望任何偶然發現這個線程的人都會發現我的回答有幫助。
注意:省略html, body { height: 100%; }
html, body { height: 100%; }
僅水平居中。
你可以試試:
body{ margin:0 auto; }
編輯
從今天開始使用 flexbox,你可以
body {
display:flex; flex-direction:column; justify-content:center;
min-height:100vh;
}
以前的回答
html, body {height:100%;}
html {display:table; width:100%;}
body {display:table-cell; text-align:center; vertical-align:middle;}
如果我有一件我喜歡分享的關於 CSS 的事情,那就是我最喜歡的將東西放在兩個軸上的方式!!!
這種方法的優點:
我總是通過使用 2 個類來做到這一點:一個用於指定父元素,其內容將居中 ( .centered-wrapper
),第二個用於指定父元素的哪個子.centered-content
居中 ( .centered-content
)。 在父級有多個子級但只有 1 個需要居中的情況下,第二個類很有用)。 在這種情況下, body
將是.centered-wrapper
,內部div
將是.centered-content
。
<html>
<head>...</head>
<body class="centered-wrapper">
<div class="centered-content">...</div>
</body>
</html>
居中的想法現在是使.centered-content
成為inline-block
。 這將很容易促進水平居中,通過text-align: center;
,並且還允許垂直居中,如您所見。
.centered-wrapper {
position: relative;
text-align: center;
}
.centered-wrapper:before {
content: "";
position: relative;
display: inline-block;
width: 0; height: 100%;
vertical-align: middle;
}
.centered-content {
display: inline-block;
vertical-align: middle;
}
這為您提供了 2 個真正可重用的類,用於將任何孩子置於任何父母的中心! 只需添加.centered-wrapper
和.centered-content
類。
那么, :before
元素是怎么回事? 它有助於vertical-align: middle;
並且是必要的,因為垂直對齊不是相對於父級的高度 -垂直對齊是相對於最高兄弟姐妹的高度!!! . 因此,通過確保存在高度為父級高度的兄弟(100% 高度,0 寬度使其不可見),我們知道垂直對齊將相對於父級的高度。
最后一件事:您需要確保您的html
和body
標簽是窗口的大小,以便它們居中與瀏覽器居中相同!
html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
我在html
上使用 flexbox 。 為了獲得不錯的效果,您可以匹配瀏覽器的 chrome,以便在大於頁面最大值的屏幕尺寸上對內容進行框架化。 我發現#eeeeee
匹配得很好。 您可以添加一個框陰影以獲得漂亮的浮動效果。
html{
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-content: center;
align-items: center;
height:100%;
margin: 0;
padding: 0;
background:#eeeeee;
}
body {
margin: 0;
flex: 0 1 auto;
align-self: auto;
/*recommend 1920 / 1080 max based on usage stats, use 100% to that point*/
width: 100%
max-width: 900px;
height: 100%;
max-height: 600px;
background:#fafafa;
-webkit-box-shadow: 0px 0px 96px 0px rgba(0,0,0,0.75);
-moz-box-shadow: 0px 0px 96px 0px rgba(0,0,0,0.75);
box-shadow: 0px 0px 96px 0px rgba(0,0,0,0.75);
}
圖片/數據由StatCounter 提供
http://bluerobot.com/web/css/center1.html
body {
margin:50px 0;
padding:0;
text-align:center;
}
#Content {
width:500px;
margin:0 auto;
text-align:left;
padding:15px;
border:1px dashed #333;
background-color:#eee;
}
寫就好了
<body>
<center>
*Your Code Here*
</center></body>
嘗試這個
body {
max-width: max-content;
margin: auto;
}
<style>
body{
max-width: 1180px;
width: 98%;
margin: 0px auto;
text-align: left;
}
</style>
只需在應用任何 CSS 之前應用此樣式。 您可以根據需要更改寬度。
那這個呢:
margin-bottom 和 margin-right 也是如此;
<style type="text/css"> html { height: 100%; } body { width: 60%; height: 50%; margin: 25% 20% 25% 20%; border: 1px solid; } </style>
<body class="center">
<div align="center">
</div>
</body>
這是使用 glitch.me 對我有用的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.