What is a CSS Stylesheet?
CSS is a type of code which can dictate the appearance of an HTML web page. Colour, font and layout can all be controlled by combining selectors and properties. Styles can be added to a site externally, internally and/or inline providing a uniform method of styling multiple web pages.
Introduction
If you have ever noticed a well styled webpage then chances are that styling has been done using CSS. A stylesheet is used within CSS to tell the browser how to style a particular web page. In this blog we will take a look at what CSS stylesheets are, how they work and how they can be used, as well as why they are so important in modern web development.
What is CSS Stylesheet?
CSS stands for ‘Cascading Style Sheets’. CSS gives styling instructions to HTML documents. Whereas HTML specifies content (headings, paragraphs, images, links) in a webpage, CSS specifies how it looks (colours, margins, fonts, layout, animations and responsive design).
In terms of a house, HTML would be considered the bones/structure of the house. CSS would be the decoration, flooring, furniture layout and lighting. If we had a webpage without CSS, it would still function but would be very unattractive; it would contain black text on a white background and would be a single column.
A CSS document contains the styling rules. This can be in its own document (with a .css extension) or can be inserted into HTML documents.
Why Does Cascading Matter?
When there are multiple styles applied to an element (either via default browser styling, external stylesheets, internal stylesheets and inline styles) the styles will be determined using the following criteria:
- Importance: If rules have been marked with ! important, they take priority.
- Specificity: If two rules have different specificities, the rule with the higher specificity will be used. ID selectors have a higher specificity than element name selectors.
- Source Order: If two rules have the same specificity, the rule which occurs last in the code will be used.
This allows general styles to be applied, and more specific changes to be made.
How does CSS work?
CSS3 works with HTML elements. HTML elements can be anything within your webpage, such as paragraphs. In HTML you could write:
<p>This is my paragraph!</p>
To change this paragraph to be pink and bold in appearance for people visiting your webpage in a browser, you can use the following CSS code:
p { color:pink; font-weight:bold; }
In this piece of code, p represents the paragraph, and so is known as a selector. Selectors can be found at the beginning of each section of CSS code, before the first curly brackets. Within the brackets, there are declarations. Within these declarations are properties and values.
The properties can include font sizing, colour and margins. Values can be changed depending on what you want to change about the specific property. Background position, border colour, border style, border width, and text alignment are properties, meaning that top, red, dotted, thick and left would be values respectively.
In the example above, colour and font-weight are properties. Pink and bold are values. The whole block of code in the brackets:
{ color:pink; font-weight:bold; }
is a declaration. Again, the p in the code refers to the paragraph in HTML, so it is a selector. Using this simple idea, different styles can be applied to your webpage. For example, if you want to change the font size, background colour, indentation of margins etc, depending upon what selector you choose to apply the styles to. If you were to apply the following code:
body { background-color: lightblue; }
The background of your page would become light blue. Or if you were to apply:
p { font-size:20px; color:red; }
Your paragraph would have a font size of 20 points and have red text.
What are Selectors and Properties ?
In CSS we have selectors and properties. Selectors allow us to select certain parts of a webpage. The ‘p’ selector will apply a rule to all paragraph tags on a webpage.
Using properties and selectors you can apply styling to any element. The ‘color’ property can be used to change the text colour of an element and the ‘font-family’ property can be used to change the font type of an element.
Important to note from CSS is that it is not a programming language (although it can respond to things such as :hover or :focus and the screen size using media queries). Content that has interactive behaviour or is dynamically updated is done using JavaScript.
What are the Types of CSS?
There are 3 types of CSS:
1) Inline CSS
Inline CSS can be added to individual elements by applying it to the elements with the style attribute.
When to Use Inline CSS:
- A quick fix for small changes in styling
- To override other styles of an element
- When creating emails or HTML applications, external CSS is not supported.
2) Internal/Embedded CSS
Internal/Embedded CSS is written within the element in the HTML page. It can be used to add styles to specific elements. The style rules should also be included in the HTML file, and must be placed within the section of the HTML document. Therefore this CSS is internal to the webpage (added inside the tags within the section).
When to Use Internal CSS:
- Only on single-page websites.
- Better control over styling than Inline CSS.
- Small/medium sized projects. External CSS may be overkill.
3) External CSS
External CSS is stored within its own CSS file. Style properties can be assigned to elements in HTML by using tag attributes (for example class, ID, headings etc). The style properties have been written in a separate file, which must be given the .css extension. This file must then be linked to the HTML document using a link tag. This means that we can assign a style just once to an element, and it will be applied to all web pages.
When to Use External CSS:
- Large scale projects, where many pages have consistent styling.
- Improving maintainability and scalability.
- To improve performance on your website (CSS caching).
What are the Benefits of CSS?
There are many reasons as to why you should implement CSS within your HTML web pages.
- Allow your web pages to be both visually appealing and also easy to read.
- Increase the functionality, and so improve the friendliness of your web pages.
- Change the design of your web pages to suit differing display devices (for example, smart phones and tablets).
- Using CSS can allow you to efficiently apply a design to several items on a web page.
- Furthermore, CSS can be applied to numerous web pages, and so facilitates website management.
Conclusion
CSS can be used to enhance HTML and so create beautifully functional webpages. Inline, internal and external CSS all exist. External CSS is most common in professional settings as it is easily shareable and can also be cached. As well as these, there are further tools such as Sass, Tailwind and CSS Modules which can effectively manage a project’s styling. Begin by working on small scale projects and endeavour to continually practise. The most effective way of learning CSS is through its implementation.
FAQs
Which CSS should I use?
For the majority of cases, external CSS is optimal. External CSS is housed within it’s own file and can then easily be applied to numerous web pages. Additionally, browsers will cache external CSS, further improving performance. Inline and internal CSS are preferable for rapid prototyping and smaller scale web pages.
How are conflicting inline, internal and external CSS resolved?
A ‘cascading’ system is implemented. Typically, inline CSS will supersede both internal and external CSS. Internal and external CSS are treated equally and so the style defined in whichever is listed last in the document will be applied. Specificity can also be altered using the `!important` tag to change the level of precedence
Is it possible to apply multiple stylesheets to one webpage?
Yes. Several external stylesheets can be referenced and these can also be integrated with internal and/or inline CSS. As previously mentioned, the sequence in which stylesheets are listed is important. Later styles will supersede earlier ones where levels of specificity are the same.
Is it still necessary to utilise a pre-processor such as Sass?
Whilst not always essential, in many cases it is no longer required to use a pre-processor due to the native support for variables (custom properties) and nesting in current CSS. Nonetheless, for extensive websites where features such as mixins, functions and superior structure are advantageous, a pre-processor like Sass remains a valuable tool.