Pregunta

You know how to do this using CSS?

enter image description here

In my navbar I would like to see a transparent triangle to the active link. If I create a PNG image with a transparent triangle and use it like this:

background: rgba (0,0,0,0.4) url (triangle.png) no-repeat bottom center;

this does not work properly because under my triangle shows the transparent rgba color rgba(0,0,0,0.4) ...

I would like to do this to make a nice effect when scrolling the page. It is possibile?

¿Fue útil?

Solución

Demo You can use the :before and :after pseudo elements to achieve this effect.

<nav>
    <ul>
        <li class="active">homepage</li>
        <li>option2</li>
        <li>option3</li>
    </ul>
</nav>

nav {
    position: fixed;
    background-color: rgba(0,0,0,.7);
    display: block;
    width: 100%;
    padding: 10px 0;
    color: white;
    font-size: 1.5em;
}

ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

ul li {
    float: left;
    width: auto;
    padding: 0 20px;
    position: relative;
}

nav li:before,
nav li:after {
    content: '';
    position: absolute;
    bottom: -35px;
    left: 0;
    right: 0;
    height: 5px;
    border: 10px transparent solid;
    border-top-color: rgba(0,0,0,.7);
    border-left-width: 0;
    border-right-width: 0;
}
nav li:before {
    right: 50%;
}
nav li:after {
    left: 50%;
}

nav li.active:before {
    border-right-width: 10px;
}

nav li.active:after {
    border-left-width: 10px;
}

nav li:last-child:after { /* covers the bottom of the navigation bar all the way to the right */
    right: -9999999px;
}

Another solution using links:

<nav>
    <ul>
        <li><a href="#" class="active">homepage</a></li>
        <li><a href="#">option2</a></li>
        <li><a href="#">option3</a></li>
    </ul>
</nav>

Otros consejos

write css style for :active class

js. No jQuery and you even get the hover effect for free.

i think this will help you..

same concept but used differently for further reference refer here : stackoverflow.com/questions/17327076/how-to-create-a-ul-with-a-triangle-for-the-active-row

Will post my solution. It's pretty complicated and though I don't know if there is other simpler way to make li>a nested elements be transparent for the background under ul. This solution uses :before/:after pseudo attributes.

I used this markup (how to avoid helper <i></i>?):

<header>
    <ul class="clearfix">
        <li><a class="active" href="">HOMEPAGE <i></i></a></li>
        <li><a href="">CONTACT <i></i></a></li>
        <li><a href="">GET OUT <i></i></a></li>
    </ul>
</header>

and CSS:

header li a {
    text-align: center;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    display: inline-block;
    padding: 25px;
    color: #FFF;
    position: relative;
}
header li a:hover {
    background: rgba(255, 255, 255, .2);
}
header li a i:after, header li a i:before {
    content:'';
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 0;
    height: 0;
    display: none;
    background: url(http://subtlepatterns.com/patterns/escheresque_ste.png);
    background-attachment: fixed;
    border-top: 15px solid rgba(0, 0, 0, .5);
}
header li a.active i:after, header li a.active i:before {
    display: block;
}
header li a:hover i:after, header li a:hover i:before {
    display: block;
    border-top-color: rgba(255, 255, 255, .1);
}
header li a i:before {
    margin-left: -15px;
    border-right: 15px solid transparent;
}
header li a i:after {
    border-left: 15px solid transparent;
}

Hopefully someone will get inspired one day.

Demo: http://jsfiddle.net/R9pKq/

<figure>
    <div><div>
</figure>

css

figure{
    width:400px;
    height:320px;
    background:skyblue url(http://img1.wikia.nocookie.net/__cb20140301204257/disney/images/4/49/Elsa-Anna.jpg);
    border:4px solid rgba(0,0,0,.8);
    margin:40px auto;
    position:relative;
}
figure div{
    position:absolute;
    bottom:0px;
    left:0px;
    width:100%;
    height:200px;
    background:rgba(255,255,255,.1);
}

figure div:before{
    content:'';
    position:absolute;
    width:0px;
    height:0px;
    left:50%;
    top:-40px;
    margin-left:-40px;
    border-bottom:40px solid rgba(255,255,255,.1);
    border-left:40px solid transparent;
    border-right:40px solid transparent;
}

Demo

or if you want to apply it to a menu

<menu>
    <li><a>Home</a></li>
    <li><a>Work</a></li> 
    <li><a>Projects</a></li> 
    <li><a>Infos</a></li> 
</menu>

css

menu{
    margin-top:40px;
}
menu li{
    float:left;
    list-style:none;
    position:relative;
}
menu li{
    padding:20px; 40px;
}
menu li:hover:before{
    content:'';
    position:absolute;
    width:0px;
    height:0px;
    left:50%;
    top:-20px;
    margin-left:-20px;
    border-bottom:20px solid rgba(0,0,0,.8);
    border-left:20px solid transparent;
    border-right:20px solid transparent;
}

Demo with Hover

to use the active class jst change menu li:hover:before to menu li.active:before

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top