how to get an exact "em" without calculating

2

How to get a em without calculating? That is, in div > .p :

.div {
  font-size: 30px;
}
div > .p {
  font-size: .9em;
}

This gives me 27px, but if I wanted 26px, as I do to get that exact pixel without being calculated until you find it. Is there any formula?

    
asked by Ulises 24.04.2018 в 01:48
source

2 answers

1

The measure em in CSS3 is a percentage of the default value of the parent node, in your example the child inherits the default value in percentage.

For example:

body {
  font-size: 16px;
}

h1 {
  /* equivale al doble o 200% del body */
  font-size: 2em; /* 32px */
}

p {
  /* equivale a 3/4 o 75% del valor del body */
  font-size: 0.75em; /* 12px */
}
<body>
  <h1>Titulo</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac nibh imperdiet, gravida est at, vestibulum arcu. Nullam in consequat.</p>
  <hr />
  <!-- Este div tiene el valor por default del body, o sea 16px -->
  <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac nibh imperdiet, gravida est at, vestibulum arcu. Nullam in consequat.</div>
</body>

Conclusion : the value in em , applied in a formula would be something like valorEnPixles / padreEnPixeles , in your example, 26 / 30 that gives 0.86em .

    
answered by 24.04.2018 в 05:23
0

It's like @Kleith mentions, just a rule of 3 and I quote:

  

The value in em , applied in a formula would be something like valorEnPixles / padreEnPixeles , in your example, 26 / 30 eso da 0.86em .

Now you must remember, that the idea of using ems , is precisely not recalculating the inherited measures, so that they are "pixel perfect" and doing that calculation would always be a lost time, above all if you are all the time manipulating the base measure of the parent container. It simply has no case.

What I recommend, is that you are not clear yet, is to look for examples, why it is currently recommended to use " em " instead of pixel in certain cases and change "the ship" in the form of layout, because you are pretending to use a dynamic measure as if it were a static measure and the truth, if you really need to use specific measures, it will always be better to use the measure directly in pixeles and not in em .

    
answered by 24.04.2018 в 18:31