CSS Child Menu LI Forcing Parent's Width to Increase - css

So I know almost exactly what I need to do, but I don't know how I need to do it.
I have a drop down menu that opens when you hover over it. The child is causing the width of the parent to increase when you hover over it and that is not what I want. I know I need to set the child to position absolutely... but when I do it the child no longer shows up and it appears the hover on the parent no longer functions. Can anyone help figure out where exactly the absolute position part needs to go in? You can see I have something that I commented out.
I'd like the child to be aligned at the bottom right of the "Action" drop down.
I also have a problem where if I set the padding / margin on the parent. It extends to child. I believe I can solve that either with the absolute positioning, or after that is solved.
Here is my HTML:
<br />
<div style="width: 90%; margin:auto; background-color:#CCC; height:36px;">
<div id="actions">
<ul>
<li class="action_arrow_down">Actions
<ul>
<li>Action</li>
<li>Another Action</li>
<li>Something else here</li>
<li class = "separated_action">Separated Link</li>
</ul>
</li>
</ul>
</div>
And my CSS:
html, body {
margin: 0;
padding: 0;
height: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
#actions {
float: right;
display: block;
background-color: #4d90fe;
color: #FFF;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
}
#actions ul{
padding: 0px;
margin: 0px;
display: block;
position: relative;
overflow: hidden;
}
#actions li ul {
display: none;
background-color: #ffffff;
border: #CCC 1px solid;
/*position:absolute;
top:100%;
left:0;*/
}
#actions li ul li {
display: block;
background-color: #ffffff;
color: #000000;
overflow: hidden;
}
#actions li ul li:hover, #actions li ul li a:hover {
background-color: #eeeeee;
}
#actions li ul li a {
color: #000000;
}
#actions li{
line-height: 36px;
list-style: none;
font-size: 14px;
text-indent: 15px;
}
#actions li:not(ul li){
background-color: #4d90fe;
}
#actions li:not(ul li):hover {
background-color: #0362fd;
}
#actions li:hover > ul {
display: block;
}
#actions li a {
text-decoration: none;
color: #ffffff;
display:block;
}
#actions li ul li {
display: block;
}
.separated_action {
border-top: #CCC 1px solid;
}
/*.action_arrow_down {
background-image: url('images/action_arrow_down.png');
background-repeat: no-repeat;
background-position: right center;
}*/
The code is probably very convoluted and could use some cleaning up. I'll remove redundant parts once I get the sub menu working how I want.
Here is a jsfiddle.

You already had it. That you "loose" your submenu when you position it absolute, is because you position the parent relatively.
Solution is to remove that line:
#actions ul{
padding: 0px;
margin: 0px;
display: block;
/*position: relative; <-- delete this line */
overflow: hidden;
}
#actions li ul {
display: none;
background-color: #ffffff;
border: #CCC 1px solid;
position:absolute;
margin-left: -80px;
}
Check your updated Fiddle.
Adjust the margin-left to shift the submenu left or right. By default the left side of the parent is the left side of the child. If you'd give the child a fix width, you could use a negative margin-left to align the right sides.
Check your 2nd updated Fiddle.

Related

Centering float:left thing vertically

I can't figure it out how to center float:left object vertically.
I imagine that I could set the position of this menu bar vertically (The height of Y-axis) I think that would be the answer
// html part
<div class="menu_simple">
<ul>
<li>One</li>
<li>Two</li>
</ul>
//css
.menu_simple ul {
float:left;
margin: 0;
padding: 0;
width:100px;
list-style-type: none;
box-shadow: 5px 8px 5px #888888;
}
.menu_simple ul li a {
border: 1px solid #0066cc;
text-decoration: none;
color: white;
padding: 10.5px 11px;
background-color: #3b3b3b;
display:block;
}
.menu_simple ul li a:visited {
color: white;
}
.menu_simple ul li a:hover, .menu_simple ul li .current {
color: white;
background-color: #5FD367;
}
Fiddle example
First you set position: absolute for the menu div, then set top to 50% and the transform option to -50%.
Source: https://css-tricks.com/centering-css-complete-guide/
Hope this helps
Use the CSS position property.
Set the page hold, in your case the <body> to position: relative; and the part you wish to move, in your case; .menu_simple to the following:
.menu_simple {
position: absolute;
top: 50%;
left: 0;
}
Flexbox works nicely for this type of thing:
http://codepen.io/simply-simpy/pen/rVwXyz
menu_simple {
display:flex;
justify-content: flex-start;
align-items: center;
height: 500px;
border: 1px solid red;
}

How do I customize dropdown menu in CSS?

http://jsfiddle.net/zcfqu/
Been playing around with this piece of code for a while and am confused a bit.
How do I:
Change the color of the each submenu?
Make the submenu the same width as the main button?
HTML
<ul id="menu">
<li>This is the button
<ul class="submenu">
<li>Button one
</li>
<li>Button two
</li>
<li>Button three
</li>
</ul>
</li>
</ul>
Remove all floats and position:absolute
Check this demo
I just removed all floats (which was causing funny jumping of li and really not needed) and position:absolute (which was causing menu to shift sideways)
Also, I didn't read through all your CSS to find which background property is overriding which one, but I just removed them all and added new ones at bottom.
#menu > li { background-color: red; }
#menu > li:hover { background-color: green; }
.submenu li { background-color: blue; }
.submenu li:hover { background-color: yellow; }
EDIT 1
Its a good idea to use CSS shorthands and reduce CSS size and also make it more readable. Also, remove all incorrect CSS and you can also write border-radius: 2px 2px 2px 2px as border-radius: 2px (and save 12 bytes :O)
EDIT 2
CSS shorthands - MDN
font shorthand - W3C
background shorthand - W3C (scroll to the very bottomo of the page)
Change the color of the each submenu
ul.submenu a:hover {
background-color: red !important;
}
This changes on hover. If you want it always the same color remove :hover
Make the submenu the same width as the main button
ul.submenu, ul.submenu>li {
width: 100%;
}
This way you don't need to apply a fixed width. The browser will calculate it using parents adapted width.
Demo
Here is the correct approach in tackling your issues
DEMO http://jsfiddle.net/kevinPHPkevin/zcfqu/37/
// be more specific when targeting
ul#menu ul.submenu li a:hover {
background-color: green;
}
// set width to match button size
ul.submenu, ul.submenu>li {
width: 100%;
}
// assign classes for different coloured buttons. You could do this with css3 and `nth child` but it would limit your browser support considerably.
ul#menu .submenu li.btn1 a {
background: red;
}
ul#menu .submenu li.btn2 a {
background: yellow;
}
ul#menu .submenu li.btn3 a {
background: blue;
}
Take a look to this, I changed the background, and the "hover" and the width. It is correct ? Fiddle
ul#menu, ul#menu ul.sub-menu and ul#menu, ul.submenu --> width: 200px;
ul#menu li a for the background
I've set each li as 150px width. This has fixed the issue.
http://jsfiddle.net/andaywells/zcfqu/34/
ul#menu ul.submenu li {width: 150px;}
You can try the css as below with no changes on the html elements. I have added some comments for your references. Only 3 changes made on the css.
/*Initialize*/
ul#menu, ul#menu ul.sub-menu {
font-family: Helvetica;
background-color: #57AD68;
color: #FFFFFF;
border-radius: 3px 3px 3px 3px;
font-size: 12px;
font-style: normal;
font-weight: 400;
height: 40px;
line-height: 39px;
padding: 0 20px;
position: relative;
text-align: center;
vertical-align: bottom;
border-radius: 3px 3px 3px 3px;
border-style: none none solid;
border-width: 0 0 1px;
cursor: pointer;
display: inline-block;
float: center;
list-style-type: none;
text-decoration: none;
}
ul#menu, ul.submenu{
margin: 0;
padding: 0;
list-style: none;
float: left;
width: 134px; /*Adjust the sub menu width*/
}
ul#menu li{
float: left;
}
/* hide the submenu */
li ul.submenu {
display: none;
}
/* Main Button */
ul#menu li a{
display: block;
text-decoration: none;
color: #ffffff;
padding: 0 20px;
background: ; /*Remove the color here to avoid overlapped*/
float:right;
border-radius: 2px 2px 2px 2px;
font-family: Helvetica;
}
ul.submenu a:hover {
background: red;
}
/* show the submenu */
ul#menu li:hover ul.submenu{
display: block;
position: absolute;
float:right;
background-color:green; /*Adjust the color of sub menu.*/
}
ul#menu li:hover li, ul#menu li:hover a {
float: none;
background: ;
}
ul#menu li:hover li a:hover {
opacity:0.9;
}

CSS DropDown Menu: Third-level list doesn't hide after leaving through second-level

I have the following markup for a CSS dropdown menu:
<ul>
<li><a>FieldOne LevelOne</a></li>
<li><a>FieldTwo LevelOne</a></li>
<li><a>FieldThree LevelOne</a>
<ul>
<li><a>FieldOne LevelTwo</a>
<ul>
<li><a>FieldOne LevelThree</a></li>
<li><a>FieldTwo LevelThree</a></li>
</ul>
</li>
<li><a>FieldTwo LevelTwo</a>
<ul>
<li><a>FieldOne LevelOn</a></li>
</ul>
</li>
</ul>
</ul>
And the following CSS:
ul ul {
display: none;
}
ul li {
list-style: none;
}
ul li:hover > ul {
display: block;
}
ul
{
background: #76b800;
padding: 0 20px;
margin-left: 5px;
border-radius: 10px;
list-style: none;
position: relative;
display: inline-table;
}
ul:after {
content: ""; clear: both; display: block;
}
ul li {
float: left;
min-width: 140px;
text-align: center;
vertical-align: bottom;
}
ul li:hover {
background-color: #4478B7;
}
ul li a
{
display: block;
padding: 10px 40px 10px 40px;
color: #fff;
text-decoration: none;
font-size: 18px;
}
ul ul {
background: #4478B7;
padding: 0;
position: absolute;
top: 100%;
z-index: 5;
margin: 0;
}
ul ul li
{
float: none;
border-top: 1px solid;
border-bottom: 1px solid;
position: relative;
border-style: solid;
border-width: 1px;
border-color: #88AAD2 white #335B8C white;
}
ul ul li:hover
{
background-color: #396599;
background-image: none;
}
ul ul li a {
color: #fff;
min-width: 140px !important;
padding: 10px 40px 10px 40px !important;
font-size: 16px !important;
}
ul ul li a:hover {
background: #233F61;
}
ul ul ul {
position: absolute;
left: 100%;
top:0;
}
The problem: When you go to the second level and you hover over a LI, the third level list appears. If you go from one LI to another in the second level, the third level list nested inside the first LI disappears and the one nested inside the second appears (if it has one).
BUT
If instead you leave the second-level list altogether without making the third-level menu disappear by navigating inside the second-level menu, once you re-list the second-level menu the third-level one that was last showed appears there next to its LI, but without content (no text from As). The lists appear with the style as though as they weren't being hovered.
You can check the effect here: http://jsfiddle.net/JE8ZM/. If you run it on IE9 or Chrome, it works. But if you run it on IE7, try going to FieldOne LevelTwo, hover over it and then leave on its left, without entering the third-level menu that showed up. Then hover over FieldThree LevelOne and see what I mean.
Thanks.
Nested sub nav menus are notoriously difficult to get working cross browser without the aid of Javascript or jQuery. Here is the best 'pure CSS' resource I know of which will solve your problem!
http://www.cssplay.co.uk/menus/final_drop2.html

How to remove space between anchor text and border when padding is already zero'd?

I'm working on a navigation menu, and have created a containing four anchor tags, as below:
<nav>
<ul>
<li>BLOG</li>
<li>ABOUT ME</li>
<li>PORTFOLIO</li>
<li>CONTACT</li>
</ul>
</nav>
I've set a top border of 10px for each link, which I want to sit tight on top of the text. Despite setting padding to 0 in all the right places, I can't get rid of the padding. I've tried changing line height but this just moves the whole nav section up or down the screen, and doesn't affect the gap between the anchor text and the top border.
Here's the CSS (not including CSS resets etc, which I can post if needed, but these mostly came from HTML5BoilerPlate.
nav ul {
padding-right: 24px;
float: right;
}
nav li {
display: inline;
font-family: Oswald;
font-size: 25px;
}
nav a {
color: #7a7a7a;
text-decoration: none;
margin-left: 16px;
}
nav li a {
border-top: 10px solid #7a7a7a;
}
nav a:hover {
color: #555555
}
nav a.active {
color: #555555;
}
nav a.highlight:hover {
color: #1f9c66;
}
a.highlight {
color: #29cf86;
}
Can anyone help? Thanks.
Set an anchor float which will remove the margin & padding in every web-browser.
#container li a {
float:left;
}
You could translate the anchors a few pixels up via relative positioning:
li {
display: inline;
margin-left: 16px;
border-top: 10px solid #7a7a7a;
}
a {
position: relative;
top: -6px;
}
Live demo: http://jsfiddle.net/XQK9h/
I think it helps you.
<style type="text/css">
nav ul {
padding-right: 24px;
float: right;
}
nav div {
border-top: 10px solid green;
display: block;
float: left;
font-family: Oswald;
font-size: 25px;
height: 10px;
margin: 10px;
}
nav a {
color: #7a7a7a;
text-decoration: none;
}
nav a:hover {
color: #555555
}
nav a.active {
color: #555555;
}
nav a.highlight:hover {
color: #1f9c66;
}
a {
position: relative;
top: -3px;
}
a.highlight {
color: #29cf86;
}
</style>
<nav>
<ul>
<div>BLOG</div>
<div>ABOUT ME</div>
<div>PORTFOLIO</div>
<div>CONTACT</div>
</ul>
</nav>

<ul> inside <li> changes <li> width

I have a menu which is a <ul>. Inside one of the <li>s I have another <ul> to add a depth level, a sub-menu. However, when hovering the <li> to make the sub-menu appear, it's width changes to match the <ul>s. Also, the sub-menu will pull the content area down, and that's not what I want.
I want the <li> to maintain it's width when it's hovered, and the sub-menu to appear on top of the content area.
Here's the jsFiddle: http://jsfiddle.net/Cthulhu/RWjcA/ (If you hover Products, you will see it happen.)
Here's a slightly cleaned up version, and without the need for Javascript: http://jsfiddle.net/dZhQN/2/
HTML
<ul id="nav">
<li><a>Home</a></li>
<li><a>Whatever</a></li>
<li>
<a>Products</a>
<ul>
<li><a>What When How</a></li>
<li><a>Who Why</a></li>
</ul>
</li>
<li><a>Contacts</a></li>
</ul>
<div id="content"></div>
​
CSS
#nav, #nav ul {
list-style: none;
text-transform: uppercase;
text-align: center;
}
#nav li {
display: inline-block;
position: relative;
}
#nav li a {
display: block;
cursor: pointer;
font-size: 12px;
padding: 24px 20px 15px;
}
#nav > li > a:hover {
color: #FFF;
background: #4A6125;
}
#nav ul {
background: #000;
display: none;
position: absolute;
top: 100%;
left: 50%;
z-index: 999;
width: 150px;
margin-left: -75px;
}
#nav ul li a {
color: #FFF;
padding: 10px;
}
#nav ul li a:hover {
text-decoration: underline;
}
#nav li:hover ul {
display: block;
}
#content {
background: gold;
height: 200px;
}
​
You can simply give a fixed height to that Div
#main_menu .menu {
list-style: none outside none;
text-align: center;
text-transform: uppercase;
height:60px;
}
Hope this will help...

Resources