force width from parent element - html

I have a main menu #navi. It's items have different width. The submenus should have the same width, as their parents. I made a JsBin to demonstrate it: http://jsbin.com/yusunohage/1
A very very very long submenu should not wider then its parent A longer menu.
Here is the HTML:
<ul id="navi">
<li class="menu1">Menu </li>
<li class="menu2">A longer menu
<ul class="children">
<li>A very very very long submenu</li>
<li>Submenu</li>
</ul>
</li>
<li class="menu3">Menu item</li>
</ul>
And the css
ol, ul {
list-style: none;
padding: 0;
}
#navi > li {
padding: 0 10px;
}
li {
float: left;
}
ul.children{
background: gold;
}
#navi li ul li {
float:none;
}

PROBLEM:
Having the parent element limit the children element without fixing the css width parameter.
SOLUTION
Setting the child width to 0px for the purpose of measurement the native parent width.
Setting the child width with parent width after measurement.
jQuery:
$('.children li').css('width', 0);
$('.children li').css('width', $('.menu2').width());
Solution here : http://jsfiddle.net/urahara/4m5vkkk2/2/

First set the height for parent elements. Then care about the width of child elements.
Just put this in your css code
.menu2{
max-width:140px;
}
Here is the Jsfiddle http://jsfiddle.net/nwL1hku4/
Edited
As you stated above that you don't know about the width of parent element. You may give max-width to child elements, to restrict their width.
Like this http://jsfiddle.net/nwL1hku4/1/

Related

CSS3 to make a thin ul fitting its li

Assume the following HTML5 document:
<!doctype html>
<html>
<head><title>try</title>
</head>
<body>
<h1>testing</h1>
<ul id='ul_id'>
<li id='li1_id'>one</li>
<li id='li2_id'>somelongertwo</li>
</ul>
</body></html>
what would be the CSS3 stylesheet so that the width of <ul id='ul_id'> element would be the smallest to fit, so would be here the width of <li id='li2_id'> plus its bullet, since the second list item is the widest item of the <ul id='ul_id'> element?
I don't want the width of my <ul id='ul_id'> to be as wide as the containing <body>
The context and motivation of this question is my optimizing (nearly minimizing) width of jqueryui menu question.
What would be the CSS3 stylesheet so that the width of <ul id='ul_id'> element would be the smallest to fit
You could change the display of the ul element to inline-block.
In doing so, it will have a "shrink-to-fit" width based on the size of the children elements.
Based on section 10.3.9 of the relevant spec for inline-block elements in normal flow:
If width is auto, the used value is the shrink-to-fit width as for floating elements.
#ul_id {
display: inline-block;
background-color: #f00;
}
<ul id='ul_id'>
<li id='li1_id'>one</li>
<li id='li2_id'>somelongertwo</li>
</ul>
Alternatively, setting the display to table would result in similar behavior.
#ul_id {
display: inline-block;
}
or
#ul_id {
float: left;
}
#ul_id:after {
clear: both;
}
One of these two should work as expected.
Width resize to fit container is caused by display: block behaviour which is default for ul element.

Using exact percentages and font-size:0 to make grids that fit perfectly?

I'm trying to work with grids which are whole numbers, that perfectly match up, e.g 20% for 5 menu items would fill 100%.
I have the following code:
#menu-primary-navigation li{display:inline-block;font-size:0;width:20%;}
#menu-primary-navigation li a{font-size:22px;}
I understand the issue of whitespace between ul li elements that means 20% elements will not fit into 100% due to the white space in between each element. However I thought I had got around this issue by making font-size:0; where the elements sit and then only setting the font size inside the element.
Yet you can see in my JSFiddle that the five menu items still do not fit perfectly to 100% of the container.
In my example, the width is 600px and each element is 20%. I can see from inspecting the elements that they are all equal to 120px. Well 120 x 5 = 600, so why does the last element always fall to the next line?
<div class="wrapper">
<ul id="menu-primary-navigation" class="menu">
<li class="menu-item">
Home
</li>
<li class="menu-item">
Tutorials
</li>
<li class="menu-item">
<a>Logo</a>
</li>
<li class="menu-item">
Projects
</li>
<li class="menu-item">
Articles
</li>
</ul>
</div>
http://jsfiddle.net/franhaselden/kq9o4t0v/
Using display: inline-block; is notoriously problematic in this situation. You could consider using display: table-cell; achieve the same result with less hassle.
HTML can remain the same, but try this CSS:
.wrapper{width:600px;}
ul,li{padding:0;margin:0;}
/* needed for use with display: table-cell; */
.menu{ display: table; width: 100%; }
/* changed to table-cell */
#menu-primary-navigation li{display:table-cell; text-align: center;}
#menu-primary-navigation li a{font-size:22px;}
However as I am supposed to be answering the question the part you went wrong is you need to set the font size on the parent i.e the ul in order to effect the white-space. Do note however, I think certain versions of IE will not like font-size: 0;
CSS:
.wrapper{width:600px;}
/* you need it set on the parent */
ul,li{padding:0;margin:0; font-size: 0; }
#menu-primary-navigation li{display: inline-block; width: 20%; text-align: center;}
#menu-primary-navigation li a{font-size:22px;}
You are setting font-size:0 on the li tag to remove whitespace. However the li's are contained in the ul, so you need to set the font-size there.
In your jsfiddle the issue is resolved by setting
.wrapper{width:600px;}
ul,li{padding:0;margin:0;font-size:0}

Floating inside an absolute-positioned div, placed inside a relative positioned element

I'm creating a pure-CSS hover dropdown menu, based on a very basic idea idea.
The HTML:
<ul id="top">
<li>
Menu item 1</li>
<li>
This one has submenu
<div class="submenu">
<ul>...</ul>
<div>
</li>
</ul>
The CSS:
div.submenu {
display: none;
position: absolute;
}
ul#top > li:hover div.submenu { display:block; }
As far as I know, this is the bare minimum to get the idea working.
The problem is I want the submenu to be multi-column, without actually using CSS3 multiple columns.
So I decided to break my submenu into multiple lists and have them float: left like this:
<ul id="top">
<li>
Menu item 1</li>
<li>
This one has submenu
<div class="submenu">
<ul>...</ul>
<ul>...</ul>
<ul>...</ul>
<div>
</li>
</ul>
...and the CSS:
div.submenu ul { float:left; }
This worked well until the point when I got a pretty big submenu in the last main menu item, producing a result like this:
The problem is it is unacceptable to have the submenu fall outside the container. I decided to mark the second half of the main menu items as class="right", and align the submenu's right border to the parent item's right border.
li.right div.submenu { right: 0; }
/* this placed the submenu to the right of the entire page;
it needs a positioning context: */
ul#top li { position:relative; }
That last line causes the <ul>'s to stop floating and just get stacked on top of each other.
Is there a way to keep them floating without setting the <div class="submenu"> to a fixed width?
Interactive demo: http://codepen.io/oli-g-sk/pen/ociet
Edit: if this helps somehow, it is allowed to set the submenu list items .submenu > ul > li to a fixed width. In fact I'm already doing it in the demo.
Try removing float:left from div.submenu ul and add these two rules:
div.submenu {
white-space: nowrap;
}
div.submenu ul {
/* float:left; <-- remove this */
display: inline-block;
}
demo: http://codepen.io/anon/pen/ApxFd

menu inline menu float left with width and height to % on pure css

#headermenu ul li {
width:195px;
height:45px;
float:left;
left:0;
text-decoration:none;
text-align:center;
font-family:century gothic;
background-color:#fef3e2;
}
this is my li css ive been trying to make the width and height to a certain percent but apparently its not working..my question is, is there a way or is it possible to set this width and height to percent i dont want to use table here because if i do the submenu when hover in the menu will be distorted meaning they will be side by side not on top of each other(i want them to be on top of each other)..any idea is appreciated..
UPDATE:
http://jsfiddle.net/guradio/gvmRX/
in this fidde you can see that i change the li size to 100 to make it fit in the this is the reason why i want to make it a percent width if possible to avoid over sized width
To be able to give a height and width in a percentage you need to give the parent the width and height.
HTML:
<ul>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
CSS:
ul li {
width: 33%;
height: 33%;
float:left;
left:0;
text-decoration:none;
text-align:center;
font-family:century gothic;
background-color:#fef3e2;
}
In this demo we have the <li> you can see it is using a width as percentage as the parent has already a set width (<ul> is a block element) but the height is not set already so the percentage height it not working.
Demo Here
Now lets set the parents height and see if it changes anything.
Added CSS:
html, body {
height: 100%;
}
ul {
height: 100%;
}
so here we are setting the html, body height to use 100% and then the the parent of <li> needs to also be set (again <ul> is a block element, so it has no set height). This allows us to use percentages for the height.
Demo Here
Any question let me know.

Div collapse after float css

I have a div called NAV and inside of NAV I have an UL with 5 li which I float to the left, the li's that is but when I do that the NAV collapses. I know this because I put a border around NAV to see if it collapses and it does. Here is the example.
collapsed http://img401.imageshack.us/img401/8867/collapsedze4.png
no collapsed http://img71.imageshack.us/img71/879/nocollapsedkx7.png
as you can see in the first image, the links in the NAV div are floated left and that
black border ontop is the actual div called NAV.
in this image you can see how it has top and bottom border and it not collapsed.
here is some of the html and css I used.
alt text http://img301.imageshack.us/img301/5514/codejc8.png
#nav #ulListNavi a {
float: left;
}
Add any overflow value other than visible to your container:
div#nav { overflow:auto; }
Then add width to restore the width
div#nav { width: 100%; overflow:auto; }
One solution is to add a "clear:both" style to an element after the last floated anchor, for instance:
<div id="nav">
<ul id="ulListNavi">
<li>Home</li>
<li>Search</li>
<li>Flowers</li>
<li>My Account</li>
<li>Contact Us</li>
</ul>
<div style="clear:both;"></div>
</div>
This causes the containing element to clear all floating elements before closing the containing box.
A few other options for clearing floats here:
http://www.quirksmode.org/css/clearing.html
http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/
As to the best way of doing it, that's almost a holy war, the purists would freak about the extra div, if you are not fussed by a little extra markup, the addition of the cleared div as suggested by Joshua and AJ will work fine, and is a reliable technique, but there are at least 17 other ways of doing it...
add this code after your ul:
<div style="clear: both"></div>
Try floating the containing element to the left too.
Don't bother with clearing elements or overflow. Add this:
#nav {
float: left;
}
When you float the LI's, the #nav no longer "contains" anything so it collapses. But if the #nav is floated also, it contains anything floated inside it, so it expands again.
(Also consider removing the #nav div and just applying the same styles to the UL.)
Your problem is because you are floating the <A> elements, but each of them is inside an <LI> element. LIs display as blocks by default, so each <LI> is forcing it's child <A> to begin on a new line.
If you float the <LI>s, I think you'll solve your problem.
#nav #ulListNavi li {
float: left;
}
The quickest solution would be to add overflow:hidden to clear the float on the parent element:
#nav{overflow:hidden;}
Without changing your HTML:
#nav
{
width: 100%;
overflow: auto;
border: solid 1px red;
}
#ulListNavi
{
margin: 0;
padding: 0;
list-style: none;
}
#nav #ulListNavi li
{
float: left;
}
#nav #ulListNavi li a
{
margin-left: 5px;
}
Works in IE8 and FF 3.5