EDIT: I'm adding an image to better convey the desired result:
[![enter image description here][1]][1]
Here is the fiddle which will make the explanation clear: https://jsfiddle.net/c7jazc9z/2/
I have an unordered list with items left-aligned. I'd like it to be centered on screen based on the longest item. This is easily achieved with display:inline-block.
However, I also want to set max-width on this list, and that's where the issue arises. When list items wrap (due to limited width), empty space occurs on the right side.
This then looks like the list is not centered below the headline (although it technically is).
How to solve this? (Use of any other HTML elements is fine)
body {
background: gray;
margin: 100px 0;
text-align: center;
}
div {
max-width: 300px;
margin: 0 auto;
}
ul {
background: pink;
display: inline-block;
list-style-type: none;
padding: 0;
text-align: left;
}
li {
font-size: 24px;
}
<div>
<h1>Headline</h1>
<ul>
<li>This is a the longest line, at least here.</li>
<li>This one's short.</li>
<li>And this one so and so.</li>
</ul>
</div>