Targeting first paragraph after a header?

Is it possible to target just the first paragraph after a header? The following code works to change the first letter of each paragraph to a different style, but I would like to just change the first paragraph, not any successive ones.

the css:

.markdown-preview-view p::first-letter {
font-family: ‘Sancreek’;
color: #761213;
font-size: 25pt;
border: solid #761213;
line-height:100%;
padding-top: 3px;
padding-bottom: 3px;
padding-right: 3px;
padding-left: 3px;
margin-right: 8px;
margin-bottom: -8px;
float:left;
}

If I try a h1 + p::first-letter instead then it doesn’t affect anything

The reason why it’s not working with direct selectors like h1 + p is because Obsidian wraps the H and P tags inside DIV tags, so you have to target their parent DIV tags instead based on what kind of children tags they have.

So it’s a bit more lengthy but definitely possible (I also refactored your code since it was redundant and also made it clearer by grouping related properties):

.markdown-preview-view div:has(h1, h2, h3, h4, h5, h6) + div:has(> p) p::first-letter
{
	float: left;
	padding-block: 3px; padding-inline: 3px;
	margin-right: 8px; margin-bottom: -8px;

	font: 25pt 'Sancreek';
	line-height: 100%;

	border: solid #761213;
	color: #761213;
}

Or if you prefer things more readable and also add styling to various parts, here’s my personal favorite approach using the newer CSS nesting feature:

.markdown-preview-view div:has(h1, h2, h3, h4, h5, h6) {
	& + div:has(> p) {
		& p::first-letter {
				float: left;
				padding-block: 3px; padding-inline: 3px;
				margin-right: 8px; margin-bottom: -8px;

				font: 25pt 'Sancreek';
				line-height: 100%;

				border: solid #761213;
				color: #761213;
		}
	}
}

Love your styling btw :eyes:

P.S. if you don’t want to lose your sanity while doing CSS styling, try using shorthand properties where possible, group related properties together and get rid of redundant ones like “padding-left/right” that can be replaced with “padding-inline”, or “-top/-bottom” with “-block”, similarly with margins, etc. (or even a short form padding: 3px for all sides at once)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.