21.04.2022
CSS utility classes assist you to increase your page’s SEO compatibility and performance.
I’m sure most of you first met utility classes with frameworks the same as I did. It allows you to design a page without passing your CSS document every time and keeping in mind the values such as padding, margin, and color of your growing website. It surely makes the coding process much easier.So how come it helps a page’s SEO performance?
The most important advantage of using the utility class over the inline style is that it reduces loading time on the browser by making the page load more HTML-oriented. Also, it simply allows you to avoid using frameworks by easily designing your page with your own custom utility classes. That way your page loads the code from local files instead of downloading from a CDN. These two are important factors to increase your page’s SEO compatibility. If your page downloads every file and code from the web during loading, that would cause a negative effect on search engines and your page would appear downwards in search results. Obviously, if your project would mostly depend on SEO performance, that would be unacceptable.
CSS allows you to specify properties in your page for a long time with root property. By using root, you can create your own utility classes very easily.
In the first step, you need to specify some properties in the root and create var by using them. Here is my example:
:root {
--green: #61BE6B;
--text-main: #456278;
--bg-grey: #abbdd9;
--bg-orange: #f0c22b;
--color-main: #6b0e22;
--color-secondary: #693a04;
--div-height: 400px;
--inner-height: 200px;
}
.green-bg {
background-color: var(--green);
}
.grey-bg {
background-color: var(--bg-grey);
}
.color-main {
color: var(--color-main);
}
.color-secondary {
color: var(--color-secondary);
}
As you can see, I specify some properties in the root and create variables based on them.
After that, I create a simple HTML layout:
<div>
<div>
<p>
Lorem ipsum dolor sit amet.
</p>
<p>
Sed ut perspiciatis unde omnis iste.
</p>
</div>
</div>
And I use my variables as custom utility classes and design my layout.
<div class="green-bg div-height">
<div class="grey-bg inner-height">
<p>
Lorem ipsum dolor sit amet.
</p>
<p>
Sed ut perspiciatis unde omnis iste.
</p>
</div>
</div>
I give both divs their own widths. The second thing is that I want to align them to the center with flex. For be able to do this, I add new properties in CSS as below:
.w-100 {
width: 100%;
}
.w-50 {
width: 50%;
}
.d-flex {
display: flex;
}
.align-center {
align-items: center;
}
.justify-center {
justify-content: center;
}
New view of my layout:
<div class="w-100 green-bg div-height d-flex align-center justify-center">
<div class="w-50 grey-bg inner-height">
<p>
Lorem ipsum dolor sit amet.
</p>
<p>
Sed ut perspiciatis unde omnis iste.
</p>
</div>
</div>
It’s easier to give separate properties for each p element by adding different utility classes for every one of them.
.text-center {
text-align: center;
}
.fontsize-1 {
font-size: 36px;
}
.fontsize-2 {
font-size: 18px;
}
As you can see below, each p has its own font size.
<div class="w-100 green-bg div-height d-flex align-center justify-center bg-color-mobile">
<div class="w-50 grey-bg inner-height">
<p class="text-center fontsize-1">
Lorem ipsum dolor sit amet.
</p>
<p class="fontsize-2">
Sed ut perspiciatis unde omnis iste.
</p>
</div>
</div>
You can also practically create your responsive design by specifying necessary classes in media queries. That helps you to minimalize your scrolling along with the documents, especially when your page gets grower.
For example, I want to create a different background color in 768px width. To add that property, first I want to specify it as a media query in CSS:
@media (max-width: 768px) {
.bg-color-mobile {
background-color: var(--bg-orange);
}
}
To make it solid, I also have to add a new property to my root which I’ve written at the beginning:
--bg-orange: #f0c22b;
@media (max-width: 768px) {
.bg-color-mobile {
background-color: var(--bg-orange);
}
}
Now our layout has different background colors on different page widths.
In my last example, I want to have two different divs in a row. When these divs are going to be next to each other in desktop view, they will look up and down in mobile view. To make it, first I have to add display flex and flex-direction properties.
--bg-blue: #144973;
--bg-yellow: #f0c22b;
/* two divs has their own bg colors */
.blue-bg {
background-color: var(--bg-blue);
}
.yellow-bg {
background-color: var(--bg-yellow);
}
/* flex properties */
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
<div class="w-100 div-height d-flex flex-row flex-column">
<div class="w-50 div-height blue-bg">
</div>
<div class="w-50 div-height yellow-bg">
</div>
</div>
Flex direction will be as a column in mobile view:
@media (max-width: 768px) {
.flex-column {
flex-direction: column;
}
}
To make it look and work like a framework, you can specify breakpoints in utility classes. That way it will be much easier to read your code. As can be seen below, I specify my classes in different page widths by giving them w-LG-50 and w-100 just like in Bootstrap.
@media (min-width: 992px) {
.w-lg-50 {
width: 50%;
}
}
<div class="w-100 div-height d-flex flex-row flex-column">
<div class="w-lg-50 w-100 div-height blue-bg">
</div>
<div class="w-lg-50 w-100 div-height yellow-bg">
</div>
</div>
We created a simple layout just as we use a framework. It is enough to simply create a utility class that we will use in many sections of our page such as buttons, titles, cards, etc. So every design should be covered by roots and variables before starting to create your page.
Of course, we are going to have to use CSS when we’re going to use pseudo-classes or properties doesn’t take a part in DOM directly. Eventually, custom utility classes will make our CSS document simple and we will handle many properties on our page before starting to write code in our CSS file.
To see this code in Codepen, click here.
Perfist Blog
Similar Articles
What is SEO Analysis? SEO analysis is a detailed examination of a website’s performance in search engines. This analysis provides insights into the site’s current status and highlights areas needing improvement. SEO analysis is essential for defining a roadmap and building a successful strategy. Accurate analysis clarifies which keywords to focus on, how to resolve […]
Read More
Mid Level SEOWhat is Search Intent? Search intent refers to the underlying goal a user has when performing a search query. Google and other search engines do not just focus on keywords but also analyze their context to understand what users are really looking for. Search intent is generally categorized into four main types: informational, navigational, commercial […]
Read More
SEOWhat is Semantic SEO? Semantic SEO is an optimization approach that helps search engines better understand user intent. This method analyzes the relationships between words, context, and the purpose behind user queries, ensuring more accurate content matching. Based on our extensive experience in digital marketing, we can confidently say that content created with a semantic […]
Read More
SEOWhat is Site Speed? Site speed refers to how quickly a web page loads. (Site speed has multiple factors. The most important of these are initial load time and load speed.) This speed directly affects the visitor experience. A slow-loading site can cause users to leave the site and choose other pages. Additionally, search engines […]
Read More
Mid Level SEO