So, as @scmccarthy22 said, padding just won't work. And he made a great answer explaining stuff, but there were no solutions.
The second top answer (with zero votes) had a solution: to add a wrapper element and put the padding on that. But, in ideal world all your styling would be done in CSS and CSS only, using JS or manipulating your HTML structure, although sometimes (or often) necessary, it's not ideal.
The third-top (/ last) answer gave a better solution using CSS only, but wasn't explained at all, and missed some things that would really make it work for the majority of people, so... here's the most valid answer addressing all of that ^^:
CSS-only Solution:
Padding has no effect, but border
does. So, add:
border: 16px solid transparent; /* (where "16px" is your padding) */
But what if you need a border? Use a combination of inset
and outline
:
inset: 2px;
outline: 2px solid red;
inset
is a property you don't see often so here is the MDN docs for it: https://developer.mozilla.org/en-US/docs/Web/CSS/inset
But basically, outlines does not take up space, and therefore doesn't affect the layout. So, when making outline
replace border
, which does take up space and does affect the layout... there's gonna be issues. We use inset
to counteract that.
It goes without saying, make sure the inset distance is the same as the outline's width.
If you needed to combine an outline with a border, you're out of luck. You'll have to mess with the HTML and add a wrapper element, like (current) second-top answer suggests. Or, if it's for accessibility (as in letting the user know that they've selected on the thing) then style the &:active
and &:focus
pseudo-classes.
contenteditable
element and any other regular block element, because essentially acontenteditable
element is just behaving like any other block element. – Benjaminbenji:after
gets scrolled too ... :) why don't you use atextarea
? :) at some point, simulating dedicated form elements gets really hairy :) – Cameratextarea
but can be vertically centered within acontenteditable
element usingdisplay:flex
. See this demo. I need text to be vertically centered, so atextarea
won't do. – Rochellerochemont