When I try to center a <ul>
the text in the <li>
centers but the bullet points stay on the far left of the page. Is there any way to get the bullet points to stay with the text when it is centered?
#abc{text-align: center; }
<div id="section1">
<div id="abc">
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<div/>
<div/>
This question is related to
html
css
html-lists
Add list-style-position: inside
to the ul
element. (example)
The default value for the list-style-position
property is outside
.
ul {_x000D_
text-align: center;_x000D_
list-style-position: inside;_x000D_
}
_x000D_
<ul>_x000D_
<li>one</li>_x000D_
<li>two</li>_x000D_
<li>three</li>_x000D_
</ul>
_x000D_
Another option (which yields slightly different results) would be to center the entire ul
element:
.parent {_x000D_
text-align: center;_x000D_
}_x000D_
.parent > ul {_x000D_
display: inline-block;_x000D_
}
_x000D_
<div class="parent">_x000D_
<ul>_x000D_
<li>one</li>_x000D_
<li>two</li>_x000D_
<li>three</li>_x000D_
</ul>_x000D_
</div>
_x000D_
ul {
padding-left: 0;
list-style-position: inside;
}
Explanation:
The first property padding-left: 0
clears the default padding/spacing for the ul
element while list-style-position: inside
makes the dots/bullets of li
aligned like a normal text.
So this code
<p>The ul element</p>
<ul>
asdfas
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
without any CSS will give us this:
but if we add in the CSS give at the top, that will give us this:
Here's how you do it.
First, decorate your list this way:
<div class="p">
<div class="text-bullet-centered">⁕</div>
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
</div>
<div class="p">
<div class="text-bullet-centered">⁕</div>
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
</div>
Add this CSS:
.p {
position: relative;
margin: 20px;
margin-left: 50px;
}
.text-bullet-centered {
position: absolute;
left: -40px;
top: 50%;
transform: translate(0%,-50%);
font-weight: bold;
}
And voila, it works. Resize a window, to see that it indeed works.
As a bonus, you can easily change font and color of bullets, which is very hard to do with normal lists.
.p {_x000D_
position: relative;_x000D_
margin: 20px;_x000D_
margin-left: 50px;_x000D_
}_x000D_
_x000D_
.text-bullet-centered {_x000D_
position: absolute;_x000D_
left: -40px;_x000D_
top: 50%;_x000D_
transform: translate(0%, -50%);_x000D_
font-weight: bold;_x000D_
}
_x000D_
<div class="p">_x000D_
<div class="text-bullet-centered">⁕</div>_x000D_
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text_x000D_
text text text text text text text text text text text text text_x000D_
</div>_x000D_
<div class="p">_x000D_
<div class="text-bullet-centered">⁕</div>_x000D_
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text_x000D_
text text text text text text text text text text text text text_x000D_
</div>
_x000D_
I found the answer today. Maybe its too late but still I think its a much better one. Check this one https://jsfiddle.net/Amar_newDev/khb2oyru/5/
Try to change the CSS code : <ul> max-width:1%; margin:auto; text-align:left; </ul>
max-width:80% or something like that.
Try experimenting you might find something new.
You can do that with list-style-position: inside;
on the ul
element :
ul {
list-style-position: inside;
}
Source: Stackoverflow.com