[英]Width of website suddenly overflows when using text-align: center; and display: inline-block;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MY NAME</title>
<style>
/*CSS RESET*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/*CSS RESET*/
body
{
background-color: white;
width: 100vw;
max-width: 100vw;
}
header
{
width: 100vw;
max-width: 100vw;
height: 15vh;
max-height: 8vw;
background-color: white;
}
h1
{
font-size: 5vw;
max-height: 5vw;
line-height: 1;
color: #333333;
}
h2
{
font-size: 2vw;
max-height: 2vw;
line-height: 0.1;
color: #333333;
}
#upperContainer
{
width: 100vw;
max-width: 100vw;
max-height: 60vh;
background-color: purple;
text-align: center;
}
#leftBlock
{
width: 50vw;
height: 60vh;
max-width: 100vw;
max-height: 60vh;
height: 60vh;
display: inline-block;
vertical-align: middle;
background-color: pink;
}
#rightBlock
{
width: 50vw;
max-height: 60vh;
background-color: yellow;
display: inline-block;
vertical-align: middle;
}
#lowerContainer
{
width: 100vw;
max-width: 100vw;
max-height: 60vh;
background-color: purple;
text-align: center;
}
#leftBlock2
{
width: 50vw;
height: 60vh;
max-width: 100vw;
max-height: 60vh;
height: 60vh;
display: inline-block;
vertical-align: middle;
background-color: green;
}
#rightBlock2
{
width: 50vw;
height: 60vh;
max-height: 60vh;
background-color: yellow;
display: inline-block;
vertical-align: middle;
}
footer
{
width: 100vw;
max-width: 100vw;
height: 15vh;
max-height: 8vw;
background-color: blue;
}
</style>
</head>
<body>
<header>
<mark><h1>MY NAME</h1></mark>
<h2>DESCRIPTION</h2>
</header>
<section id="upperContainer"><article id="leftBlock"></article><article id="rightBlock"></article></section>
<!--UNCOMMENT TO SEE PROBLEM <section id="lowerContainer"><article id="leftBlock2"></article><article id="rightBlock2"></article></section>-->
<footer>FOOTER</footer>
</body>
</html>
我的問題是:當我在html中刪除注釋標簽“”時,頁面的寬度突然比瀏覽器窗口寬。 你們知道如何解決這個問題嗎? 我覺得這有點奇怪。
這是更新的JsFiddle https://jsfiddle.net/4erj4w2p/1/ 。 問題是您的寬度單位-vw(視口寬度)。 它也考慮到滾動條的寬度。 請參考: vw為什么將滾動條包含在視口中?
更新的CSS:
/*CSS RESET*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/*CSS RESET*/
body
{
background-color: white;
width: 100%;
max-width: 100%;
}
header
{
width: 100%;
max-width: 100%;
height: 15vh;
max-height: 8vw;
background-color: white;
}
h1
{
font-size: 5vw;
max-height: 5vw;
line-height: 1;
color: #333333;
}
h2
{
font-size: 2vw;
max-height: 2vw;
line-height: 0.1;
color: #333333;
}
#upperContainer
{
width: 100%;
max-width: 100vw;
max-height: 60vh;
background-color: purple;
text-align: center;
}
#leftBlock
{
width: 50%;
height: 60vh;
max-width: 100vw;
max-height: 60vh;
height: 60vh;
display: inline-block;
vertical-align: middle;
background-color: pink;
}
#rightBlock
{
width: 50%;
max-height: 60vh;
background-color: yellow;
display: inline-block;
vertical-align: middle;
}
#lowerContainer
{
width: 100%;
max-width: 100vw;
max-height: 60vh;
background-color: purple;
text-align: center;
}
#leftBlock2
{
width: 50%;
height: 60vh;
max-width: 100vw;
max-height: 60vh;
height: 60vh;
display: inline-block;
vertical-align: middle;
background-color: green;
}
#rightBlock2
{
width: 50%;
height: 60vh;
max-height: 60vh;
background-color: yellow;
display: inline-block;
vertical-align: middle;
}
footer
{
width: 100%;
max-width: 100vw;
height: 15vh;
max-height: 8vw;
background-color: blue;
}
首先,您的#rightBlock中沒有高度。添加此代碼:
height: 60vh;
然后,當您添加第二部分LowerContainer時,您將“自動”創建一個垂直滾動條。 由於滾動條的原因,您需要更多的水平空間來顯示內容,這是因為您擁有水平滾動條。
如果將height = 10vh代替,則不會有任何問題。
由於100vw,這提供了額外的寬度。 我用100%更新了100vw,效果很好。 請通過更新進行檢查。
還可以請您介紹一下大眾嗎? 與px有關嗎? 還是只是地區問題??
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.