Skip to content
Get lifelong access to all courses and certifications with Full Access.
Get lifelong access to all courses and certifications with Full Access.
What is CSS

What is CSS

ūüĎč Introduction

CSS stands for Cascading Style Sheets

CSS describes how HTML elements are to be displayed

It is used to style your web page.

Use CSS to set the layout of your web page with beautiful colors, fonts, and much more.


CSS Example

<style>

body {background-color:lightblue; text-align:center;}
h1 {color:blue; font-size:40px;}
p {font-family:verdana; font-size:20px;}

</style>
Try it Yourself ¬Ľ

Click on the "Try it Yourself" button to see how it works.


CSS Syntax

A CSS rule consists of a selector and a declaration block:

CSS selector

The selector points to the HTML element to style (h1).

The declaration block (in curly braces) contains one or more declarations separated by semicolons.

Each declaration includes a CSS property name and a value, separated by a colon.

In the following example all <p> elements will be center-aligned, red and have a font size of 32 pixels:

Example

<style>
p {font-size:32px; color:red; text-align:center;}
</style>

Same example can also be written like this:

<style>
p {
    font-size: 32px;
    color: red;
    text-align: center;
}
</style>
Try it Yourself ¬Ľ

Want to learn CSS? Try our course

Get started ‚ĚĮ

External Style Sheet

A CSS style sheet can be stored in an external file:

mystyle.css

body {background-color: orange; font-family:verdana}
h1 {color: white;}
p {font-size: 20px;}

External style sheets are linked to HTML pages with <link> tags:

Example

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="mystyle.css">
<body>

<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>

</body>
</html>

Try it Yourself ¬Ľ


Inline Style

Example

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="mystyle.css">
<body>

<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>
<p style="font-size:25px">This is a paragraph.</p>
<p style="font-size:30px">This is a paragraph.</p>

</body>
</html>

Try it Yourself ¬Ľ


Cascading Order

If different styles are specified for HTML elements, the styles will cascade into new styles with the following priority:

  • Priority 1: Inline styles
  • Priority 2: External and internal style sheets
  • Priority 3: Browser default
  • If different styles are defined on the same priority level, the last one has the highest priority.

Example

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="mystyle.css">

<style>
body {background-color: lightblue;}
</style>

<body style="background-color: olivedrab">
<h1>Multiple Styles Cascades into One</h1>
<p>Try experimenting by removing styles to see how the cascading stylesheets work.</p>
<p>Try removing the inline first, then the internal, then the external.</p>
</body>

</html>

Try it Yourself ¬Ľ


Full CSS FREE Tutorial

This has been a short description of CSS.

For a full CSS tutorial go to W3Schools CSS Tutorial.

For a full CSS reference go to W3Schools CSS Reference.


Want to become a Certified Front End Developer?

Learn and get certified with our certificate program

The steps:

  • Enroll to the program
  • Complete the training modules
  • Pass the certification exam
  • Become certified!
  • Get started! ūüĎá

Related courses:

Learn CSS

Learn Web Design

W3Schools Full Access


Subscribe to this blog's RSS feed using https://campus.w3schools.com/blogs/blog.atom

Previous article What is W3.JS
Next article How to make an accordion

Leave a comment

* Required fields