samedi 18 avril 2015

Navigation Drop-down menu is inheriting the width of its parent and cropping its children if they were long

I have created my main navigation using CSS display: table as it shown below:


The with of the dropdown menus are not exceeding the width of the parent, which means if there was a long text inside the drop-down menu, it will be cropped as the image below demonstrate:


enter image description here


How can I make my dropdown menus adapt to the width of their children and not to the width of its parent just like shown in the image below:


enter image description here


So the drop-down menus will stretch to fit the width of there parent and if any of their children is longer than that width, their width should increase to adapt.


HTML:



<nav class="mainMenu">
<ul>
<li> <a <?php href( 'index.php');?>>Index</a>
</li>
<li> <a <?php href( 'modules.php');?>>Modules</a>
</li>
<li class="subMenu"> <a <?php href( 'page.php');?>>Dropdown</a>

<ul>
<li> <a <?php href( '');?>>Short Text</a>
</li>
<li> <a <?php href( '');?>>Dropdown Longer Text</a>
</li>
</ul>
</li>
<li class="subMenu"> <a <?php href( 'page.php');?>>Dropdown</a>

<ul>
<li> <a <?php href( '');?>>Sub menu item which is longer than its parent</a>
</li>
<li> <a <?php href( '');?>>Short Text</a>
</li>
</ul>
</li>
</ul>
</nav>


CSS:



/*Header*/
#header {
background-color: #fff;
width: 100%;
display: inline-block;
position: relative;
z-index: 10000;
}
/*Header > Content Container*/
#header .content {
width: 1024px;
padding: 0 20px;
margin: 0 auto;
position: relative;
}
/*Header > Main Menu*/
#header .mainMenu {
display: table;
float: right;
}
#header .mainMenu ul {
display: table-row;
position: relative;
}
#header .mainMenu ul li {
display: table-cell;
position: relative;
}
/*Header > Main Menu > Menu Links*/
#header .mainMenu ul li a {
padding: 10px 15px;
}
#header .mainMenu ul li a:hover {
color: #2B508F;
}
#header .mainMenu ul li a.active, #header .mainMenu ul li a.active:hover {
color: #fff;
background-color: #2B508F;
cursor: default !important;
}
/*Header > Main Menu > Dropdown Menus*/
#header .mainMenu ul ul {
background-color: #fff;
padding: 1px;
min-width: 100%;
position: absolute;
top: 100%;
left: 0;
text-align: left;
overflow: hidden;
z-index: 10000;
opacity: 0;
visibility: hidden;
pointer-events: none;
-webkit-transition: all .1s linear;
-moz-transition: all .1s linear;
-ms-transition: all .1s linear;
-o-transition: all .1s linear;
transition: all .1s linear;
}
#header .mainMenu ul li:hover > ul {
pointer-events: auto;
visibility: visible;
opacity: 1;
}
#header .mainMenu ul ul li {
width: 100%;
display: block;
}
/*Header > Main Menu > Dropdown Menu Links*/
#header .mainMenu ul ul li a {
display: block;
}
#header .mainMenu ul ul li a:hover {
}
#header .mainMenu ul ul li a.active, #header .mainMenu ul ul li a.active:hover {
}
/*Header > Main Menu > Dropdown Menu Parent Link*/
#header .mainMenu .subMenu:hover > a {
color: #2B508F;
}
#header .mainMenu .subMenu:hover > a.active {
color: #2B508F;
}


JSFiddle: http://ift.tt/1HjLuJf


Aucun commentaire:

Enregistrer un commentaire