The styling of nested lists with mixed or multiple child elements isn’t great. If you have only a single paragraph of text or a single nested list item within a parent list item then there’s no issue, but as soon as you have more than one child element the styling starts to get a bit unpredictable. This is most noticeable when you have nested lists along with paragraph text in the same parent list item, but also affects things like code blocks and quotes.
This has come up as a support issue (for example How to insert subitem into list without breaking layout? and Nested indenting issue).
The issue is the explicit lack of a bottom margin on child lists and paragraphs regardless of whether there are succeeding elements. So for example, this:
- List item
- Child list item
More list item text
- List item
Is rendered as:
List item
- Child list item
More list item text
List item
And as an image for posterity:
This is because the first "List item" text is wrapped in a <p>
tag that has a bottom margin, whereas the child list item and "Child item text" has no bottom margin.
The offending CSS rules (found in "all.css") are:
ul ul, ol ul, ul ol, ol ol {
margin-bottom: 0;
}
.post-text ul p:last-of-type,
.wmd-preview ul p:last-of-type,
.post-text ol p:last-of-type,
.wmd-preview ol p:last-of-type {
margin-bottom: 0;
}
Now, we don’t want all nested lists to have no bottom margin and we don’t the last paragraph to have no bottom margin unless it is the last element in that list item, not just the last paragraph.
The simple solution is to remove both of those declarations and replace with:
ul :last-child, ol :last-child {
margin-bottom: 0;
}
That gives us no bottom margin on the last child element of anything inside lists, which would also cover the last list item itself, meaning we could get rid of this (also in "all.css") too:
.post-text ul li:last-child,
.wmd-preview ul li:last-child,
.post-text ol li:last-child,
.wmd-preview ol li:last-child {
margin-bottom: 0;
}
Just for reference, this is how the previous example looks with the improved CSS:
Maybe I’m overlooking some other case where those rules are important or maybe they're used elsewhere and simply removing them isn't the best option but at least change the rule affecting paragraphs from :last-of-type
to :last-child
and stop removing the bottom margin on all nested lists...
This is also a problem with non-nested lists when multiple paragraphs are used. This:
- List item
Second paragraph for item
- List item
- List item
is rendered like this:
List item
Second paragraph for item
List item
List item
as an image for posterity:
The second paragraph of the first item shouldn't be visually closer to the second item than it is to the preceding paragraph in the first item.
TL;DR Better nested list spacing please.
s-prose
component in Stacks. Some context: meta.stackexchange.com/questions/353446/new-post-formatting