简体   繁体   English

如何使用填充使弹性元素更大?

[英]How do I make a flex element bigger with padding?

I have this div, and I'm trying to have the padding on top and bottom be a little bit more, to distance the text from the edge.我有这个 div,我试图让顶部和底部的填充多一点,以使文本与边缘保持距离。 The div I'm talking about is the parent one (#skip).我正在谈论的 div 是父级(#skip)。 The CSS is as follows: CSS如下:

I tried using padding and margin, but both don't have the intended effect.我尝试使用填充和边距,但两者都没有预期的效果。 Any suggestions?有什么建议么?

 .break { flex-basis: 100%; width: 0; }.subtext { font-size: 30px; text-transform: capitalize; } #skip { flex-wrap: wrap; display: flex; cursor: pointer; flex-direction: row; align-items: center; justify-content: center; text-align: center; transition-duration: 0.5s; position: absolute; width: auto; top: 470px; left: 50%; transform: translateX(-50%); height: 55px; background: #FFFFFF; box-shadow: 0px 4px 4px #9f9f9f; border-radius: 100px; text-transform: uppercase; font-family: 'Lato', sans-serif; font-style: normal; font-weight: normal; font-size: 60px; line-height: 28px; color: #A8C8A3; transition-duration: 0.3s; }
 <div id="skip" style="height: 5vh;padding-top: ;" class="position-absolute">Entra <div class="break"></div> <div class="subtext">Home Page Classica</div> </div>

 .subtext { font-size: 30px; text-transform: capitalize; cursor: auto; } #skip { flex-wrap: wrap; display: flex; cursor: pointer; flex-direction: row; align-items: center; justify-content: center; text-align: center; transition-duration: 0.5s; position: absolute; padding: 30px; top: 470px; left: 50%; transform: translateX(-50%); height: 505px; background: #FFFFFF; box-shadow: 0px 4px 4px #9f9f9f; border-radius: 100px; text-transform: uppercase; font-family: 'Lato', sans-serif; font-style: normal; font-weight: normal; font-size: 60px; line-height: 28px; color: #A8C8A3; transition-duration: 0.3s; }
 <div id="skip" class="position-absolute">Entra <div class="subtext">Home Page Classica</div> </div>

Try This!尝试这个!

Update:更新:

Don't make the width: auto;不要设置width: auto; Then you can do your padding然后你可以做你的填充

  1. remove the styling in your html删除 html 中的样式
  2. clean up your code, delete duplicates.清理你的代码,删除重复项。
  3. add a wrapper, so you can center the div vertically and horizontally on the page (I assumed that was your goal)添加一个包装器,这样您就可以在页面上垂直和水平居中 div(我认为这是您的目标)
  4. define a width for the #skip element定义#skip 元素的宽度
  5. add padding, remove eventually line-height添加填充,最终删除行高

 body, html { height: 100%; } #wrapper { display: flex; align-items: center; /* align vertical */ justify-content: center; /* align horizontal */ height: 100%; } #skip { flex-direction: column; padding: 60px 30px; min-width: 30%; border-radius: 100px; background: #FFFFFF; box-shadow: 0 4px 4px #9f9f9f; color: #A8C8A3; text-align: center; text-transform: uppercase; font-size: 60px; font-family: 'Lato', sans-serif; /* line-height: 28px; */ cursor: pointer; transition-duration: 0.3s; }.break { }.subtext { font-size: 30px; }
 <div id="wrapper"> <div id="skip" class="position-absolute">Entra <div class="break"></div> <div class="subtext">Home Page Classica</div> </div> </div>

I had removed some of the code just to clear things up a little.我删除了一些代码只是为了稍微清理一下。

The cleanest way to move the text away from the edge is actually to use padding, but as you found, it annoyingly changes the width of our parent element.将文本从边缘移开的最简洁的方法实际上是使用填充,但正如您所发现的,它会令人讨厌地改变我们父元素的宽度。 We can negate that effect by adding the box-sizing: border-box property to the element we will be applying the padding to.我们可以通过将box-sizing: border-box属性添加到我们将应用填充的元素来消除这种影响。 This way, the padding actually gets put onto the inside of the element, rather than the outside.这样,填充实际上被放在元素的内部,而不是外部。

In my code snippet below, I gave the parent #skip the display: flex property, along with the flex-direction: column , justify-content: center , and align-items: center .在下面的代码片段中,我为父#skipdisplay: flex属性,以及flex-direction: columnjustify-content: centeralign-items: center When centering items in a column flex, justify-content aligns them vertically, while align-items aligns them horizontally.当在列 flex 中居中时, justify-content将它们垂直对齐,而align-items将它们水平对齐。

In order to space everything out, I went ahead and added a margin to the .subtext element as well.为了将所有内容分开,我继续为.subtext元素添加了边距。

The reason there were so many issues with spacing on yours, is because you were collapsing the parent by assigning the #skip element a height that would have been smaller than the resulting height from the font-size you had set.您的间距存在如此多问题的原因是,您通过为#skip元素分配的高度会小于您设置的font-size的结果高度来折叠父级。

In terms of responsiveness, it is generally best practice to let the elements base their height on the content of the element.在响应性方面,通常最好的做法是让元素的高度基于元素的内容。

You should also refrain from defining line-heights in pixels, try to use em or rem 's instead.您还应该避免以像素为单位定义行高,而是尝试使用emrem They adjust based on the font-size of the parent element.它们根据父元素的字体大小进行调整。

 .subtext { font-size: 30px; text-transform: capitalize; margin-top: 32px; } #skip { display: flex; flex-direction: column; align-items: center; justify-content: center; box-sizing: border-box; padding: 32px; transition-duration: 0.5s; background: #FFFFFF; box-shadow: 0px 4px 4px #9f9f9f; border-radius: 100px; text-transform: uppercase; font-family: 'Lato', sans-serif; font-size: 60px; color: #A8C8A3; }
 <div id="skip"> Entra <div class="subtext">Home Page Classica</div> </div>

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

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