Inline Elements Shifting When Made Bold on Hover

Inline elements shifting when made bold on hover

Pre-set the width by using an invisible pseudo-element which has the same content and styling as the parent hover style. Use a data attribute, like title, as the source for content.

li {
display: inline-block;
font-size: 0;
}

li a {
display:inline-block;
text-align:center;
font: normal 16px Arial;
text-transform: uppercase;
}

a:hover {
font-weight:bold;
}

/* SOLUTION */
/* The pseudo element has the same content and hover style, so it pre-sets the width of the element and visibility: hidden hides the pseudo element from actual view. */
a::before {
display: block;
content: attr(title);
font-weight: bold;
height: 0;
overflow: hidden;
visibility: hidden;
}
<ul>
<li><a href="#" title="height">height</a></li>
<li><a href="#" title="icon">icon</a></li>
<li><a href="#" title="left">left</a></li>
<li><a href="#" title="letter-spacing">letter-spacing</a></li>
<li><a href="#" title="line-height">line-height</a></li>
</ul>

Text shifting on bold hover

Instead of using the bold attribute to highlight an element, the proper approach is to give the currently hovered link a different color - that way, you won't have these kinds of problems!

Simply remove the bold attribute and change the color to something different, like so:

a:hover 
{
text-decoration:none;
color:#000000;
}

Remember that making something bold makes it's size larger to render, and that this is the generally preferred approach.

Here's a working jsFiddle - http://jsfiddle.net/yzsfH/5/

How to stop the movement of box on hover?

The box isn't moving, it's just getting a little bit wider (right aligned, so the left border goes to the left a little bit). That's due to the bold and italic font taking a little more horizontal space than the regular font.

To avoid this, you can use a fixed width on the box: Erase the padding-right and padding-leftand give it text-align: center and an appropriate width setting.

Here's the according fiddle: https://jsfiddle.net/ghwzzyjy/1/

Inline elements shifting when made bold on hover

Pre-set the width by using an invisible pseudo-element which has the same content and styling as the parent hover style. Use a data attribute, like title, as the source for content.

li {
display: inline-block;
font-size: 0;
}

li a {
display:inline-block;
text-align:center;
font: normal 16px Arial;
text-transform: uppercase;
}

a:hover {
font-weight:bold;
}

/* SOLUTION */
/* The pseudo element has the same content and hover style, so it pre-sets the width of the element and visibility: hidden hides the pseudo element from actual view. */
a::before {
display: block;
content: attr(title);
font-weight: bold;
height: 0;
overflow: hidden;
visibility: hidden;
}
<ul>
<li><a href="#" title="height">height</a></li>
<li><a href="#" title="icon">icon</a></li>
<li><a href="#" title="left">left</a></li>
<li><a href="#" title="letter-spacing">letter-spacing</a></li>
<li><a href="#" title="line-height">line-height</a></li>
</ul>

Hover two elements without shifting effect

Tried it with the transition statements only on the '.logo__name' , '.logo__name h1 div' , '.logo__name h1 span' tags. Shouldn't be necessary on the hover tags, since it's already implied by the non-hover version. This what you're lookin' for?

.logo__name {  position: absolute;  top: 0px;  left: 50%;  transform: translateX(-50%);  padding: 10px;  padding-bottom: 15px;  transition: all 300ms ease;}
.logo__name:hover { background-color: #e54b4b;}
.logo__name:hover h1 { color: #ffffff;}
.logo__name:hover h1 span { color: #ffffff;}
.logo__name h1 { font-family: 'Sacramento', Cursive; font-size: 2rem; font-weight: normal; margin: 0; padding: 0; text-align: center;}
.logo__name h1 div { height: 33px; overflow: hidden; transition: all 300ms ease;}
.logo__name h1 span { font-family: 'Pt Serif', Serif; font-size: 0.85rem; text-transform: uppercase; margin-top: -50px; display: block; transition: all 300ms ease;}
<!DOCTYPE html><html><head>  <meta charset="utf-8">  <meta name="viewport" content="width=device-width">    <link href="https://fonts.googleapis.com/css?family=PT+Serif:400,700|Sacramento|Pathway+Gothic+One" rel="stylesheet">  <title>Myriam Lefebvre</title></head><body><div class="logo__name"> <h1><div>Myriam lefebvre</div><br /><span>Développeure-front-end</span></h1></div></body></html>


Related Topics



Leave a reply



Submit