Whilst creating a navigation bar for my site I decided to make the active page tab show up in bold for usability purposes, but when I change the font-weight
on the element it only slightly makes the element wider, an example I made using hover effects instead demonstrates my issue and i've never known a way to solve it..
http://jsfiddle.net/amx7E/
HTML
<ul id="mainNav">
<li class="navItem">
<a class="navLink" id="activeLink">Link 1</a>
</li>
<li class="navItem">
<a class="navLink">Link 2</a>
</li>
<li class="navItem">
<a class="navLink">Link 3</a>
</li>
</ul>
CSS
* {
font-family: Arial;
font-size: 14px;
list-style: none;
margin: 0;
padding: 0;
text-decoration: none;
}
#mainNav {
background: RGB(200, 230, 240);
border-bottom: 1px solid RGB(0, 0, 0);
height: 30px;
margin: 0 auto;
position: relative;
width: 100%;
}
.navItem {
display: block;
float: left;
position: relative;
}
.navItem:last-child .navLink {
border-right: 1px solid RGB(0, 0, 0);
}
.navLink {
border-bottom: 1px solid RGB(0, 0, 0);
border-left: 1px solid RGB(0, 0, 0);
display: inline-block;
height: 30px;
line-height: 30px;
padding: 0 15px;
white-space: nowrap;
}
.navItem:hover .navLink {
background: RGB(120, 200, 250);
color: RGB(255, 255, 255);
cursor: pointer;
font-weight: bold;
}
#activeLink {
background: RGB(90, 170, 220);
color: RGB(255, 255, 255);
font-weight: bold;
}
#activeLink:hover {
background: RGB(110, 190, 240);
}
.navItem:hover .subNav {
display: block;
}
Note that when using relative weights, only four font weights are considered — thin (100), normal (400), bold (700), and heavy (900).
To bold the text in HTML, use either the strong tag or the b (bold) tag. Browsers will bold the text inside both of these tags the same, but the strong tag indicates that the text is of particular importance or urgency. You can also bold text with the CSS font-weight property set to “bold.”
400 – Normal. 500 – Medium. 600 – Semi Bold (Demi Bold) 700 – Bold.
To create a CSS bold text effect, you must use the font-weight property. The font-weight property determines the “weight” of a font, or how bold that font appears. You can use keywords or numeric values to instruct CSS on how bold a piece of text should be.
Other than the width
route here are two other possibilities, it's down to personal preference as to whether you think they are suitable or not. Both these ideas work on the same principal, that you use a separate element to show the bold state, and this element either doesn't (idea one) or does (idea two) affect the UI with it's dimensions.
http://jsfiddle.net/3Jyge/2/
Use pseudo selectors. This method relies on the browser supporting quite recent advances i.e. :before
and content: attr()
so probably isn't reliable just yet.
css:
ul {
list-style: none;
}
ul li {
float: left;
}
ul li:hover a {
visibility: hidden;
}
ul li:hover:before {
position: absolute;
font-weight: bold;
content: attr('data-text');
}
markup:
<ul>
<li data-text="one"><a href="#">one</a></li>
<li data-text="two"><a href="#">two</a></li>
<li data-text="three"><a href="#">three</a></li>
</ul>
The other is a bit more straight-forward, although it relies on preping your markup first — and those who use screen readers may understandably dislike your site; unless you can find a nice way to hide the duplicate text from them.
markup:
<ul>
<li><a href="#">
<span class="a">one</span>
<span class="b">one</span>
</a></li>
<li><a href="#">
<span class="a">two</span>
<span class="b">two</span>
</a></li>
<li><a href="#">
<span class="a">three</span>
<span class="b">three</span>
</a></li>
</ul>
css:
ul {
margin: 0; padding: 0;
list-style: none;
}
ul li {
float: left;
}
ul li a span.b {
visibility: hidden;
font-weight: bold;
}
ul li a span.a {
position: absolute;
}
ul li:hover a span.b {
visibility: visible;
}
ul li:hover a span.a {
visibility: hidden;
}
At the end of the day the better solutions would be:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With