What are selectors and properties in CSS?
Cascading Style Sheets (CSS) is a fundamental technology used to style web documents. It consists of three essential components: selectors, properties, and values. In this article, we will delve into each of these components, providing examples along the way to illustrate their usage.
Spis treści
Selectors
CSS selectors are patterns used to select and style HTML elements. They allow you to define how specific elements should be displayed, whether it's changing their color, size, layout, or other styling properties. Let's dive into the different types of selectors and how they work.
Type Selectors:
Type selectors, also known as element selectors, target HTML elements by their tag names. They are the simplest and most common selectors. For example, to style all paragraphs (<p> elements) on a webpage, you can use the following CSS:
p {
color: blue;
}
In the accompanying HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<p>This is a blue paragraph.</p>
<p>Another blue paragraph.</p>
</body>
</html>
When you run the html file in any web-browser, you will see the following output:
Here, all <p> elements will have blue text.
Class Selectors:
Class selectors target elements with specific class attributes. They start with a period (.) followed by the class name. For example, let's style all elements with the class "highlight":
.highlight {
background-color: yellow;
font-weight: bold;
}
In the HTML:
<div class="highlight">This is highlighted text.</div>
<p>This is a regular paragraph.</p>
<div class="highlight">Another highlighted text.</div>
Output:
This CSS had made elements with the "highlight" class appear with a yellow background and bold text.
ID Selectors:
ID selectors target elements with unique id attributes. They start with a hash (#) followed by the id name. IDs should be unique on a page. For instance, let's style an element with the ID "header":
#header {
font-size: 24px;
text-align: center;
}
And in the HTML page:
<header id="header">This is the header</header>
Output:
This CSS had changed the font size and text alignment of the element with the "header" ID.
Universal Selectors:
The universal selector (*) matches all elements on a page. It's a powerful but potentially performance-intensive selector. For example, to remove all default margin and padding from all elements:
* {
margin: 0;
padding: 0;
}
Use the universal selector sparingly, as it can affect many elements and impact performance.
Attribute Selectors:
Attribute selectors target elements with specific attributes and values. For instance, to style all links (<a> elements) with a specific target attribute value
a[target="_blank"] {
text-decoration: underline;
}
HTML page:
<a href="https://example.com" target="_blank">Visit Example</a>
Output:
This CSS had underlined links with the target attribute set to "_blank."
Properties in CSS
CSS properties are rules that control the visual aspects of HTML elements. They define attributes such as color, size, spacing, and more. These properties are the building blocks for creating stylish and responsive web designs.
The Basics of CSS Properties
CSS properties are the building blocks of web design. They define how HTML elements should appear on a web page. A CSS property consists of a property name and a value, separated by a colon. Here's a basic structure:
property-name: property-value;
For instance, if you want to set the color of a text element to red, you would use the color property like this:
color: red;
Applying CSS Properties to HTML Elements
To apply CSS properties to HTML elements, you need to select the target element(s) and specify the properties within a set of curly braces {}. This is typically done within a <style> block in the HTML <head> section or in an external CSS file. Here's an example of how to apply CSS properties to an HTML element:
<!DOCTYPE html>
<html>
<head>
<style>
/* CSS code */
h1 {
color: blue;
font-size: 24px;
}
</style>
</head>
<body>
<!-- HTML element -->
<h1>This is a Heading</h1>
</body>
</html>
Open this html file in a web-browser and you will see the following output:
In this example, we select the <h1> element and set its color property to blue and its font-size property to 24 pixels.
Common CSS Properties
- color
The color property defines the text color of an element. You can use predefined color names, hexadecimal color codes, RGB values, or HSL values. Here's an example:
/* Using predefined color name */
p {
color: green;
}
/* Using hexadecimal color code */
span {
color: #FF5733;
}
- Font
The font-size property determines the size of the text within an element. You can specify it in pixels, em units, percentages, or other valid units.
p {
font-size: 18px;
}
h2 {
font-size: 2em;
}
- Background Color
The background-color property sets the background color of an element. You can use the same color values as with the color property:
div {
background-color: #F5A623;
}
- Margin and padding
margin and padding properties control the spacing around and inside an element, respectively. They can be set individually for each side (top, right, bottom, left) or in shorthand:
/* Individual margin values */
div {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
}
/* Shorthand margin and padding */
p {
margin: 10px; /* All sides */
padding: 5px 10px; /* Top and bottom, right and left */
}
- Border
The border property allows you to define the border around an element. You can specify the border width, style, and color:
div {
border: 2px solid #333;
}
CSS properties play a crucial role in web design by controlling the visual aspects of HTML elements. Understanding how to use these properties effectively is essential for creating appealing and well-structured web pages.
Conclusion
In conclusion, CSS selectors and properties are the fundamental building blocks of web design and styling. Selectors allow you to target specific HTML elements, groups of elements, or even elements with specific attributes, giving you fine-grained control over how your web content is presented. Understanding the various types of selectors empowers you to create stylish and responsive web layouts efficiently.
On the other hand, CSS properties define the visual attributes of HTML elements. These properties enable you to control everything from text color and font size to background colors, spacing, and borders. By using CSS properties effectively, you can transform plain HTML documents into visually appealing and engaging web pages.