CSS Jquery vertical navigation menu with horizonta

2019-07-05 11:28发布

I'd like to create a navigation menu like this:

|main-item1|
|main-item2|    |sub-item1| |sub-item2| |sub-item3|
|main-item3|
|main-item4|

what I see now is this:

|main-item1|
|main-item2|    
     |sub-item1| |sub-item2| |sub-item3|  |main-item3|  |main-item4|

I've found another question like this here on stackoverflow, but I couldn't manage to adapt the code.

The html code I have is this:

        <div>
        <nav>
        <ul id="mainmenu">
                <li><a href="chi_siamo">Chi siamo</a></li>
                <li><a href="servizi">Servizi</a>
                <ul class="submenu">
                    <li>
                        <a href="speciale">speciale</a>
                    </li>
                    <li>
                        <a href="#">privati</a>
                    </li>
                    <li>
                        <a href="">aziende</a>
                    </li>
                    <li>
                        <a href="">cerimonie</a>
                    </li>
                    <li>
                        <a href="">consulenza</a>
                    </li>
                </ul>
                </li>
                <li><a href="location">Location</a></li>
                <li><a href="contatti">contatti</a></li>
                <li class="last"><a href="partner">partner</a></li>
                </ul>
                </nav>
    </div>

And this is the css:

    #mainmenu {
  position: fixed;
  left: 20px;
  top: 50%;
  z-index: 999999;
  margin-top:-200px;
}

  #mainmenu li {
    height: 40px;
    margin: 5px;
    position: relative;
  }

  #mainmenu a {
    background: none repeat scroll 0 0 #333;
    color: #fff;
    display: block;
    font-family: Folio;
    font-size: 30px;
    padding: 2px 15px;
    text-decoration: none;
    text-transform: uppercase;
    width: 160px;
    height: 40px;
    /*background: url(Images/dotnav.png) 0 100% no-repeat;*/
 /*text-indent: -10000px;*/
    overflow: hidden;
  }


  #mainmenu a:hover,
  #mainmenu li.active a {
    background-position: 0 0;

  }
.submenu
{
    list-style-type: none; 
    position:relative;
    float:left;
}
.submenu li
{
    display: inline; 
    float:left; 
    position:relative
}

It would be ok to use some jquery plugin, also because I'd like to add some effect on hovering, but I thought it was better to align everything with css first.

Thanks

2条回答
何必那么认真
2楼-- · 2019-07-05 12:11

this is what you may want:

the css:

#mainmenu {
  position: fixed;
  left: 20px;
  top: 50%;
  z-index: 999999;
  margin-top:-200px;
}

  #mainmenu li {
    height: 40px;
    margin: 5px;
    position: relative;
  }

  #mainmenu a {
    background: none repeat scroll 0 0 #333;
    color: #fff;
    display: block;
    font-family: Folio;
    font-size: 30px;
    padding: 2px 15px;
    text-decoration: none;
    text-transform: uppercase;
    width: 160px;
    height: 40px;
    /*background: url(Images/dotnav.png) 0 100% no-repeat;*/
 /*text-indent: -10000px;*/
    overflow: hidden;
  }


  #mainmenu a:hover,
  #mainmenu li.active a {
    background-position: 0 0;

  }
.submenu
{
    list-style-type: none; 
    position:relative;
    float:left;

}
.submenu li
{
    display: inline; 
    float:left; 
    position:relative;

}
#mainmenu .submenu li{
    margin:0px; 
}

and the html

<div>
        <nav>
        <ul id="mainmenu">
                <li><a href="chi_siamo">Chi siamo</a></li>
                <li><a href="servizi" style="float:left">Servizi</a>
                <ul class="submenu">
                    <li>
                        <a href="speciale">speciale</a>
                    </li>
                    <li>
                        <a href="#">privati</a>
                    </li>
                    <li>
                        <a href="">aziende</a>
                    </li>
                    <li>
                        <a href="">cerimonie</a>
                    </li>
                    <li>
                        <a href="">consulenza</a>
                    </li>
                </ul>
                </li>

                <li style="clear:both;"><a href="location">Location</a></li>
                <li><a href="contatti">contatti</a></li>
                <li class="last"><a href="partner">partner</a></li>
                </ul>
                </nav>
    </div>

In html I added at "servizi" float left and in css i added :

#mainmenu .submenu li{
        margin:0px; 
    }

So they are now at the same level.

查看更多
做个烂人
3楼-- · 2019-07-05 12:29

try this:

#mainmenu>li {
height: 40px;
margin: 5px;
position: relative;
clear:both

}

and float to links:

#mainmenu a {

float:left;

background: none repeat scroll 0 0 #333;
color: #fff;
display: block;
font-family: Folio;
font-size: 30px;
padding: 2px 15px;
text-decoration: none;
text-transform: uppercase;
width: 160px;
height: 40px;
overflow: hidden;
}
查看更多
登录 后发表回答