CSS Selectors and Properties
CSS (Cascading Style Sheets) is a powerful language used to style and layout web pages. To apply styles, CSS uses selectors to target HTML elements, and properties to define the specific styles that should be applied to these elements. Understanding how CSS selectors and properties work is essential for controlling the look and feel of a website. In this article, we’ll explore CSS selectors and properties, and how they can be used to create visually appealing web pages.
What Are CSS Selectors?
CSS selectors are patterns used to select HTML elements on a webpage that you want to style. They tell the browser which HTML element or group of elements to apply specific styles to. Selectors can target elements by their tag name, class, ID, attribute, or other characteristics.
Types of CSS Selectors:
Element Selector (Type Selector):
The element selector targets all elements of a specified type.
For example, the following CSS will apply the style to all
<p>
(paragraph) elements on a page.
Class Selector:
The class selector targets elements with a specific class attribute. It is defined by a period (
.
) followed by the class name.This allows you to apply styles to multiple elements that share the same class.
HTML Example:
ID Selector:
The ID selector targets a single element with a specific ID attribute. It is defined by a hash (
#
) followed by the ID name.Each ID must be unique within a webpage.
HTML Example:
Universal Selector:
The universal selector (
*
) targets all elements on the page.It's useful for applying a global style, like setting margin and padding to zero.
Attribute Selector:
The attribute selector targets elements based on the presence or value of their attributes.
HTML Example:
Descendant Selector:
The descendant selector targets an element that is a child, grandchild, or further descendant of another element.
HTML Example:
Child Selector:
The child selector (
>
) targets direct children of a specified element.
HTML Example:
Pseudo-Classes:
Pseudo-classes allow you to style elements in specific states, such as when they are hovered over, focused, or active.
HTML Example:
Pseudo-Elements:
Pseudo-elements target parts of an element, such as the first letter or the content before an element.
HTML Example:
What Are CSS Properties?
CSS properties define the style applied to an element. They dictate how an element will look, from colors and fonts to positioning and layout. Each property has a corresponding value that specifies the desired style.
Common CSS Properties:
Color and Background Properties:
color
: Sets the text color.background-color
: Sets the background color of an element.background-image
: Sets a background image for an element.
Text Properties:
font-family
: Specifies the font for text.font-size
: Sets the size of the font.font-weight
: Defines the thickness of the font (e.g., normal, bold).line-height
: Sets the amount of space between lines of text.
Box Model Properties:
width
andheight
: Define the dimensions of an element.padding
: Adds space inside an element, between the content and the border.margin
: Adds space outside an element, between it and other elements.border
: Defines the border style, width, and color of an element.
Layout Properties:
display
: Specifies how an element is displayed on the page (e.g., block, inline, flex, grid).position
: Defines the positioning method for an element (e.g., relative, absolute, fixed).top
,right
,bottom
,left
: Specifies the offset of an element in a positioned container.
Visibility and Opacity:
visibility
: Controls whether an element is visible or hidden, while still taking up space.opacity
: Sets the transparency level of an element.
Border Radius and Box Shadow:
border-radius
: Rounds the corners of an element.box-shadow
: Adds shadow effects to elements.
Flexbox and Grid Properties:
flex
: Defines how flex items are distributed in a flex container.grid-template-columns
: Specifies the number of columns in a CSS grid layout.
Last updated
Was this helpful?