Description and Basic Syntax
CSS - Cascading Style Sheets
give content style
fonts, color, backgrounds
describes the presentation of the HTML document
CSS Terminology
Selectors - CSS selectors are patterns used to select an HTML element or a set of elements that you want to style (type or element selectors, class selectors, ID selectors, attribute selectors, pseudo-class selectors)
Properties - CSS properties are the attributes that you use to style HTML elements. They can be used to control the appearance of elements, such as their color, font, size, and spacing. CSS properties can also be used to control the behavior of elements, such as their position on the page and how they interact with other elements.
Values - CSS values are the data that you use to set the values of CSS properties. CSS values can be simple, such as the color
red
, or they can be more complex, such as a linear gradient or a shadow.
Cascading effect
Styles cascade from top to bottom as they are overridden while the CSS file progresses
CSS Selectors
Type selectors
Use the element type.
Class selectors
Use the class attribute on the element.
ID selectors
Use the element's ID attribute which should have a unique value.
Calculating Specificity
CSS specificity is a property of CSS selectors that determines which selector has the highest priority and is therefore applied to an element. The higher the specificity, the higher the priority.
every selector has a specificity weight
the higher the specificity weight the more superiority the selector is given when styling a conflict appears
id selector > class selector > type selector
Combining selectors
Descendant combinator: a space
character between 2 selectors. It matches all the elements indicated by the second selector that have an ancestor matching the first selector.
Child combinator: a >
character between 2 selectors. It is more strict than the descendant combinator, meaning it only selects the direct children of the first selector matching the criteria from the second selector.
CSS Selector Reference
A more comprehensive list of selectors and selectors combinations can be found at the following source:
Common property values
Display: block / inline / inline-block / none
Width/Height: length (px, pt, cm, etc) % / auto
Margin/Padding: length (px, pt, cm, etc) / % / auto
Background-color: (keywords, hexadecimal notation, RGB)
The Box Model
Every element on the page is a rectangular box that may have properties set for: height
, width
, borders
, margins
.
Width - depends on the display property
set using the
width
propertyblock elements - 100%
inline elements - expand and contract horizontally to accommodate their content
Height - depends on the element’s content
set using the
height
property (only for non-inline elements)Margin
the amount of space that surrounds the element
outside the element's border
Padding
the amount of space between an element’s content and its border
Border
outline around the element
solid, double, dash, dotted
border corners can be rounded using the border-radius property
Inline elements treat margins and paddings differently:
Margins only work horizontally - left and right
Padding works on all sides but the top and bottom paddings can bleed into the lines above and below an element.
Box sizing:
The box-sizing
property defines how the width and height of an element are calculated: should they include padding and borders, or not and has the following possible values: content-box
, padding-box
, and border-box
Positioning Content
The float
Property:
allows us to remove an element from the normal page flow
can take left or right value
originally used to wrap text around images
all other elements will flow around the floated element
heavily used in multiple columns layouts
can impact styles of surrounding elements
zero-height container problem for floated elements
The inline-block
method:
done by setting the display property to inline-block value
space between inline-block elements needs to be removed
The position property
static
- the normal position in the documents flow
fixed
- positioned relative to the browser’s window (viewport)
relative
- relative to itself and also adding the possibility to use z-index on that element
absolute
- positions the elements relative to the first parent element that is non-statically positioned
Media Queries
Media queries in CSS allow you to apply different styles to a web page depending on the characteristics of the device or environment in which the page is being viewed. For example, you can use media queries to create different styles for desktop computers, laptops, tablets, and smartphones. You can also use media queries to create different styles for printers, screen readers, and other devices.
Media queries are defined using the @media
rule. The @media
rule takes a media type expression as its argument. The media type expression specifies the characteristics of the device or environment in which the page is being viewed.
You can also use media queries to combine multiple media type expressions. For example, the following media query will apply the specified styles to the web page when it is being viewed on a screen that is at least 480 pixels wide and in portrait mode:
Rems
A rem (root em) is a relative unit of measurement in CSS that is equal to the font size of the root element. The root element is the <html>
element, so 1 rem is equal to the font size set in the browser's settings.
Rems are useful for creating responsive web pages because they allow you to scale font sizes and other CSS values based on the root element's font size. This means that your web pages will look good on all devices, regardless of the user's browser settings or device type.
For example, the following CSS rule will set the font size of all <p>
elements to 1 rem:
Here are some tips for using rems effectively:
Use rems to set font sizes and other CSS values that need to be scalable.
Use rems to create responsive web pages that look good on all devices.
Avoid using rems to set fixed values, such as the width of a container. If you need to set a fixed value, use pixels or another absolute unit of measurement.
Last updated
Was this helpful?