This post has been read 134 times.
CSS – Cascading Style Sheets, is a core technology of the web, alongside HTML and JavaScript. While HTML is used to structure content, CSS is responsible for the visual presentation, allowing web developers to style websites and create attractive layouts. If you’re new to web development, understanding the basics of Cascading Style Sheets will help you to control how your website looks and feels. This guide will walk you through the fundamentals of CSS to get you started.
What is CSS?
CSS is a stylesheet language used to describe the presentation of a document written in HTML or XML. It defines how elements should be rendered on screen, on paper, in speech, or on other media.
For example, Cascading Style Sheets can control:
- Colors of text and backgrounds
- Layouts (such as grids or flexboxes)
- Font styles, sizes, and types
- Spacing (margins, padding)
- Animation and transitions
Without CSS, websites would be plain and difficult to navigate. The Cascading Style Sheets allows for creativity and customization, ensuring that content is both functional and visually appealing.
How CSS Works
The Cascading Style Sheets work by associating styles with HTML elements. These styles are applied to specific parts of the web page through CSS rules, which are made up of selectors and declarations.
Here’s a simple CSS rule:
p { color: blue; font-size: 16px; }
p
is the selector, targeting all<p>
(paragraph) elements.- Inside the curly braces
{}
, we have the declarations:color
andfont-size
.color: blue;
sets the text color of paragraphs to blue.font-size: 16px;
sets the font size to 16 pixels.
Cascading Style Sheets Syntax
The basic syntax of a CSS rule consists of three parts:
- Selector: This defines which HTML element(s) the rule will apply to. For example,
h1
,p
, ordiv
. - Property: The aspect of the element that you want to change, such as
color
,font-size
, ormargin
. - Value: The specific setting for the property, such as
blue
forcolor
or16px
forfont-size
.
Here’s the structure:
selector { property: value; }
Multiple declarations can be included inside a single set of curly braces, separated by semicolons.
How to Add Cascading Style Sheets to a Web Page
There are three main ways to add CSS to an HTML document:
1. Inline Styles
The Inline Cascading Style Sheets is written directly within an HTML element using the style
attribute. This method is rarely used in large projects but can be handy for quick styling changes.
Example:
<p style="color: red;">This is a red paragraph.</p>
2. Internal Styles
Internal CSS is included in the <head>
section of an HTML document inside a <style>
tag. This method is useful for styling a single page.
Example:
<head> <style> h1 { color: green; } </style> </head>
3. External Styles
External CSS is the most common and efficient way to style multiple web pages. The CSS rules are written in a separate .css
file, which is linked to the HTML file.
Example of linking an external CSS file:
<head> <link rel="stylesheet" href="styles.css"> </head>
In the styles.css
file, you can have:
h1 { color: purple; } p { font-size: 18px; }
This separates structure from presentation, making your code easier to maintain.
Common Cascading Style Sheets Properties for Beginners
Let’s take a look at some basic CSS properties that every beginner should know:
1. Color
Defines the color of text.
Example:
2. Background
Sets the background color or image for an element.
Example:
body { background-color: #f0f0f0; }
3. Font-Family
Defines the font type used for text.
Example:
p { font-family: Arial, sans-serif; }
4. Font-Size
Specifies the size of the text.
Example:
h2 { font-size: 24px; }
5. Padding
Adds space inside the element’s border.
Example:
div { padding: 10px; }
6. Margin
Adds space outside the element’s border.
Example:
div { margin: 20px; }
7. Border
Creates a border around an element.
Example:
8. Text-Align
Sets the horizontal alignment of text.
Example:
h1 { text-align: center; }
9. Display
Controls how an element is displayed (block, inline, or none).
Example:
div { display: block; }
10. Width and Height
Sets the dimensions of an element.
Example:
div { width: 300px; height: 150px; }
CSS Box Model
Understanding the box model is crucial for Cascading Style Sheets layout. Every element on a web page is considered a rectangular box, and the box model defines how the size of each element is calculated.
- Content: The actual content of the box (text, image, etc.).
- Padding: Clears an area around the content.
- Border: Surrounds the padding (if any) and content.
- Margin: Clears space outside the border.
Here’s how it looks:
div { margin: 20px; padding: 10px; border: 1px solid black; }
Cascading and Inheritance
CSS stands for Cascading Style Sheets because styles can cascade from parent elements to child elements. The cascade determines which styles are applied when multiple rules target the same element.
Priority of CSS Rules:
- Inline styles (inside the
style
attribute) has the highest priority. - Internal styles (inside the
<style>
tag in the HTML file) has medium priority. - External styles (linked stylesheet) has the lowest priority.
If multiple rules apply, the more specific selector takes precedence. For instance, a class selector (.classname
) will override a general element selector (p
).
CSS is an essential part of web development that allows you to control the look and feel of your websites. Starting with simple properties like color, font size, and layout will help you understand the power of the cascading style sheets. As you get comfortable with the basics, you can dive into more advanced topics like Cascading Style Sheets Grid, Flexbox, animations, and media queries to create responsive, dynamic, and visually appealing web designs.
The key to obtaining a master key in the CSS is practice and loads of experiments—so start coding, and soon you’ll be styling websites with ease!
Author: DRTSWebWorks
Read More: