I have a very simple layout, just two un-ordered list by using <ul><li>, with each of them inside a <div> .
<div id="main">
<div id="fist-list">
<ul id="colors">
<li id="white">White</li>
<li id="green">Green</li>
</ul>
</div>
<div id="second-list">
<ul id="numbers">
<li id="one">One</li>
<li id="two">Two</li>
</ul>
</div>
</div>
li{
float: left;
width: 30px;
}
#second-list{
padding-left:30px;
}
you can check my simple code here .
I tried to use CSS padding-left to seperate the two list 30px away from each other, but my CSS does not work, why?
If you still want the float at the li
You can float the list left, then it will work.
like this:
#second-list{
padding-left:30px;
float:left
}
Still I would make it another way, but I need to see what you want to achieve. Do you have a picture of it?
There's several problems here:
You're using Float, which destroys the layout
Div's are block-level elements, and thus will always break the line.
Simply remove the float, and make the divs inline-blocks, like so:
http://jsfiddle.net/a8dy6/12/
good luck!
write overflow:hidden in you div to clear because it child's float
div{overflow:hidden}
#second-list, #fist-list{float:left}
check this http://jsfiddle.net/sandeep/a8dy6/18/
Edit: as per you comment below
li{clear:left;}
check this http://jsfiddle.net/sandeep/a8dy6/19/
EDIT:
Reason: actually the #second-list takes the padding
if you define an element an float then you have to clear it parent otherwise parent collapse.
check this when didn't clear the parent http://jsfiddle.net/sandeep/a8dy6/22/
So; you have to clear it's parent when the child have float.
check this after clear parent http://jsfiddle.net/sandeep/a8dy6/23/
Related
I am trying to display navigation items (horizontally) in a blue colored ribbon. Somehow, the background-color property is not getting applied to the ul element. I tried to put it inside a div element with background as blue. Still, it doesn't work
Html snippet as,
<div style="background-color:blue;">
<ul style="list-style-type:none;background-color:blue;">
<li style="float:left;margin-right:10px;">cassandra</li>
<li style="float:left;margin-right:10px;">mongodb</li>
<li style="float:left;">couchdb</li>
</ul>
</div>
Why is my background color not showing if I have display: inline?
This is the same issue as this. The div is coming out at height 0, same as the list as the float doesn't take up any space.
If you specify the height or tell them to display:inline-block it'll work.
Fiddle: http://jsfiddle.net/7vp4vz6f/
You are using float property for the li elements, so you need to apply some sort of clearfix for container to adjust it's size according to the content size. You can try with the overflow CSS property:
body > div { overflow: auto}
JSFiddle
<div style="background-color: blue; overflow: hidden;">
<ul style="list-style-type:none;background-color:blue;">
<li style="float:left;margin-right:10px;">cassandra</li>
<li style="float:left;margin-right:10px;">mongodb</li>
<li style="float:left;">couchdb</li>
</ul>
</div>
Your elements have no width and height, that's why.
Also, consider using a stylesheet, one of the many advantages is that you don't run into such issues very often.
I try to created multicolumn menu here (updated link is HERE):
I made used of display: inline-block strategy to make (horizontal) rows. And seems it works.
But it stops work when I wrapped it into the span which comes with position:relative.
<section>
<button>123</button>
<button>123</button>
<span class="dropdown">
* <!-- this is a link that should be inline with buttons -->
<div class="menu">
<div class="row">
<ul>
<li>11111111111111111111111111</li>
<li>1111111111111</li>
<li>111</li>
</ul>
</div>
<div class="row">
<ul>
<li>2222222222</li>
<li>222222222222222222222</li>
</ul>
</div>
<div class="row">
<ul>
<li>33</li>
<li>33</li>
<li>33</li>
</ul>
</div>
</div>
</span>
</section>
The href link in the span represents a button or link where I would click to make menu appeared.
I have to have position relative in the span to make menu appears on right place, relatively to it.
(all buttons and link should be on the same horizontal line)
Question: how to make it working?
It works though if I change span to div and make fixed size for it like width:600px (and this width have be more or less of the menu width to make it work like expected, which is weird), but/so I want it to be span (with no specific/hard-coded width).
UPDATE:
I've updated my example to show how it works with span as block and buttons: http://jsfiddle.net/uz0do787/32/
Just put a little more detail that was not shown on previous demo, to show what I want.
I want all buttons and the href link be on the same line, but making span "display:block" breaks that order.
Simply add display:block to span
see DEMO
See why you need to add display:block
The HTML <span> element is a generic inline container for phrasing
content, which does not inherently represent anything. It can be used
to group elements for styling purposes (using the class or id
attributes), or because they share attribute values, such as lang. It
should be used only when no other semantic element is appropriate.
<span> is very much like a element, but <div> is a block-level
element whereas a <span> is an inline element.
Source:Mozilla Developer Network
Do you mean something like this?: Fiddle
.dropdown {
position:relative;
display: block;
padding: 10px;
}
a {
position:absolute;
top: -15px;
left: 100px;
}
Span - this is an inline element. You can't wrap block with inline element. To make it work with span, add "display: block;" property to the .dropdown
Like:
.dropdown {
position:relative;
display: block;
}
I guess If fixed it by switching to display:table then I can stay with span not trying to make it block and it does not jump then to the next line:
Before:
http://jsfiddle.net/uz0do787/32/
After
http://jsfiddle.net/uz0do787/34/
But in any case, I think there is a room for re-factoring - the menu should be independent to show itself by js by x,y depending on the span/link location so that the DOM not to be so dependable on each other and not to be so fragile.
And I just do not understand, why when I apply "display:table" (on that solution/answer I proposed) span stays on place with buttons, but with "display:inline-block" the span breaks the menu layout. What makes display:table works like expected comparing to inline-block solution.
I have this simple HTML:
<a style="display:block;text-align:right" href="link.com">Test Link</a>
I want to place a link to the far right of a parent DIV. However, I tried the above code but it leads the link to fill all the space meaning it's clickable across the whole width of the div.
The only ways to avoid this are to give a fixed width to the link or to wrap the link in another DIV. Is there any other way? Or to float the link but it will break the layout
You can use a float for 'floating' it to the right side of your div.
<a style="float:right" href="link.com">Test Link</a>
See this fiddle:
http://jsfiddle.net/wstmrtgz/
JSFiddle example
I've used display:inline-block and float:
.parent {
display:inline-block;
width:100%;
}
.link {
display:inline-block;
float:right;
text-align:right;
}
EDIT: Having seen your comment about not being able to use floats, you can also do this using display:inline-block and fixed widths.
float will fix this
dont forget the clear the float !
<div style="width: 200px; background-color: red;">
<a style="float:right;" href="link.com">Test Link</a>
<div style="clear:both;"></div>
</div
see the difference with and without the clear in the following fiddles!
With clear
http://jsfiddle.net/j9q8jgvm/
without clear
http://jsfiddle.net/j9q8jgvm/
My horizontal navigation bar looks like this:
| MENU-ITEM 1 | MENU-ITEM-2 | MENU-ITEM 3 | SEARCH FIELD |
The menu-items have equal width, but since the website is cms-driven, the count of items and therefore the width of the menu-item-list will change.
I'm looking for a CSS solution for automatically stretching the search-field on the right to use 100% of the remaining space inside the navigation bar. The navigation-bar's total width is static (about 950px).
html is something like this, but maybe I need wrappers anyway:
<div id="nav">
<ul id="nav-items">
<li class="nav-item">MENU-ITEM 1</li>
<li class="nav-item">MENU-ITEM 1</li>
<li class="nav-item">MENU-ITEM 1</li>
</ul>
<div id="search-cont">
<input id="search">
</div>
</div>
Here is a fiddle with the basics: http://jsfiddle.net/Qg2ag/
The idea is:
The wrapper of an input field must have display:block and overflow:hidden.
The menu near it must have the float:left and the items in it must be inline or inline-block.
So, the floated block eats it's width from the block with an overflow, so you can set width: 100% in it safely. And I've added padding-right: 6px to the input's wrapper so there is no need in ajusting it's width or using other box model. Of course the size of this padding can vary if you'd change the input's style.
Maybe it helps if you use display: inline; and float: left; on the li-elements.
This will keep them in one line. You can style these tags now. If you're using a-tags inside the lis you may consider styling these instead of the lis.
The search-bar will then be displayed in one line with these elements but also remain at a width of 100%.
Check out this fiddle.
I'm creating a site where I've encountered a huge IE lag when hovering over the menus.
I'm using Cufon in combination and it seems like it's causing a huge lag when I apply height, width, margins or paddings to the li:hover element.
So, I need to figure out a smart way of doing this otherwise.
The site is here, http://w3box.com/mat
You can clearly see the menu I guess.
So, what I want is to push the entire menu downwards so it's like 3 or 4 pixels above the bottom of the height line. So it matches about the same vertical position as the logo font to the left.
Then, I want the hover effect to be larger in height. Hard to explain, but when hovering over a menu item, imagine a box where the text is positioned at the very bottom of the box. Like this;
http://img710.imageshack.us/img710/2791/menuheader.jpg
Now, you may notice the arrow looking thingy sticking at the bottom. I don't really need that, but if you have any idea on how to do it, I'd appreciate the help! ;)
I have not tried, but I think this may be an option.
You have everything with in one div, why dont you try to put div with in divs?
this is your current code for header.
<div id="header">
<img class="LogoChef" src="img/LogoKokk2.png" alt="Logo"/>
<img class="LogoMatkalender" src="img/MatkalenderLogo.png" alt="Logo"/>
<ul class="menuwrapper">
<li><h4>Logg ut</h4></li>
<li><h4>Kontakt</h4></li>
<li><h4>Kontrollpanel</h4></li>
<li><h4>Oppskrifter</h4></li>
<li><h4>Hjem</h4></li>
</ul>
</div>
You can try something like this, so you have more control over the different objects.
<div id="header" style="float:left;vertical-align:bottom">
<div id="imgChef">
<img class="LogoChef" src="img/LogoKokk2.png" alt="Logo"/>
</div>
<div id="imgMat" style="float:left;vertical-align:bottom">
<img class="LogoMatkalender" src="img/MatkalenderLogo.png" alt="Logo"/>
</div
<div id="menu" style="float:right;vertical-align:bottom">
<ul class="menuwrapper">
<li><h4>Logg ut</h4></li> <li><h4>Kontakt</h4></li>
<li><h4>Kontrollpanel</h4></li>
<li><h4>Oppskrifter</h4></li>
<li><h4>Hjem</h4></li>
</ul>
</div>
</div>
I am not sure that may be the right combination, but I think with the three divs inside the div you will gain more control over the elements inside the header div.
Omit the h4 in the menu since i think it is not needed. Than set display:block on <a> and use line-height and padding-left , padding-right to make the anchor expand the right size. Also notice that li:hover is not supported in IE6/7 without some tweaks. To position the menu on same level as logo just set a margin-top on ul element.
There're too many rules for me, too many useless rules.
Don't have the time to correct all and test it on FF/IE, but this works ofr example :
.menuwrapper li {
float:right;
list-style: none;
padding: 30px 23px 3px 23px;
position: relative;
top: 7px;
}
What about vertical-align?