I am trying to build a simple horizontal list, where each list item is a div and I want them all to sit next to one another. When I try to use the code below though, the divs end up on separate lines. Here's what I've got:
HTML:
<ul id="navlist">
<li><div>...</div></li>
<li><div>...</div></li>
<li><div>...</div></li>
</ul>
CSS:
#navlist li
{
display: inline;
list-style-type: none;
padding-right: 20px;
}
I have tried to give my divs a max width and a width but that doesn't work either. Basically, they show up without bullet points on separate lines.
Some help on fixing this would be very appreciated, thanks!!
#navlist li { display:inline }
#navlist div { display:inline }
Making the <li> inline while leaving the <div> as block is your problem.
Alternatively, you may want inline-block for the <li> if you are going to be controlling sizes or margins.
You may also be interested in this demo: http://phrogz.net/JS/ul2menu/purecss_testsuite.html
I'm not sure why you have <div> inside your <li>, but I presume you have your reasons.
CSS
* {
margin: 0;
padding: 0;
}
ul {
background: #48D;
height: 35px;
line-height: 25px;
width: 300px;
}
li {
float: left;
list-style-type: none;
margin: 5px 0 0 5px;
}
li div {
background: #6AF;
padding: 0 5px;
}
HTML
<ul>
<li><div>Text</div></li>
<li><div>Text</div></li>
<li><div>Text</div></li>
</ul>
Each div inside the list items is displayed as a block by default. Display them inline as well and it should work.
#navlist div, #navlist li
{
display: inline;
}
#navlist li
{
list-style-type: none;
padding-right: 20px;
}
Try float: left; on the list items, perhaps something like that:
#navlist li
{
float: left;
list-style-type: none;
padding-right: 20px;
}
Also, make sure to specify a height to the ul because the elements will go out of the flow, meaning that the ul won't have any height. You can also use a clearfix to fix this behavior:
.clear:after
{
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clear
{
display: inline-block;
}
html[xmlns] .clear
{
display: block;
}
* html .clear
{
height: 1%;
}
You just add class="clear" to the <ul>. Google clearfix css for more infos :)
Related
IE11 is wrapping the rightmost of my four nav items to the next line, when all four together are supposed to fit on one line and have a combined width of 320px. How can I stop this?
HTML
<body>
<div id="nav">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</div>
</body>
CSS
html, body, div, span, nav {
margin:0;
padding:0;
border:0;
width: 100%;
box-sizing: border-box;
}
nav{
display: block;
}
#nav ul {
text-align: justify;
width: 320px;
}
#nav ul:after {
margin-left: 100%;
content: "";
}
#nav ul li {
display: inline;
}
#nav a:link {
border-bottom: 0;
}
#nav a:hover {
color: #f00;
}
The markup and code work fine in Firefox 50. I have read this question but it is not clear how the answer might be applied to my problem.
https://jsfiddle.net/rrjh4g2L/
If you set fixed width for #nav ul and you know that you always will have only 4 items in the menu, just set width: 25%; for li elements and use float: left;
ul { width: 320px; overflow: hidden; } /* or use clearfix hack if you want */
li { width: 25%; float: left; }
It should render correctly across all browsers.
I am having some problems getting my menu to center on the screen. I thought setting the display to block, and making the left and right margins to auto, would do this for me; however, I was wrong. Here is a JSFiddle to help show the problem. Thanks for the help.
<ul id="menuList">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
#menuList{
display:block;
width:100%;
margin-left:auto;
margin-right:auto;
}
#menuList ul{
margin: 0px;
padding: 0px;
list-style-type: none;
}
#menuList li
{
list-style: none;
float: left;
margin-right: 0.5em;
}
#menuList a
{
display: block;
width: 8em;
color: black;
text-decoration: none;
text-align: center;
}
set menu to inline-block and parent to text-align:center
JS Fiddle
replace body with your parent id or class
body {
text-align:center;
}
#menuList {
display:inline-block;
}
The best way to center an element is by using margin: 0 auto; but the element need to have a fixed width (not 100% as you have).
So just add:
#menuList {
width:408px;
margin:0 auto;
}
Vitorino's answer is generally a bad idea. You don't want to put text-align: center on your body.
You could, however, set that CSS on the ul, and display the menu items inline(-block). As such.
#menuList ul {
margin: 0px;
padding: 0px;
list-style-type: none;
text-align: center;
}
#menuList li {
list-style: none;
display: inline-block;
margin-right: 0.5em;
}
You have to change the width, example:
#menuList{
display:block;
width:70%;
margin-left:auto;
margin-right:auto;
}
You used display block, so that your elements start new line, and then floats, that they would stay in same line. Just don't use this weird combo.
If you need items stay in line use 'display:inline;'
if you need items to stick to either left or right side of parent element, use floats. Be careful as floats often mess all the thing up. There are other ways to float things, that doesn't pull them out of document flow.
Here is fixed CSS:
#menuList{
display:block;
width:100%;
margin: 0 auto;
text-align:center;
}
#menuList ul{
margin: 0px;
padding: 0px;
list-style-type: none;
}
#menuList li
{
list-style: none;
display: inline;
margin-right: 0.5em;
}
#menuList a
{
display: inline-block;
width: 8em;
color: black;
text-decoration: none;
text-align: center;
}
I need to have an UL on the right side of a SPAN (http://jsfiddle.net/Shg9L/8/).
When the width is not enough I would like the UL to keep being on the right side of the SPAN but the LI items to start stacking ...
The problem is that when I resize down the window the UL goes under the SPAN.
The code I currently have is (http://jsfiddle.net/Shg9L/8/):
HTML
<div>
<span>Categories</span>
<ul>
<li>Book</li>
<li>Computer</li>
<li>Tablet</li>
<li>Toy</li>
<li>Music</li>
<li>Audio</li>
<li>Game</li>
</ul>
</div>
CSS
div {.clear;}
div span {
background-color: #E0E0E0;
float: left;
margin-right: 8px;
margin-bottom: 8px;
padding: 8px;
}
div ul {
list-style: none;
margin: 0;
padding: 0;
float: left;
.clear;
}
div ul li {
background-color: #F0F0F0;
float: left;
margin-right: 8px;
margin-bottom: 8px;
padding: 8px;
}
.clear:after {
content: "";
display: table;
clear: both;
}
Does anyone knows how to solve this?
Thank You,
Miguel
Fiddle
remove float:left; in your div ul
You can use div {white-space:nowrap;} to make sure it doesn't break on the contents of the div. The ul has display:inline-block; instead of float: left;. Float left makes it go to the left, leaving the flow of the text, and inline-block still keeps it in the flow of the text, while not taking up a new line.
demo
I would like to use the full width of the UL-element for the floated LI-elements. Is this somehow possible with using %-values for the padding of the LI-elements? I can't use a fixed width for the LIs, since the content is not the same lenght.
This is my HTML code:
<ul>
<li>January</li>
<li>February</li>
<li>March</li>
<li>April</li>
<li>May</li>
<li>June</li>
<li>...</li>
</ul>
And here comes my CSS:
ul {
overflow: auto;
list-style: none;
margin: 0;
padding: 0;
width: 600px;
background-color: blue;
}
li {
float: left;
padding-left: 3%;
padding-right: 3%;
background-color: #dd0000;
border-left: 1px solid #ffffff;
}
li:hover {
background-color: #ff0000;
}
Find the example at JsFiddle: http://jsfiddle.net/6Uy4y/
So the red LI-elements should end, where the blue UL ends, even when changing the width of the UL.
Thanks for pointing me into the right direction!
It looks like this is the start of tabular data. I'd use a <table>. If I'm mistaking, you can fake a table with CSS.
ul {
display: table;
list-style: none;
margin: 0;
padding: 0;
width: 100%;
}
li {
display: table-cell;
padding: 0 3%;
}
Here's a quick little demo: http://jsbin.com/iwacum/1/edit
I'm trying to build a HTML/CSS dropdown menu which is flexible in width. Due to the position:absolute for the second level of the navigation, I don't get the width of the first level. Removing the position:absolute will move all following elements on hover...
How can I solve this?
Here is the code:
ul {
margin: 0;
padding: 0;
list-style: none;
}
.level_1 > li {
float: left;
width: 45%;
background-color: #2FA4CF;
margin-right: 6px;
}
.level_1 > li:hover ul {
display: block;
}
.level_2 {
display: none;
position: absolute;
width: 45%;
}
.level_2 li {
background-color: #535B68;
}
<ul class="level_1">
<li>
Level one (1)
<ul class="level_2">
<li>Level two (1)</li>
</ul>
</li>
<li>Level one (2)</li>
</ul>
<p>Paragraph</p>
See the result here: http://jsfiddle.net/5uf2Y/
Hover "Level one (1)" and you will see, that the second level is not the same size like the first level...
You have forgotten two elements for display 100%.
Correction here
1st elements forgets it's :
Position relative on level_1 > li
.level_1 > li {
float: left;
width: 45%;
background-color: #2FA4CF;
margin-right: 6px;
**position:relative;**
}
2nd elements corrections it's :
change size of 2nd li
.level_2 {
display: none;
position: absolute;
width: 100%;
}
With "width:100%" on .level_2 it automatically turns out with the width of its parent.
Add position:relative to level_1 > li
.level_1 > li {
float: left;
width: 45%;
background-color: #2FA4CF;
margin-right: 6px;
position:relative;
}
Try to set the body { width:100%;} property, it will fix this issue, like shown below (added to your original CSS):
body{ width:100%;}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.level_1 > li {
float: left;
width: 45%;
background-color: #2FA4CF;
margin-right: 6px;
}
.level_1 > li:hover ul {
display: block;
}
.level_2 {
display: none;
position: absolute;
width: 45%;
}
.level_2 li {
background-color: #535B68;
}
Hey man you have a margin of 6px on your first row li thats why its a little bigger. I would use a margin left rather than right. That should fix the spacing.