Published on 3/9/2022 by
NevuloWhen designing and
, you’ll find yourself using , a language for creating style sheets. Style sheets are used to describe the presentation of HTML documents and how elements get displayed.CSS and styling elements really is the backbone for a lot of user experiences visually speaking; raw HTML will only get you so far.
This guide is top-to-bottom (mostly) explanation about what CSS really is, how it’s used, and common concepts and properties to understand, so you can make your own beautiful webpages.
CSS stands for “Cascading Style Sheets”, which is a mechanism for adding style to web documents, such as adjusting fonts, colours, spacing, layout, and much more. (We’ll get back to that “cascading” part soon!)
CSS is built up on “rulesets” which target elements in a HTML document through selectors. Selecting an element to be styled looks like this:
1body {2 background: black;3}
This snippet would select the body
element, and turn the background to “black”. body
can be replaced with any element you want to select, and then you can add declarations through properties and values inside the curly braces.
This is what your average HTML element looks like under the hood:
This is known as the “
”. Abstractly speaking, when making a website, you’re essentially structuring and styling numerous boxes.You’ve got the actual size of your element (in this case, 750 pixels wide by 150 pixels tall), then padding on the outer edge of the element to increase its internal size.
The border wraps around the padding (the space inside the element), and on the very outside is the margin (the space outside the element).
There are a few ways to include CSS styling in a HTML document, but the two most common methods are:
creating a .css
file with all of your rulesets, then referencing that CSS file as a stylesheet in the <head>
of your document
1<link href="./style.css" rel="stylesheet" />
including CSS rulesets directly in the HTML document in the <style>
tag, also in the <head>
of your document
1<style>2 .class {3 color: pink;4 }5</style>
When styling any element through CSS, the foundation is built up of “rulesets”, which include a selector to access an element (or multiple that meet the requirements of the selector) to be styled.
After the selector, everything between the curly braces is known as a “
”, which can contain one or more declarations. Declarations are a key/value pair, consisting of the CSS property as the key, and a value for the property.There are a few ways to “select” an element to be styled in CSS, depending on your requirements. You can find a full list of selectors
.element
)input { background: red; }
will match any <input>
elements on the page, making the background red
.class
)class
attribute, including a list of “classes” delimited by spaces<h1 class='bold'></h1>
could be selected in CSS through .bold
#id
)id
attribute matching the selector valueid
attribute<div id='article'></div>
could be selected in CSS through #article
Once you’ve selected an element, you can start adding declarations to change its styling. Note that if you don’t select a valid element, there will be no effect. You can use “Inspect Element” to see what styling is applied on an element to troubleshoot these issues.
Some inspiration for understanding what you can style through these “declarations”. You can see an entire list of CSS properties
.color
: adjusts the foreground colour of an element. The colour of text elements will depend on the color
value.
background
: adjusts the background colour of an element
font-size
: changes the size of font for text, specified in pixels or other units
font-family
: defines the type of font used for the text in the element (e.g., Arial, monospace font, etc)
display
: used for adjusting the layout mode elements use, for positioning and structure
padding
: adds internal space to an element, making it appear bigger
margin
: adds external space around an element, pushing other elements away
border
: applies a customisable border around an element
height
: adjusts the height of an element, specified in pixels or relative units
width
: adjusts the width of an element, specified in pixels or relative units
They don’t call it “Cascading Style Sheets” for nothin’!
The cascade describes how CSS declarations and rulesets are applied to a HTML document, including how conflicts between two declarations on the same element get handled (i.e., one ruleset sets margin-left
to 0
, but another sets it to 12px
), and which gets priority.
It controls the order in which CSS declarations are applied, which is dictated by three other concepts:
Inheritance controls what happens if there’s no value for a specific property on an element. Essentially, if you set a CSS property like color
on an element, any child elements will inherit the colour from the parent unless child elements have explicitly defined a colour.
There’s a distinction between
properties; non-inherited properties (such asborder
) will use the initial value for that property, with the browsers default style sheet. All properties specify an initial
value.If you want to explicitly define that an element should inherit a property from its parent, almost all CSS properties include an inherit
value (e.g., color: inherit
), which will set the value of the property to the
The more specific a selector is, the more precedence it has over other declarations. Specificity is a weight given to all declarations, determined by 3 factors:
If multiple declarations have equal specificity, the last declaration found in the CSS gets applied to the element.
Subscribe to the Nevuletter so you never miss new posts.
Comments
• 0
You need to be signed in to comment