简体   繁体   English

响应式网页具有最大宽度的怪异故障

[英]Responsive webpage weird glitch with max-width

I have my webpage resize itself using "@media screen" and it works perfectly. 我的网页使用“ @media screen”调整自身大小,并且效果很好。 It's setup to if the page reaches a certain width, it'll shrink the content to fit the page. 将其设置为如果页面达到特定宽度,它将缩小内容以适合页面。 However, there's this weird glitch that happens with my "hr" and "main p" At roughly 800px the hr and p both extend past its container, I don't want that to happen. 但是,我的“ hr”和“ main p”会发生这种奇怪的故障,在大约800px处,hr和p都超出了其容器,我不希望这种情况发生。 Here's the link( https://jsfiddle.net/3oezsrj6/1/ ) and here's my code. 这是链接( https://jsfiddle.net/3oezsrj6/1/ ),这是我的代码。 To see what I'm talking about, drag the width of the page and make it smaller and watch the content resize, but then at a certain point, on the right the content will spill out the container. 要查看我在说什么,请拖动页面的宽度使其变小,然后观察内容的大小,但是在特定的位置,右边的内容将溢出容器。

main{
    width: 960px;
    min-width: 320px;
    background-color: white;
    margin: auto;
    border-style: solid;
    border-width: 0 3px;
}
footer{
    width: 960px;
    margin: auto;
    border: 3px solid black;
    background-color: #333;
}
hr{
    border: 0;
    border-top: 3px solid black;
    width: 700px;
    margin: auto;
}
main p{
    font-size: 15pt;
    width: 700px;
    text-align: justify;
    text-indent: 50px;
    margin: auto;
}
@media screen and (max-width: 959px){
    main{
        width: 95%;
    }
    footer{
        width: 95%;
    }
}
@media screen and (max-width: 699px){
    hr{
        width: 90%;
    }
    main p{
        width: 90%;
    }
}

<main>
    <h1>Welcome</h1>
    <hr>
    <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries
    </p>
</main>
<footer>
    <p>Yeet</p>
</footer>

Your container is actually 700px wide with 15px margin on either side and 2x 3px wide borders. 您的容器实际上是700px宽,两边的15px margin15px margin为2x 3px This makes the width closer to 736px and your media query needs to reflect that size and not the width of the container. 这使宽度更接近736px并且您的媒体查询需要反映该大小,而不是容器的宽度。

@media screen and (max-width: 737px){ }

It is because of fixed width you have given for your p and hr. 这是因为您给定的p和hr宽度固定。 Here is the updated fiddle: https://jsfiddle.net/3oezsrj6/5/ 这是更新的小提琴: https : //jsfiddle.net/3oezsrj6/5/

The changes I made is : 我所做的更改是:

hr{
    border: 0;
    border-top: 3px solid black;
    width: 90%;
    margin: auto;
}
main p{
    font-size: 15pt;
    width: 90%;
    text-align: justify;
    text-indent: 50px;
    margin: auto;
}

You can change the width as per your requirements in percentage not in pixels as that will fix its width and hence not be responsive. 您可以根据需要更改宽度(以百分比为单位),而不是以像素为单位,这将固定宽度,因此无法响应。

Update below CSS and you're done... 在CSS之下更新,您就可以完成...

hr{
    max-width: 700px;
    width: 100%;
}
main p{
    width: 100%;
    max-width: 700px;
}

 *{ margin: 0; padding; 0; text-align: center; } body{ background-color: gray; } main{ width: 960px; min-width: 320px; background-color: white; margin: auto; border-style: solid; border-width: 0 3px; } footer{ width: 960px; margin: auto; border: 3px solid black; background-color: #333; } hr{ border: 0; border-top: 3px solid black; max-width: 700px; width: 100%; margin: auto; } main p{ font-size: 15pt; width: 100%; max-width: 700px; text-align: justify; text-indent: 50px; margin: auto; } @media screen and (max-width: 959px){ main{ width: 95%; } footer{ width: 95%; } } @media screen and (max-width: 699px){ hr{ width: 90%; } main p{ width: 90%; } } 
 <main> <h1>Welcome</h1> <hr> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries </p> </main> <footer> <p>Yeet</p> </footer> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM