CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of an HTML document.
While HTML structures the content (like headings, paragraphs, images, etc.), CSS styles that content defining how it looks in terms of layout, color, font, spacing, animations, and even responsiveness.
Cascading in CSS means that when multiple rules target the same element, the browser decides which one to apply based on priority. It follows three main principles:
CSS can be applied in three main ways: inline, internal, and external.
Note: To add Comments on CSS start with /* and end with */. They are ignored by the browser and are used to explain your code or leave notes.
CSS syntax defines how styles are applied to HTML elements and consists of a selector and a declaration block. The selector targets the element(s) you want to style (such as p, .class, or #id), and the declaration block, enclosed in curly braces {}, contains one or more declarations separated by semicolons.
Each declaration includes a property and a value. In the example below, we set the font size of a paragraph to 16 pixels. To apply the same styles to multiple selectors, we separate them with commas.
Selectors determine which HTML elements a style rule applies to. CSS offers many types of selectors:
p, h1).container)#header)h1, h2, h3)nav ul li)input[type="text"]):hover, :first-child)::before, ::after)
When multiple rules target the same element, CSS uses specificity to determine which rule takes priority. Here’s the general order from lowest to highest specificity:
* (least specific)div, p.class, attribute selectors#id (most specific)
CSS allows you to specify colors using various formats:
Gradients allow you to create smooth transitions between two or more colors, adding depth and visual interest. CSS supports linear and radial gradients via the background or background-image property.
CSS provides box-shadow to add shadows around elements, creating a sense of depth, and text-shadow to add shadows behind text, improving readability or adding style. Both accept values for horizontal and vertical offset, blur radius, spread, and color.
repeat, no-repeat).cover, contain, or specific dimensions).center center, top left, etc.).
CSS gives you control over the appearance of text through font-related properties. You can specify which fonts to use with the font-family property, using either common system fonts or custom web fonts. To ensure fallback options, it's best to list multiple fonts in order of preference. CSS also lets you control font size, weight, and style to create a consistent and readable design.
In addition to font properties, CSS allows you to control how text is aligned and styled. With properties like text-align, text-decoration, and text-shadow, you can enhance readability and create visual emphasis.
In CSS, every HTML element is considered a rectangular box, and the Box Model describes how these boxes are structured and rendered on the page. Understanding the Box Model is essential for controlling spacing, layout, and element dimensions. Each box consists of four layers: content, padding, border, and margin, from innermost to outermost.
width and height.
The total size of an element is calculated by adding together the content, padding, border, and margin. By default, CSS uses the content-box model, where width and height apply only to the content. You can switch to the border-box model using the box-sizing property, which includes padding and border within the specified width and height.
CSS provides flexible ways to control the size of elements using the width and height properties, as well as constraints using min-width, max-width, min-height, and max-height. These properties help ensure that elements adapt well across different screen sizes while maintaining usability and design structure.
Using min- and max- values ensures better control in responsive layouts. For example, an image might scale with the screen but never go below a readable or viewable size thanks to min-width, or never grow too large thanks to max-width. In the below example the image scales with the screen size but will never be smaller than 200px or larger than 600px.
CSS units define the size of elements, spacing, and text. They can be absolute or relative, but in modern web design, relative units are more flexible and responsive. Below are the most commonly used units in web development:
html). More predictable than em.
This example uses a mix of percentages, rem, em, and viewport units to create a flexible, responsive container that adjusts across screen sizes.
The display property in CSS controls how an element is rendered in the layout.
Whether it behaves as a block, inline, or flex container, for example.
It’s one of the most important properties for determining layout behavior and element positioning in modern web design.
<div>, <p> by default).<span>).inline, but allows setting width and height.display: flex.
In this example, .container uses flex to layout items in a row, .text stays inline with other content, and .hidden-box is removed from the layout using display: none.
The overflow property controls what happens when content exceeds the dimensions of its container. It helps manage scrolling and clipping behavior. Common values include:
The visibility property controls whether an element is visible or not, but unlike display: none, it keeps the space the element occupies. Common values include:
In this example, the .box will show scrollbars if the content overflows, and it remains visible in the layout.
The position property in CSS controls how an element is placed in the document flow and allows for precise control of its location using the top, right, bottom, and left properties.
Different position values change how the element behaves relative to its surroundings.
relative until a scroll threshold is met, then acts like fixed.The z-index property in CSS controls the stacking order of elements along the z-axis (front-to-back). This becomes useful when elements overlap. It decides which element appears on top.
Elements with a higher z-index number appear in front of elements with a lower number. However, z-index only works on elements with a position value other than static (e.g., relative, absolute, fixed, or sticky).
position: relative; or other non-static positioning is requiredz-index: 1; will place the element behind z-index: 2;z-index is auto, which follows source order
In the example above, the blue box (z-index: 2) will appear on top of the red box (z-index: 1) even though it comes later on the page.
position is set
Flexbox (short for Flexible Box Layout) is a one-dimensional layout method used in CSS to align and distribute space among items within a container.
It allows you to arrange items horizontally or vertically with ease, making responsive design simpler and more efficient. By turning an element into a flex container, its direct children (flex items) gain access to powerful alignment and spacing properties.
display: flex or display: inline-flex.display: flex – Turns the container into a flex container.flex-direction – Sets the direction of items (row, row-reverse, column, column-reverse).justify-content – Aligns items along the main axis (start, center, space-between, etc.).align-items – Aligns items along the cross axis (stretch, center, flex-start, etc.).flex-wrap – Allows items to wrap onto multiple lines if needed.
flex-grow – Defines how much a flex item will grow relative to others.flex-shrink – Defines how much a flex item will shrink when necessary.flex-basis – Sets the initial size of a flex item before space is distributed.align-self – Overrides align-items for individual flex items.order – Controls the order in which items appear in the layout.CSS Grid Layout is a two-dimensional layout system that allows you to design web pages using a grid-based structure. It lets you control both rows and columns, making it ideal for creating complex layouts without relying on floats or positioning tricks.
A pseudo-class is a keyword added to a selector that lets you style elements based on their state, position, or user interaction, without needing a class or ID.
In the below example when the user hovers over the button, its background changes to green.
A pseudo-element lets you style specific parts of an element that you can’t easily target with regular selectors.
In the below example, only the first line of each paragraph will be bold and blue
CSS Variables (also called custom properties) allow you to store values in reusable names, making your styles easier to maintain and update. Instead of repeating values like colors or padding across different rules, define them once and reuse them anywhere.
In the below example the root sets the font size and text color for all h1 elements using reusable variables.
The transform property in CSS allows you to visually manipulate elements by rotating, scaling, skewing, or translating them in 2D or 3D space. It does not affect the actual layout or position in the document flow, but it changes how the element appears on the screen.
The transform-origin property lets you set the point around which a transformation occurs (default is the center of the element). For example, you can rotate around the top-left corner or bottom center.
In the example below, The transform: rotate(45deg); rule rotates the element by 45 degrees clockwise from its center.
Transitions allow you to change property values smoothly (over a duration) rather than instantly. You can create hover effects, color fades, size changes, and more with minimal code.
In the below example, when hovered, the button smoothly changes its background color over 0.3 seconds
color, transform)0.5s)ease, linear, ease-in-out)
Animations allow you to define keyframes (specific steps in a motion) and control how an element moves, fades, transforms, or behaves over time — even looping infinitely if needed.
In the below example, the .pulse class applies a green background color to an element. When you hover over this element, it triggers an infinite animation called pulse that lasts 2 seconds and uses an ease-in-out timing function.
The @keyframes pulse defines the animation sequence: at the start (0%) and end (100%), the element is at its normal scale (1), while at the midpoint (50%), it scales up to 1.2 times its size, creating a smooth pulsing effect that repeatedly grows and shrinks the element.
infinite)normal, alternate)forwards)Media queries allow you to apply CSS rules conditionally based on the characteristics of the user’s device, such as screen width, height, orientation, and resolution. They are a cornerstone of responsive design, helping your website adapt to different screen sizes, from phones and tablets to large desktop monitors.
A media query uses the @media rule, followed by a condition, and wraps a block of CSS rules. The most commonly used condition is screen width, using min-width or max-width to target devices based on their viewport size.
In the above example, when the screen width is 768 pixels or less, the .container class switches to a vertical layout and applies different padding. This ensures the content remains readable and well-structured on smaller screens.
Advanced CSS selectors give you more power and precision when targeting elements in your HTML. They go beyond basic element, class, and ID selectors, allowing you to apply styles based on relationships, attributes, and state.
In the above example, all odd-numbered list items inside a <ul> will have a light gray background.
CSS functions are built-in methods that perform calculations, return values, or define behaviors within CSS properties. They help make your styles more flexible, powerful, and dynamic.
In the above example, .container defines one grid column between 150px and 400px, no matter the screen size.
The object-fit property defines how content like images or videos should be resized to fit their container. It's particularly useful when dealing with fixed-size boxes that display media.
The object-position property controls the alignment of the replaced element (like an image or video) inside its container when using object-fit. It works similar to background-position.
In the below example, the image is aligned to the top-left corner of its container.
In CSS, scroll behavior refers to how scrolling occurs inside a container or on the page. CSS provides several properties that control how the browser handles scrolling, such as the movement style, snap positions, and spacing when an element is brought into view.
Defines how scrolling happens when navigating to an element (for example, when clicking anchor links or using JavaScript’s scrollIntoView()).
These properties work together to create controlled scroll snapping. The container uses scroll-snap-type to define how snapping occurs, and the child elements use scroll-snap-align to define where they should snap within the container.
x) or vertical (y) axis.These properties are similar to margin and padding, but they apply when scrolling an element into view (such as through anchor navigation or JavaScript).
This example enables smooth scrolling and sets up vertical snapping with proximity behavior:
.scroll-container {
scroll-behavior: smooth;
scroll-snap-type: y proximity;
}
.snap-item {
scroll-snap-align: center;
}
By default, browsers display scrollbars in their own style, but you can customize their appearance using CSS. This allows you to match the scrollbar with your website’s design and improve the overall user experience.
You can style scrollbars using pseudo-elements in CSS, especially for WebKit-based browsers (like Chrome, Edge, and Safari). Firefox supports a different syntax.
This example sets a custom width, thumb color, and track background for a vertical scrollbar in WebKit browsers.
CSS filters allow you to apply graphical effects like blur, brightness, contrast, and more to elements, such as images or divs, without needing to edit the images themselves.
Blend modes control how elements blend or mix colors with their background or other overlapping elements, similar to blend modes in image editing software like Photoshop.
In the example below, a grayscale, brightness, and blur filter is applied to an image, and a blend mode is used to mix the image's colors with the background gradient. Flexbox centers the image on the screen, and the body uses a horizontal gradient to enhance the blending effect.
CSS Architecture refers to the organized structure and methodology of writing CSS code to make styles scalable, maintainable, and easier to understand in large projects.
BEM (Block Element Modifier) is a popular naming convention for CSS classes that helps create reusable components and code consistency by clearly defining blocks, their elements, and their modifiers.
button.button__icon.button--primary or button__icon--small.
In the example below, the CSS classes follow the BEM convention. A card block contains elements like card__title and card__button, with a modifier card__button--disabled that changes the button’s style.
__ to separate blocks and elements, and double hyphens -- for modifiers.