Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS sliding border

Tags:

html

jquery

css

Thanks to codeSpy, I have this: http://jsfiddle.net/p9tBR/

What I can't figure out is how to change the blue line as I change pages. For example, if I'm on page 2, I want the blue line to be under the 2 instead of the 1. When I'm on page 2-4, the line goes back to the 1. Sorry I'm awful at explaining this so here's a picture.

1

HTML:

<header>
    <ul>
    <li><a href="1.html" id="current">1</a></li>
    <li><a href="2.html">2</a></li>
    <li><a href="3.html">3</a></li>
    <li><a href="4.html">4</a></li>
    <span></span>
</ul>
</header>

CSS:

body {
font-family: sans-serif;
}

ul {
padding: 0;
position: absolute;
left: 50%;
width: 500px;
margin: 0 0 0 -250px;
list-style-type: none;
}

ul:hover > span {
background: #d0332b;
}

ul { margin-top: 50px;}

ul li {
font-weight: bold;
width: 25%;
float: left;
padding: 7px 0;
text-align: center;
cursor: pointer;
}

ul li:hover {
color: #d0332b;
}

ul li:nth-child(2):hover ~ span {
left: 25%;
}

ul li:nth-child(3):hover ~ span {
left: 50%;
}

ul li:nth-child(4):hover ~ span {
left: 75%;
}

span {
position: absolute;
bottom: -42px;
display: block;
width: 25%;
height: 7px;
background: #00b6ff;
}

ul li, span {
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
transition: all 0.2s ease;
position: relative;
}

a {
text-decoration: none;
color: #232323;
}

a:hover {
display: block;
color: #d0332b;
}
like image 504
user2970932 Avatar asked Nov 09 '13 23:11

user2970932


People also ask

How do you slide border in CSS?

Try using a combination of "overflow: hidden" on the parent and "transform: translateX(-100%)" on the child. This allows you to slide it in without altering the box model which results in a higher FPS animation.

Can border be animated CSS?

CSS border animation is useful for giving a border image or container element a unique style. Some designers view border animation as little more than a finishing touch to a web design. But, used well, CSS border animation can help to: Positively impact user engagement.

How do you animate border bottom in CSS?

To expand the bottom border on hover, you can use transform:scaleX'(); (mdn reference) and transition it from 0 to 1 on the hover state. The border and transition are set on a pseudo element to prevent transitioning the text and avoid adding markup.


1 Answers

Interesting CSS, not seen that done before.

If you add a hover state for the first link too:

ul li:nth-child(1):hover ~ span {
    left: 0%;
}

and add an "active" class for the current tab, then it works quite nicely. The "inactive" class names are required so that the .active style doesn't override the :hover styles.

<header>
    <ul>
        <li class="inactive"><a href="1.html" id="current">1</a></li>
        <li class="active"><a href="2.html">2</a></li>
        <li class="inactive"><a href="3.html">3</a></li>
        <li class="inactive"><a href="4.html">4</a></li>
        <span></span>
    </ul>
</header>


ul li.active:nth-child(1) ~ span,
ul li.inactive:nth-child(1):hover ~ span {
    left: 0%;
}

ul li.active:nth-child(2) ~ span,
ul li.inactive:nth-child(2):hover ~ span {
    left: 25%;
}

ul li.active:nth-child(3) ~ span,
ul li.inactive:nth-child(3):hover ~ span {
    left: 50%;
}

ul li.active:nth-child(4) ~ span,
ul li.inactive:nth-child(4):hover ~ span {
    left: 75%;
}

http://jsfiddle.net/p9tBR/4/

like image 193
Douglas Avatar answered Oct 30 '22 13:10

Douglas