How not to affect next elements of the same tag

1

I have a horizontal menu with multiple elements <li> . But passing the mouse on the last item, that is on the last element <li> - "Services", opens another list of items that also contains elements with the li tag. p>

The problem is that in this second list of items I do not want the styles from the previous list (first list) to be applied. How can I get around this?

Follow the HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<style>
.breadcrumb {
    padding: 0px;
    background: #D4D4D4;
    list-style: none; 
    overflow: hidden;
    margin-top: 20px;
}
.breadcrumb>li+li:before {
    padding: 0;
}
.breadcrumb li { 
    float: left; 
}
.breadcrumb li.active a {
    background: brown;                   
    background: #ffc107 ; 
}
.breadcrumb li.completed a {
    background: brown;                   
    background: hsla(153, 57%, 51%, 1); 
}
.breadcrumb li.active a:after {
    border-left: 30px solid #ffc107 ;
}
.breadcrumb li.completed a:after {
    border-left: 30px solid hsla(153, 57%, 51%, 1);
} 

.breadcrumb li a {
    color: white;
    text-decoration: none; 
    padding: 10px 0 10px 45px;
    position: relative; 
    display: block;
    float: left;
}
.breadcrumb li a:after { 
    content: " "; 
    display: block; 
    width: 0; 
    height: 0;
    border-top: 50px solid transparent; 
    border-bottom: 50px solid transparent;
    border-left: 30px solid hsla(0, 0%, 83%, 1);
    position: absolute;
    top: 50%;
    margin-top: -50px; 
    left: 100%;
    z-index: 2; 
} 
.breadcrumb li a:before { 
    content: " "; 
    display: block; 
    width: 0; 
    height: 0;
    border-top: 50px solid transparent;    
    border-bottom: 50px solid transparent;
    border-left: 30px solid white;
    position: absolute;
    top: 50%;
    margin-top: -50px; 
    margin-left: 1px;
    left: 100%;
    z-index: 1; 
} 
.breadcrumb li:first-child a {
    padding-left: 15px;
}
.breadcrumb li a:hover { 
    background: #ffc107  ; 
}
.breadcrumb li a:hover:after { 
    border-left-color: #ffc107   !important; 
}
</style>

<nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
        <div class="container">
            <div class="row">
                <ul class="breadcrumb">
                    <li class=""><a href="#">Inicio</a></li>
                    <li class=""><a href="#">Noticias</a></li>
                    <li class="dropdown mega-dropdown ">
                        <a href="#" class="dropdown-toggle  Negrito" data-toggle="dropdown">Serviços <span class="caret"></span></a>        
                        <div class="dropdown-menu mega-dropdown-menu">
                            <div class="widget-box">
                                <div class="widget-title">
                                    <ul class="nav nav-tabs nav-tabs-carousel">
                                        <li class="active"><a data-toggle="tab" href="#tab1">Serviço 1</a> </li>
                                        <li><a data-toggle="tab" href="#tab2">Serviço 2</a></li>
                                        <li><a data-toggle="tab" href="#tab3">Serviço 3</a></li>
                                        <li><a data-toggle="tab" href="#tab4">Serviço 4</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</nav>
    
asked by anonymous 26.07.2017 / 19:25

1 answer

1

As our friend @Bsalvo said, add concrete classes to the elements that want to add the desired styles:

.breadcrumb .bread-li { 
    /* estilos aqui */
}
.breadcrumb .bread-li.active .bread-link {
    /* estilos aqui */
}

Or you can use a child-selector as follows:

.breadcrumb > li {
    /* estilos aqui */
}
.breadcrumb > li.active > a {
    /* estilos aqui */
}

With a little magic from both worlds, you can do something like the code below:

  

Issue:

     

I've added the nav-tabs class since I see that the intention is to create a mega-menu with several tabs options.

<!-- BIBLITECAS JQUERY E BOOTSTRAP -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<!-- ESTILO NECESSITA SER IMPLEMENTADO DEPOIS DO CSS BOOTSTRAP -->
<style>
.breadcrumb {
    padding: 0px;
    background: #D4D4D4;
    list-style: none;
    margin-top: 20px;
    display: block;
    overflow:hidden;
}
.breadcrumb > li + li:before {
    padding: 0;
}
.breadcrumb .bread-li { 
    float: left; 
}
.breadcrumb .bread-li.active > a {
    background: brown;                   
    background: #ffc107 ; 
}
.breadcrumb .bread-li.completed > a {
    background: brown;                   
    background: hsla(153, 57%, 51%, 1); 
}
.breadcrumb .bread-li.active > a:after {
    border-left: 30px solid #ffc107 ;
}
.breadcrumb .bread-li.completed > a:after {
    border-left: 30px solid hsla(153, 57%, 51%, 1);
} 

.breadcrumb .bread-li > a {
    color: white;
    text-decoration: none; 
    padding: 10px 0 10px 45px;
    position: relative; 
    display: block;
    float: left;
}
.breadcrumb .bread-li > a:after { 
    content: " "; 
    display: block; 
    width: 0; 
    height: 0;
    border-top: 50px solid transparent; 
    border-bottom: 50px solid transparent;
    border-left: 30px solid hsla(0, 0%, 83%, 1);
    position: absolute;
    top: 50%;
    margin-top: -50px; 
    left: 100%;
    z-index: 2; 
} 
.breadcrumb .bread-li > a:before { 
    content: " "; 
    display: block; 
    width: 0; 
    height: 0;
    border-top: 50px solid transparent;    
    border-bottom: 50px solid transparent;
    border-left: 30px solid white;
    position: absolute;
    top: 50%;
    margin-top: -50px; 
    margin-left: 1px;
    left: 100%;
    z-index: 1; 
} 
.breadcrumb .bread-li:first-child > a {
    padding-left: 15px;
}
.breadcrumb .bread-li > a:hover { 
    background: #ffc107  ; 
}
.breadcrumb .bread-li > a:hover:after { 
    border-left-color: #ffc107   !important; 
}
.breadcrumb > li.dropdown {
    position: static;
}
</style>

<!-- O CÓDIGO -->
<nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
        <div class="container">
            <div class="row">
                <ul class="breadcrumb">
                    <li class="bread-li"><a href="#">Inicio</a></li>
                    <li class="bread-li"><a href="#">Noticias</a></li>
                    <li class="bread-li dropdown mega-dropdown ">
                        <a href="#" class="caret-link dropdown-toggle Negrito" data-toggle="dropdown">Serviços <span class="caret"></span>
                        </a>        
                        <div class="dropdown-menu mega-dropdown-menu">
                            <div class="widget-box">
                                <div class="widget-title">
                                    <ul class="nav nav-tabs nav-tabs-carousel">
                                        <li class="active"><a data-toggle="tab" href="#tab1">Serviço 1</a></li>
                                        <li><a data-toggle="tab" href="#tab2">Serviço 2</a></li>
                                        <li><a data-toggle="tab" href="#tab3">Serviço 3</a></li>
                                        <li><a data-toggle="tab" href="#tab4">Serviço 4</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</nav>

Example in jsFiddle using just child-selectors : link

However, it is not always advisable to use the second option, because when it comes to large projects, nesting elements in CSS in this way can be a headache.

  

NOTE: Some tags like links " <a> " are not properly closed HTML5 does not care about this and automatically closes them, you have to take this into account as this is not a good practice, you may have validation issues in previous versions of HTML and may affect your work when you are working on your platform.

    
26.07.2017 / 21:04