CSS: Control space between bullet and <li>
HtmlCssHtml ListsHtml Problem Overview
I'd like to control how much horizontal space a bullet pushes its <li>
to the right in an <ol>
or <ul>
.
That is, instead of always having
* Some list text goes
here.
I'd like to be able to change that to be
* Some list text goes
here.
or
*Some list text goes
here.
I looked around but could only find instructions for shifting the entire block left or right, for example, http://www.alistapart.com/articles/taminglists/
Html Solutions
Solution 1 - Html
Put its content in a span
which is relatively positioned, then you can control the space by the left
property of the span
.
li span {
position: relative;
left: -10px;
}
<ul>
<li><span>item 1</span></li>
<li><span>item 2</span></li>
<li><span>item 3</span></li>
</ul>
Solution 2 - Html
To summarise the other answers here – if you want finer control over the space between bullets and the text in a <li>
list item, your options are:
(1) Use a background image:
<style type="text/css">
li {
list-style-type:none;
background-image:url(bullet.png);
}
</style>
<ul>
<li>Some text</li>
</ul>
Advantages:
- You can use any image you want for the bullet
- You can use CSS
background-position
to position the image pretty much anywhere you want in relation to the text, using pixels, ems or %
Disadvantages:
- Adds an extra (albeit small) image file to your page, increasing the page weight
- If a user increases the text size on their browser, the bullet will stay at the original size. It'll also likely get further out of position as the text size increases
- If you're developing a 'responsive' layout using only percentages for widths, it could be difficult to get the bullet exactly where you want it over a range of screen widths
2. Use padding on the <li>
tag
<style type="text/css">
ul {padding-left:1em}
li {padding-left:1em}
</style>
<ul>
<li>Some text</li>
</ul>
Advantages:
- No image = 1 less file to download
- By adjusting the padding on the
<li>
, you can add as much extra horizontal space between the bullet and the text as you like - If the user increases the text size, the spacing and bullet size should scale proportionally
Disadvantages:
- Can't move the bullet any closer to the text than the browser default
- Limited to shapes and sizes of CSS's built-in bullet types
- Bullet must be same colour as the text
- No control over vertical positioning of the bullet
(3) Wrap the text in an extra <span>
element
<style type="text/css">
li {
padding-left:1em;
color:#f00; /* red bullet */
}
li span {
display:block;
margin-left:-0.5em;
color:#000; /* black text */
}
</style>
<ul>
<li><span>Some text</span></li>
</ul>
Advantages:
- No image = 1 less file to download
- You get more control over the position of the bullet than with option (2) – you can move it closer to the text (although despite my best efforts it seems you can't alter the vertical position by adding
padding-top
to the<span>
. Someone else may have a workaround for this, though...) - The bullet can be a different colour to the text
- If the user increases their text size, the bullet should scale in proportion (providing you set the padding & margin in ems not px)
Disadvantages:
- Requires an extra unsemantic element (this will probably lose you more friends on SO than it will in real life ;) but it's annoying for those who like their code to be as lean and efficient as possible, and it violates the separation of presentation and content that HTML / CSS is supposed to offer)
- No control over the size and shape of the bullet
Here's hoping for some new list-style features in CSS4, so we can create smarter bullets without resorting to images or exta mark-up :)
Solution 3 - Html
This should do it. Be sure to set your bullets to the outside. you can also use CSS pseudo elements if you can drop them in IE7 downward. I don't really recommend using pseudo elements for this kinda thing but it does work to control distance.
ul {
list-style: circle outside;
width: 100px;
}
li {
padding-left: 40px;
}
.pseudo,
.pseudo ul {
list-style: none;
}
.pseudo li {
position: relative;
}
/* use ISO 10646 for content http://la.remifa.so/unicode/named-entities.html */
.pseudo li:before {
content: '\2192';
position: absolute;
left: 0;
}
<ul>
<li>Any Browser really</li>
<li>List item
<ul>
<li>List item</li>
<li>List item</li>
</ul>
</li>
</ul>
<ul class="pseudo">
<li>IE8+ only</li>
<li>List item
<ul>
<li>List item with two lines of text for the example.</li>
<li>List item</li>
</ul>
</li>
</ul>
Solution 4 - Html
Old question, but the :before pseudo element works well here.
<style>
li:before {
content: "";
display: inline-block;
height: 1rem; // or px or em or whatever
width: .5rem; // or whatever space you want
}
</style>
It works really well and doesn't require many extra rules or html.
<ul>
<li>Some content</li>
<li>Some other content</li>
</ul>
Cheers!
Solution 5 - Html
For list-style-type: inline:
It's almost the same like DSMann8's answer but less css code.
You just need to
<style>
li:before {
content: "";
padding-left: 10px;
}
</style>
<ul>
<li>Some content</li>
</ul>
Cheers
Solution 6 - Html
You can use the padding-left
attribute on the list items (not on the list itself!).
Solution 7 - Html
There seems to be a much cleaner solution if you only want to reduce the spacing between the bullet point and the text:
li:before {
content: "";
margin-left: -0.5rem;
}
Solution 8 - Html
Here is another solution using css counter and pseudo elements. I find it more elegant as it doesn't require use of extra html markup nor css classes :
ol,
ul {
list-style-position: inside;
}
li {
list-style-type: none;
}
ol {
counter-reset: ol; //reset the counter for every new ol
}
ul li:before {
content: '\2022 \00a0 \00a0 \00a0'; //bullet unicode followed by 3 non breakable spaces
}
ol li:before {
counter-increment: ol;
content: counter(ol) '.\00a0 \00a0 \00a0'; //css counter followed by a dot and 3 non breakable spaces
}
I use non breakable spaces so that the spacing only affects the first line of my list elements (if the list element is more than one line long). You could use padding here instead.
Solution 9 - Html
Using text-indent on li works best.
text-indent: -x px; will move the bullet closer to li and vice-versa.
Using relative on span the negative left might not work properly with older versions for IE. P.S- avoid giving positions as much as you can.
Solution 10 - Html
The following solution works well when you want to move the text closer to the bullet and even if you have multiple lines of text.
margin-right
allows you to move the text closer to the bullet
text-indent
ensures that multiple lines of text still line up correctly
li:before {
content: "";
margin-right: -5px; /* Adjust this to move text closer to the bullet */
}
li {
text-indent: 5px; /* Aligns second line of text */
}
<ul>
<li> Item 1 ... </li>
<li> Item 2 ... this item has tons and tons of text that causes a second line! Notice how even the second line is lined up with the first!</li>
<li> Item 3 ... </li>
</ul>
Solution 11 - Html
Old question but still relevant.
I recommend using negative text-indent
.
list-style-position
must be outside
.
Pros:
- Bullet is properly automatically positioned
- Change font size does not break the bullet position
- No extra elements ( no ::pseudo-elements too )
- Works for both RTL and LTR without change in CSS.
Cons:
- try
- to
- find
- one :)
Solution 12 - Html
It seems you can (somewhat) control the spacing using padding on the <li> tag.
<style type="text/css">
li { padding-left: 10px; }
</style>
The catch is that it doesn't seem to allow you to scrunch it way-snug like your final example.
For that you could try turning off list-style-type and using •
<ul style="list-style-type: none;">
<li>•Some list text goes here.</li>
</ul>
Solution 13 - Html
An unordered list starts with the ul tag. Each list item starts with the The list items will be marked with bullets (small black circles) by default:
<!DOCTYPE html>
<html>
<body>
<h2>Normal List </h2>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<body>
<h2>Normal List </h2>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>
The text-indent property specifies the indentation of the first line in a text-block.
Note: Negative values are allowed. The first line will be indented to the left if the value is negative.
<ul style='text-indent: -7px;'>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Solution 14 - Html
You can also use a background image replacement as an alternative, giving you total control over vertical and horizontal positioning.
See the answer to this Question
Solution 15 - Html
ul
{
list-style-position:inside;
}
Definition and Usage
The list-style-position property specifies if the list-item markers should appear inside or outside the content flow.
Source: http://www.w3schools.com/cssref/pr_list-style-position.asp
Solution 16 - Html
You can just give a padding-left
to your <li>
element.
Solution 17 - Html
More info w3schools
ul {
list-style: none;
}
ul li::before {
content: "\2022";
color: black;
display: inline-block;
width: 8px;
}
Solution 18 - Html
If you'd prefer not to add additional markup e.g. <span>
, you could use:
-
list-style-position: inside;
to move the list-style closer to your text from the left -
And if you still need to pull the list items closer you can use the
::before
selector on theli
element to move the text toward the bullet.
ul {
list-style-type: disc;
list-style-position: inside;
padding-left: 16px; /* or margin */
}
li::before {
content: '';
margin-left: -8px;
}
- List item text
- List item text
- List item text
- List item text
Solution 19 - Html
You can use ul li:before
and a background image, and assign position: relative
and position:absolute
to the li
and li:before
, respectively. This way you can create an image and position it wherever you want relative to the li.
Solution 20 - Html
Use padding-left:1em; text-indent:-1em
for the <li>
tag.
Then, list-style-position: inside
for the <ul>
tag.
<ul style='list-style-position: inside;'>
<li style='padding-left: 1em; text-indent: -1em;'>Item 1</li>
</ul>
Solution 21 - Html
If your list-style is inside
then you could remove the bullet and create your own ... e.g. (in scss!)
li {
list-style: none;
&:before {
content: '- ';
}
}
And if you list style is outside
then you could do something like this:
li {
padding-left: 10px;
list-style: none;
&:before {
content: '* '; /* use any character you fancy~! */
position: absolute;
margin-left: -10px;
}
}