Pankaj Shah web agency director in London with over 20 years of experience in web design and project management

I hope you enjoy reading our blog posts.

If you want DCP to build you an awesome website, click here.

What is CSS? A Comprehensive Guide with Examples

In the world of web development, CSS plays a crucial role in defining the visual presentation of websites. Whether you are designing a personal blog, an e-commerce platform, or a business website, CSS is an essential technology that helps make your web pages look professional and visually appealing. But what exactly is CSS, and how does it work? In this article, we will explore what CSS is, why it’s important, and how it is used in web design, complete with examples.

What is CSS A Comprehensive Guide with Examples

What is CSS?

CSS stands for Cascading Style Sheets. It is a style sheet language used to control the presentation of web pages, including layout, colours, fonts, spacing, and overall design. CSS works in conjunction with HTML (HyperText Markup Language) to style the content of a webpage. While HTML is responsible for structuring the content (such as headings, paragraphs, and images), CSS determines how that content should be displayed.

For example, while HTML would be used to create a header or a paragraph, CSS would be used to change the font size, background colour, or margins of those elements. This separation of structure (HTML) and presentation (CSS) allows for greater flexibility in web design, making it easier to create and maintain visually cohesive websites.

Why is CSS Important?

CSS is essential for modern web development for several reasons:

  1. Improved User Experience: With CSS, you can design visually attractive and responsive websites that are easy to navigate, enhancing the user experience. You can adjust the design for different screen sizes, making sure your site looks great on mobile devices as well as desktops.

  2. Separation of Concerns: CSS allows developers to separate content from presentation, making it easier to update and manage both. If you need to change the colour scheme or typography across your site, you only need to update the CSS file instead of modifying every HTML page individually.

  3. Reusability: CSS can be applied across multiple HTML documents. For example, you can use a single CSS file to style all the pages on your website, ensuring a consistent look and feel across the entire site. This is not only time-saving, but also makes maintenance easier.

  4. Efficiency and Performance: Proper use of CSS can make your site faster and more efficient. By loading one external CSS file, you can style an entire website, reducing the need to repeat styles in each individual HTML file.

Basic CSS Syntax

The basic syntax of CSS consists of selectors and declarations. A CSS rule begins with a selector that targets the HTML element you want to style, followed by curly braces {} containing one or more declarations. Each declaration consists of a property and its value.

Here’s an example of a CSS rule:

h1 {
color: blue;
font-size: 24px;
}

In this example:

  • The h1 is the selector, targeting all <h1> elements in the HTML document.
  • Inside the curly braces, there are two declarations: color: blue; and font-size: 24px;. These declarations tell the browser to make all <h1> elements blue and change the font size to 24 pixels.

CSS Selectors

Selectors are used to target HTML elements that you want to style. There are several types of selectors in CSS:

  • Type Selector: Targets HTML elements by their type (e.g., p, h1, div).

p {
color: red;
}

  • Class Selector: Targets elements with a specific class attribute. Class selectors are prefixed with a dot (.).

.highlight {
background-color: yellow;
}

  • ID Selector: Targets an element with a specific ID. ID selectors are prefixed with a hash (#).

#header {
background-color: #f4f4f4;
}

  • Attribute Selector: Targets elements based on an attribute.

input[type=”text”] {
border: 1px solid black;
}

  • Universal Selector: Targets all elements in the document.

* {
margin: 0;
padding: 0;
}

Examples of CSS in Action

Examples of CSS in Action​

Example 1: Styling Text

One of the most common uses of CSS is styling text. You can change the colour, font size, font family, and more.

p {
color: #333;
font-family: Arial, sans-serif;
line-height: 1.5;
}

In this example, the CSS targets all <p> elements and applies the following styles:

  • The text color is set to a dark gray (#333).
  • The font family is set to Arial, with a fallback to sans-serif.
  • The line-height is set to 1.5 to improve readability.

Example 2: Adding a Background Image

CSS can also be used to add background images to elements. For example, to add a background image to a webpage:

body {
background-image: url(‘background.jpg’);
background-size: cover;
background-position: center;
}

This CSS rule sets a background image for the entire page (body element). The background-size: cover; ensures the image covers the entire page, while background-position: center; centers the image.

Example 3: Styling Links

CSS can be used to style links, such as changing their color when hovered over.

a {
color: blue;
text-decoration: none;
}

a:hover {
color: red;
text-decoration: underline;
}

In this example:

  • All <a> (anchor) elements are styled with a blue color and no underline.
  • When the user hovers over a link, the colour changes to red, and an underline appears.

Example 4: Creating a Simple Layout

CSS allows you to create responsive layouts using techniques like Flexbox or Grid. Here’s an example using Flexbox to create a simple two-column layout:

.container {
display: flex;
flex-direction: row;
}

.left {
flex: 1;
background-color: #f4f4f4;
padding: 20px;
}

.right {
flex: 2;
background-color: #e2e2e2;
padding: 20px;
}

In this example:

  • The .container class uses display: flex; to create a flexible container.
  • The .left and .right classes define two columns with different background colours and padding. The .right column takes up twice the space of the .left column.

Advanced CSS Features

1. Media Queries

Media queries are used to apply different styles depending on the screen size or device. This is essential for responsive design, ensuring that your website looks good on desktops, tablets, and smartphones.

@media (max-width: 768px) {
body {
background-color: lightgray;
}
}

In this example, the background colour will change to light gray when the screen width is 768 pixels or less.

2. CSS Animations

CSS can also be used to create simple animations. For example, you can animate the movement of an element:

@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}

.box {
animation: slide 2s infinite;
}

n this example, the .box element will slide horizontally by 100 pixels over 2 seconds and repeat the animation infinitely.

Conclusion

CSS is a fundamental technology that enables web designers and developers to create visually appealing, functional, and responsive websites. From basic text styling to advanced layouts and animations, CSS offers a wide range of tools that can help bring your web designs to life. Whether you’re just starting out with web development or looking to refine your skills, understanding CSS and its capabilities is crucial to creating modern, user-friendly websites. By mastering CSS, you can ensure that your websites look great and provide an optimal experience for users across all devices and screen sizes.

Here is a list of 10 websites where you can learn CSS for free:

list of 10 websites where you can learn CSS for free
  1. W3Schools

    • W3Schools is one of the most popular online learning platforms for web development. Its CSS tutorials are beginner-friendly and cover everything from basic syntax to advanced techniques, complete with interactive examples.
  2. Mozilla Developer Network (MDN)

    • MDN provides comprehensive and well-organised documentation on CSS, offering in-depth explanations and examples for both beginners and advanced developers. It’s a valuable resource for learning the intricacies of CSS.
  3. FreeCodeCamp

    • FreeCodeCamp offers a responsive web design certification, which includes an extensive section on CSS. It’s a hands-on learning experience where you build real projects while learning.
  4. Codecademy

    • Codecademy offers an interactive learning platform where you can start learning CSS from scratch. Although some courses require a subscription, their basic CSS course is free and well-structured.
  5. CSS-Tricks

    • CSS-Tricks is a well-known resource for web developers. It features a wide variety of tutorials, guides, and articles about CSS, along with a comprehensive guide called “A Complete Guide to CSS.”
  6. The Odin Project

    • The Odin Project is a free, open-source curriculum that offers an excellent HTML and CSS course as part of its full-stack web development path. You’ll build projects as you go, reinforcing your CSS skills.
  7. Coursera (Intro to HTML5 and CSS3)

    • Coursera offers free access to a number of web development courses, including one on HTML and CSS. While certification comes at a cost, auditing the course and accessing the materials is free.
  8. Khan Academy

    • Khan Academy offers interactive lessons in CSS through a series of videos and coding challenges. It’s a great resource for beginners who prefer video tutorials along with hands-on coding practice.
  9. Scrimba

    • Scrimba offers a free CSS course that is interactive and beginner-friendly. It provides screencasts where you can pause the video and edit the code directly within the learning interface.
  10. SoloLearn

    • SoloLearn is a mobile-friendly platform that offers bite-sized lessons on CSS, allowing you to learn on the go. Their courses are interactive and designed for beginners who want to start learning CSS quickly.

These resources cover everything from the basics of CSS to advanced techniques, and all of them are available for free!

Author

Picture of Pankaj Shah

Pankaj Shah

Pankaj Shah is the founder of DCP Web Designers, an award-winning London-based web design and digital marketing agency. With over 20 years of experience, he specialises in WordPress web design, WooCommerce, SEO and helping businesses build effective online solutions.
Tell Us Your Thoughts

This website (dcpweb.co.uk) uses cookies to improve your browsing experience and help us understand how our site is used. By continuing to browse this website, you agree to our use of cookies.

To learn more about how we collect, use, and protect your data, please read our Privacy Policy.

DCP Web Designers is one of London’s most established web design companies.

Since 2004, we have designed and developed websites for companies across a wide range of industries, from local service businesses to ecommerce brands and professional organisations.

Our focus is on creating websites that not only look professional, but also perform well in search engines, attract the right audience and support long-term business growth.

If you are looking for experienced web designers who understand how to build websites that deliver real results, our team is here to help.

Privacy Policy

Last Updated: 01/07/2024

Different Colour Productions Ltd (“we,” “us,” or “our”) is committed to protecting your privacy. This Privacy Policy outlines our practices concerning the collection, use, and disclosure of personal information when you visit our website or engage with our services. By using our website and services, you consent to the terms outlined in this Privacy Policy.

1. Information We Collect

We collect various types of information to provide and improve our services. The types of information we may collect include:

1.1. Personal Information: This may include your name, email address, phone number, and any other information you provide when you contact us, request information, or subscribe to our newsletter.

1.2. Log Data: When you visit our website, we automatically collect information, such as your IP address, browser type, pages visited, and the time and date of your visit.

1.3. Cookies and Similar Technologies: We use cookies and other tracking technologies to improve your experience on our website. You can adjust your browser settings to reject cookies or be alerted when cookies are being used.

2. How We Use Your Information

We use the collected information for various purposes, including:

2.1. Providing Services: To provide web design and related services you have requested from us.

2.2. Communication: To respond to your inquiries, send updates, and provide customer support.

2.3. Analytics: To analyse and improve our website and services, as well as monitor usage patterns.

3. Information Sharing and Disclosure

We do not sell or rent your personal information to third parties. However, we may share your information with third parties under the following circumstances:

3.1. Service Providers: We may share your information with trusted service providers who help us deliver our services, such as hosting providers, analytics providers, and marketing services.

3.2. Legal Obligations: We may disclose your information when required by law, to comply with legal processes, or to protect our rights, privacy, safety, or property.

4. Your Choices

You have choices regarding your personal information:

4.1. Access and Update: You can access and update your personal information by contacting us.

4.2. Marketing Communications: You can opt out of receiving marketing communications from us by following the unsubscribe instructions in our emails or emailing [email protected]

5. Security

We take appropriate measures to protect your personal information from unauthorised access, disclosure, alteration, or destruction.

6. Links to Other Websites

Our website may contain links to third-party websites. We are not responsible for the privacy practices of these websites. We recommend reviewing their respective privacy policies.

7. Changes to this Privacy Policy

We may update this Privacy Policy from time to time to reflect changes in our practices. Any changes will be posted on this page, and the date at the top will indicate the latest update.

8. Contact Us

If you have any questions or concerns about this Privacy Policy or our practices, please contact us at: [email protected]

By using our website and services, you acknowledge that you have read and agree to this Privacy Policy. Different Colour Productions Ltd is committed to safeguarding your personal information and respecting your privacy rights.

ThreeBestRated Top 3 Website Designers in London 2026 award for DCP Web Designers Certificate