Deepak Deepak - 2 days ago 3
CSS Question

Manage sub links in sidebar

I have a left sidebar which includes links and sub links. It looks as follows -

enter image description here

when I hover mouse on links sub link display inside of the sidebar(In image I have hovered mouse of posts links and its sub links are displaying inside the sidebar).

Now my question is or what I want is sub links should be displayed outside of sidebar(at right side of sidebar).

Here is what I tried so far



.sidenav {
height: 90%;
width: 50%;
position: fixed;
z-index: 1;
top: 0;
left: 0;
/*background-color: #111;*/
background:#23282D;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
margin-top:62px;
}

.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 18px;
color: #818181;
display: block;
transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
color: #f1f1f1;
}

.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

#main {
transition: margin-left .5s;
padding: 16px;
margin-top:50px;
}

@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}

<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" id='sidebar_close'>&times;</a>
<ul style='list-style:none;'>
<li class='myclass'><a href="#">Dashboard</a></li>
<li class='post_list'> <a href="#">Posts</a>
<ul style='list-style:none;' class='sub_links'>
<li><a href='#'>All</a></li>
<li><a href='#'>Add</a></li>
<li><a href='#'>Category</a></li>
</ul>
</li>
<li class='post_list'>
<a href="#">Pages</a>
<ul style='list-style:none;' class='sub_links'>
<li><a href='#'>All</a></li>
<li><a href='#'>Add</a></li>
</ul>
</li>
<li><a href="#" class='media_upload'>Media</a></li>
<li class='post_list'> <a href="#">User</a>
<ul style='list-style:none;' class='sub_links'>
<li><a href='#'>All</a></li>
<li><a href='#'>Add</a></li>
<li><a href='#'>Profile</a></li>
</ul>
</li>
<li><a href="#">Setting</a></li>
<li><a href="#">Logout</a></li>
</ul>
</div>





Please Help me to manage sub links.

Thanks.

Answer

you need to use position to achieve this set li (.post_list) to position:relative; and set submenu (ul) to position:absolute; ,and you have to remove overflowx:hidden; from sidenav

this is the code that you should add:

.post_list{

      position: relative;
}

.post_list ul{

    list-style: none;
    position: absolute;
    left: 100%;
    background: #23282D;
    top: 0;
    display: none;
}

.post_list:hover ul{

  display: block;
}

jsfiddle

your code will become like this :

.sidenav {
    height: 90%;
    width: 50%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    /*background-color: #111;*/
    background:#23282D;
    /*overflow-x: hidden;*/
    transition: 0.5s;
    padding-top: 60px;
    margin-top:62px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 18px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
    margin-top:50px;
}

.post_list{
  
      position: relative;
}

.post_list ul{
  
    list-style: none;
    position: absolute;
    left: 100%;
    background: #23282D;
    top: 0;
    display: none;
}

.post_list:hover ul{
  
  display: block;
}



@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}  
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" id='sidebar_close'>&times;</a>
  <ul style='list-style:none;'>
  <li class='myclass'><a href="#">Dashboard</a></li>
 <li class='post_list'> <a href="#">Posts</a>
    <ul style='list-style:none;' class='sub_links'>
        <li><a href='#'>All</a></li>
        <li><a href='#'>Add</a></li>
        <li><a href='#'>Category</a></li>
    </ul>
 </li>
  <li class='post_list'>
  <a href="#">Pages</a>
<ul style='list-style:none;' class='sub_links'>
        <li><a href='#'>All</a></li>
        <li><a href='#'>Add</a></li>
    </ul>
  </li>
  <li><a href="#" class='media_upload'>Media</a></li>
<li class='post_list'> <a href="#">User</a>
    <ul style='list-style:none;' class='sub_links'>
        <li><a href='#'>All</a></li>
        <li><a href='#'>Add</a></li>
        <li><a href='#'>Profile</a></li>
    </ul>
 </li>
  <li><a href="#">Setting</a></li>
  <li><a href="#">Logout</a></li>
  </ul>
</div> 

Comments