19
kiki
3y

css quick maffs

so you did this:

.foo:hover {
transform: scale(1.1);
}

and now ugly scrollbar is there when the element is scaled.

No, don't do overflow: hidden. There's a better way. Instead, do this:

.container {
padding: 1rem;
box-sizing: border-box;
}

the element total width is calculated based on the width of its content. That's true unless you specified width and height explicitly (if you did so, you're a doofus, I'm sorry).

Scaling makes content somewhat larger. With border-box, paddings work differently with the total width.

By default, if you set width to say 100px, and paddings are 20px, total width will be 140px — it's your 100px of content plus two paddings of 20px. width property set the width of the content, not the total width.

With border-box, width property sets the total width. So if you set width to 100px and paddings to 20px, total width would be 100px, just like you set it, and content will be 60px wide — it's 100px minus 20px times two.

The key part is it doesn't end with explicit width. The algorithm remains. When some node is rendered, its total width is calculated. When you use border-box, the total width will stay the same even if your content grows by some value that is less than your paddings. So, your content was 100px, you scaled it, and it became 110px. Well, then that extra 10px will be subtracted from your paddings, and they will be 15px each instead of 20px.

No more ugly scroll bar. Yaaay!

aight bye

Comments
  • 0
    Yep inner padding is something I learned early on - it has prevented a lot of extra work.
  • 1
    @ojt-rant it’s not about paddings. It’s about border-box. Matter of fact, all my websites include this:

    *, *::before, *::after { box-sizing: border-box; }

    https://github.com/mvoloskov/...
  • 1
    @kiki right... it's not only about paddings, but also borders and inflation of content area.
  • 0
    damn u really are god.

    Can you help me fix my dumb page if i pay with virtual hugs and positive messages lmao
  • 0
  • 0
    damn, devrant needs a bookmark feature ASAP.
  • 3
    @minsomai press Favorite
  • 0
    @kiki ah yes sorry, I misread your comment. I was thinking about "box-sizing: content-box;" If you apply that to your element, the padding acts as an inner padding. Which means if you add padding to it, you'll never accidentally push the element outside of your maximum allowed width or height (unless of course the content of the box does that for you).
Add Comment