On a page, we tend to have sections. Sometimes these sections have different background colors. For these, you want the padding between the sections to be uniform so there is consistency and a feeling of order.
There’s an easy way to set this up with a few classes:
sectionsection-minisection-cont
The Section Utility
In a section utility, I tend to add a padding-block to create the uniform spaces between sections, as I mentioned before. This is very intuitive and shouldn’t be a problem for you.
.section { padding-block: var(--spacing-section);}Then creating the sections with different background colors is a simple matter of adding the background color to the section in the HTML.
<section class="section bg-blue-300">…</section><section class="section bg-purple-300">…</section>Just like with wrappers, I realized that the padding can and should change at different breakpoints to make them look more right — based on the size of the viewport available.
Doing this is simple because we can just change the --spacing-section CSS variable.
:root { --spacing-section: 3rlh;
@media (width >= 800px) { --spacing-section: 4rlh; }}Mini Sections
Some sections — like a strip of logos — don’t contain much content and shouldn’t occupy the same amount of whitespace.
These sections should have smaller paddings. I create this through a utility called section-mini.
<section class="section bg-blue-300">Normal</section><section class="section-mini bg-purple-300">Mini</section>And I start with a default of giving it half the whitespace given to the section utility. To make CSS variables work with Tailwind theme values, we can include an inline property to the theme declaration.
@theme inline { --spacing-section: 2.5rlh; --spacing-section-mini: calc(var(--spacing-section) * 0.5);}
.section-mini { padding-block: var(--spacing-section-mini);}Continuous sections
Sometimes you want two sections of the same color, but they should not have double the amount of padding between them.
For situations like this, I created the section-cont (short for continuous) utility.
<section class="section bg-blue-300">Normal</section><section class="section-cont bg-blue-300">Continuous</section>In this case, the padding between these two sections should equal one full section-padding value. So I set padding-top of the 2nd section block to 0.
.section-cont { padding-block: var(--spacing-section-cont);
.section + &, & + & { padding-block-start: 0; }}Here’s an example of this at work. (Dotted borders for illustration purposes).
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sed asperiores suscipit minus doloribus adipisci, mollitia similique maxime impedit perspiciatis debitis obcaecati nisi nobis assumenda quidem! Quos ipsa fuga voluptate distinctio!
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sed asperiores suscipit minus doloribus adipisci, mollitia similique maxime impedit perspiciatis debitis obcaecati nisi nobis assumenda quidem! Quos ipsa fuga voluptate distinctio!
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson