Understanding CSS Fundamentals
CSS (Cascading Style Sheets) is a language used to control the appearance of website pages. With CSS, developers can change colors, text sizes, spacing, layouts, and various visual elements to make websites more attractive and user-friendly. This article explains the fundamentals of CSS with simple code examples for beginners.
Table of Contents
What Is CSS?
CSS (Cascading Style Sheets) is a stylesheet language used to control the appearance of HTML elements on a website. While HTML is used to build a page's structure, CSS is used to improve its visual presentation.
For example, HTML can create headings, paragraphs, and buttons. CSS can then change the heading color, text size, background color, and button position.
CSS is an essential part of website development because it helps create pages that are organized, attractive, and comfortable to use.
The Functions of CSS in Website Development
CSS has several main functions, including:
- Controlling colors: Changing text, background, and other element colors.
- Adjusting text: Setting font sizes, font families, and font weights.
- Managing layouts: Controlling element positions and spacing.
- Creating responsive designs: Helping websites adapt to different screen sizes.
- Maintaining design consistency: Allowing multiple pages to share the same styling rules.
With CSS, developers can create websites that are more organized and easier to maintain.
How to Write CSS
CSS rules consist of a selector, a property, and a value.
Example CSS code:
h1 {
color: blue;
font-size: 32px;
}
Code explanation:
- h1 is the selector that targets HTML heading elements.
- color is the property that controls text color.
- blue is the value that sets the text color to blue.
- font-size is the property that controls text size.
- 32px is the value that sets the text size to 32 pixels.
This code makes the text inside an h1 element blue and sets its size to 32 pixels.
An Example of Using CSS with HTML
Here is a simple example of using CSS to style a website page. In this example, CSS is written inside the style tag within an HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Learning CSS</title><style>
body {
background-color: #f0f8ff;
font-family: Arial, sans-serif;
}h1 {
color: #2563eb;
text-align: center;
}p {
color: #333333;
font-size: 18px;
}
</style>
</head>
<body>
<h1>Welcome</h1>
<p>This is an example of a website page
that uses CSS.</p>
</body>
</html>
Code explanation:
- background-color sets the page's background color.
- font-family specifies the font used on the page.
- text-align: center places the heading in the center.
- color controls the text color.
- font-size determines the paragraph text size.
When this code is opened in a browser, the page will have a light blue background, a blue heading in the center, and a paragraph with larger text.
Types of CSS
There are three common ways to apply CSS to an HTML document.
A. Inline CSS
Inline CSS is written directly inside an HTML element using the style attribute.
<p style="color: red;">
This is a red paragraph.
</p>
This method is suitable for simple changes to a single element, but it is less practical when styling many elements.
B. Internal CSS
Internal CSS is written inside the <style> tag within the <head> section of an HTML document.
<style>
p {
color: green;
}
</style>
This rule makes paragraphs targeted by the p selector green within that document.
C. External CSS
External CSS is written in a separate file with the .css extension, such as style.css.
Contents of style.css:
body {
font-family: Arial, sans-serif;
}h1 {
color: purple;
}
To connect it to HTML, add the following code inside the <head> section:
html
<link rel="stylesheet" href="style.css">
External CSS is commonly used in website projects because styling rules can be separated from the HTML structure and reused across multiple pages.
Tips for Learning CSS as a Beginner
To understand CSS more easily, beginners can follow these steps:
- Learn selectors, properties, and values first.
- Start by changing colors, text sizes, and backgrounds.
- Try creating simple designs such as profile cards or personal information pages.
- Gradually learn margin, padding, border, and layout concepts.
- Practice each code example and observe the results in a browser.
- Write clean, organized code so it is easier to read and debug.
Consistent practice will help improve your understanding of how CSS works.
Conclusion
CSS is an important part of website development because it controls the appearance and layout of HTML elements. By understanding CSS fundamentals, including selectors, properties, values, and the use of inline, internal, and external CSS, beginners can start creating more attractive and well-organized website pages.
To master CSS, learn the concepts step by step and keep practicing with simple projects. A solid understanding of CSS also provides a foundation for learning responsive web design and other web development technologies.
Uploaded by
Muazzam Azri Lubis
Tech Contributor