Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

CSS general concepts

What is CSS really?

Just like HTML, CSS is not a programming language (although there are many controversies and discussions on this topic) and it is not a markup language either, it is a stylesheet language.

CSS is a styling (design) language that allows web pages to be customized. This means it enables selective application of styles to elements in HTML documents.

CSS is the language of style rules we use to apply styling to HTML content, for example setting background colors and fonts, and arranging content into multiple columns.

For example, to select all paragraphs in a document and color the text red, we use the following CSS code:

p {
  color: red;
}

Let's try the following: copy the three lines above, place them in a new file and save this file with the name style.css in the css folder created in previous lessons.

site structure

Now, to apply this simple CSS code to the HTML document and actually see how the text changes color to red, we need to tell the index.html file where the stylesheet is located.

Basically, we need to create a link between index.html and style.css like this:

 <link href="css/style.css" rel="stylesheet"> 
🔧 Example: Title, image, paragraphs, unordered list and link

Anatomy of a CSS Rule

Let's analyze the code in the image below in detail.

CSS Declaration

Image taken from developer.mozilla.org.

The entire structure is called a rule set, but most often it is simply referred to as a CSS rule. Let's see what individual elements make up a CSS rule:

  1. The selector – is represented by the name of the HTML element at the beginning of the rule set. It selects the element(s) to be styled (in our case <p>).
  2. The declaration – is represented by each line inside a rule. A CSS rule can contain multiple declarations. In our case, the declaration is represented by the line color: red; and it specifies one of the properties we want to modify.
  3. The property (properties) – are the ways through which we can style an existing HTML element. In our case, color is a property that can be applied to <p> elements. In CSS, we can choose which properties we want to modify for a specific element.
  4. The property value – is located to the right of the property, immediately after the colon(:) or separated by a single space. There are many CSS values we could use and which we will discuss later.

Let's also look at other important parts of CSS syntax:

So, to modify multiple values belonging to multiple properties simultaneously, we proceed as follows:

p {
  color: red;
  width: 600px;
  border: 1px solid orange;
}

Selecting Multiple Elements

We can also select several different elements at once and apply a single rule to all of them:

p, li, h1 {
  color: red;
}

Different Types of Selectors

There are many types of selectors.

Above we presented only element selectors, which select all elements of a certain type from the HTML document, but we can use more specific selectors. Here are some of the most commonly used types of CSS selectors:

Selector Name What It Selects Example
Element Selectors All HTML elements of the specified type p - selects all <p> elements
ID Selector The element on the page with the specified ID. It is recommended to use only one ID per element. Also, using the same ID more than once in the same HTML document is not allowed. #my-id - selects <p id="my-id"> or <a id="my-id">
Class Selector The element on the page with the specified class. The class selector can be used repeatedly for various elements and multiple times in the same document. .my-class - selects <p class="my-class"> or <a class="my-class">
Attribute Selector The element on the page with the specified attribute. img[src] - selects <img src="myimage.png"> but not <img>
Pseudo-class Selector The specified element(s), but only when in a specific state. a:hover - Selects <a>, but only when the mouse pointer hovers over a link.

There are many other selectors to explore, which we will discuss at the appropriate time.

Text and Fonts

Now that we've discussed some basic elements of CSS, let's start adding a few more rules and information to our style.css file so that our first website looks better.

Let's start by modifying the font so that our text looks a bit nicer:

  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">

The line above simply establishes a link between our page and a stylesheet from Google Fonts, which allows us to download the Open Sans font along with our page.

This way we can use this font in our own stylesheet.

html {
  font-size: 10px; /* px = "pixels": we set the font height to 10px */
  font-family: "Open Sans", sans-serif;
}
🔧 Example: Fonts

The rule above sets the font size for the entire page (10 pixels in height) and sets the use of a global font, since the html selector is the parent element of the entire page and all elements will inherit the same font size and font type (family).

Any text in a CSS document enclosed between /* and */ is a CSS comment, which the browser ignores when reading and rendering the code. This allows you to write useful notes to remind yourself later what the code does or to create separations between large blocks of code targeting different areas of the site.

To do this, add the following lines of code to the style.css file:

h1 {
  font-size: 60px;
  text-align: center;
}

p, li {
  font-size: 16px;    
  line-height: 2;
  letter-spacing: 1px;
}
🔧 Example: Fonts

You can adjust the values expressed in (px) to whatever values you want, so the design looks the way you like.

It's All About Boxes

Imagine that most HTML elements are actually boxes, stacked on top of each other or placed side by side.

cutii-html-css

In fact, that's exactly how it is.

It's no surprise that CSS layout is mainly based on the box model.

Each of the blocks (boxes) that occupy space on a web page has properties such as:

modelul-cutiei-css
  1. padding – the space around the content (element);
  2. border – the solid line right at the edge of the padding;
  3. margin – the space outside the element or between elements;
  4. width – sets the width of an element;
  5. background-color – the color behind the element's content, including the padding;
  6. color – the color of an element's content (usually text);
  7. text-shadow – sets a shadow for the text inside an element;
  8. display – sets how an element is displayed (a more complex topic we'll discuss later).

So, let's start adding more properties to our page. Do this as a continuation of the rules already in the style.css file and even try changing the values to see what other stylistic results you can achieve.

Changing the Background Color

html {
  background-color: limegreen;
}

The rule above sets a background color for the entire page.

Add to the style.css file only the declaration background-color: limegreen; inside the html { } rule.

html {
  font-size: 10px; /* px = "pixels": we set the font height to 10px */
  font-family: "Open Sans", sans-serif;
  background-color: limegreen;
}

Page Body

body {
  width: 700px;
  margin: 0 auto;
  background-color: tomato;
  padding: 0 20px 20px 20px;
  border: 5px solid navy;
}

Let's go through the declarations above piece by piece:

Positioning and Styling the Title

h1 {
  margin: 0;
  padding: 20px 0;    
  color: limegreen;
  text-shadow: 3px 3px 1px black;
}

You may have noticed there's quite a bit of space at the top of the page. This happens because browsers apply default styling to the H1 element (among others).

To get rid of that space, we set the margin to 0.

Next, we set padding for the title on the top and bottom, and the title color to match the html (root element).

An interesting property we used here is text-shadow, which applies a shadow to the text inside the h1 element. The four values are interpreted as follows:

Centering an Image

img {
  display: block;
  margin: 0 auto;
}

Finally, we will center the image to make it look better on our page.

We will use the same trick as above margin: 0 auto;, but we also need to do something else.

The <body> element is of type block, which means it occupies space on the page and can have margin and other spacing values applied by default.

Images are inline elements, the opposite of the block type described above.

So, to be able to apply a margin to the image, we need to change its behavior on the page, and we do this using the display property with the value block.

The instructions above are valid if you use an image smaller than the 700px width set on the <body> element. If the image used is larger, it will overflow into the rest of the page. You can fix this by reducing the image size using a graphic editor or by reducing the image size (<img>) with CSS by setting the width property to a smaller value (400px for example – width: 400px;).

Don't worry too much if you don't fully understand the display: block; property. We will talk much more about it in the upcoming lessons.

Conclusion

If you followed the instructions correctly, the page we built together should look like this:

🔧 Example: Fonts
Top