Reputation: 519
In the following code, I have set bottom border the ul
. On the hover over the items in the ul
, I want to show the bottom border or the hover item exactly on the ul border. I'm unable to set the position of both borders (ul
and the ul
items) so that they go on each other on the hover.
I've tried using absolute position and the line height but could not get much success.
This is how I'd like:
Here's the code:
HTML:
<div class="box">
<ul>
<li><a href="#">test 1</a></li>
<li><a href="#">test 2</a></li>
</ul>
</div>
CSS:
ul{
list-style: none;
padding: 0 0 10px;
margin: 0;
border-bottom: 5px solid green;
overflow: hidden;
}
ul li{
float: left;
margin-left: 20px;
}
a{
text-decoration: none;
display: block;
}
a:hover{
border-bottom: 5px solid red;
}
Demo: http://jsfiddle.net/nNvfy/
Upvotes: 7
Views: 1822
Reputation: 20834
A little late to the party, but here is a solution without negative values and using positions
. Basically I used only background colors
:
.box{
background:green;
padding-bottom:5px;
}
ul{
list-style:none;
margin:0;
padding:0 0 0 10px;
background:white;
}
ul li{
display:inline-block;
vertical-align:top;
margin-left: 20px;
}
ul li a{
background:white;
}
li a:hover{
border-bottom:5px solid red;
}
Upvotes: 3
Reputation: 1555
Add the 5px
transparent
border
and make it red
on hover
. Try this code
<div class="box">
<ul class="clearfix">
<li><a href="#">test 1</a></li>
<li><a href="#">test 2</a></li>
</ul>
</div>
.clearfix:before,
.clearfix:after{
display: table;
content: "";
line-height: 0;
}
.clearfix:after{
clear: both;
}
ul{
list-style: none;
padding: 0;
margin: 0;
border-bottom: 5px solid green;
}
ul li{
float: left;
margin-left: 20px;
padding: 0 0 10px;
border-bottom: 5px solid transparent;
margin-bottom: -5px;
}
a{
text-decoration: none;
display: block;
}
li:hover{
border-color: red;
}
Check this DEMO
Upvotes: 1
Reputation: 71140
You want to position both your ul
and li
- offset the bottom of the child li
by the width of your border, whilst setting it to transparent
then use the li:hover
event/selector to set the broder color to red.
Use the below CSS:
ul {
list-style: none;
margin: 0;
border-bottom: 5px solid green;
position:relative;
padding:0;
}
ul li {
padding:10px 10px;
margin-left: 20px;
display:inline-block;
position:relative;
bottom:-5px; /* <-- offset bottom of li by border width so it overlaps ul green border */
border-bottom: 5px solid transparent; /* add border, make transparent so it doesnt 'jump' on hover */
}
a {
text-decoration: none;
display: block;
}
li:hover {
border-color:red; /* <-- use the li hover event (not a hover) to color the border */
}
Upvotes: 11
Reputation: 4319
I have a workaround for your solution. Just don't set the border to the ul
, but insert an div
-element with a border. So you don't depend on the list.
HTML:
<div class="box">
<ul>
<li><a href="#">test 1</a></li>
<li><a href="#">test 2</a></li>
</ul>
<div id="border"></div>
</div>
CSS:
.box {
width: 500px
}
#border {
width: 100%
position: absoulte;
margin-top: -15px;
border-bottom: 5px solid green;
}
ul{
list-style: none;
padding: 0 0 10px;
margin: 0;
overflow: hidden;
}
ul li{
float: left;
margin-left: 20px;
border-bottom: 5px solid green;
}
ul li:hover {
border-bottom: 5px solid red;
}
a{
text-decoration: none;
display: block;
}
Upvotes: 0
Reputation: 198
here you go, working version: http://jsfiddle.net/hKVp6/
ul{
width: 100%;
float: left;
list-style: none;
margin: 0;
border-bottom: 5px solid green;
}
li{
float: left;
margin-left: 20px;
padding: 0 0 10px;
}
li:hover {
border-bottom: 5px solid red;
margin-bottom: -5px; /* same as bottom border */
}
li a{
text-decoration: none;
display: block;
}
Upvotes: 4
Reputation: 7207
you can try like this: DEMO
CSS:
ul {
list-style: none;
padding: 0 0 0px;
margin: 0px;
border-bottom: 5px solid green;
position:absolute;
width:100%;
}
ul li {
float: left;
margin-left: 20px;
border-bottom: 5px solid transparent;
margin-bottom:-5px;
z-index:999;
}
a {
text-decoration: none;
display: block;
}
ul li:hover {
z-index:9999;
border-bottom: 5px solid red;
}
Upvotes: 1