Removing default styling from HTML list elements - html

So I have some code that looks like:
<ul>
<li>
<ul>
<li> ... </li>
</ul>
</li>
</ul>
This has indented itself. I have no styling to indent this. According to the computed styling there is no margin-left, yet everything is actually indented, I guess this is the default behaviour of nested ul elements?
Regardless, on every nested ul, I have a class that is called comment-children I need to say only 5 down can indent (so .comment-children .comment-children .comment-children .comment-children .comment-children done, great) but at a width of 640px, all nesting must be turned off.
The part I am having the trouble with is that the ul elements are nested by default http://jsfiddle.net/d7az0jv3/
What do you want to do
Remove all default nesting and let me nest it my self via the class comment-children
At 640px remove all nesting.

Your example is insufficient to demonstrate what you want to do involving the class comment-children, but generally, to remove the indentation on lists across browsers, you should implement the rules
ul, li { margin-left: 0; padding-left: 0; }
Here's an updated jsfiddle
If you want to only nest elements up to a certain level, my recommendation would be to apply a class to the base ul that sets the indentation, and then add a rule that stops the indentation at a certain depth below that base class. Here is an updated version of your code with the nesting stopping at level 5.
HTML:
<ul class="comment">
<li>level one</li>
<li>
<ul>
<li>level two</li>
<li>
<ul>
<li>level three</li>
(etc., up to level seven)
CSS:
ul {
list-style:none;
}
ul, li { /* reset the margin and padding */
margin: 0;
padding: 0;
}
.comment ul {
/* 1 em margin for the UL elements under .comment */
margin-left: 1em;
}
.comment ul ul ul ul ul {
/* stop the nesting! */
margin-left: 0;
}
jsfiddle for this

Related

Styling CSS Unordered Lists

Is it possible to style an unordered list so that the second line and the ones after that are indented the same as the first line of the list item?
Please see the example for exactly what I mean
O----First Line
--SECOND LINE SHOULD START HERE
--EVERY OTHER LINE SHOULD BE LIKE THIS ALSO
Just to supplement my comment, here is a jsfiddle demonstrating what I mentioned. http://jsfiddle.net/R5ptL/
<ul>
<li>Parent</li>
<ul>
<li>Child1</li>
<li>Child2</li>
<li>Child3</li>
</ul>
<li>Parent2</li>
</ul>
And if you want them to be the same style...
ul, li {
list-style-type: circle; /* or whatever style you choose */
}
EDIT: How to do this with multiple unordered lists AND CSS only: http://jsfiddle.net/R5ptL/1/
use the css first-child selector to apply the indent to every line other than the first.
ex:
ul li:first-child{margin:0px;}
ul li{margin:5px;}
li:not(first-child) {
margin-left: 20px;
}
or
li {
margin-left: 20px;
}
li:first-child {
margin-left: 0;
}
It's like this: (HTML solution, not CSS)
<ul>
<li> first item </li>
<li> second item
<ul>
<li>first item of second list</li>
<li>second</li>
</ul>
</li>
<li> continue primary list </li>
</ul>
In short, you nest a complete new UL inside the primary UL.
My first answer was apparently incorrect after further testing. This should work though:
ul li {
text-indent:-10px;
margin-left:10px;
}
NOTE: This answer runs under the assumption that every line other than the first is simply wrapped text. If those other lines are meant to be sub-points, go with gwin003's answer.

CSS Child Selector (>) not working with certain properties

When I'm trying to select all direct child of a parent element using ">", it works with some properties like border and all, but not with font-properties like color, font-weight etc..
My HTML is
<ul>
<li>Item 1</li>
<li>
<ol>
<li>Subitem 2A</li>
<li>Subitem 2B</li>
</ol>
</li>
</ul>
CASE1 CSS:
ul>li {
color:#F00;
}
But here the color:#F00 property gets applied to all the "li" elements, But i want it to get applied only for the direct "li"s of "ul".
CASE 2
CSS:
ul>li {
border: solid 1px #000;
}
This one works well for me and the border gets applied only to the direct li child only.
I know it can be resolved by overriding with some other classes and all. But i want to know, why some css properties get inherited and others not.
It's happening due to the default inheritance capability of certain CSS Properties. Values of these kind of properties will be transmitted to the child by default.
This document from W3C gives detailed list of inheritance in various CSS properties. Full property table
try this
Demo
<ul>
<li>Item 1</li>
<li>
<ol>
<li>Subitem 2A</li>
<li>Subitem 2B</li>
</ol>
</li>
</ul>
css
ul > li {
color:#F00;
}
ul > li > ol > li {
color:#000;
}
try this
ul > li ol li {color:black;}
As the listing element has been inheriting the color property from its parent, you need to override it.
You can add below style before yours as like
li {
color: #000;
}
ul>li {
color:#F00;
}
It overrides the color: inherit value.
I think you might find the answer you need here: http://www.w3schools.com/cssref/sel_firstchild.asp
You should be able to select these elements with
ul:first-child {
// css
}
Hope this helps

How to remove indentation from an unordered list item?

I want to remove all indentation from ul. I tried setting margin, padding, text-indent to 0, but no avail. Seems that setting text-indent to a negative number does the trick - but is that really the only way to remove the indentation?
Set the list style and left padding to nothing.
ul {
list-style: none;
padding-left: 0;
}​
ul {
list-style: none;
padding-left: 0;
}
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
To maintain the bullets you can replace the list-style: none with list-style-position: inside or the shorthand list-style: inside:
ul {
list-style-position: inside;
padding-left: 0;
}
ul {
list-style-position: inside;
padding-left: 0;
}
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
My preferred solution to remove <ul> indentation is a simple CSS one-liner:
ul { padding-left: 1.2em; }
<p>A leading line of paragraph text</p>
<ul>
<li>Bullet points align with paragraph text above.</li>
<li>Long list items wrap around correctly. Long list items wrap around correctly. Long list items wrap around correctly. Long list items wrap around correctly. Long list items wrap around correctly. </li>
<li>List item 3</li>
</ul>
<p>A trailing line of paragraph text</p>
This solution is not only lightweight, but has multiple advantages:
It nicely left-aligns <ul>'s bullet points to surrounding normal paragraph text (= indenting of <ul> removed).
The text blocks within the <li> elements remain correctly indented if they wrap around into multiple lines.
Legacy info:
For IE versions 8 and below you must use margin-left instead:
ul { margin-left: 1.2em; }
Add this to your CSS:
ul { list-style-position: inside; }
This will place the li elements in the same indent as other paragraphs and text.
Ref: http://www.w3schools.com/cssref/pr_list-style-position.asp
display:table-row; will also get rid of the indentation but will remove the bullets.
Remove the padding:
padding-left: 0;
Can you provide a link ?
thanks
I can take a look
Most likely your css selector isnt strong enough or can you try
padding:0!important;
Live demo: https://jsfiddle.net/h8uxmoj4/
ol, ul {
padding-left: 0;
}
li {
list-style: none;
padding-left: 1.25rem;
position: relative;
}
li::before {
left: 0;
position: absolute;
}
ol {
counter-reset: counter;
}
ol li::before {
content: counter(counter) ".";
counter-increment: counter;
}
ul li::before {
content: "●";
}
Since the original question is unclear about its requirements, I attempted to solve this problem within the guidelines set by other answers. In particular:
Align list bullets with outside paragraph text
Align multiple lines within the same list item
I also wanted a solution that didn't rely on browsers agreeing on how much padding to use. I've added an ordered list for completeness.
I have the same problem with a footer I'm trying to divide up. I found that this worked for me by trying few of above suggestions combined:
footer div ul {
list-style-position: inside;
padding-left: 0;
}
This seems to keep it to the left under my h1 and the bullet points inside the div rather than outside to the left.
Doing this inline, I set the margin to 0 (ul style="margin:0px"). The bullets align with paragraph with no overhang.
The following worked for me.
In Chrome, Edge and Firefox (which needs special treatment).
Bullets are kept and are on the same line with surrounding paragraphs.
ul {
padding-left: 0;
margin-left: 17px;
}
/* Different value for Firefox */
#-moz-document url-prefix() {
ul {
margin-left: 14px;
}
}

A List Inside A Horizontal List

I'm experimenting with a design. It seems I can't get my CSS right. I have a horizontal list with a list in each of it's list items. The nested list doesn't seem to behave properly. What am I doing wrong here?
http://jsfiddle.net/89sqw/
The nested list doesn't have the squared list type, and the margin is all wrong.
Everything you apply to #tfList li is valid to your nested list items too (unless you override). Also, you shouldn't have two elements with the same id, use classes instead.
Several prolems:
1) As mentioned by #bfavaretto, you can't have multiple elements with the same ID
2) You aren't closing your "P" tags. Closing tags have a slash (</p>)
3) You are using display: inline on an element which will contain block elements. This is invalid not good practice and will likely give you problems. Use inline-block instead:
#some-item {
display: inline-block;
vertical-align: top;
*display: inline;
*zoom: 1;
}
Edit: Tip - you can use special "child" selectors to only select immediate children of an element. http://jsfiddle.net/ryanwheale/F3cqD/
<ul>
<li>
Level 1
<ul>
<li>Level 2</li>
<li>Level 2</li>
</ul>
</li>
<li>Level 1</li>
</ul>
And these styles
ul > li {
color: red;
}
ul > li > ul > li {
color: green;
}
The issue is easy to miss, but it comes from having overridden the display property of #tfList li elements to display: inline, and then mistakenly trying to re-set it to display: block;.
The correct display for a list item is:
display: list-item;
Also, to get the spacing back on the list items, set the left padding on the ul element.
Fiddle

Nested list with left aligned "bullets"

I have a requirement to display a hierarchy of projects. The obvious solution is a series of nested unordered lists. However, the problem I need to overcome is that the node value needs to be left aligned across the entire tree with the project name being indented as expected in a tree. Here is a sample
Project Root Node
1 Task 1
1.1 Travel
1.2 Do Work
2 Task 2
2.1 Perform Testing
2.1.1 UI Testing
2.1.2 Connection Testing
The markup is fairly simple...
<ul>
<li><span></span>Project Root Node
<ul>
<li><span>1</span>Task 1
<ul>
<li><span>1.1</span>Travel</li>
<li><span>1.2</span>Do Work</li>
</ul>
</li>
...and so on...
Turning the bullets off is easy. Left aligning every li is easy. But the problem is getting the span to stay to the left while the text after the span is properly indented and aligned for that level.
The closest I could get it was that since the span for each level has the same number of characters I could just add a right margin and that would push the following text over. The problem is that since the characters in the span vary slightly in width, the text could be one or two pixels off in vertical alignment with the row above or below.
The other solution I had was to include a "level number" class when rendering the li element along with an appropriate style that set inline-block and width. My problem with that is making sure I defined enough levels to cover any tree depth.
an ordered list with a bit of counters magic, no IE7 and below, though it degrades to an indented numbered list.
CSS
ol {
counter-reset: item;
padding: 0;
margin: 0;
margin-left: 20px !ie7;
}
ul {margin: 0; padding: 0; list-style: none;}
li {
display: block;
}
ol li:before {
display: inline-block;
content: counters(item, ".") " ";
counter-increment: item;
width: 50px;
}
ol li li:before {width: 70px;}
ol li li li:before {width: 90px;}
ol li li li li:before {width: 110px;}
and the HTML:
<ul>
<li>Project Root Node
<ol>
<li>Task 1
<ol>
<li>Travel (1.1)
<ol>
<li>Travel (1.1.1)</li>
<li>Do Work (1.1.2)</li>
</ol>
</li>
<li>Do Work (1.2)</li>
</ol>
</li>
<li>Task 2
<ol>
<li>Travel (2.1)</li>
<li>Do Work (2.2)</li>
</ol>
</li>
<li>Task 3
<ol>
<li>Travel (3.1)</li>
<li>Do Work (3.2)</li>
</ol>
</li>
</ol>
</li>
</ul>
adjust the widths of the :before psuedo elements to create the indented text
updated to make project node not numbered and include IE7 remargin
UPDATE
link to JSFiddle containing a samle of code using dls (definition lists) instead bearing in mind the need for IE7 support
JSFiddle using DL
This might be a little "hackish," but what I would do is use the nested unordered lists, as you have them, and place the spans in there as well with classes like list-level-1, list-level-2, and so on. For JavaScript-disabled browsers, it'll display as a normal nested list. Using jQuery or another JavaScript library, convert the lists into series of <div>s (with two classes like list-item-number and list-item-level-1, list-item-level-2). Set the margin-left for the latter to appropriate widths.
<div class="row"><div class="list-item-number">1</div><div class="list-item-level-1">Fruits</div></div>
<div class="row"><div class="list-item-number">1.1</div><div class="list-item-level-2">Apples</div>
<div class="row"><div class="list-item-number">1.1.1</div><div class="list-item-level-3">Red Delicious</div>
<div class="row"><div class="list-item-number">1.1.2</div><div class="list-item-level-3">Fuji</div>
You could even use a table to a similar effect (and superior semantic meaning, in my opinion), but I have had bad luck with tables and jQuery.
For a pure HTML/CSS solution, your best option is to use a table. Remember that tables aren't inherently bad -- they're just bad for layout.
To keep everything aligned correctly, I would take the spans out of normal flow by floating them and give them a negative margin. This will cause them not to affect the layout of the text unless they actually run into it. You'll need a rule for each level of the list, but that's just kind of endemic to nested lists in CSS*.
ul {
padding: 0;
margin: 0;
list-style-type: none;
}
li {
margin-left:1em;
padding-left: 0;
}
li li span {
display: block;
float: left;
margin-left: -2em;
}
li li li span { margin-left: -3em; }
I just tested it in the horribly broken IE 5 for Mac and it worked perfectly, so I assume newer versions should be OK, unlike solutions using pseudo-elements and generated content (which are stylistically superior but break in Explorer <8).
* Repetitive CSS like this can be trivially generated — a quick Ruby one-liner would be (2..10).each {|n| puts("#{'li '*n}span {margin-left: -#{n}em}")}, with 10 replaced by the number of levels