px vs em/rem padding

Hey, i recently found out that using px for padding isn't necessarily a negative thing, especially if you don't want it to scale with text size. I also heard it shouldn't cause overflow but have found out otherwise, unless i'm doing something wrong? The first image is using em, the second is using px, and as you can see, the px causes the text to overflow. if i reduce the px value it somewhat fixes it however even if the value is larger i don't believe it should overflow? I'd appreciate any insight since i'd like to be able to get out of using em/rem for a lot of things haha. Thanks in advance. https://codepen.io/deerCabin/pen/mdNpbjZ
R
CodePen
px vs em
...
No description
No description
71 Replies
Chris Bolson
Chris Bolson2mo ago
The px isn't causing the overflow. It is simply the length of the longest "words". 60px is a lot more than 1em so you can't really compare. By default the browser won't break up the words so it has no choice but to overflow outside if it's container.
snxxwyy
snxxwyyOP2mo ago
oh right i see, thanks
clevermissfox
clevermissfox2mo ago
You’re giving everything padding of 5px on the * selector, anything that doesn’t get its padding inline overwritten is automatically adding 10px. so for the html and body combined you have 20px of inline space . Your padding/—gap value is 60px. Your width on the wrapper for smaller screens is 100% - 60px * 2 which has to also include the padding-inline of 5px*2; then you have padding of 60px * 2 on the container and 10px on the <p>. So if the screen size is 320px, subtract the padding on the html and body 320-20px=300px, the wrapper width is then 300-120=280px; so container has 280px to live in which has to include the 10px of padding. Subtract the inline padding on the container from there that is set with the —gap custom property. 280-10-120= 150px Then you have the paragraph element which has to fit in there as well as it’s 10px of padding. That gives the paragraph 140px width to live in which is not enough for even the longest word
snxxwyy
snxxwyyOP2mo ago
oh okay that makes sense, i appreciate the explanation
clevermissfox
clevermissfox2mo ago
I’m a proponent of using em and clamp so padding scales with the element though; I only use a pixel unit for things like border width , text-decoration-thickness , letter spacing , anything that has to be a couple pixels. But have started using em for those too
snxxwyy
snxxwyyOP2mo ago
yeah that's fair. I was having a chat with b1 and he pointed out that increasing the browser font size can make em paddings on things like buttons too big eventually.
clevermissfox
clevermissfox2mo ago
Using em always keeps it the same relative to the font size . If the font size is 1rem and the padding-inline is 1.5em , then it will always be 1.5x the font size. If your clamp brings the font-size down to 0.75rem, the padding will be 0.751.5=1.125rem. If the font size is 2rem, the padding will 21.5=3rem. But if you set padding in pixels , say 20px, if the font is too small the padding will look enormous;if the font is big the padding will look small. So you’d have the same problem with pixels. Anyone that’s increasing the base body font size that much via the user agent styles is not worried about the aesthetics of the page, they’re used to things getting wonky and they just want to read the content. At least with ems, paddings will still be balanced the way you intended. Everyone has their preferences, there’s no harm in setting with pixels you just can’t keep it balanced at different screen sizes without resetting padding on everything anytime you reset the font size. You could use custom properties to make this easier but this is what EM does best so it’s a great tool for that use case. I get ocd about efficiency so if there’s something that helps me write more DRY-ly, less lines in the file and increases efficiency (another example being clamp) I prefer to use it. Other people prefer to change their font sizes manually in a few media queries, at which point you could adjust all your paddings or the custom property you set for it Just personal preference on which tools one chooses to utilize for this particular scenario
snxxwyy
snxxwyyOP2mo ago
yeah that makes sense, i see, thank you
b1mind
b1mind2mo ago
My point is once you get to a point the big font + big padding is bad and just makes harder to read because its now squished in the container. #justTestIt I'm not a big fan of using just REM/EM in clamps either mix of both px + magic vw + rem (or the reverse) #alwaysDepends Mobile its a massive issue btw (but most people don't realize it until they use a bigger font xD)
b1mind
b1mind2mo ago
Why You Should Use px Units for margin, padding, & Other Spacing Te...
When users increase their text size, they're not trying to make the space around text bigger.
b1mind
b1mind2mo ago
I'd love to see some of your examples for those clamp/padding cases though* cleverMissFox 😄 Always willing to learn a new thing or change my mind.
clevermissfox
clevermissfox2mo ago
I get your point and I agree, it can get to a big bad ugly point . I just prefer to use em. Same is true with small. What kind of examples do you mean? A clamp and padding in em ?
b1mind
b1mind2mo ago
Well yea just in general cause I've read you say this before so figured you had something I could test. I know you use clamp a lot for different box-model stuff So you don't have to media query or something aswell*?
clevermissfox
clevermissfox2mo ago
Sure most of my codepen uses clamp and padding in ems, although I've changed my Approach I used to put the clamp on the HTML and I don't anymore. I'll use media queries or container queries when needed but I don't often find myself needing to change the font size or the padding of anything
b1mind
b1mind2mo ago
❤️ your one follow is one my fav devs
clevermissfox
clevermissfox2mo ago
I find the ems approach most attractive to my designer side to keep everything scaling at the same rate, up or down. I get kind of OCD about it sometimes I have another code pen that I believe I have more follows on that's more of a test account than actual finished anything haha but now I have to see who you're talking about Oh my God Cassie evans! She is such an SVG Queen!!!!
b1mind
b1mind2mo ago
Yea I could see that, its not something I get to obsessed about. I'm also never really designing that strict outside set sizes in the system and I don't want those to change. I've never had a case where I used pixels for padding and went.. damn that should have been rem though either 😂 so its like I just test, usually when I'm doing squish tests for responsiveness. I'm one those "pixels perfect is a lie" when it comes to the web too though Building to expand and contract most of the time so xD
clevermissfox
clevermissfox2mo ago
No for sure, I agree pixel perfect is pretty unobtainable even in design. Everything can be perfectly centered but visually the glyphs don't LOOK centered. I've had to give up on that dream.
b1mind
b1mind2mo ago
Idk why but outside xtra large titles I font the font changing size when it gets smaller is a no for me most times too
clevermissfox
clevermissfox2mo ago
I do despise getting the layout responsive and having to have a media query just for font size though. Clamp and min are some of my faves.
b1mind
b1mind2mo ago
Yea but that is more exact I'm that person lol
clevermissfox
clevermissfox2mo ago
I can understand that too
b1mind
b1mind2mo ago
But I don't need to do it often its mostly headings I don't change <p> ever really ... Also if you look at design systems having concise in font size is like top of the list. So having odd sizes not based on the system to me is worse magic numbery turns me off big time too xD This goes to show though its really #itDepends cause I def see your side too looking at the work you do its very persice to a set size/design. Having it look the same at scale makes sense in that regards You gotta make sure everything scales though... or it breaks the illusion
b1mind
b1mind2mo ago
No description
b1mind
b1mind2mo ago
that toggle screams at me... I've been forgotten
clevermissfox
clevermissfox2mo ago
I haven't found the perfect clamp system yet that I love the rates for all sizes. Luckily most static designs for non-motion graphics don't have to grow or shrink so you can find the perfect typeface and size
b1mind
b1mind2mo ago
Also the alignment changes for the + but I think thats not rem issues xD
clevermissfox
clevermissfox2mo ago
Hahaha oh nooo!!! Idk what happened there I remember I was experimenting. This one is about a year old so don't judge me too harshly I just started last Sept And the skewed border I remember doesn't scroll well
b1mind
b1mind2mo ago
nanan I respect your stuff no judgment, trying to learn your pov I just clicked the ones I saw clamps in xD not a fan of the 62.5% trick either >.>;;
clevermissfox
clevermissfox2mo ago
I did start off with the 62.5% too which I thiught was sooo magical . Didn't realize
b1mind
b1mind2mo ago
Yea its dangerous*
clevermissfox
clevermissfox2mo ago
Yep exactly. It was a bad habit I have since broken
b1mind
b1mind2mo ago
Ok not getting off topic like the works, I know my opinion is not a popular one 😄 I def need to use more calc/clamps in my grids so trying to learn. I have a friend that scales stuff on his 4k with larger(24px) font and still zooms like 120% 🤣 He was the first person that was like hey B this don't look good. Since then I've been trying to pay attention to what I want to work with fontsize/viewports
clevermissfox
clevermissfox2mo ago
It's easy to get in a habit too, I just happened to start learning woth clamp , I wouldn't be surprised if something changed and I didn't adopt it . Just because it's new and fancy it doesn't always mean best and if it ain't broke.
b1mind
b1mind2mo ago
Clamp scares me a little I think cause most people use them with rem and magic vw xD
clevermissfox
clevermissfox2mo ago
And tbh I don't test up to 24px AND 120 zoom
b1mind
b1mind2mo ago
So I'm like uhhhhhh I can set one media query and change that H1 for mobile 😂 And this is my main point people need to test and not just think REM/EM for everything is a good idea
clevermissfox
clevermissfox2mo ago
Wait, Rem and vw in the clamp, why is that not good or why scary
b1mind
b1mind2mo ago
My point about clamp too like if you use REM only for largest fonts and find that sweet formula you like on that clamp maker.... but don't up your font size to test? 😄 danger zone ... that is what happen to me well one of the things cause my heading was then overflowing cause user changed the font 😉
clevermissfox
clevermissfox2mo ago
I've never tested both 24px body font AND 120% zoom tbh. One or the other but never both
b1mind
b1mind2mo ago
oh no thats just him my buddy and his giant monitor xD The issue would happen without zoom or not Zoom actually works better than rem 😦 from a user standpoint imo (rare do I see things break on zoom cause it hits media queries) Most users that set fontsize are not doing it in the browser though (like him) its a OS setting So they are increasing font in OS (browsers use it) and then is zooming websites.
clevermissfox
clevermissfox2mo ago
this is up to 24px body font and i dont think it looks terrible. normal vs 24px
No description
No description
b1mind
b1mind2mo ago
Right but it wouldn't look bad as px either o wait right one is px?
clevermissfox
clevermissfox2mo ago
Sorry I thought they sent in order. Large one/left is 24px, right is clamped
b1mind
b1mind2mo ago
I gotta full screen these discord distorts stuff 🤔 yea idk this does not win me over any 😂 I wish I could find this other post it had lots of nice examples
clevermissfox
clevermissfox2mo ago
Hahaha I was just quelling my own worries that I wasnt testing zoom and large fonts
b1mind
b1mind2mo ago
So I know sighted users will go 32px or up to where they need, I know its not the average either but I've been atleast doing 32px
clevermissfox
clevermissfox2mo ago
clamp, 24px, 32px
No description
No description
No description
b1mind
b1mind2mo ago
So my thing is you fit more in a smaller space too 😄 24px isn't bad 32px def gets tight but that is also from a max-height?
clevermissfox
clevermissfox2mo ago
What were you saying about not using rem and viewport in clamp ?
b1mind
b1mind2mo ago
not viewport but using only REM values in the clamp calc having a min/max pixel value it can't grow over You don't need to respect the user font size that much <p> is honestly the only one that really matter imo but we won't get that spicy in this thread That is only an issue when you are doing large fonts/designs Like my folio xD
clevermissfox
clevermissfox2mo ago
I am not opposed to using pixel in the clamp just never came across a pattern that does so. Will it still zoom when it's at a min/max if it's px only ?
b1mind
b1mind2mo ago
I've seen some those breaking because root font size grows them past the magic number if that makes sense mmm good question. But again zoom triggers breakpoints I need to test that though good question. Zooms odd... like how it treats vw/vh is odd I guess that would be the one time I would use vw for font-size too is large screen filling fonts cause zoom wouldn't effect it either xD oh that feels so wrong but it sounds right (time for 🧑‍🔬 )
clevermissfox
clevermissfox2mo ago
Gosh typography
b1mind
b1mind2mo ago
right?!? (I'm terrible at it so take my opinions lightly LOL)
clevermissfox
clevermissfox2mo ago
I know you're not terrible 😆
b1mind
b1mind2mo ago
I'm good at derailing threads xD
clevermissfox
clevermissfox2mo ago
can you figure out why the <a> and <button> with the same class applied to both, pretty much all the same styles and a display of block,; the button automatically centers the icon and the <a> doesnt? its not text-align: center but i cant find what the difference is between the <a> and the <button> https://codepen.io/Miss-Fox/pen/abeEmmO?editors=1100
b1mind
b1mind2mo ago
text-align center? yea that been there 😂
clevermissfox
clevermissfox2mo ago
sorry, i kept testing that and it wasnt wworking but then it did. but it doesnt vertically align in the <a>
clevermissfox
clevermissfox2mo ago
is that just vertical align?
b1mind
b1mind2mo ago
horizontal? idk axis confuse me
clevermissfox
clevermissfox2mo ago
Idj why it's not working vertical-align middle but text align center wasn't working either . No worries just thought I'd check since you were on
b1mind
b1mind2mo ago
I make them .btn utils grid xD
clevermissfox
clevermissfox2mo ago
For sure ill prob do so, it's just triggering my ocd that I can't figure out which property is missing from <a> to make them match 😆
Chris Bolson
Chris Bolson2mo ago
An interesting thread to follow 👍 Re the button vertical centering it is probably due to the intrinsic button styling which is automatically centering it vertically. https://html.spec.whatwg.org/multipage/rendering.html#button-layout
If the element is an input element, or if it is a button element and its computed value for 'display' is not 'inline-grid', 'grid', 'inline-flex', or 'flex', then the element's box has a child anonymous button content box with the following behaviors: The box is a block-level block container that establishes a new block formatting context (i.e., 'display' is 'flow-root'). If the box does not overflow in the horizontal axis, then it is centered horizontally. If the box does not overflow in the vertical axis, then it is centered vertically.
clevermissfox
clevermissfox2mo ago
Chris! Amazing find!!! I did set both the link and the button to display block but couldn’t figure out what property was missing from the link to make it center vertically too. My brain is absolutely fried today 😵‍💫 but will read through this tomorrow and see what I can figure out to make them look the same ✨ you’re the best
pangalan1
pangalan12mo ago
you can fix this with overflow-wrap: break-word its from josh comeau's css reset aswell
clevermissfox
clevermissfox2mo ago
I just took a look at this to see what I had broken along the way but I dont see this. Do you happen to remember which browser /oS you used on this screenshot ? This is the pen in case that's helpful. And is it only the first toggle that's shrunken or is it all of them?
No description
Want results from more Discord servers?
Add your server