The CSS Handbook - Everything Developers Should Know

Published on:
January 2, 2024

Speaking Cascading Style Sheets or CSS is similar to speaking English or any other spoken language- there are plenty of terms, but you only use a handful of them regularly. However, like reading a dictionary isn't an effective method to understand a language, listing the CSS properties isn't a way to know CSS.

Some people believe that CSS is extremely difficult and time-consuming to learn. Others believe that because it is not a programming language, it is so simple that you shouldn't bother learning it. But in reality, CSS is complicated, yet it doesn't have to be.

CSS is extremely powerful and includes a plethora of useful features. If you learn a few fundamental concepts, you should be able to look at or imagine any design and transform it into a reality. But how do you get started? To simplify things, we are here with our CSS handbook, where we will introduce you to its basics. 

So, let’s get the ball rolling! Here, we will discuss:

What is CSS for?

What is CSS for

In essence, CSS governs how things appear on your web pages. As developers put it, “HTML is your content, JavaScript adds interaction, but the appearance comes down to CSS.” It can style documents written in a markup language such as HTML. The latest version is CSS 3.

It was created in 1994 by Hkon Wium Lie, who also worked at CERN, the same research center that created HTML and the Internet. Decades later, the language is as vital as ever, with more than 97 percent of all websites employing CSS. 

Where is it used? CSS can be used for extremely simple text styles in documents, such as changing the color and size of headings and links. That means it can be used to design a layout, such as transforming a single column of text into a layout with a primary content section and a sidebar for supplementary information. It can even be used to create effects like animation. 

HTML vs. CSS

HTML vs. CSS

HTML and CSS work together to create the web pages we know and love. However, these are distinct languages, and it is critical to understand their respective functions. HTML (Hypertext Markup Language) specifies the contents of a web page, such as text, links, photos, and videos. 

An HTML file lists all the "things" on a page but does not specify how they will appear in a browser. As we now know, CSS governs the appearance of these elements. CSS guarantees that HTML content is shown to users as designers intended. 

As developers put it, “If HTML were the engine components of a car, CSS would be the body style and the paint job.“ So, consider HTML to be the cornerstone & Cascading Style Sheets to be the design options. To make a web page, you'll need both & JavaScript to make it interactive. 

CSS Benefits

As it turns out, separating content code from style code has several advantages. Such as:

  • Less coding: CSS allows developers to apply the same styling to various pages and page elements across a website, saving time and decreasing the possibility of errors. Changing a site-wide style involves only a small amount of code.
  • More styling options: CSS allows you to do much more than the original HTML styling system. Customize a website exactly to your liking with a clear vision, CSS knowledge, and some time.
  • Standardization: CSS is the uniform language for formatting web pages, so a developer or designer can grasp the styling for any website by glancing at the CSS files.
  • Improved performance: CSS improves performance by reducing the amount of repetitious styling code. Smaller files result from less code, meaning faster page loading. 
  • Easy Formatting Changes: If you need to modify the format of a specific set of pages, CSS makes it simple. There is no reason to correct every page. Simply alter the corresponding CSS stylesheet; the changes will be reflected on all pages that use that style sheet.
  • Device Compatibility: Responsive web design is important. Today's web pages must be accessible and easily navigable on all devices. CSS and HTML work together to enable responsive mobile, tablet, desktop, and smart TV design.

As Flavio Copes explains - “CSS is a marvelous tool & in the last few years, it has grown a lot with incredible features like CSS Grid, Flexbox & CSS Custom Properties.”

How does CSS work?

Since we have cleared what CSS is and how it styles web pages, it’s time to know how it works. 

CSS has a simple English-based syntax governed by a set of rules. As previously stated, HTML was designed to use style components/page markup. It’s to describe the content simply. 

For instance: <p>This is a paragraph.</p>.

But how should the paragraph be formatted? CSS syntax is fairly simple. It contains a selector as well as a declaration block. You choose an element and then specify what you intend to do with it. 

However, there are some ground rules to follow. Don't worry. The structure rules are rather basic. The selector identifies the HTML components to be styled. One or more declarations are separated by semicolons in the declaration block.

Each declaration has a colon-separated list of CSS property names and values. A semicolon always ends CSS declarations, and curly braces encircle declaration blocks.

Now, let us understand them in more detail.

CSS Syntax

CSS is divided into three parts: selectors, properties, and values.

  • Selectors are the elements that are being selected.
  • Properties are the characteristics of the element being styled.
  • Values define the applied style.

For example,


selector {
   property: value;
}

Here’s another example,


   h1{
      color: blue;
   }

In this example, the elements under h1 will all turn blue. One final aspect of CSS syntax is comments, which are ignored by browsers and are only used by humans to document their work. In CSS, these are depicted as follows:


/* this is a comment */

CSS Modules

In a repo, “CSS modules are files in which each class & animation names are scoped locally in default.” CSS Modules isn’t an official spec or a browser implementation. Instead, it's a way in a starting phase (using Webpack/Browserify) that modifies class names & selectors to be scoped. CSS Modules ensure that all of the styles for a single component:

  • Live in a single location.
  • Only use this component and nothing else.

Additionally, with CSS modules, any component can have a genuine reliance, such as:


import buttons from "./buttons.css";
import padding from "./padding.css";

element.innerHTML = `
`;

CSS Selectors

Selectors are used in CSS to specify the HTML elements we want to style. It can include every element on the page or only select a few elements. 

There are many other selectors as well, such as:


*{
    /* Universal selector */
}
tag {
   /* Type selector */
}
tag:before {
  /* Type selector (psuedo element) */
}
.class {
   /* Class selector */
}
.class:hover {
   /* Class selector (psuedo class) *)
}
[atter] {
  /* Attribute selector */
}
#id {
   /* ID selector */
}

Here are the primary categories of selectors, in order of least specific to most particular:

  • Universal selector - which selects all the items.
  • Type selector - uses HTML tags to target element(s) (including pseudo-elements).
  • Class & attribute selectors – target elements based on their class or attribute (including pseudo-classes).
  • ID selector - identifies an element by its id.

Fonts & Colors

CSS allows us to experiment with fonts and colors and make our HTML elements seem nice. You have two options for the family names of certain fonts:

  • Generic: a collection of fonts with a common appearance (such as 'Serif’).
  • Font Family: a particular font family (such as 'Times New Roman/Arial')

You can use predefined color names or RGB, HEX, HSL, RGBA, and HSLA values for colors.

<div class='container'>

  <h1 class='heading1'>

       CSS nis Cooooool!!!

  </h1>

<div>


.container {
   width: 500px;
   height: 100px;
   background-color: lightcyan;
   text-align: center;
}
.heading1 {
   font-family: 'Courier New';
   color: tomato;
}

In this example, we have a div element with the container class. There is a h1 tag with some content inside it within this div. Then, we selected the container class in the stylesheet and set its width, height, background color, and text-align. Finally, we assigned the font family and color properties to the .heading1 class allocated to the h1 tag.

CSS Cascade, Specificity, and Inheritance

CSS includes built-in features that control how your code impacts page components aside from the fundamentals. You should know three additional essential CSS terms to avoid future confusion: cascade, specificity, and inheritance.

CSS's essential idea is a cascade. After all, it's in the name, the initial C of CSS — Cascading Style Sheets — so it must be significant.

What does it imply?

Cascade is the procedure, or algorithm, which defines the characteristics applied to each element on the page. According to the cascade rule, if an element's CSS attribute is assigned multiple CSS values, the browser will render the value that was processed last.

What happens when numerous rules target an element, each with a distinct selector, and all change the same property? It is where specificity comes in. The more specific rule will always be favored. If two or more rules share the same specificity, the last rule to appear wins.

Beginners can discover it difficult to understand what is more particular in practice. It also needs clarification to professionals who only look at those guidelines sometimes or simply ignore them.

Another CSS feature that makes writing code considerably more efficient is inheritance. Certain CSS declarations applied to an element are inherited down to child elements via inheritance. In CSS, inheritance also governs what occurs when a property on an element has no value given.

css property

External, Internal, or Inline CSS?

type of css

We have covered all the fundamentals of CSS. But what about actually applying it to the HTML content? Cascading Style Sheets, like HTML, are created in basic, plain language using a text editor or word processor on your machine, and there are 3 primary methods to add that CSS code to your HTML-designed pages. 

Enters - External, Internal, and Inline CSS! 

External style sheets are saved as a CSS file (.css) and can be used to control the design of a complete website with a single file. To use this form of style sheet, your .html files must have a header section that connects to the external style sheet and looks like this:

<head>

  <link rel="stylesheet" type="text/css" href="mysitestyle.css">

</head>

It will attach the.html file to your style sheet & each CSS instruction in that file will be applied to the linked .html pages. CSS rules inserted directly into the header of a specific .html page are known as internal style sheets. It is especially beneficial if you have a single page on your site that has a distinct appearance. 

Here's an example -

<head>

   <style>

     body { background-color: thistle; }

     p { font-size: 20px; color: mediumblue; }

   </style>

 </head>

It gives this page a thistle background color & paragraphs in a 20-point medium blue font. Finally, inline styles are CSS snippets injected directly into HTML code & applicable to a single coding instance without requiring an extra CSS folder. 

For example:

<h1 style="font-size: 40px; color: violet;"> Check out this headline! </h1>

It would result in a headline on a single.html page appearing in violet, 40-point font. Overall, an external style sheet is the best approach for implementing CSS on a website, whereas internal style sheets and inline styles can be used when individual style changes are required.

As Scott Morris explains, “When HTML is the base, walls, frames & girders assisting your site, think CSS the window styles, paint color & landscaping that follows.”

Conclusion

That's all you need to know to get started creating gorgeous websites. CSS isn't difficult to master for such an influential technology, and this knowledge goes a long way in designing websites, altering page styles, and troubleshooting frequent issues.

Whether you've never used code before or are an HTML expert eager to broaden your skillset, learning CSS is a valuable undertaking you can begin immediately. Need help starting with CSS? MarsDevs can help. Book a slot with MarsDevs to kickstart your CSS undertaking today! 

FAQs

  1. What is CSS?

CSS (Cascading Style Sheets) is used to style and layout web pages, such as changing your content's font, color, size, and spacing, splitting it into numerous columns, or adding animations and other decorative features.

  1. Is CSS good for beginners?

CSS is an excellent programming language for novices because it is simple to grasp & numerous resources and tutorials are available that demonstrate how to use it.

  1. Do you need help learning CSS?

CSS is a simple & easy-to-learn language. Its basic rules and syntax are simple, and you might begin decorating web pages after one day of instruction (assuming you're familiar with HTML).

  1. Why is CSS called cascading?

Cascading style sheets are so named because many style sheets might be active for the same document simultaneously.

  1. What is CSS's latest version?

CSS3 is the most current and widely used standard. It adheres to the XHTML standard.


Similar Posts