Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how get current text size in css

Tags:

css

I'm tring to write a css that for all string that I can put on increase these strings about 3px. I don't know how can I do this, I think something like this:

.increaseSize {
getSize()+3px;
}

Anyone can help me?

like image 904
Foppy Avatar asked Jul 04 '17 08:07

Foppy


People also ask

How do I set relative font size in CSS?

It is related to the font size of the parent container. One em (1em) is equal to the current font size. So for example, if the parent element has the font size of 16px than 1em is equal to 16px, 2em is equal to 32px, and so on. Making your design responsive becomes much easier if you use em units instead of px.


2 Answers

I would seek a different and much simpler approach.

p.myText {
  font-size: 16px;
}

.increaseSize {
  font-size: 1.5em
}
<p class="myText">This is some standard text and <span class="increaseSize">this is a bigger font size text</span></p>

This would make any .increaseSize element font size 50% bigger than its parent.

You can use calc() but maybe browser compatibility is a problem. This is a safer solution.

Does this help you?

like image 61
Jose Gomez Avatar answered Oct 25 '22 23:10

Jose Gomez


You can use it like the following example:

div {
  font-size: 1em;
}
span.increase {
  font-size: calc(100% + 3px);
}
<div>
  Hello <span class="increase">World</span>
</div>

With the % unit you can get the actual font size of the element. Now you can use calc (browser compatibility) to add the 3px to the current font-size.

You shouldn't calculate the font-size yourself for responsive web-design. You can / should use relative units like em, rem or % to achieve responsive font-size.

like image 36
Sebastian Brosch Avatar answered Oct 25 '22 23:10

Sebastian Brosch