Properties
The Building Blocks of CSS Styling
You've learned about CSS selectors, which allow you to target specific HTML elements.
Now, let's dive into CSS properties, which are the building blocks of CSS styling.
Properties are the individual aspects of an element's appearance that you can control, like its color, font, size, and position.
Each property has a name (e.g., color
, font-size
, margin
) and a value (e.g., blue
, 16px
, 10px
).
Think of CSS properties as the knobs and dials that you use to adjust the look and feel of your web pages.
Basic Syntax
The basic syntax for setting a CSS property is:
Common CSS Properties
There are many CSS properties, and you don't need to memorize them all at once. Let's explore some of the most common and useful ones, grouped by category:
Text Properties
color
: Sets the text color.
font-size
: Sets the size of the font. Common units includepx
(pixels),em
(relative to the font size of the parent element), andrem
(relative to the font size of the root element).
font-family
: Specifies the font to use. You can specify multiple fonts as a fallback in case the browser doesn't have one available.
font-weight
: Sets the weight (boldness) of the font. Common values includenormal
,bold
,lighter
,bolder
, and numeric values (e.g.,400
for normal,700
for bold).
text-align
: Sets the horizontal alignment of text. Common values includeleft
,right
,center
, andjustify
.
line-height
: Sets the height of a line of text. A value without units (e.g.,1.5
) is usually best, as it's relative to the font size.
Background Properties
background-color
: Sets the background color of an element.
background-image
: Sets a background image for an element.
background-repeat
: Specifies how the background image should be repeated. Common values includerepeat
,no-repeat
,repeat-x
, andrepeat-y
.
background-position
: Specifies the position of the background image.
Box Model Properties (Very Important!)
The CSS box model is a fundamental concept in web development. It describes the rectangular boxes that are generated for HTML elements and how these boxes interact with each other. We have a separate page for this, refer to the Box Model page.
width
: Sets the width of the element's content area.height
: Sets the height of the element's content area.padding
: Sets the padding (space between the content and the border) on all four sides of the element.border
: Sets the border around the element.margin
: Sets the margin (space outside the border) on all four sides of the element.
Layout Properties
display
: Specifies the display type of an element. Common values includeblock
,inline
,inline-block
,flex
,grid
, andnone
. Layout properties will be on its own separate page, check out the Layout page.
-
position
: Specifies the positioning method used for an element. Common values includestatic
,relative
,absolute
,fixed
, andsticky
. The positioning property will also be on the Layout page. -
float
: Floats an element to the left or right of its container, allowing text and other inline elements to wrap around it.
Border Properties
border-width
: Sets the width of the border.
border-style
: Sets the style of the border. Common values includesolid
,dashed
,dotted
,double
, andnone
.
border-color
: Sets the color of the border.
border
: A shorthand property for setting theborder-width
,border-style
, andborder-color
in a single declaration.
Other Properties
cursor
: Specifies the type of cursor to display when the mouse pointer is over an element.
opacity
: Sets the opacity (transparency) of an element. Values range from0
(completely transparent) to1
(completely opaque).
visibility
: Specifies whether an element is visible or hidden. Values includevisible
andhidden
. The difference betweenvisibility: hidden;
anddisplay: none;
is thatvisibility: hidden;
still takes up space on the page, whiledisplay: none;
removes the element from the page layout entirely.
Shorthand Properties
Many CSS properties have shorthand versions that allow you to set multiple values in a single declaration.
We saw an example with the border
property.
Here are some other common shorthand properties:
margin
andpadding
: You can use these to set the top, right, bottom, and left margins/paddings in a single declaration.
font
: You can use this to set thefont-style
,font-variant
,font-weight
,font-size
,line-height
, andfont-family
in a single declaration.
Units in CSS
CSS properties often require you to specify units. Here are some of the most common units:
- Absolute Units:
px
(pixels): Represents a fixed size on the screen.pt
(points): Traditionally used for print media.
- Relative Units:
em
: Relative to the font size of the parent element.rem
: Relative to the font size of the root element (<html>
).%
: Relative to the size of the parent element.vw
: Relative to 1% of the viewport's width.vh
: Relative to 1% of the viewport's height.
Using relative units (especially em
and rem
) is generally recommended for creating responsive designs that adapt to different screen sizes.
Experiment and Explore!
This is just a glimpse of the vast world of CSS properties. The best way to learn is to experiment and explore. Try changing the values of different properties and see how they affect the appearance of your web pages. Don't be afraid to consult the CSS documentation for more information.
Last updated on