使用text-align:center时网站的宽度突然溢出;并显示:inline-block;

时间:2016-05-16 17:34:07

标签: html css

<!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中的评论标签“”时,页面的宽度突然比浏览器窗口宽。你们有任何想法如何解决这个问题吗?我觉得这是一种奇怪的错误。

3 个答案:

答案 0 :(得分:1)

这是更新的JsFiddle https://jsfiddle.net/4erj4w2p/1/。问题是你的宽度单位 - vw(视口宽度)。它还考虑了滚动条宽度。请参阅:Why does vw include the scrollbar as part of the viewport?

更新了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;
}

答案 1 :(得分:0)

首先,#rightBlock中没有高度..添加:

height: 60vh;

然后,当你添加第二部分lowerContainer时,你自动&#34;创建一个垂直滚动条。由于您的滚动条,您需要更多的水平空间来显示您的内容,这是因为您拥有水平滚动条。

如果你改为高度= 10vh,你就不会有任何问题。

答案 2 :(得分:0)

由于100vw,这提供了额外的宽度。 我用100%更新了100vw,它工作正常。 请通过更新来检查。

还能告诉我关于vw的事吗?它与px有什么关系吗?或只是一个区域问题?