3

I want to create a div, p or span that contains some text with a colored background and I wanted to make the div, p or span fits exactly the words inside of it. here's what I tried doing:

.bigHeader {
  font-size: 150px;
  width: 300px;
  height: fit-content;
  background-color: #FA7268;
  color: white;
}
<p class='bigHeader'>Text</p>

padding right and left are exactly as I want, but padding top and right aren't exactly as I excpected.

here's what I got:

enter image description here

and here's what I want:

enter image description here

regardless of the different sizes, shortly I want the padding top and bottom to disapear.

AIMEUR Amin
  • 320
  • 1
  • 19

1 Answers1

4

You need to use line-height:

.bigHeader {
  font-size: 150px;
  width: 300px;
  height: fit-content;
  background-color: #FA7268;
  color: white;
  line-height: 110px;
}
<p class='bigHeader'>Text</p>

It may be useful to set a percentage font size on html and body (we use 62.5% where I work) and then use REM units for font-size and line-height. This ensures your sizing stays constant.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133