I am exploring ways to improve the flow of my headers by adjusting their indentation based on their parent element. For example, I want H1 to have no indent, but if H2 follows H1, then indent it slightly. The same goes for H3 following H1 or H2, and so on, creating a cascading effect. Additionally, I would like to apply an indent to the content after each header tag. After conducting research, I attempted to achieve this using the following CSS code:
h1 {margin-left:0em;}
h1 ~ *:not(h1) {margin-left: 1em;}
h2 ~ *:not(h2) {margin-left: 2em;}
However, this solution falls short when another H1 is introduced, as demonstrated below:
Heading 1
Heading 2.1
Paragraph 2.1.1
Paragraph 2.1.2
Paragraph 2.1.3
Heading 2.2
Paragraph 2.2.1
Paragraph 2.2.2
Paragraph 2.2.3
Heading 1.2 <this is wrong???
Heading 2.1
Paragraph 2.1.1
Paragraph 2.1.2
Paragraph 2.1.3
Heading 2.2
Paragraph 2.2.1
Paragraph 2.2.2
Paragraph 2.2.3
I am aiming to accomplish this without relying on JavaScript, only utilizing CSS. Is there a way to achieve this effectively?