Add space between <li> elements - css

I have a nav-menu on which it seems that I can't add a space (margin: 3px;) between the <li> elements.
You can see the HTML and CSS code on this jsfiddle or below.
You will see that I've added a border-bottom: 2px solid #fff; to the #access li to simulate the space between elements, but that is not going to work because under the nav-menu I will have a bunch of different colors. If I add margin-button: 2px it doesn't work.
This is the HTML:
<nav id="access" role="navigation">
<div class="menu-header-menu-container">
<ul id="menu-header-menu" class="menu">
<li id="menu-item-41" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-41">
About Us
</li>
<li id="menu-item-35" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-35">
Services
</li>
<li id="menu-item-34" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-34">
Environmental Surface Cleaning
</li>
<li id="menu-item-33" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-33">
Regulations
</li>
<li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-32">
Contact Us
</li>
</ul>
</div>
This is the CSS:
#access {
background: #0f84e8; /* Show a solid color for older browsers */
display: block;
margin: 0 auto 6px 55px;
position: absolute;
top: 100px;
z-index: 9999;
}
#access ul {
font-size: 13px;
list-style: none;
margin: 0 0 0 -0.8125em;
padding-left: 0;
}
#access li {
position: relative;
padding-left: 11px;
}
#access a {
border-bottom: 2px solid #fff;
color: #eee;
display: block;
line-height: 3.333em;
padding: 0 10px 0 20px;
text-decoration: none;
}
#access li:hover > a,
#access ul ul :hover > a,
#access a:focus {
background: #efefef;
}
#access li:hover > a,
#access a:focus {
background: #f9f9f9; /* Show a solid color for older browsers */
background: -moz-linear-gradient(#f9f9f9, #e5e5e5);
background: -o-linear-gradient(#f9f9f9, #e5e5e5);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#f9f9f9), to(#e5e5e5)); /* Older webkit syntax */
background: -webkit-linear-gradient(#f9f9f9, #e5e5e5);
color: #373737;
}
#access ul li:hover > ul {
display: block;
}

UPDATE 2021
My original answer was from 2012 when many of the Level 3 CSS Selectors did not exist. To achieve this we would need JS or other explicit CSS styles/classes to achieve it. As #AlphaX has pointed out the best solution now is simply
li.menu-item:not(:last-child) {
margin-bottom: 3px;
}
OLD ANSWER
add:
margin: 0 0 3px 0;
to your #access li and move
background: #0f84e8; /* Show a solid color for older browsers */
to the #access a and take out the border-bottom. Then it will work
Here: http://jsfiddle.net/bpmKW/4/

You can use the margin property:
li.menu-item {
margin:0 0 10px 0;
}
Demo: http://jsfiddle.net/UAXyd/

There is a powerful feature of flex that allows for specifying space between every child without having to reference the "last-child" through gap. I find myself using more often than margin at this point:
ul {
display: flex;
flex-direction: column;
gap: 10px;
}
Example
li {
background: red;
}
ul {
background: silver;
display: flex;
flex-direction: column;
gap: 10px;
}
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>

Since you are asking for space between , I would add an override to the last item to get rid of the extra margin there:
li {
background: red;
margin-bottom: 40px;
}
li:last-child {
margin-bottom: 0px;
}
ul {
background: silver;
padding: 1px;
padding-left: 40px;
}
<ul>
<li>Item 1</li>
<li>Item 1</li>
<li>Item 1</li>
<li>Item 1</li>
<li>Item 1</li>
</ul>
The result of it might not be visual at all times, because of margin-collapsing and stuff... in the example snippets I've included, I've added a small 1px padding to the ul-element to prevent the collapsing. Try removing the li:last-child-rule, and you'll see that the last item now extends the size of the ul-element.

Most answers here are not correct as they would add bottom space to the last <li> as well, so they are not adding space ONLY in between <li> !
The most accurate and efficient solution is the following:
li.menu-item:not(:last-child) {
margin-bottom: 3px;
}
Explanation:
by using :not(:last-child) the style will be applie to all items (li.menu-item) but the last one.

Simple and fast. Just put css into ul element (the 'gap' property defines space between li elements):
display: flex;
align-items: flex-start;
flex-direction: column;
gap: 40px;

#access a {
border-bottom: 2px solid #fff;
color: #eee;
display: block;
line-height: 3.333em;
padding: 0 10px 0 20px;
text-decoration: none;
}
I see that you had used line-height but you gave it to <a> tag instead of <ul>
Try this:
#access ul {line-height:3.333em;}
You wouldn't need to play with margins then.

I just want to say guys:
Only Play With Margin
It is a lot easier to add space between <li> if you play with margin.

Related

How to center absolute dynamic width ul submenu?

I found a lot of posts about centering submenu <ul> absolute positioned, but none of them solved the problem of center the submenu that have dynamic width determined by the text length of the <li> children...
Most of those posts offer a solution based on the use of negative margin-left,
and this means that it can work only for a specific width, but not for dynamic width!
So I have prepared a quick FIDDLE HERE with a very basic menu,
please can you help me to figure out how is possible to automatically center submenus?
nav {
background-color: red;
}
ul {
background-color: rgb(88, 164, 228);
padding: 0;
margin: 0;
list-style-type: none;
}
li {
display: inline-block;
margin: 0 20px;
}
ul ul {
background: rgb(119, 193, 255);
position: absolute;
outline: 1px solid black;
}
ul ul li {
margin: 0;
display: block;
}
<nav>
<ul>
<li>Menu</li>
<li>Menu
<ul>
<li>aa aa aa aa</li>
<li>bb bb</li>
</ul>
</li>
<li>Menu
<ul>
<li>cc cc cc</li>
<li>dd dd dd dd dd</li>
<li>ee ee ee</li>
</ul>
</li>
<li>Menu
<ul>
<li>ff ff</li>
<li>gg gg</li>
</ul>
</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>

CSS Submenu as Wide as Its Parent

I am making a horizontal menu and sub menu (level 2) inside a wrapper. Please imagine this menu is on the top right of the page / wrapper. The problem is, since the sub menu is also horizontal it can (will) be too wide and will overflow outside the wrapper.
Here it is:
http://jsfiddle.net/5DWer/
There is "menu-wrapper" there, but it is not the wrapper I was referring above.
The wrapper is right after "Tab 3" so "Tab 3 sub 2" is outside the wrapper.
I think the solution is to have the second level menu to start at the same point below the first level so it will never flow outside the wrapper (assuming the first level is wide enough). In the fiddle link: "tab 3 sub 1" starts right below "tab 1". I can't just use margin-left or left because I don't know under which tab the sub menu will start.
Is this possible or is there other solution? If possible in pure CSS, but I'll take Javascript if it isn't.
Thanks in advance :)
Thanks for the explanation. Sorry, here is the code:
<div class="menu-wrapper">
<ul class="menu">
<li>tab 1</li>
<li>tab 2</li>
<li>tab 3</li>
<ul>
<li>tab 3 sub 1</li>
<li>tab 3 sub 2</li>
</ul>
</ul>
</div>
and the CSS
.menu-wrapper {
width: 100%;
}
.menu {
max-width: 450px;
float: right;
}
.menu li a,
.menu li {
display: inline-block;
text-decoration: none;
}
.menu li:hover > ul {
display: block;
}
.menu li ul {
display: none;
width: 404px;
position: absolute;
}
.menu li li{
width: 200px;
margin: 0;
}
.menu li ul ul {
top: 100%;
left: 50%;
width: 200px;
}
.menu ul li:hover > ul {
border-left: 0;
display: block;
}
.menu li ul li a {
display: block;
padding: 8px 10px;
padding: 0.571428571rem 0.714285714rem;
width: 180px;
width: 12.85714286rem;
white-space: normal;
}
here is the solution to your problem: (I added a 3rd subtab to show it works)
http://jsfiddle.net/5DWer/3/
However, like I mentioned in the fiddle as comment:
You have to manually specify the width of the second-level ul.
Also, you have to nest your second level properly, like this:
<div class="menu-wrapper">
<ul class="menu">
<li>tab 1</li>
<li>tab 2</li>
<li>tab 3
<ul>
<li>tab 3 sub 1</li>
<li>tab 3 sub 2</li>
</ul>
</li>
</ul>
</div>
and not outside the li.
For reference (jsfiddle code):
HTML:
<div class="menu-wrapper">
<ul class="menu">
<li>tab 1</li>
<li>tab 2</li>
<li>tab 3
<ul>
<li>tab 3 sub 1</li>
<li>tab 3 sub 2</li>
<li>tab 3 sub 3</li>
</ul>
</li>
</ul>
</div>
And CSS:
.menu-wrapper {
width: 100%;
}
.menu {
max-width: 450px;
float: right;
}
.menu li {
display: inline-block;
text-decoration: none;
font-family: Arial, sans-serif;
background-color: yellow;
width: 50px;
height: 20px;
text-align: center;
}
.menu li ul {
display: none;
width: 500px; /* caveat : you have to specify the width manually */
margin-top: 10px;
}
.menu li:hover ul {
display: block;
float: right;
}
.menu li ul li {
float: right;
background-color: orange;
height: 100%;
}
.menu li ul li a {
padding: 8px 10px;
padding: 0.571428571rem 0.714285714rem;
width: 180px;
width: 12.85714286rem;
white-space: normal;
}
Looking at your code, it seems to be operating as expected. It seems to me, from what I can see (given the float:right for example) that this is more of a ui/design problem than a code problem. If not, maybe you can provide further details on your actual design so I can provide a css solution.

How do i add a style to a specific list element, i.e. single <li> inside <nav> in this?

I have a problem regarding CSS styling for my list.
Here is the code.
CSS
NAV {
width: 940px;
height: 50px;
float: left;
font-family: Geneva,Arial;
border: 1px solid #000000;
background-color: #D0DBF0;}
NAV ul {
margin: 0px auto;
padding-top: 15px;
padding-left: 70px;
list-style-type: none;
}
NAV li {
display: inline;
}
NAV li a {
float: left;
text-align: center;
border-right: 2px solid #00DBF0;
width: 100px;
font-size: 14px;
padding: 0px 10px;
color: #0000FF;
text-decoration: none;
}
HTML
<NAV>
<UL>
<LI>Home</LI>
<LI>About Us</LI>
<LI>Contact Us</LI>
<LI>Red Widgets</LI>
<LI>Blue Widgets</LI>
<LI>Green Widgets</LI>
</UL>
</NAV>
So here i have designed everything for navigation list, but for the first list i.e home.
<LI>Home</LI> i want right border. please help me.
You can do it using the :first-child pseudo-class:
nav li:first-child
or
nav li:first-child a
depending on whether you want to target the list item (<li>) or anchor (<a>).
You should add a class, or id.
For example (Let's also assume later you want a "current selected" item):
CSS:
.first a { /* specific style for first item */ }
.current a { /* specific style for current item */}
HTML:
<NAV>
<UL>
<LI class="first">Home</LI>
<LI>About Us</LI>
<LI class="current">Contact Us</LI>
...
<!-- if the first item happens to be the curent one: -->
<LI class="first current">Home</LI>
</UL>
</NAV>
JsFiddle here

positioning, block size and background in a CSS drop down menu

This may be an easy one. I looked through previous questions (and other places on the web) but cannot find a good solution for my current problem.
I am trying to have a centered drop down menu with CSS based on a list. Nothing very complicated.
This one has a very simple solution for it, but I cannot find what I am doing wrong.
I have two problems at this point (it's mostly in the first list, have not really looked at the links in the list yet) :
(1) I would like the list coming down to have a background as a large rectangle that encompass all the items in the sub-list
(2) the items in the sub-list are "truncated", a new line is inserted so that the width does not exceed the width of the list title.
Thanks.
The CSS part
#navbar ul {
text-align: center;
width: 100%;
font-variant: small-caps;
padding: 5px 0;
margin-top: 0;
margin-left: 0;
}
#navbar ul li {
background-color: #ccc;
margin-right: 2%;
color: #069;
text-decoration: none;
position: relative;
display: inline;
padding: 5px 4px;
}
#navbar li a {
text-decoration: none; }
#navbar li ul {
display: none;
}
#navbar li:hover ul, #navbar li.hover ul {
display: block;
position: absolute;
text-align: left;
margin: 8px 0 0 0;
padding: 0;
background-color: #eee; }
#navbar li:hover li, #navbar li.hover li {
padding: 4px 0;
clear: left;
}
#navbar li:hover li a, #navbar li.hover li a {
background-color: #eee;
border-bottom: 1px solid #fff;
color: #000; }
#navbar li li a:hover {
background-color: #333; }
The HTML part
<div id="navbar">
<ul>
<li>
Link 1
<ul>
<li>item 1.1 and more</li>
<li>item 1.2</li>
<li>item 1.3</li>
<li>item 1.4 truncated?</li>
<li>item 1.5</li>
<li>item 1.6</li>
</ul>
</li>
<li>
Link 2
<ul>
<li>Link 2.1</li>
<li>Link 2.2</li>
<li>Link 2.3</li>
<li>Link 2.4</li>
<li>Link 2.5</li>
<li>Link 2.6</li>
</ul>
</li>
<li>
Link 3
<ul>
<li>Link 3.1</li>
<li>Link 3.2</li>
<li>Link 3.3</li>
<li>Link 3.4</li>
<li>Link 3.5</li>
<li>Link 3.6</li>
</ul>
</li>
</ul>
</div>
1) It looks like you already have a background rectangle. Do you just want more padding?
If so, add padding like so:
#navbar li:hover ul, #navbar li.hover ul {padding:10px}
2) As for truncating, try
#navbar li:hover ul, #navbar li.hover ul {word-wrap: break-word;}
I would however suggest you look take a look at this article, to help you with drop down menus: http://matthewjamestaylor.com/blog/centered-dropdown-menus
EDIT: Unfortunately, word-wrap is a CSS3 property, and is not supported by all browsers. Additionally, word wrap with cross browser CSS does not appear to be trivial. This post word wrap in css / js has some more information.

Is it possible to make a pure CSS Tree diagram?

I've made an attempt here. However, it has two problems:
IE
Last element of a list being a sublist
Is there a better way of doing this?
You didn't really go into detail on what your criteria are, but from what you have said, I'd suggest taking a look at SlickMap CSS.
Update: Got it! I just remembered where I'd seen what you're looking for:
jsTree is a jQuery plugin which creates a tree widget with the kind of styling you want and uses <ul> internally to represent it.
Try the code below, it's pure CSS and no JS/jQuery (which IMO is way more compatible), works by manipulating borders and spacing.
Works on FF/Chrome & IE. Enjoy :-)
.parent a,
li {
font-size: 22px;
color: #000;
font-family: "Source Sans Pro", sans-serif;
}
.child a,
li {
font-size: 18px;
color: #000;
}
.subchild a,
li {
font-size: 18px;
color: #888888;
}
.s2child a,
li {
font-size: 16px;
color: #ccc;
}
.tree,
.tree ul {
list-style-type: none;
margin-left: 0 0 0 10px;
padding: 0;
position: relative;
overflow: hidden;
}
.tree li {
margin: 0 0 0 15px;
padding: 0 12px 0 20px;
position: relative;
}
.tree li::before,
.tree li::after {
content: '';
position: absolute;
left: 0;
}
/* horizontal line on inner list items */
.tree li::before {
border-top: 2px solid #000;
top: 14px;
width: 15px;
height: 0;
}
/* vertical line on list items */
.tree li:after {
border-left: 2px solid #000;
height: 100%;
width: 0px;
top: -5px;
}
/* lower line on list items from the first level because they don't have parents */
.tree > li::after {
top: 15px;
}
/* hide line from the last of the first level list items */
.tree > li:last-child::after {
display: none;
}
/* hide line from before the Home or starting page or first element */
.tree > li:first-child::before {
display: none;
}
.tree ul:last-child li:last-child:after {
height: 20px;
}
.tree a:hover {
color: red;
font-weight: 500;
text-shadow: 1px 2px 2px #F3F3F3;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pure CSS Tree</title>
<link href="tree.css" rel="stylesheet" type="text/css">
</head>
<body>
<ul class="tree">
<li class="parent">Grand Parent
<ul>
<li class="child">Parent
</li>
<li class="child">Parent
<ul>
<li class="subchild">Child
<ul>
<li class="s2child">Grand Child
</li>
<li class="s2child">Grand Child
</li>
<li class="s2child">Grand Child
</li>
<li class="s2child">Grand Child
</li>
<li class="last s2child">Grand Child
</li>
</ul>
</li>
<li class="subchild">Child
<ul>
<li class="s2child">Grand Child
</li>
<li class="s2child">Grand Child
</li>
</ul>
</li>
</ul>
</li>
<li class="child">Parent
</li>
</ul>
</li>
</ul>
</body>
</html>

Resources