visit
The way we’ll go through each principle is fairly simple, the topic will be explained, and example shown, and a nice snippet will be provided so it’s clear how to apply it. For the sake of simplicity, we’ll use vanilla CSS.
Without further ado, let’s dive in.
<!-- Example One -->
<h2 class="title">Example One</h2>
<h3 class="subtitle">Assigns spacing to each element individually.</h3>
<div class="example-one">
<div class="example-one-element element-1"></div>
<div class="example-one-element element-2"></div>
<div class="example-one-element element-3"></div>
<div class="example-one-element element-4"></div>
<div class="example-one-element element-5"></div>
<div class="example-one-element element-6"></div>
<div class="example-one-element element-7"></div>
<div class="example-one-element element-8"></div>
<div class="example-one-element element-9"></div>
</div>
/* Example One */
.example-one {
display: grid;
grid-template-columns: repeat(3, 1fr);
margin-bottom: 100px;
}
.example-one-element {
width: 200px;
height: 200px;
background-color: blue;
}
.element-1 {
margin: 10px 45px;
}
.element-2 {
margin: 30px 5px;
}
.element-3 {
margin: 5px 45px;
}
.element-4 {
margin: 17px 10px;
}
.element-5 {
margin: 15px 15px;
}
.element-6 {
margin: 5px 5px;
}
.element-7 {
margin: 35px 35px;
}
.element-8 {
margin: 15px 45px;
}
.element-9 {
margin: 15px 35px;
}
The second example shows 12 red squares, spaced using the
grid-gap
property.<!-- Example Two -->
<h2 class="title">Example Two</h2>
<h3 class="subtitle">Assigns the same vertical and horizontal spacing.</h3>
<div class="example-two">
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
<div class="example-two-element"></div>
</div>
/* Example Two */
.example-two {
margin: 0 50px;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 50px;
}
.example-two-element {
width: 200px;
height: 200px;
background-color: red;
}
The third example shows ten green squares that have been spaced so that the vertical space is larger than the horizontal one, still, by keeping a clear and constant proportion, it looks nice. In this case, we're using
margin
for spacing. <!-- Example Three -->
<h2 class="title">Example Three</h2>
<h3 class="subtitle">Assigns more vertical than horizontal.</h3>
<div class="example-three">
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
<div class="example-three-element"></div>
</div>
/* Example Three */
.example-three {
margin: 0 50px;
display: grid;
grid-template-columns: repeat(5, 1fr);
}
.example-three-element {
width: 200px;
height: 200px;
background-color: green;
margin: 0 40px 160px 0;
}
The fourth example is similar to the third but we have more horizontal than vertical space. We used the
grid-row-gap
and grid-column-gap
properties for spacing. <!-- Example Four -->
<h2 class="title">Example Four</h2>
<h3 class="subtitle">Assigns more horizontal than vertical space.</h3>
<div class="example-four">
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
<div class="example-four-element"></div>
</div>
/* Example Four */
.example-four {
margin: 0 50px;
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-column-gap: 60px;
grid-row-gap: 30px;
}
.example-four-element {
width: 200px;
height: 200px;
background-color: purple;
}
Example 5
Shows little contrast between a light element and a light background.
Example 6
Shows little contrast between a dark element and a dark background.
Example 7
Shows great contrast between a light element and a dark background.
Example 8
Shows great contrast between a dark element and a dark background.
The core concept here is that if we use a typeface for a piece of text, say a
<p>
element, we use the same typeface for all, or at least most <p>
elements. On principle, we don’t use more than three different fonts unless we really have a great reason for it.Another great example of this is alignment, it likes commitment, so once we choose, we’re there for good.Same with colors, we should always resist the urge to have a thousand colors around, as fun as making great palettes is, simple is the word we need to remember, each color must serve a purpose. A color for text, a color for titles, a color for contrast and, yes, a color might serve more than one purpose. Lastly, the shape of elements is important, border-radius should be the same, or, as spacing, proportional. Same with shadows.Our ninth example shows how horrible things look when every element follows its own rules, and there is no uniformity. <!-- Example Nine -->
<h2 class="title">Example Nine</h2>
<h3 class="subtitle">Shows how irregular elements look bad.</h3>
<div class="example-nine">
<div class="example-nine-container-1">
<p class="example-nine-container-1-text">This says something neutral</p>
</div>
<div class="example-nine-container-2">
<p class="example-nine-container-2-text">This says something better</p>
</div>
<div class="example-nine-container-3">
<p class="example-nine-container-3-text">This says something worse</p>
</div>
</div>
/* Example Nine */
.example-nine {
width: 100%;
height: 300px;
background-color: rgb(205, 232, 248);
display: flex;
flex-direction: row;
}
.example-nine-container-1 {
background-color: lightblue;
width: 150px;
height: 75px;
text-align: right;
padding: 15px;
}
.example-nine-container-2 {
background-color: lightgreen;
width: 200px;
height: 200px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
text-align: center;
margin: 50px 0 20px 150px;
border-radius: 10%;
border: 3px solid green;
box-shadow: 0px 0px 7px 7px rgba(10, 107, 2, 0.3);
}
.example-nine-container-3 {
background-color: lightpink;
width: 800px;
height: 200px;
text-align: center;
border-radius: 2px;
margin-top: 50px;
margin-left: 70px;;
}
.example-nine-container-1-text {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 15px;
color: blue
}
.example-nine-container-2-text {
font-family: Arial, Helvetica, sans-serif;
font-size: 30px;
color: green;
}
.example-nine-container-3-text {
font-family: 'Times New Roman', Times, serif;
font-size: 60px;
color: purple
}
<!-- Example Ten -->
<h2 class="title">Example Ten</h2>
<h3 class="subtitle">Shows how much better the previous example looks when uniformity is applied.</h3>
<div class="example-ten">
<div class="example-ten-container">
<p class="example-ten-container-text">This says something amazing!</p>
</div>
<div class="example-ten-container">
<p class="example-ten-container-text">This says something amazing!</p>
</div>
<div class="example-ten-container">
<p class="example-ten-container-text">This says something amazing!</p>
</div>
<div class="example-ten-container">
<p class="example-ten-container-text">This says something amazing!</p>
</div>
</div>
/* Example Ten */
.example-ten {
width: 100%;
height: 300px;
background-color: rgb(169, 252, 206);
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
}
.example-ten-container {
background-color: lightgreen;
width: 200px;
height: 200px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
text-align: center;
border-radius: 10%;
border: 3px solid green;
box-shadow: 0px 0px 7px 7px rgba(10, 107, 2, 0.3);
}
.example-ten-container-text {
font-family: Arial, Helvetica, sans-serif;
font-size: 30px;
color: green;
}
I hope you find this article useful, and you can find all the code for all examples in this .
Happy coding!