Styling A Website - Selectors and Visual Rules Cheatsheet - Codecademy
Styling A Website - Selectors and Visual Rules Cheatsheet - Codecademy
<!-- How to link an external stylesheet with href, rel, and type attributes -->
Purpose of CSS
CSS, or Cascading Style Sheets, is a language that is used in combination with HTML
that customizes how HTML elements will appear. CSS can define styles and change the
layout and design of a sheet.
<head>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
h1, h2 {
color: red;
}
<head>
<style>
h1 {
color: blue;
}
</style>
</head>
Selector Chaining
CSS selectors define the set of elements to which a CSS rule set applies. For instance, to
select all <p> elements, the p selector can be used to create style rules.
https://www.codecademy.com/learn/paths/web-development/tracks/styling-a-website/modules/learn-css-selectors-visual-rules/cheatsheet 2/8
5/11/2020 Styling a Website: Selectors and Visual Rules Cheatsheet | Codecademy
!important Rule
The CSS !important rule is used on declarations to override any other declarations for a
property and ignore selector specificity. !important rules will ensure that a specific
declaration always applies to the matched elements. However, generally it is good to
avoid using !important as bad practice.
#column-one {
width: 200px !important;
}
.post-title {
color: blue !important;
}
Chaining Selectors
CSS selectors can be chained so that rule sets apply only to elements that match all
criteria. For instance, to select <h3> elements that also have the section-heading class,
the selector h3.section-heading can be used.
.calendar-cell {
color: #fff;
}
Inline Styles
CSS styles can be directly added to HTML elements by using the style attribute in the
element’s opening tag. Each style declaration is ended with a semicolon. Styles added in
this manner are known as inline styles.
Selector Specificity
Specificity is a ranking system that is used when there are multiple conflicting property
values that point to the same element. When determining which rule to apply, the
selector with the highest specificity wins out. The most specific selector type is the ID
selector, followed by class selectors, followed by type selectors. In this example, only
color: blue will be implemented as it has an ID selector whereas color: red has a type
selector.
https://www.codecademy.com/learn/paths/web-development/tracks/styling-a-website/modules/learn-css-selectors-visual-rules/cheatsheet 4/8
5/11/2020 Styling a Website: Selectors and Visual Rules Cheatsheet | Codecademy
h1#header {
color: blue;
} /* implemented */
h1 {
color: red;
} /* Not implemented */
CSS ID selectors
The CSS ID selector matches elements based on the contents of their id attribute. The
values of id attribute should be unique in the entire DOM. For selecting the element
having job-title as the value of the id attribute, a # needs to be prepended.
#job-title {
font-weight: bold;
}
div p { }
section ol li { }
CSS declarations
https://www.codecademy.com/learn/paths/web-development/tracks/styling-a-website/modules/learn-css-selectors-visual-rules/cheatsheet 5/8
5/11/2020 Styling a Website: Selectors and Visual Rules Cheatsheet | Codecademy
In CSS, a declaration is the key-value pair of a CSS property and its value. CSS
declarations are used to set style properties and construct rules to apply to individual or
groups of elements. The property name and value are separated by a colon, and the
entire declaration must be terminated by a semi-colon.
/*
CSS declaration format:
property-name: value;
*/
/* CSS declarations */
text-align: center;
color: purple;
width: 100px;
Font Size
The font-size CSS property is used to set text sizes. Font size values can be many
different units or types such as pixels.
font-size: 30px;
Background Color
The background-color CSS property controls the background color of elements.
background-color: blue;
Opacity
The opacity CSS property can be used to control the transparency of an element. The
value of this property ranges from 0 (transparent) to 1 (opaque).
opacity: 0.5;
Font Weight
The font-weight CSS property can be used to set the weight (boldness) of text. The
provided value can be a keyword such as bold or normal .
https://www.codecademy.com/learn/paths/web-development/tracks/styling-a-website/modules/learn-css-selectors-visual-rules/cheatsheet 6/8
5/11/2020 Styling a Website: Selectors and Visual Rules Cheatsheet | Codecademy
font-weight: bold;
Text Align
The text-align CSS property can be used to set the text alignment of inline contents.
This property can be set to these values: left , right , or center .
text-align: right;
h1 {
color: blue;
text-align: center;
}
p {
color : #2a2aff ;
}
span {
color : green ;
}
Resource URLs
https://www.codecademy.com/learn/paths/web-development/tracks/styling-a-website/modules/learn-css-selectors-visual-rules/cheatsheet 7/8
5/11/2020 Styling a Website: Selectors and Visual Rules Cheatsheet | Codecademy
In CSS, the url() function is used to wrap resource URLs. These can be applied to
several properties such as the background-image .
background-image: url("../resources/image.png");
Background Image
The background-image CSS property sets the background image of an element. An image
URL should be provided in the syntax url("moon.jpg") as the value of the property.
background-image: url("nyan-cat.gif");
Font Family
The font-family CSS property is used to specify the typeface in a rule set. Fonts must
be available to the browser to display correctly, either on the computer or linked as a
web font. If a font value is not available, browsers will display their default font. When
using a multi-word font name, it is best practice to wrap them in quotes.
h2 {
font-family: Verdana;
}
#page-title {
font-family: "Courier New";
}
h1 {
color: aqua;
}
li {
color: khaki;
}
https://www.codecademy.com/learn/paths/web-development/tracks/styling-a-website/modules/learn-css-selectors-visual-rules/cheatsheet 8/8