Key Concepts
Review core concepts you need to learn to master this subject
The link element
Purpose of CSS
CSS class and ID selectors
Writing CSS code in separate files
Groups of CSS Selectors
Writing CSS code in HTML file
CSS Selector chaining
CSS !important rule
The link element
The link element
The <link>
element is used to link HTML documents to external resources like CSS files. <link>
commonly uses the href
attribute to specify the URL to the external resource, rel
to specify the relationship of the linked document to the current document, and type
to define the type of content being linked.
Purpose of CSS
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.
CSS class and ID selectors
CSS class and ID selectors
CSS classes can be reusable and applied to many elements. Class selectors are denoted with a period .
followed by the class name. CSS ID selectors should be unique and used to style only a single element. ID selectors are denoted with a hash sign #
followed by the id name.
Writing CSS code in separate files
Writing CSS code in separate files
CSS code can be written in its own files to keep it separate from the HTML code. The extension for CSS files is .css
. These can be linked to an HTML file using a <link>
tag in the <head>
section.
Groups of CSS Selectors
Groups of CSS Selectors
Match multiple selectors to the same CSS rule, using a comma-separated list. In this example, the text for both h1
and h2
is set to red.
Writing CSS code in HTML file
Writing CSS code in HTML file
CSS code can be written in an HTML file by enclosing the code in <style>
tags. Code surrounded by <style>
tags will be interpreted as CSS syntax. In the example, CSS code of foreground color for h1
element has been defined inside an HTML block.
CSS Selector chaining
CSS 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.
CSS !important rule
CSS !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.
Chaining Selectors
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.
CSS Type Selectors
CSS Type Selectors
CSS type selectors are used to match all elements of a given type or tag name. Unlike for HTML syntax, we do not include the angle brackets when using type selectors for tag names. When using type selectors, elements are matched regardless of their nesting level in the HTML.
CSS class selectors
CSS class selectors
The CSS class selector matches elements based on the contents of their class
attribute. For selecting elements having calendar-cell
as the value of the class
attribute, a .
needs to be prepended.
HTML attributes with multiple values
HTML attributes with multiple values
Some HTML attributes can have multiple attribute values. Multiple attribute values are separated by a space between each attribute.
Inline Styles
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
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.
Separating HTML code from CSS code
Separating HTML code from CSS code
It is common practice to separate content code in HTML files from styling code in CSS files. This can help make the code easier to maintain, by keeping the syntax for each file separate, and any changes to the content or styling can be made in their respective files.
CSS ID selectors
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.
CSS descendant selector
CSS descendant selector
The CSS descendant selector combinator is used to match elements that are descended from another matched selector. They are denoted by a single space between each selector and the descended selector. All matching elements are selected regardless of the nesting level in the HTML.
CSS declarations
CSS declarations
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 font size
CSS 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.
Background colors
Background colors
The CSS background-color
property controls the background color of elements.
Controlling opacity in CSS
Controlling opacity in CSS
The opacity
property can be used to control the transparency of an HTML element . The value of this property ranges from 0
to 1
. An element having the value 0
for the opacity
property will be fully transparent . Conversely , the value 1
means it is fully opaque . In the example , the value .5
indicates any HTML element having hint-text
class will be semi transparent .
CSS font weight
CSS 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
.
Setting alignment of inline content
Setting alignment of inline content
The text-align
property can be used to set alignment of inline contents. This property can be set to these values : left
, right
, or center
. For instance, to right-align the text inside a <p>
tag, text-align: right
is added to the rule set.
CSS Rule Sets
CSS Rule Sets
A CSS rule set contains one or more selectors and one or more declarations. The selector(s), which in this example is h1
, points to an HTML element. The declaration(s), which in this example are color: blue
and text-align: center
style the element with a property and value. The rule set is the main building block of a CSS sheet.
Setting foreground text color in CSS
Setting foreground text color in CSS
Using the color
property, foreground text color of an element can be set in CSS. The value can be a valid color name supported in CSS like green
or blue
. Also, 3 digit or 6 digit color code like #22f
or #2a2aff
can be used to set the color.
CSS resource URLs
CSS resource URLs
In CSS, the url()
function is used to wrap resource URLs. These can be applied to several properties such as the background-image
.
Setting background image in CSS
Setting background image in CSS
The background-image
CSS property sets the background image of an element. An image url should be provided in this syntax - url("blue-bg.jpg")
as the value of the property.
CSS font families
CSS font families
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.
CSS color name keywords
CSS color name keywords
Color name keywords can be used to set color property values for elements in CSS.
- 1The basic structure of every web page, HTML, is very plain on its own. The beautiful websites that you see across the internet are styled with a variety of tools, including CSS. CSS, or Cascad…
- 2Although CSS is a different language than HTML, it’s possible to write CSS code directly within HTML code using inline styles. To style an HTML element, you can add the style attribute directly…
- 3Inline styles are a fast way of styling HTML, but they also have limitations. If you wanted to style, for example, multiple elements, you would have to add inline styling to each element manually…
- 4Developers avoid mixing code by storing HTML and CSS code in separate files (HTML files contain only HTML code, and CSS files contain only CSS code). You can create a CSS file by using the **.css…
- 5Perfect! We successfully separated structure (HTML) from styling (CSS), but the web page still looks bland. Why? When HTML and CSS code are in separate files, the files must be linked. Otherwise,…
- 6CSS can select HTML elements by using an element’s tag name. A tag name is the word (or character) between HTML angle brackets. For example, in HTML, the tag for a paragraph element is . The CSS …
- 7CSS is not limited to selecting elements by tag name. HTML elements can have more than just a tag name; they can also have attributes. One common attribute is the class attribute. It’s also possi…
- 8We can use CSS to select an HTML element’s class attribute by name. So far, we’ve selected elements using only one class name per element. If every HTML element had a single class, all the style …
- 9If an HTML element needs to be styled uniquely (no matter what classes are applied to the element), we can add an ID to the element. To add an ID to an element, the element needs an id attribute: …
- 10CSS can select HTML elements by their tag, class, and ID. CSS classes and IDs have different purposes, which can affect which one you use to style HTML elements. CSS classes are meant to be reused…
- 11Specificity is the order by which the browser decides which CSS styles will be displayed. A best practice in CSS is to style elements while using the lowest degree of specificity, so that if an ele…
- 12When writing CSS rules, it’s possible to require an HTML element to have two or more CSS selectors at the same time. This is done by combining multiple selectors, which we will refer to as chaini…
- 13In addition to chaining selectors to select elements, CSS also supports selecting elements that are nested within other HTML elements. For instance, consider the following HTML: … ….
- 14In the last exercise, instead of selecting all h5 elements, you selected only the h5 elements nested inside the .description elements. This CSS selector was more specific than writing only h5. Addi…
- 15There is one thing that is even more specific than IDs: !important. !important can be applied to specific attributes instead of full rules. It will override any style no matter how specific it is…
- 16In order to make CSS more concise, it’s possible to add CSS styles to multiple CSS selectors all at once. This prevents writing repetitive code. For instance, the following code has repetitive st…
- 17Throughout this lesson, you learned how to select HTML elements with CSS and apply styles to them. Let’s review what you learned: - CSS can change the look of HTML elements. In order to do this, …
- 1In this lesson, you’ll learn the basic structure and syntax of CSS so that you can start styling web page elements.
- 2To style an HTML element using CSS, you need to write a CSS declaration inside the body of a CSS selector. h1 { color: blue; } The example above selects the element. Inside of the selector’s…
- 3If you’ve ever used a formatted word processor, chances are that you probably also used a feature that allowed you change the font you were typing in. Font refers to the technical term typeface , …
- 4Changing the typeface isn’t the only way to customize text. Often times, different sections of a web page are highlighted by modifying the font size. To change the size of text on your web page…
- 5In CSS, the font-weight property controls how bold or thin text appears. p { font-weight: bold; } In the example above, all paragraphs on the web page would appear bolded. The font-weight …
- 6No matter how much styling is applied to text (typeface, size, weight, etc.), text always appears on the left side of the browser. To align text we can use the text-align property. The text-align…
- 7Before discussing the specifics of color, it’s important to make two distinctions about color. Color can affect the following design aspects: - Foreground color - Background color Foreground co…
- 8Opacity is the measure of how transparent an element is. It’s measured from 0 to 1, with 1 representing 100%, or fully visible and opaque, and 0 representing 0%, or fully invisible. Opacity can b…
- 9CSS has the ability to change the background of an element. One option is to make the background of an element an image. This is done through the CSS property background-image. Its syntax looks lik…
- 10Incredible work! You used CSS to alter text and images throughout a website. Throughout this lesson, you learned concepts including: - CSS declarations are structured into property and value pair…
What you'll create
Portfolio projects that showcase your new skills
Healthy Recipes
Using CSS selectors, you’ll give a recipe website style.
Olivia Woodruff Portfolio
It's time to build fluency in CSS Fundamentals. In this next Pro Project, we're going to practice Visual Rules in CSS so you can hone your skills and feel confident taking them to the real world. Why? It's important to have a good grasp on the basic styling principles of a webpage. What's next? A coffee lover, photographer, developer. You got this!
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory