css屬性display設(shè)置為flex的部分(具有class屬性top-level-section)的行為類似于內(nèi)聯(lián)元素:om填充子元素,溢出父元素(id為main的div)。
知道為什么會(huì)發(fā)生這種情況以及如何解決嗎?
https://jsfiddle.net/ruud00000/qd6c3nrb/1/
CSS:
html {
scroll-behavior: smooth;
display: flex;
flex-direction: column;
justify-content: center;
}
input, textarea, button {
padding: 10px 17px;
}
html, body, section, footer, div, input, textarea, button {
font-family: 'DM Sans',sans-serif;
font-size: large;
}
html, body, section, footer, div {
border-width: thin;
}
button {
background-color: chocolate;
margin: auto;
display: flex;
border: none;
color: white;
}
body {
display: flex;
flex-direction: column;
border-color: rgb(0, 0, 0);
margin:0px;
width: 100%;
align-items: center;
}
section {
display: flex;
flex-direction: column;
border-style: solid;
border-color: grey;
margin:1px;
padding: 20px;
}
.top-level-section {
width: 95%;
/*padding: 20px;*/
}
@media (max-width: 1280px) {
/* CSS that should be displayed if width is equal to or less than 800px goes here */
html {
width: 100%;
}
}
div {
border-style: solid;
border-color: rgba(128, 128, 128, 0.301);
margin:1px;
}
footer {
display: flex;
flex-direction: column;
border-style: dashed;
border-color: grey;
margin:1px;
}
h1 {
font-size: xxx-large;
}
h2 {
font-size: xx-large;
color: grey;
}
h3 {
font-size: x-large;
color: rgba(128, 128, 128, 0.301);
}
p {
font-size: large;
}
h1, h2, h3, p, button {
display: inline-block;
}
#navbar {
position: fixed;
top:0px;
height: 50px;
width: 100%;
background-color: white;
display: flex;
border-style: none;
margin: 0px;
align-items: center;
justify-content: space-around;
}
#navbar-pages {
padding:1px;
}
#navbar div {
margin: 0px;
border-style:none;
}
#main {
margin-top: 50px;
display: flex;
flex-direction: column;
align-items: center;
}
#alleclubs, #ikwilkennismaken, #allelocaties, #meeroverkegelen, #interesse {
display: none;
}
.close-section-button {
display: flex;
margin: auto;
}
HTML:
<body>
<div id="main">
<section class = "top-level-section" id="home">
<p>[TODO ]</p>
</section>
</div>
</body>
我試圖隔離這個(gè)問題,但是當(dāng)我從頭開始創(chuàng)建一個(gè)包含一個(gè)節(jié)的div時(shí),我無法重現(xiàn)它。
這是因?yàn)槟鷽]有將框大小設(shè)置為border-box。
雖然您已經(jīng)將該部分的寬度設(shè)置為95%,但是當(dāng)上面的框大小沒有設(shè)置為border-box時(shí),20px的填充將與此分開計(jì)算。添加此屬性后,填充將被設(shè)置為指定寬度的一部分。
嘗試將框大小:邊框添加到& quot。頂級(jí)節(jié)& quot或者& quot。第& quot班級(jí)。或者也可以全局添加,如下所示:
* {
box-sizing: border-box;
}