Introduction

Cascading Stylesheets — or CSS — is the first technology you should start learning after HTML. While HTML is used to define the structure and semantics of your content, CSS is used to style it and lay it out. For example, you can use CSS to alter the font, color, size, and spacing of your content, split it into multiple columns, or add animations and other decorative features.

Syntax

CSS is a rule-based language — you define rules specifying groups of styles that should be applied to particular elements or groups of elements on your web page. For example "I want the main heading on my page to be shown as large red text."

  • The following code shows a very simple CSS rule that would achieve the styling described above:
  • 
                        h1 {
                        color: red;
                        font-size: 5em;
                        }
                    

    The rule opens with a selector . This selects the HTML element that we are going to style. In this case we are styling level one headings  <h1>.

    We then have a set of curly braces { }. Inside those will be one or more declarations, which take the form of property and value pairs. Each pair specifies a property of the element(s) we are selecting, then a value that we'd like to give the property.

    Before the colon, we have the property, and after the colon, the value. CSS properties have different allowable values, depending on which property is being specified. In our example, we have the color property, which can take various color values. We also have the font-size property. This property can take various size units as a value.

  • A CSS stylesheet will contain many such rules, written one after the other.
  • 
                            h1 {
                            color: red;
                            font-size: 5em;
                            }
                        
                            p {
                            color: black;
                            }
                    

    You will find that you quickly learn some values, whereas others you will need to look up. The individual property pages on MDN give you a quick way to look up properties and their values when you forget, or want to know what else you can use as a value.

    Modules

    As there are so many things that you could style using CSS, the language is broken down into modules. You'll see reference to these modules as you explore MDN and many of the documentation pages are organized around a particular module. For example, you could take a look at the MDN reference to the Backgrounds and Borders module to find out what its purpose is, and what different properties and other features it contains. You will also find links to the CSS Specification that defines the technology (see below).

    At this stage you don't need to worry too much about how CSS is structured, however it can make it easier to find information if, for example, you are aware that a certain property is likely to be found among other similar things and are therefore probably in the same specification.

    For a specific example, the Backgrounds and Borders module — you might think that it makes logical sense for the background-color and border-color properties to be defined in this module. And you'd be right.

    Specifications

    All web standards technologies (HTML, CSS, JavaScript, etc.) are defined in giant documents called specifications (or simply "specs"), which are published by standards organizations (such as the W3C, WHATWG, ECMA, or Khronos) and define precisely how those technologies are supposed to behave.

    CSS is no different — it is developed by a group within the W3C called the CSS Working Group. This group is made of representatives of browser vendors and other companies who have an interest in CSS. There are also other people, known as invited experts, who act as independent voices; they are not linked to a member organization.

    New CSS features are developed, or specified, by the CSS Working Group. Sometimes because a particular browser is interested in having some capability, other times because web designers and developers are asking for a feature, and sometimes because the Working Group itself has identified a requirement. CSS is constantly developing, with new features coming available. However, a key thing about CSS is that everyone works very hard to never change things in a way that would break old websites. A website built in 2000, using the limited CSS available then, should still be usable in a browser today!

    As a newcomer to CSS, it is likely that you will find the CSS specs overwhelming — they are intended for engineers to use to implement support for the features in user agents, not for web developers to read to understand CSS. Many experienced developers would much rather refer to MDN documentation or other tutorials. It is however worth knowing that they exist, understanding the relationship between the CSS you are using, browser support

    How is CSS structured

    Applying CSS to your HTML

    External stylesheet
    In the Getting started with CSS we linked an external stylesheet to our page. This is the most common and useful method of attaching CSS to a document as you can link the CSS to multiple pages, allowing you to style them all with the same stylesheet. In most cases, the different pages of a site will all look pretty much the same, therefore you can use the same set of rules for the basic look and feel.

  • An external stylesheet is when you have your CSS written in a separate file with a .css extension, and you reference it from an HTML element:
  •                     <!DOCTYPE html>
                        <html>
                          <head>
                            <meta charset="utf-8">
                            <title>My CSS experiment</title>
                            <link rel="stylesheet" href="styles.css">
                          </head>
                          <body>
                            <h1>Hello World!</h1>
                            <p>This is my first CSS example</p>
                          </body>
                        </html>

  • The CSS file might look something like this:
  • 
                        h1 {
                          color: blue;
                          background-color: yellow;
                          border: 1px solid black;
                        }
                        
                        p {
                          color: red;
                        }

    The href attribute of the element needs to reference a file on your filesystem.

  • In the example above, the CSS file is in the same folder as the HTML document, but you could place it somewhere else and adjust the specified path to suit, for example:
  • 
                            <!-- Inside a subdirectory called styles inside the current directory -->
                            <link rel="stylesheet" href="styles/style.css">
                            
                            <!-- Inside a subdirectory called general, which is in a subdirectory called styles, inside the current directory -->
                            <link rel="stylesheet" href="styles/general/style.css">
                            
                            <!-- Go up one directory level, then inside a subdirectory called styles -->
                            <link rel="stylesheet" href="../styles/style.css">

    Internal stylesheet
    An internal stylesheet is where you don't have an external CSS file, but instead place your CSS inside a  <style> element contained inside the HTML  <head>.

  • So the HTML would look like this:
  • 
                            <!DOCTYPE html>
                            <html>
                              <head>
                                <meta charset="utf-8">
                                <title>My CSS experiment</title>
                                <style>
                                  h1 {
                                    color: blue;
                                    background-color: yellow;
                                    border: 1px solid black;
                                  }
                            
                                  p {
                                    color: red;
                                  }
                                </style>
                              </head>
                              <body>
                                <h1>Hello World!</h1>
                                <p>This is my first CSS example</p>
                              </body>
                            </html>
                        

    This can be useful in some circumstances (maybe you're working with a content management system where you can't modify the CSS files directly), but it isn't quite as efficient as external stylesheets — in a website, the CSS would need to be repeated across every page, and updated in multiple places if changes were required.

    Inline styles

  • Inline styles are CSS declarations that affect one element only, contained within a style attribute:
  • 
                        <!DOCTYPE html>
                        <html>
                          <head>
                            <meta charset="utf-8">
                            <title>My CSS experiment</title>
                          </head>
                          <body>
                            <h1 style="color: blue;background-color: yellow;border: 1px solid black;">Hello World!</h1>
                            <p style="color:red;">This is my first CSS example</p>
                          </body>
                        </html>
                    

    Please don't do this, unless you really have to! It is really bad for maintenance (you might have to update the same information multiple times per document), and it also mixes your presentational CSS information with your HTML structural information, making the code harder to read and understand. Keeping different types of code separated makes for a much easier job for all who work on the code. There are a few places where inline styles are more common, or even advisable. You might have to resort to using them if your working environment is really restrictive (perhaps your CMS only allows you to edit the HTML body). You will also see them used a lot in HTML email in order to get compatibility with as many email clients as possible.

    Reference

  • All the documentation in this page is taken from MDN