I have vertical drop down menu, and I want that the sub menu will be vertical too.
Seemingly, if you set float:left for the sub menu li it should be fine, but the problem is, that the sub menu ul which is nested in li has a width that depends on it's parent (li) width and don't expands enough, so that the lines (li) of sub menu are presented vertically.
You can see an example: http://jsfiddle.net/r6qNJ/
I thought about two solutions:
Solution 1:
Expanding a width of a nested ul to a very big one
ul.sub{width:999px;}
The problem is, that by this way I have to dissable a background and border of a sub menu ul, as it has a non nature width.
An example: http://jsfiddle.net/r6qNJ/1/
Solution 2
Using display:flex property, so that the width of a child element will not depend on it's parent width.
An example: http://jsfiddle.net/r6qNJ/2/
The problem is, that it's not supported in old browsers.
So, what is more elegant and efficient CSS solution you can offer?
(I am looking for a pure CSS solution, without changing HTML, since I am talking about changing in wordpress theme and prefer to make changes only in css files)
You can have this option with white-space:
ul.sub{
position:absolute;
left:0px;
top:100%;
padding:0;
display:inline-block;
white-space:nowrap;
}
ul.sub li{
display:inline-block;
margin-right:-4px;
width:80px;
float:none;
}
The demo http://jsfiddle.net/r6qNJ/23/
Related
I often want to horizontally center an horizontal menu (an unordered list with the li elements floated) without specifyling a fixed width, so that the menu remains centered in all resolutions.
To my knowledge there's no way to achieve that, but perhaps there is some little know trick to achieve it?
Won't this do the trick?
ul{
list-style:none;
width:100%;
text-align:center;
}
li{
display:inline-block;
}
Fiddle
Here I've got a jquery menu which is working perfectly. But Ive given it a fixed width of 400px and so what happens is that if I add more than certain number of links to the main ul they will flow in the next line and that is absolutely not desired.
I tried overflow:hidden and line-height to somehow overcome the issue BUT NO RESULT anyway.
Here is the menu : http://jsfiddle.net/b5Wdc/
As you see there, the red color link flows on the next line and that is the problem.
What do should I write to hide the overflown links in this situation?
Thank you all anyway.
From our conversation in the comments on the question, it seems that your menu is completely fixed and any "extra" items should always be hidden and there is no dynamic display or wrapping required. So you can just use CSS to hide all menu items that you know won't fit in. Since a menu item has a width of 99px and the menu is 400px you know you will only ever show 4 items. This purely CSS will hide the rest:
.HeadMenu #nav > li:nth-child(n+5) {
display:none;
}
However it requires a minimum of IE8 for the nth-child CSS selector support.
Since you mentioned jQuery in the question you could accomplish the same in JavaScript if you need to support IE8 with:
$('.HeadMenu #nav > li:nth-child(n+5)').hide()
Alternatively, keep the CSS solution (as it's cleaner) and use selectivizr to bring nth-child selector support to IE8.
if you change your styles to the following i think it may work:
.HeadMenu .HeadMenuMain
{
display:block;
position:relative;
margin:0;
width:400px;
padding:0;
direction:rtl;
height:40px;
white-space:nowrap; //will make elements stay on one row
}
.HeadMenu .HeadMenuMain li
{
display:inline-block; //will make elements stay on one row with the nowrap
list-style:none;
position:relative;
}
http://jsfiddle.net/b5Wdc/2
Adding an overflow:hidden to the navigation menu will do the trick:
.HeadMenu #nav {
overflow: hidden;
}
EDITED
So basically I was able to create the drop down shown above in html css, but when i converted it into a wordpress theme the drop down didnt work anymore,can someone help me out? Thank!
`homeabout
work
by client
by category
clients
contact
`
I did it here using a pseudo element that is positioned absolutely to the left of the li element. This then butts it up right up to the border on the parent UL element. In order to get the line on the bottom li to align with the bottom of the border I had to bump the li's down with a top position property, so I added some margin so it wont overlap with anything underneath it.
ul{
padding:16px 8px 0px 0px;
border-left:1px solid #000;
}
li{
display:block;
padding-left:12px;
position:relative;
top:9px;
height:20px;
}
Other then using images, this is probably the easiest way involving the least amount of css.
http://jsfiddle.net/PfChj/4/
EDIT
Here's the modified fiddle. I pretty much redid your css because it was a little hard for me to follow with all those sub ul and li children. Sometimes it's better to use a class for readibility, so you're going to have to redo your styles a bit. The sub menu is positioned in the center of the top li which has a set width now. If you don't want it at the center and what your top li's to flex with the link widths, you can modify this.
http://jsfiddle.net/FYnS4/2/
I'm trying to use only css to turn a nested div and a normal div into something where the top one can be hovered to view more content without moving all the layout around.
Essentially:
[hover me for more]
[content-that-gets-overlapped-by-hover-content]
or see the nearly-working example on jsfiddle:
http://jsfiddle.net/tchalvakspam/vVgY2/9/
Unfortunately, when you do overflow:visible, it seems to be nearly useless because you can't give the content that overflows any background style, so it remains unreadable.
Is that right, there is no way to give overflow:visible overflowing content a background? If that is the sad state of affairs, what is the shortest amount of changes that could be done to that content to turn it into a readable hover-to-expand section?
Finally found the solution in the form of a sibling selector on the hover to give the next element after the hovered element a margin to take the place of the now-absolute hover element.
http://jsfiddle.net/tchalvakspam/MBcDW/
So the pertinent css becomes:
#fixed-height{
position:relative;
width:100%;
height:1.25em;
overflow:hidden;
background-color:lightblue;
color:red;
z-index:10;
}
#fixed-height:hover{
overflow:visible;
height:auto;
position:absolute;
max-width:20em;
}
#fixed-height:hover + #right-below{
margin-top:1.25em;
}
I have a UL > li's in my html, and the li's are set to float: left.
My doubt is how to separate them, inside a father div, equally, so that doesn't matter how many li's are in the ul, they still have the same spacing between them?
If li's are not 'good' to do that, what is better?
As per I understand is better you can write like this:
li + li{
margin-left:10px;
}
You can also use display:table property for this. Write like this:
ul{
display:table;
width:100%
}
li{
display:table-cell;
}
You may set a border to the ul and set a margin to child lis. Or you may set border both to ul and lis. The reason for parent border is, when lis merge, they will combine their borders creating a double border where they meet.
Here is a Live Demo showing both methods.