Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bold font makes width change

Tags:

html

css

I have a div with some text inside and a border.

When I hover on that div, I want the font to be bold.​

This makes my div a bit wider - which causes an annoying flicker.

div {
  border: 1px solid black;
  display: inline-block;
  padding: 20px;
}

span {
  padding: 20px;
}

div:hover {
  font-weight: bold;
}
<div><span>This is my div</span></div>

How can I resolve this problem?

EDIT:

The div's width has to grow with content, but not with weight.

like image 844
guy mograbi Avatar asked Jul 15 '26 01:07

guy mograbi


2 Answers

Your div is inline-block and doesn't have an explicit width, so it shrink-wraps the content. The font is a proportional font, so it gets thicker (and takes up more horizontal space) when it is made bold.

Either give the div a fixed width, set it to block, or use a monospace font.

like image 149
Quentin Avatar answered Jul 17 '26 15:07

Quentin


You can simulate the bold affect using Text Shadow. so you would have

div:hover { 
text-shadow: 0 0 1px black, 0 0 1px black;
} 

You can control the affect by changing the 1px to a higher or lower number. You will need to change the color of the shadow depending on your text color too. Check out a whole article about this solution here: https://www.sitepoint.com/quick-tip-fixing-font-weight-problem-hover-states/

like image 36
adinas Avatar answered Jul 17 '26 15:07

adinas