[英]how can i make a padding keeps outside of my div
我不知道我会怎么问这个问题,但是就这样,我正在训练 css 并且我实际上是在复制一些随机布局,我发现这个网站他在 div 之外使用填充(pic1,pic2),这样他的内容保持集中,我想知道我该怎么做,我的方式(pic3,pic4),因为你可以看到填充保持在 div 内(它保存我的部分产品)使部分不集中。
tsx
<div className={styles.sectionHeader}>
<h1>Top Picks</h1>
<a href="/">Ver todos</a>
</div>
<div className={styles.sectionWithTopPicks}>
<div className={styles.eachItemSection}>
<Image src={img1} width={800} height={800} layout="responsive" />
<p>texto do produto</p>
<h5>R$ 50,00</h5>
</div>
<div className={styles.eachItemSection}>
<Image src={img1} width={800} height={800} layout="responsive" />
<p>texto do produto</p>
<h5>R$ 50,00</h5>
</div>
<div className={styles.eachItemSection}>
<Image src={img1} width={800} height={800} layout="responsive" />
<p>texto do produto</p>
<h5>R$ 50,00</h5>
</div>
<div className={styles.eachItemSection}>
<Image src={img1} width={800} height={800} layout="responsive" />
<p>texto do produto</p>
<h5>R$ 50,00</h5>
</div>
</div>
</div>
scss
.productBox {
display: block;
position: relative;
box-sizing: border-box;
height: 700px;
width: 100%;
background: saddlebrown;
padding: 0 40px 0 40px;
h1 {
font-size: 4rem;
}
}
.sectionHeader {
display:flex;
justify-content: space-between;
a {
font-size: 32px;
font-weight: 400;
font: sans-serif;
}
}
.eachItemSection{
width:25%;
height: 500px;
padding: 0 0 0 22px;
img{
align-self: center;
width:
}
}
.sectionWithTopPicks{
padding-top: 50px;
flex-wrap: wrap;
background: black;
display: flex;
justify-content: space-around;
p {
color: white
}
h5 {
color: white
}
}
您如何在这里看到CSS 盒模型解释padding
和border
是 html 元素的一部分。
你也可以在这里玩。 因此,如果您想要 div 之外的空间,则可以使用边距。
对于对齐,我可以建议使用margin-left: auto; margin-right:auto;
margin-left: auto; margin-right:auto;
我理解你的目标,但你必须调整代码。
使用 flexbox 方法,您可以:
.sectionWithTopPick { display: flex; }
.sectionWithTopPick { display: flex; }
flex-grow: 0; flex-shrink: 1; flex-basis: 25%
来适应父 flex-box。 flex-grow: 0; flex-shrink: 1; flex-basis: 25%
flex-grow: 0; flex-shrink: 1; flex-basis: 25%
(这告诉内部 div 永远不要超过 25%,但必要时缩小)。 这个 div 也有填充。我同意 Ale Macedo 的回答,但我会做出以下更改。
.eachItemSection {
flex-grow: 0;
flex-shrink: 1;
flex-basis: 25%;
display: flex;
flex-direction: column;
padding: 2vh .5vw;
&:first-child {
padding-left: 1vw;
} &:last-child {
padding-right: 1vw;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.