Prevent HTML/CSS-Navbar from changing height while using border-bottom - html

I got a little problem when using a border-bottomwith a hover-effect on my navbar.
Is there any way to stop that? Or, lets say, give the Navbar the height it would actually by while hovering?
I would preffer a solution for not changing the height on hovering, but I'm open for anything.
Here is a JSFiddle of my Navbar: https://jsfiddle.net/ay3u7trd/
Thanks!

Add border-bottom: 3px solid RGBA(0,0,0,0); to your a { }. This will add the 3px padding to all of your hyperlinks without applying a specific color (the background color native to the object will appear).
Then once the :hover effect takes place it just shows up right there, since the border is already present you have no weird height issues.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
border-bottom: 1px solid #121214;
z-index: 10;
text-transform: uppercase;
}
li {
float: left;
border-right: 1px solid #121214;
text-decoration: none;
}
a {
color: #ff8800;
border-bottom: 3px solid RGBA(0,0,0,0);
}
a:hover {
color: #ff8800;
text-decoration: none;
border-bottom: 3px solid #1290FF;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #ff8800;
text-decoration: none;
color: #121214;
transition: background 0.3s ease-in;
}
.active {
background-color: #ff8800;
color: #121214;
text-decoration: none;
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

Add a default border bottom to all of them and make it the same color as the background.
a {
color: #ff8800;
border-bottom: 3px solid #333;
}
a:hover {
color: #ff8800;
text-decoration: none;
border-bottom-color: #1290FF;
}
Updated demo
UPDATE – Don't forget to change the bottom border color on the active element
.active {
border-bottom-color: #ff8800;
}
Or you make the default border color transparent (probably the better solution)
a {
color: #ff8800;
border-bottom: 3px solid rgba(0,0,0,0);
}

The right answer depends on the UX you want to achieve. The easiest way would be to move the border to the <a> element (or to move the hover to the <li> element). And use a 3px border in all cases-- black for non-hovered, blue for hovered.
Like this: https://jsfiddle.net/sh5a8v6y/
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
z-index: 10;
text-transform: uppercase;
}
li {
float: left;
border-right: 1px solid #121214;
text-decoration: none;
}
a {
color: #ff8800;
border-bottom: 3px solid #121214;
}
a:hover {
color: #ff8800;
text-decoration: none;
border-bottom: 3px solid #1290FF;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #ff8800;
text-decoration: none;
color: #121214;
transition: background 0.3s ease-in;
}
.active {
background-color: #ff8800;
color: #121214;
text-decoration: none;
}

a:hover {
color: #ff8800;
text-decoration: none;
border-bottom: 3px solid #1290FF;
padding-bottom:11px;
}
Fiddle

One simple approach is to change the ul properties as follows:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: visible;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
border-bottom: 1px solid #121214;
z-index: 10;
text-transform: uppercase;
height: 48px;
}

Related

How to style each <a> link with a different color on hover?

I'm styling a nav bar <a> tags and I want each <a> tag to have a different color-coded solid border-bottom in line with a logo.
I only want the color border to show on hover. At the moment this is how I have it -
header {
height: 50px;
}
nav {
text-align: center;
padding: 10px;
margin-top: 20px;
}
nav a {
color: #000000;
text-decoration: none;
display: inline-block;
padding: 5px;
}
nav a:link {
color: #000000;
text-decoration-line: none;
}
<header>
<nav>
Home
What we do
Who we are
Who we work with
Say hello
Blog
</nav>
</header>
Because each <a> tag is a different color I thought this would be the best way to do it but how do I place the rule for it only applying on a:hover ?
You need to move those settings to your stylesheet and use more specific selectors: I used :nth-child(x) below:
header {
height: 50px;
}
nav {
text-align: center;
padding: 10px;
margin-top: 20px;
}
nav a {
color: #000000;
text-decoration: none;
display: inline-block;
padding: 5px;
}
nav a:link {
color: #000000;
text-decoration-line: none;
}
nav a:hover {
border-bottom: 2px solid rgb(255,29,142);
}
nav a:nth-child(2):hover {
border-bottom: 2px solid rgb(133,52,146);
}
nav a:nth-child(3):hover {
border-bottom: 2px solid rgb(255,128,55);
}
nav a:nth-child(4):hover {
border-bottom: 2px solid rgb(0,182,223);
}
nav a:nth-child(5):hover {
border-bottom: 2px solid rgb(63,190,150);
}
nav a:nth-child(6):hover {
border-bottom: 2px solid rgb(255,222,32);
}
<header>
<nav>
Home
What we do
Who we are
Who we work with
Say hello
Blog
</nav>
</header>
You can maintain the effect you want without using inline CSS.
Using the nth-of-type selector, you can target the exact anchor tag that you want within your navigation.
Apply the color to the border on :hover
header {
height: 50px;
}
nav {
text-align: center;
padding: 10px;
margin-top: 20px;
}
nav a {
color: #000000;
text-decoration: none;
display: inline-block;
padding: 5px;
border-bottom: 2px solid transparent;
}
nav a:link {
color: #000000;
text-decoration-line: none;
}
nav a:nth-of-type(1):hover {
border-color: rgb(255, 29, 142);
}
nav a:nth-of-type(2):hover {
border-color: rgb(133, 52, 146);
}
nav a:nth-of-type(3):hover {
border-color: rgb(255, 128, 55);
}
nav a:nth-of-type(4):hover {
border-color: rgb(0, 182, 223);
}
nav a:nth-of-type(5):hover {
border-color: rgb(63, 190, 150);
}
nav a:nth-of-type(6):hover {
border-color: rgb(255, 222, 32);
}
<header>
<nav>
Home
What we do
Who we are
Who we work with
Say hello
Blog
</nav>
</header>
If you have to use inline style then this solution may be help you. Please have a look at the snippet.
header {
height: 50px;
}
nav {
text-align: center;
padding: 10px;
margin-top: 20px;
}
nav a {
position: relative;
color: #000000;
text-decoration: none;
display: inline-block;
padding: 5px;
border-bottom: 2px solid transparent;
}
nav a:after {
content: '';
position: absolute;
width: 100%;
top: 100%;
left: 0;
border-bottom: 2px solid #fff; /* same as background color */
}
nav a:hover:after {
border-color: transparent;
}
nav a:link {
color: #000000;
text-decoration-line: none;
}
<header>
<nav>
Home
What we do
Who we are
Who we work with
Say hello
Blog
</nav>
</header>
you can do it using nth-of-type selector, otherwise i you really need to define it inline, you can use below code :
<a href="http://google.com"
onMouseOver="this.style.color='#0F0'"
onMouseOut="this.style.color='#00F'">Google</a>

Spacing between navigation links

My problem is that I want to have spacing between the navigation links. I have searched over the internet but all I get are reducing the space.
To be specific, I want to have spacing in between each navigation link that are bordered with a black border.
These are my codes for the navigation bar. I would really appreciate some help. thank you.
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
background-color: #fff;
border: 5px solid #000;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li {
text-align: center;
border-bottom: 5px solid #000;
}
li:last-child {
border-bottom: none;
}
li a:hover {
background-color: #555;
color: white;
}
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
Here it is:
<style>
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
border: 5px solid #000;
background-color: #fff;
}
li {
text-align: center;
margin-bottom:10px;
}
li:last-child {
margin-bottom:0;
}
li a:hover {
background-color: #555;
color: white;
}
</style>
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
Demo: https://jsfiddle.net/4fLbx4xa/
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
<style>
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
background-color: #fff;
}
li{
width:100%;
display: block;
margin-top:10px; //this is the height you want
border: 5px solid #000;
color: #000;
background:#000;
padding:10px 0;
}
li a {
text-align: center;
padding: 8px 16px;
text-decoration: none;
color:#fff;
}
li:first-child {
margin-top:0;
}
li:hover {
background-color: #555;
color: white;
}
</style>
You need to use margin to add the white space, but the borders needed a little tweaking, see comments below
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
background-color: #fff;
/*border: 5px solid #000; moved to LI element*/
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
margin-bottom:10px; /*add some margin to create the white space*/
border: 5px solid #000; /*add the border to each LI element rather than UL*/
}
li {
text-align: center;
/*border-bottom: 5px solid #000; remove this bottom border as handled in LI css*/
}
Not sure what you want to achieve but i understood a line between the links, this might help you also if you want it below the links..
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 400px;
background-color: #fff;
border: 5px solid #000;}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;}
li {
text-align: center;
float: left;
border-right:solid 1px black;
}
li a:hover {
background-color: #555;
color: white;
}
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

Border-Top & Text issue

I have a simple navigation bar with an hover element.
.navip {
float: left;
text-decoration: none;
font-weight: lighter;
}
.navip > a {
display: block;
color: black;
text-decoration: none;
font-size: 20px;
font-weight: lighter;
line-height: 40px;
}
.navip > a:hover {
border-top: 3px solid blue;
}
When i hover on a, the border displays. But its taking down the text a little bit.
How i can fix this?
JsFiddle: http://jsfiddle.net/w0btkceg/
Edit: Solved it! I had to increase the height to 45 and add "border-top: 3px solid transparent" to the "navip a" class.
try to use
.navip > a:hover {
border-top: 3px solid blue;
margin-top: -3px;
}
I use this for menu:
.menu-item {
margin: 5px;
font-size: 1em;
font-weight: bold;
float: left;
border-top-style: solid;
border-top-width: 4px;
border-top-color: transparent;
}
.menu-item a {
padding: 5px 0;
text-decoration: none;
}
.menu-item-selected {
border-top-color: green;
}
.menu-item:hover {
border-top-color: green;
}
<div class="menu-item">
Test 1
</div>
<div class="menu-item menu-item-selected">
Test 2
</div>
<div class="menu-item">
Test 3
</div>
<div class="menu-item">
Test 4
</div>
Just do something like:
.navip > a {
display: block;
color: black;
text-decoration: none;
font-size: 20px;
font-weight: lighter;
line-height: 40px;
border-top: 3px solid transparent;} /* add a transparent border to the a */
.navip > a:hover {
border-top: 3px solid blue; /* Now just change the color value of the border from transparent to a color on over */
}
See working example here
Solution: Add a transparent border to the a , then add a color to it on hover. That way, the text won't move because the border existed before hover.
You could try box-sizing:border-box; this will include the border in the size of the box and is probably the most elegant solution.
Or my previous fix for this was just to have 10 padding for the normal link then padding:8px; for the hover version which had a border. Which would counter the border size.
Ok, you can declare a border-top property for the .navip > a{}, with 0 opacity on it, and then on hover, use your border color, like this one(check here)
.navip > a {
display: block;
color: black;
text-decoration: none;
font-size: 20px;
font-weight: lighter;
line-height: 37px; /*readjust for the border stuff*/
border-top: 3px solid rgba(255,255,255,.0);/*or transparent*/
}
.navip > a:hover {
border-top: 3px solid blue;
}
OR you can use a negative margin-top property on hover, or top property if you can make the .navip > a to position relative, check it here:
.navip > a {
position: relative;
display: block;
color: black;
text-decoration: none;
font-size: 20px;
font-weight: lighter;
line-height: 40px;
}
.navip > a:hover {
margin-top: -3px;/*or top: -3px*/
border-top: 3px solid blue;
}
You could use a box-shadow instead of a border-top, like this:
.navip > a:hover {
box-shadow: inset 0 3px 0 0 blue;
}
More info here: MDN Box Shadow

CSS3 Transitions like Twitter header

You know how Twitter's menu does a slide in from the BOTTOM of their bottom-border? I'm trying to do the same with transitions on css3 and my border-bottom slides in from the top to bottom AT the bottom when I want it go slide from the bottom to the top AT the bottom like Twitter's menu.
Here's my fiddle: http://jsfiddle.net/8emkgzyb/
ul {
padding: 0;
margin: 0;
}
ul li {
list-style: none;
padding: 0px;
display:inline;
}
ul li a {
text-decoration: none;
font-size: 25px;
padding: 4px;
display:inline;
color: #000;
transition: all .3s;
line-height: 20px;
border-bottom-style: solid;
border-bottom-color: red;
border-bottom-width: 0px;
}
ul li a:hover {
transition: all .3s;
border-bottom-style: solid;
border-bottom-color: red;
border-bottom-width: 4px;
}
html, body {
margin: 0;
padding: 0;
}
and HTML
<ul>
<li>Home</li>
<li>About</li>
<li>Locations</li>
</ul>
something like that?
I don't know if I understood it correct.
JSFIDDLE
ul li a {
height: 24px;
text-decoration: none;
font-size: 25px;
padding: 4px;
color: #000;
transition: all .3s;
line-height: 20px;
border: 0px solid red;
display: block;
}
ul li a:hover {
transition: all .3s;
height: 20px;
border-bottom: 4px solid red;
}

Border-bottom needs to move up on hover

I want to have a border (looks like underline) that moves up on hover.
So if this is a link:
LINK
Then if I hover on it
LINK
""""
Example from the website:
http://matthias-schoenaerts.com/
(navigation bar)
I want it as simple as possible.
This is what I came up with:
http://jsfiddle.net/Lxxqz3pL/
HTML:
<ul id="nav">
<li>About Us</li>
<li>Our Products</li>
<li>FAQs</li>
<li>Contact</li>
<li>Login</li>
</ul>
CSS:
/* Begin Navigation Bar Styling */
#nav {
width: 100%;
float: center;
margin: 0 0 3em 0;
left: 0;
padding: 0;
list-style: none;
background-color: #333333;
border-bottom: 1px solid #ccc;
border-top: 1px solid #ccc;
position: absolute;
}
#nav li {
float: left;
}
#nav li a {
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #a3a3a3;
}
#nav li a:hover {
transition: border .5s ease-in;
background-color: #fff;
border-bottom: 3px solid red;
}
/* End navigation bar styling. */
Here is updated CSS, does it what you trying to get?
/* Begin Navigation Bar Styling */
#nav {
width: 100%;
float: center;
margin: 0 0 3em 0;
left: 0;
padding: 0;
list-style: none;
background-color: #333333;
border-bottom: 1px solid #ccc;
border-top: 1px solid #ccc;
position: absolute;
overflow: hidden;
}
#nav li {
float: left;
}
#nav li a {
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #a3a3a3;
}
#nav li a:after{
display:block;
width:100%;
height:0px;
content:" ";
border:1px solid red;
position: relative;
top:10px;
}
#nav li a:hover:after{
transition: 0.5s ease-in;
transform: translate(0px, -10px);
}
/* End navigation bar styling. */
I've modified your code in areas to get the desired effect
DEMO http://jsfiddle.net/Lxxqz3pL/3/
#nav li a {
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #a3a3a3;
padding: 22px 0 35px;
color: #a3a3a3;
border-bottom: 3px solid #6a901b;
transition: all 0.5s ease;
}
#nav li a:hover {
transition: all 0.5s ease;
color: #fff;
padding-bottom: 5px;
}
How about something like this? FIDDLE.
Just keep the background fixed, add a border at the bottom, and make the height of the anchor smaller.
Relevant CSS
#nav li {
float: left;
height: 40px;
}
#nav li a {
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #a3a3a3;
height: 20px;
transition: height 0.5s;
}
#nav li a:hover {
height: 10px;
border-bottom: 3px solid red;
}
It looks like the example site uses flexNav, a jQuery plugin.
http://jasonweaver.name/lab/flexiblenavigation/
Here's a quick-fix solution. I added a transition to <li> padding to compensate for the added border.
http://jsfiddle.net/Lxxqz3pL/1/