0

I've been looking at em as a more responsive design over using px and I can't find anything on the conversions for things like borders and padding.

I know in font size you would do the pixel size you want divided by the default font-size (16px in most cases) but what about with borders and padding where there isn't necessarily a default?

Cheers

scottdavidwalker
  • 730
  • 1
  • 8
  • 24

3 Answers3

3

You simply still use the font size. Example:

body {
  font-size: 10px;
}

div {
  border-width: 2em;
}

In this case, the div has a border-width of 20px. The font-size of an element also influences the other dimensions then:

div {
  font-size: 12px;
  border-width: 2em;
}

Now the div has a border-width of 24px, despite the body's other font size.

Boldewyn
  • 78,902
  • 44
  • 148
  • 207
1

You can use media query for responsive design

@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* STYLES GO HERE */
font-size:1em;
border-width: 2em;
}
@media only screen
and (min-device-width : 481px)
and (max-device-width : 760px) {
/* STYLES GO HERE */
font-size:1.2em;
border-width: 3em;
}
erarat
  • 166
  • 2
  • 9
1

Em inherits and scales relative to its parent. Here is an article that explains it in detail:https://j.eremy.net/confused-about-rem-and-em/

Here is a very similar question that may shed more light on em calculations: How is an em calculated?

Community
  • 1
  • 1
Boris
  • 576
  • 5
  • 20