With the following HTML, what is the easiest method to display the list as two columns?
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
Desired display:
A B
C D
E
The solution needs to work with Internet Explorer.
Modern Browsers
leverage the css3 columns module to support what you are looking for.
http://www.w3schools.com/cssref/css3_pr_columns.asp
CSS:
ul {
columns: 2;
-webkit-columns: 2;
-moz-columns: 2;
}
http://jsfiddle.net/HP85j/8/
Legacy Browsers
Unfortunately for IE support you will need a code solution that involves JavaScript and dom manipulation. This means that anytime the contents of the list changes you will need to perform the operation for reordering the list into columns and reprinting. The solution below uses jQuery for brevity.
http://jsfiddle.net/HP85j/19/
HTML:
<div>
<ul class="columns" data-columns="2">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
</ul>
</div>
JavaScript:
(function($){
var initialContainer = $('.columns'),
columnItems = $('.columns li'),
columns = null,
column = 1; // account for initial column
function updateColumns(){
column = 0;
columnItems.each(function(idx, el){
if (idx !== 0 && idx > (columnItems.length / columns.length) + (column * idx)){
column += 1;
}
$(columns.get(column)).append(el);
});
}
function setupColumns(){
columnItems.detach();
while (column++ < initialContainer.data('columns')){
initialContainer.clone().insertBefore(initialContainer);
column++;
}
columns = $('.columns');
}
$(function(){
setupColumns();
updateColumns();
});
})(jQuery);
CSS:
.columns{
float: left;
position: relative;
margin-right: 20px;
}
EDIT:
As pointed out below this will order the columns as follows:
A E
B F
C G
D
while the OP asked for a variant matching the following:
A B
C D
E F
G
To accomplish the variant you simply change the code to the following:
function updateColumns(){
column = 0;
columnItems.each(function(idx, el){
if (column > columns.length){
column = 0;
}
$(columns.get(column)).append(el);
column += 1;
});
}
I was looking at #jaider's solution which worked but I'm offering a slightly different approach that I think is more easy to work with and which I've seen to be good across browsers.
ul{
list-style-type: disc;
-webkit-columns: 2;
-moz-columns: 2;
columns: 2;
list-style-position: inside;//this is important addition
}
By default un-ordered list display the bullet position outside but then in some browsers it would cause some display problems based on the browser's way of laying out your website.
To get it to display in the format:
A B
C D
E
etc. use the following:
ul li{
float: left;
width: 50%;//helps to determine number of columns, for instance 33.3% displays 3 columns
}
ul{
list-style-type: disc;
}
This should solve all your problems with displaying columns. All the best and thanks #jaider as your response helped to guide me to discover this.
I tried posting this as a comment, but couldn't get the columns to display right (as per your question).
You are asking for:
A B
C D
E
... but the answer accepted as the solution will return:
A D
B E
C
... so either the answer is incorrect or the question is.
A very simple solution would be to set the width of your <ul> and then float and set the width of your <li> items like so
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
ul{
width:210px;
}
li{
background:green;
float:left;
height:100px;
margin:0 10px 10px 0;
width:100px;
}
li:nth-child(even){
margin-right:0;
}
Example here http://jsfiddle.net/Jayx/Qbz9S/1/
If your question is wrong, then the previous answers apply (with a JS fix for lacking IE support).
Now days, for the expected result, display:grid; would do (be the easiest ?):
ul {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
you can also get the columns shrinking on the left and able to have different width:
ul {
display: grid;
grid-template-columns: repeat(2, auto);
justify-content: start;
}
li {
margin-left: 1em;
border: solid 1px;/*see me */
}
<ul>
<li>A</li>
<li>B</li>
<li>C 123456</li>
<li>D</li>
<li>E</li>
</ul>
I like the solution for modern browsers, but the bullets are missing, so I add it a little trick:
http://jsfiddle.net/HP85j/419/
ul {
list-style-type: none;
columns: 2;
-webkit-columns: 2;
-moz-columns: 2;
}
li:before {
content: "• ";
}
Here's a possible solution:
Snippet:
ul {
width: 760px;
margin-bottom: 20px;
overflow: hidden;
border-top: 1px solid #ccc;
}
li {
line-height: 1.5em;
border-bottom: 1px solid #ccc;
float: left;
display: inline;
}
#double li {
width: 50%;
}
<ul id="double">
<li>first</li>
<li>second</li>
<li>third</li>
<li>fourth</li>
</ul>
And it is done.
For 3 columns use li width as 33%, for 4 columns use 25% and so on.
This is the simplest way to do it. CSS only.
add width to the ul element.
add display:inline-block and width of the new column (should be less than half of the ul width).
ul.list {
width: 300px;
}
ul.list li{
display:inline-block;
width: 100px;
}
<ul class="list">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
You can use CSS only to set two columns or more
A E
B
C
D
<ul class="columns">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
ul.columns {
-webkit-columns: 60px 2;
-moz-columns: 60px 2;
columns: 60px 2;
-moz-column-fill: auto;
column-fill: auto;
}
This can be achieved using column-count css property on parent div,
like
column-count:2;
check this out for more details.
How to make floating DIV list appear in columns, not rows
You can do this really easily with the jQuery-Columns Plugin for example to split a ul with a class of .mylist you would do
$('.mylist').cols(2);
Here's a live example on jsfiddle
I like this better than with CSS because with the CSS solution not everything aligns vertically to the top.
more one answer after a few years!
in this article: http://csswizardry.com/2010/02/mutiple-column-lists-using-one-ul/
HTML:
<ul id="double"> <!-- Alter ID accordingly -->
<li>CSS</li>
<li>XHTML</li>
<li>Semantics</li>
<li>Accessibility</li>
<li>Usability</li>
<li>Web Standards</li>
<li>PHP</li>
<li>Typography</li>
<li>Grids</li>
<li>CSS3</li>
<li>HTML5</li>
<li>UI</li>
</ul>
CSS:
ul{
width:760px;
margin-bottom:20px;
overflow:hidden;
border-top:1px solid #ccc;
}
li{
line-height:1.5em;
border-bottom:1px solid #ccc;
float:left;
display:inline;
}
#double li { width:50%;}
#triple li { width:33.333%; }
#quad li { width:25%; }
#six li { width:16.666%; }
In updateColumns() need if (column >= columns.length) rather than if (column > columns.length) to list all elements (C is skipped for example) so:
function updateColumns(){
column = 0;
columnItems.each(function(idx, el){
if (column >= columns.length){
column = 0;
}
console.log(column, el, idx);
$(columns.get(column)).append(el);
column += 1;
});
}
http://jsfiddle.net/e2vH9/1/
With Bootstrap... This answer (https://stackoverflow.com/a/23005046/1128742) got me pointed towards this solution:
<ul class="list-unstyled row">
<li class="col-xs-6">Item 1</li>
<li class="col-xs-6">Item 2</li>
<li class="col-xs-6">Item 3</li>
</ul>
http://jsfiddle.net/patrickbad767/472r0ynf/
The legacy solution in the top answer didn't work for me because I wanted to affect multiple lists on the page and the answer assumes a single list plus it uses a fair bit of global state. In this case I wanted to alter every list inside a <section class="list-content">:
const columns = 2;
$("section.list-content").each(function (index, element) {
let section = $(element);
let items = section.find("ul li").detach();
section.find("ul").detach();
for (let i = 0; i < columns; i++) {
section.append("<ul></ul>");
}
let lists = section.find("ul");
for (let i = 0; i < items.length; i++) {
lists.get(i % columns).append(items[i]);
}
});
This worked for me.
is the best solution because the cells in a row have the same height without taking care of the content
ul {
display: grid;
grid-template-columns: repeat(2, 1fr);
list-style-position: inside;
list-style-type: none;
}
li { border: solid 1px; }
<ul>
<li>asdasd asdad asdasd asdasd adasdasdasd adasdadadada adadas dasdadasdasd asd aA</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
Assuming that a multi-column layout may work well with a centered layout, justify a grid layout with a horizontal gap.
p {
text-align: justify;
}
ul {
display: grid;
grid-template-columns: repeat(3, auto);
justify-content: space-around;
gap: 0 3em;
text-transform: capitalize;
}
<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.</p>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
<li>five</li>
<li>six</li>
<li>seven</li>
<li>eight</li>
<li>nine</li>
<li>ten</li>
</ul>
<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.</p>
Though I found Gabriel answer to work to a degree i did find the following when trying to order the list vertically (first ul A-D and second ul E-G):
When the ul had an even number of li's in it, it was not evenly spreading it across the ul's
using the data-column in the ul didn't seem to work very well, I had to put 4 for 3 columns and even then it was still only spreading the li's into 2 of the ul generated by the JS
I have revised the JQuery so the above hopefully doesn't happen.
(function ($) {
var initialContainer = $('.customcolumns'),
columnItems = $('.customcolumns li'),
columns = null,
column = 0;
function updateColumns() {
column = 0;
columnItems.each(function (idx, el) {
if ($(columns.get(column)).find('li').length >= (columnItems.length / initialContainer.data('columns'))) {
column += 1;
}
$(columns.get(column)).append(el);
});
}
function setupColumns() {
columnItems.detach();
while (column++ < initialContainer.data('columns')) {
initialContainer.clone().insertBefore(initialContainer);
column++;
}
columns = $('.customcolumns');
updateColumns();
}
$(setupColumns);
})(jQuery);
.customcolumns {
float: left;
position: relative;
margin-right: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<ul class="customcolumns" data-columns="3">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li>H</li>
<li>I</li>
<li>J</li>
<li>K</li>
<li>L</li>
<li>M</li>
</ul>
</div>
Here is an easy way to make a multiple column list using nothing more than simple CSS. The style tags can obviously be put into a CSS if desired.
<p>Materials List</p>
<ul style="display: inline-block; float: left">
<u>Item Description<u>
<li>1/2" x 4' wood dowel</li>
<li>1/2" x 12" PVC pipe</li>
<li>1/2" PVC pipe end cap</li>
<li>7/16" x 3" light duty expansion spring</li>
<li>6" plastic zip ties</li>
<li>Light weight antenna</li>
</ul>
<div style="display: inline-block; margin-left: 1em">
<u>Qty</u>
<div style="text-indent: 0.5em">3</div>
<div style="text-indent: 0.5em">1</div>
<div style="text-indent: 0.5em">1</div>
<div style="text-indent: 0.5em">2</div>
<div style="text-indent: 0.5em">8</div>
<div style="text-indent: 0.5em">1</div>
</div>
<p></p>
Thisd was a perfect solution for me, looking it for years:
http://css-tricks.com/forums/topic/two-column-unordered-list/
Related
Here is a simple html + css code:
https://jsfiddle.net/bxn72cyk/
The problem here is ul list doesn't look nice if one element has a different height than others.
How to fix this list no matter of each height with a css?
ul {
list-style: none;
}
li {
border: 1px solid red;
float: left;
padding: 10px;
line-height: 20px;
width: 30%;
}
.different {
height: 19px;
background-color: green;
}
<ul>
<li>a</li>
<li>d</li>
<li class="different">smaller height</li>
<li>set it from the left</li>
<li>d</li>
<li>d</li>
<li>d</li>
<li>d</li>
<li>d</li>
</ul>
P.S. Setting li {min-height: 20px} is one of the possible solutions, but it's not applicable. Any other options? It's not applicable as the actual size of each button could be even smaller than 20px under some circumstances.
I have been trying to figure out how to put space in between a numbered list and the text and still have the text wrap nicely under the line above it. I found some script on Stack Overflow that works but for some reason I can only get bullets, no numeric's.
Thanks.
ul {
margin: 0;
padding: 50;
}
/* The wider the #list_wrapper is, the more columns will fit in it */
#list_wrapper {
width: 1000px
}
/* The wider this li is, the fewer columns there will be */
ul.multiple_columns li {
text-align: right;
float: right;
list-style: decimal;
height: 30px;
width: 400px;
}
<form id="form1" runat="server">
<div>
<div id="list_wrapper">
<ul class="column-count: 2;">
<li>One: If I knew how to spell the ABC's I would write and write and write and write and write and write and write some more and write and write and write and write and write and write and write.</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
<li>Seven</li>
<li>Eight</li>
<li>Nine</li>
</ul>
</div>
</div>
</form>
Change anything ul (unordered list) to ol (ordered list).
CSS becomes:
ol{
margi... }
ol.multiple_columns li{
text-align: right;
float: ri... }
HTML becomes:
<ol class="column-count: 2;">
<li>One: If I knew ...</li>
<li>Two</li>
...
</ol>
In order to make the list numbers bold, but not the text, you would change the ol class in your CSS to include font-weight:900;:
ol {
margin: 0;
padding: 50;
font-weight: 900;
}
Then wrap the text of your <li> to include <span style="font-weight:normal;"></span>, like so:
<li><span style="font-weight:normal;">One: If I knew how to spell ... </span></li>
Without the span returning the text to a normal weight, the number and text will both be bold. BTW, font-weight ranges from 100-900. 900 is the heaviest weight available.
I hope someone can explain this to me and offer a solution..
I created a fiddle to demonstrate the issue and to explain the question
http://jsfiddle.net/s6ow1oq3/
Here is the question..
When elements such as <li> or <div> are floated left and they are set to a particular width but they don't/can't have a fixed height,,, how can I insure that element that breaks into new row gets aligned to the beginning of the next row?
In the fiddle example the second example demonstrates the issue while the first one is desired.
Here is the fiddle code too:
CSS:
li {
list-style: none;
}
#one li, #two li {
width: 22%;
float: left;
margin: 7px;
padding: 7px;
border:1px solid #DDD;
}
.clearfix {
clear: both;
float: none;
}
HTML:
<!-- Unordered List Examples -->
<div id="one">
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
<div class="clearfix">
</div>
<hr/>
<div id="two">
<ul>
<li>a</li>
<li>b</li>
<li>c1<br/>c2<br/>c3<br/>c4</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
<div class="clearfix">
</div>
Assuming it's always 4 columns, you could use li:nth-child(4n + 1) to clear the first element on every row. For example:
li:nth-child(4n + 1) {
clear: both;
}
jsFiddle: http://jsfiddle.net/s6ow1oq3/2/
Use display: inline-block; - the most under-appreciated display type IMO.
The main thing you can't do is have 100% width coverage, but since your example doesn't require the items to butt up against one another, it's a perfect use case for inline-block
The trick is to make sure that all of your width's and margins add up to roughly 99% instead of 100%.
http://jsfiddle.net/ryanwheale/s6ow1oq3/3/
#one li, #two li {
display: inline-block;
vertical-align: top;
width: 22%;
margin: 1.2%;
padding: 7px;
border:1px solid #DDD;
}
I have these nested ul and li . When i fill background color, nested li leaves indented portion white. I have a number of li like this that gets filled from database so i cannot give margin left to individual text in li . How can i do this so that background fills whole line along with the indentation?
Right now it looks like this
I want it like this
Any suggestions how can do this? Thanks in advance. I cannot change the html markup as i'll have to change a lot of code. Is there a way to do this using this markup. these li are coming from db query so i dont have exact number of li in this case.
Demo http://jsbin.com/uReBEVe/1/
By default, <ul> has padding-left to accomodate the bullet point.
If you add this to your CSS:
ul {padding-left:0}
ul>li {padding-left:40px}
You should get the effect you want.
EDIT: Also you need to correct your HTML :p <ul> can ONLY have <li> as children.
Best thing to do is to use a structure which makes it easy for database management , html and styling(CSS) .
HTML:
<body>
<ul class="main">
<li>1.</li>
<li><ul>2</ul></li>
<li><ul><li><ul>3.</ul></li></ul></li>
</ul>
</body>
CSS:
.main{
position:relative;
right:40px;
}
li{
list-style:none;
background:red;
margin-top:1px;
}
Fiddle 1.
I dont know if ul not containing li is valid or invalid.If its invalid then you can use:
<body>
<ul class="main">
<li>1.</li>
<li><ul><li>2</li></ul></li>
<li><ul><li><ul><li>3.</li></ul></li></ul></li>
</ul>
</body>
Fiddle 2
Flexible, Multi-Level Nesting Solution
This is very similar to another question I answered here, and I've composed a similar solution for you below. You will want valid html by having all nested li elements inside their own ul (as others have noted here), and it would be best to control all this by some class on the outermost ul (though that is not required, but makes targeting this list a whole lot easier).
The key here is supplying the background through the :before pseudo-element, which is made to span the whole width of the outermost ul.
Here is my demo jsbin.
HTML
<ul class="full-width-list">
<li>A</li>
<li>
<ul>
<li>B</li>
<li>
<ul>
<li>B</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
.full-width-list {
position: relative;
padding: 0 0 0 4px;
}
.full-width-list ul {
margin: 0;
padding: 0
}
.full-width-list li {
list-style:none;
padding: 0;
margin: 0;
height: 1.2em;
line-height: 1.2em;
}
.full-width-list ul > li {
margin-top: 4px;
padding: 0 0 0 36px;
}
.full-width-list li:first-child:before {
content: '';
height: 1.2em;
position: absolute;
left: 0;
right: 0;
z-index: -1;
background:red;
}
.full-width-list li:first-child:hover:before {
background:green
}
Limitations
This solution has two main limitations:
None of the ul or li elements can have a position other than the default static set on them, as the :before pseudo-element of the li elements needs to have its only positioned parent be the .full-width-list element.
There has to be a set height on the li items. In my example I use 1.2em, but whatever height you set, it means that the li elements cannot go to two or more lines of text (so this solution only works with a single line of text).
You can do this with :before hack as you have no access to the code
Working jsBin Demo
CSS
li{list-style:none;background:red;margin-top:4px; }
li:hover{background:green}
li:hover:before {background:green}
li:before {background:red; width:100%; content:'.'; position:absolute; left:0; z-index: -1;}
This works at arbitrary depths without hacks or nonsense.
The people saying "can't" and "impossible" in this thread really need to learn what those words mean with respect to CSS (generally, "haven't figured out how yet.") :)
The idea is simple: set a :before selector which fits the left and right edges by absolute positioning and paints a background color. You need to set a z-index: to put it behind its content, a content: '\0020' to force it to paint (that's a non-breaking space,) and you're good.
You can bound this by setting it inside a position: relative container.
<!doctype html>
<html>
<head>
<style>
li {
list-style-type : none;
margin-bottom : 0.25em;
}
li:before {
position : absolute;
left : 0;
right : 0;
background-color : #eef;
content : "\00a0";
z-index : -1;
}
</style>
</head>
<body>
<ul>
<li>Test</li>
<li><ul>
<li>Test</li>
<li><ul>
<li>Test</li>
</ul></li>
</ul></li>
</ul>
</body>
</html>
Your markup is broken, you should nest li in a single ul like this:
<ul>
<li>Text</li>
<li>Text 1</li>
</ul>
This was your markup
<ul>
<li>A</li>
<ul>
<li>B</li>
<ul>
<li>B</li>
</ul>
</ul>
<ul>
I assume you see why this is wrong.
I've fixed the JSBin for you and it has the correct effect.
EDIT: You could of course add the padding-left by looping over all lis using javascript.
You could not be sure enough about browser consistency until markup cleanup and consistency, sad but true. All the suggestions from above looks good, there is bit of alternative from my practical view.
The markup:
<ul>
<li>A</li>
<li><p>B</li>
<li><p><p>B</li>
<li><p><p><p>B</li>
....
</ul>
And CSS:
li p {
padding-left: 1em;
display: inline;
}
JSbin
p tag is optional to close in HTML subset, and generally should works in every browser anyway no matter of doctype. In case you are on xHTML and worry about validation an option could be using closing tags like:
<ul>
<li>A</li>
<li><p></p>B</li>
<li><p></p><p></p>B</li>
....
</ul>
Try this:
<ul class="lvl1">
<li>A</li>
<ul class="lvl2"><li>B</li>
<ul class="lvl3"><li>B</li></ul>
</ul>
</ul>
li {
background: none repeat scroll 0 0 #FF0000;
list-style: none outside none;
margin-top: 4px;
}
ul { padding:0px;}
ul.lvl1>li {padding-left:30px;}
ul.lvl2>li {padding-left:60px;}
ul.lvl3>li {padding-left:90px;}
See here: http://jsfiddle.net/x5K4a/
1) Your HTML is invalid (missing <li> around <ul>)
2) The only way to make indentation work as you expected is a CSS rule for each level.
ul ul li.line { padding-left: 20px !important }
ul ul ul li.line { padding-left: 40px !important; }
...
http://jsbin.com/uReBEVe/12/edit
if it is just a matter of background-color, you can use a shadow of same color.
http://codepen.io/gc-nomade/pen/fxBAl (html structure fixed)
<ul class="ulparent">
<li>
<p>A</p>
<ul>
<li>
<p>B</p>
<ul>
<li>
<p>B</p></li>
</ul>
</li>
</ul>
</li>
</ul>
.ulparent {overflow:hidden;}
li p {background:green;box-shadow:-200px 0 0 green;/* -200px for instance or whatever suits your need */margin:4px 0;}
li p:hover {background:red;box-shadow:-200px 0 0 red;}
Else, if it is a background-image, i would use pseudo-element and background-attachment:fixed;(demo included in codepen , using a linear-gradient as image )
I am going to give you the proper idea how to apply css rules over the HTML contents.Below the css rules I have created just copy it and see the answer.It is the child combinator which I used!I inspect whole the answers provided by the different users which is not followed the css rules at all. Just let me know! Hope the answer!
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style type="text/css">
li{
list-style:none;
background:red;
margin-top:4px;
}
body>ul>ul>li{
margin: 4px 0 0 -40px;
}
body>ul>ul>ul>li{
margin: 4px 0 0 -80px;
}
body>ul>ul>li {
padding:0px 0px 0px 40px;
}
body>ul>ul>ul>li{
padding:0px 0px 0px 80px;
}
li:hover{
background:green;
}
</style>
</head>
<body>
<ul>
<li>A</li>
<li>
<ul>
<li>B</li>
<li>
<ul>
<li>C</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
Save the image at first to your local drive or drag and drop this image into the new tab browser to see more visible.
Here is the proper HTML structure that you should follow, with each UL element having two LI elements. One for the value of each line and one as the parent for the next indented value.
<ul>
<li>A</li>
<li>
<ul>
<li>B</li>
<li>
<ul>
<li>C</li>
</ul>
</li>
</ul>
<li>
</ul>
For the CSS, this solution requires you to have a max number of 'levels' in your list hierarchy (see code comment)
li {
list-style:none;
padding-left:0px;
box-sizing:border-box;
}
ul {
padding-left:0
}
ul > li:nth-of-type(1):hover {
background:green
}
ul li:nth-of-type(1) {
padding-left:50px;
background:red;
margin-top:4px
}
ul li li:nth-of-type(1) {
padding-left:100px;
}
ul li li li:nth-of-type(1) {
padding-left:150px;
}
/*
Continue incrementing the padding for the li
children for however many levels you want
*/
Make note, the nth-of-type selector is supported by all browsers EXCEPT for IE8 and below.
See JSBin for working example: http://jsbin.com/uReBEVe/51
Good luck!
Both UL and OL inherit margins. Your fix would be to zero out the margin:
ul, ol
{
margin:0;
}
You can add this CSS in your code to get your desired results:
li {
list-style: none;
background: red;
margin-top: 4px;
}
ul {
padding: initial !important;
}
ul ul li {
padding-left: 40px;
}
ul ul ul li {
padding-left: 80px;
}
li:hover {
background: green;
}
Result on jsbin is here: http://jsbin.com/uReBEVe/33/edit
#AsrafulHaque has the correct idea about using padding to extend the background width without changing nesting indents.
However, because you don't know how many < li> there will be, you can't expect this to be a pure CSS solution.
You're attempting to do a pretty awkward thing but it would be possible to loop over them and inject dynamic padding using javascript/jquery or something:
i = 40;
$('img.yourImageClass').each(function() {
$(this).css('padding-left', i+'px');
i = i + 40;
});
You could also do this type of injection with pre-processing on the server side I am sure, but definitely not with CSS alone. You need a dynamic solution (i.e. the ability to use variables) to support your dynamic output.
A very very fiddly jsfiddle but it works with a little nudge in the right direction from jQuery. Not a great resolve but a resolve none the less.
HTML
<ul>
<li>A</li>
<ul>
<li>B</li>
<ul>
<li>B</li>
</ul>
</ul>
</ul>
CSS
ul {
list-style-type:none;
margin-top:5px;
padding-left:40px;
float:left;
width:400px;
overflow:hidden;
background:#ff0000;
}
li {
padding-top:5px;
}
ul div {
position:absolute;
left:0;
width:100%;
border-top:3px solid #fff;
}
jQuery
$(document).ready(function(){
$('ul').prepend('<div></div>');
});
jsFiddle here. Hopefully this works for you!
You can do like this
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<ul class="mainUL">
<li>A</li>
<ul><li>B</li>
<ul><li>C</li></ul>
</ul>
</ul>
</body>
</html>
CSS Code
li{list-style:none;background:red;margin-top:4px; }
li:hover{background:green}
li:hover:before {background:green}
li:before {background:red; width:100%; content:'.'; position:absolute;left:0; z-index: -1;color:red;}
.mainUL {padding-left: 0px;}
You can see the working demo : http://jsbin.com/uReBEVe/71/edit
from your demo:
if you apply
ul{
padding:0;
margin:0;
}
everything sits flush to the wall like you want.
if you want text indents
ul ul li{
text-ident:20px;
}
which is nesting. will only targets li's that are in ul's that are nested in ul's. then what you want works and you don't need to change your code
you can also keep nesting that code
add more ul's and li's depending on the depth of your structure, but this should give you a very good base
Is it possible to style this html ...
<ul>
<li>Dogs</li>
<li>Cats</li>
<li>Lions</li>
<li>Tigers</li>
<li>Zebras</li>
<li>Giraffes</li>
<li>Bears</li>
<li>Hippopotamuses</li>
<li>Antelopes</li>
<li>Unicorns</li>
<li>Seagulls</li>
</ul>
... like this ...
... without adding classes to specific list items, or resorting to javascript? And if so how?
The line breaks are not fixed; the list widens to take up additional space, and list items are center aligned.
Just
li + li::before {
content: " | ";
}
Of course, this does not actually solve the OP's problem. He wants to elide the vertical bars at the beginning and end of lines depending on where they are broken. I will go out on a limb and assert that this problem is not solvable using CSS, and not even with JS unless one wants to essentially rewrite the browser engine's text-measurement/layout/line breaking logic.
The only pieces of CSS, as far as I can see, that "know" about line breaking are, first, the ::first-line pseudo element, which does not help us here--in any case, it is limited to a few presentational attributes, and does not work together with things like ::before and ::after. The only other aspect of CSS I can think of that to some extent exposes line-breaking is hyphenation. However, hyphenating is all about adding a character (usually a dash) to the end of lines in certain situations, whereas here we are concerned about removing a character (the vertical line), so I just can't see how to apply any kind of hyphenation-related logic, even with the help of properties such as hyphenate-character.
We have the word-spacing property, which is applied intra-line but not at line beginnings and endings, which seems promising, but it defines the width of the space between words, not the character(s) to be used.
One wonders if there's some way to use the text-overflow property, which has the little-known ability to take two values for display of overflow text at both left and right, as in
text-overflow: '' '';
but there still doesn't seem to be any obvious way to get from A to B here.
This is possible with flex-box
The keys to this technique:
A container element set to overflow: hidden.
Set justify-content: space-between on the ul (which is a flex-box) to force its flex-items to stretch to the left and right edges.
Set margin-left: -1px on the ul to cause its left edge to overflow the container.
Set border-left: 1px on the li flex-items.
The container acts as a mask hiding the borders of any flex-items touching its left edge.
.flex-list {
position: relative;
margin: 1em;
overflow: hidden;
}
.flex-list ul {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
margin-left: -1px;
}
.flex-list li {
flex-grow: 1;
flex-basis: auto;
margin: .25em 0;
padding: 0 1em;
text-align: center;
border-left: 1px solid #ccc;
background-color: #fff;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" rel="stylesheet"/>
<div class="flex-list">
<ul>
<li>Dogs</li>
<li>Cats</li>
<li>Lions</li>
<li>Tigers</li>
<li>Zebras</li>
<li>Giraffes</li>
<li>Bears</li>
<li>Hippopotamuses</li>
<li>Antelopes</li>
<li>Unicorns</li>
<li>Seagulls</li>
</ul>
</div>
Before showing the code, it's worth mentioning that IE8 supports :first-child but not :last-child, so in similar situations, you should use the :first-child pseudo-class.
Demo
#menu{
list-style: none;
}
#menu li{
display: inline;
padding: 0 10px;
border-left: solid 1px black;
}
#menu li:first-child{
border-left: none;
}
<ul id="menu">
<li>Dogs</li>
<li>Cats</li>
<li>Lions</li>
<li>More animals</li>
</ul>
Use :after pseudo selector. Look http://jsfiddle.net/A52T8/1/
<ul>
<li>Dogs</li>
<li>Cats</li>
<li>Lions</li>
<li>Tigers</li>
<li>Zebras</li>
<li>Giraffes</li>
<li>Bears</li>
<li>Hippopotamuses</li>
<li>Antelopes</li>
<li>Unicorns</li>
<li>Seagulls</li>
</ul>
ul li { float: left; }
ul li:after { content: "|"; padding: 0 .5em; }
EDIT:
jQuery solution:
html:
<div>
<ul id="animals">
<li>Dogs</li>
<li>Cats</li>
<li>Lions</li>
<li>Tigers</li>
<li>Zebras</li>
<li>Giraffes</li>
<li>Bears</li>
<li>Hippopotamuses</li>
<li>Antelopes</li>
<li>Unicorns</li>
<li>Seagulls</li>
<li>Monkey</li>
<li>Hedgehog</li>
<li>Chicken</li>
<li>Rabbit</li>
<li>Gorilla</li>
</ul>
</div>
css:
div { width: 300px; }
ul li { float: left; border-right: 1px solid black; padding: 0 .5em; }
ul li:last-child { border: 0; }
jQuery
var maxWidth = 300, // Your div max-width
totalWidth = 0;
$('#animals li').each(function(){
var currentWidth = $(this).outerWidth(),
nextWidth = $(this).next().outerWidth();
totalWidth += currentWidth;
if ( (totalWidth + nextWidth) > maxWidth ) {
$(this).css('border', 'none');
totalWidth = 0;
}
});
Take a look here. I also added a few more animals. http://jsfiddle.net/A52T8/10/
I know I'm a bit late to the party, but if you can put up with having the lines left-justified, one hack is to put the pipes before the items and then put a mask over the left edge, basically like so:
li::before {
content: " | ";
white-space: nowrap;
}
ul, li {
display: inline;
}
.mask {
width:4px;
position: absolute;
top:8px; //position as needed
}
more complete example:
http://jsbin.com/hoyaduxi/1/edit
This should solve the problem without using borders.
li {
display: inline-block !important;
list-style: none;
margin: 0 auto;
height: 14px;
}
ul li::after {
content: " | ";
margin: 0 10px;
}
ul li:last-child:after {
content: '';
margin: 0 10px;
}
<div>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
<li>Seven</li>
<li>Eight</li>
<li>Nine</li>
<li>Ten</li>
<li>Eleven</li>
<li>Twelve</li>
<li>Thirteen</li>
<li>Fourteen</li>
<li>Fifteen</li>
<li>Sixteen</li>
<li>Seventeen</li>
<li>Eighteen</li>
<li>Nineteen</li>
<li>Twenty</li>
<li>Twenty One</li>
<li>Twenty Two</li>
<li>Twenty Three</li>
<li>Twenty Four</li>
<li>Twenty Five</li>
<li>Twenty Six</li>
<li>Twenty Seven</li>
<li>Twenty Eight</li>
<li>Twenty Nine</li>
<li>Thirty</li>
</ul>
</div>
One solution is to style the left border like so:
li { display: inline; }
li + li {
border-left: 1px solid;
margin-left:.5em;
padding-left:.5em;
}
However, this may not give you desirable results if the entire lists wraps, like it does in your example. I.e. it would give something like:
foo | bar | baz
| bob | bill
| judy
I came across a solution today that does not appear to be here already and which seems to work quite well so far. The accepted answer does not work as-is on IE10 but this one does.
http://codepen.io/vithun/pen/yDsjf/ credit to the author of course!
.pipe-separated-list-container {
overflow-x: hidden;
}
.pipe-separated-list-container ul {
list-style-type: none;
position: relative;
left: -1px;
padding: 0;
}
.pipe-separated-list-container ul li {
display: inline-block;
line-height: 1;
padding: 0 1em;
margin-bottom: 1em;
border-left: 1px solid;
}
<div class="pipe-separated-list-container">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
<li>Seven</li>
<li>Eight</li>
<li>Nine</li>
<li>Ten</li>
<li>Eleven</li>
<li>Twelve</li>
<li>Thirteen</li>
<li>Fourteen</li>
<li>Fifteen</li>
<li>Sixteen</li>
<li>Seventeen</li>
<li>Eighteen</li>
<li>Nineteen</li>
<li>Twenty</li>
<li>Twenty One</li>
<li>Twenty Two</li>
<li>Twenty Three</li>
<li>Twenty Four</li>
<li>Twenty Five</li>
<li>Twenty Six</li>
<li>Twenty Seven</li>
<li>Twenty Eight</li>
<li>Twenty Nine</li>
<li>Thirty</li>
</ul>
</div>
Slightly modified SCSS version which gives you control of the pipe | size and will eliminate padding from first and last list items while respects borders.
$pipe-list-height: 20px;
$pipe-list-padding: 15px;
.pipe-list {
position: relative;
overflow: hidden;
height: $pipe-list-height;
> ul {
display: flex;
flex-direction: row;
> li {
position: relative;
padding: 0 $pipe-list-padding;
&:after {
content: " ";
position: absolute;
border-right: 1px solid gray;
top: 10%;
right: 0;
height: 75%;
margin-top: auto;
margin-bottom: auto;
}
&:first-child {
padding-left: 0;
}
&:last-child {
padding-right: 0;
&:after {
border-right: none;
}
}
}
}
}
<div class="pipe-list">
<ul>
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
Yes, you'll need to use pseudo elements AND pseudo selectors: http://jsfiddle.net/cYky9/
You can use the following CSS to solve.
ul li { float: left; }
ul li:before { content: "|"; padding: 0 .5em; }
ul li:first-child:before { content: ""; padding: 0; }
Should work on IE8+ as well.