CSS Cheat Sheet 2024 : Key Properties, Selectors, and Styling Techniques

tutorials

CSS (Cascading Style Sheets) is the language used to style HTML documents. From defining colors and layout to adjusting font styles and positioning, CSS plays an essential role in creating visually appealing web pages. This cheat sheet serves as a quick reference guide to CSS properties, values, and other core concepts.

1. CSS Syntax and Selectors

CSS syntax defines how to apply styles to specific HTML elements using selectors. Selectors target HTML elements, while properties and values define how these elements are styled.

NameDescriptionExample
SelectorTargets the HTML element(s) to be styled.p { color: blue; } – styles all <p> elements with blue text.
PropertyThe style attribute to be applied (e.g., color, font-size).color, font-size, margin, etc.
ValueDefines the specific style for the property.color: red;, font-size: 16px;
CommentsAdds a comment for clarity, not affecting code./* This is a comment */

2. CSS Selectors and Combinators

Selectors define which elements a style applies to, and combinators allow more precise targeting.

SelectorDescriptionExample
* (Universal Selector)Selects all elements.* { margin: 0; padding: 0; }
#idSelects an element by its unique ID.#main { background-color: yellow; }
.classSelects all elements with the specified class..highlight { color: red; }
elementSelects all elements of a specific type.h1 { font-size: 24px; }
element, elementSelects multiple elements.h1, h2, h3 { margin: 0; }
element elementSelects descendants of an element.div p { color: blue; }
element > elementSelects direct children of an element.div > p { font-weight: bold; }

3. Text and Font Styling

CSS allows you to control the typography of your website by modifying text and font styles. The following properties are used to style text in different ways.

PropertyDescriptionValuesExample
colorSets the color of the text.Color name, hex, RGB, HSLcolor: #333; or color: rgb(51, 51, 51);
font-familyDefines the font type for text.Font name, generic familyfont-family: Arial, sans-serif;
font-sizeSets the size of the font.px, em, %font-size: 16px;
font-weightSets the thickness of the text.normal, bold, lighter, numeric valuesfont-weight: bold;
font-styleItalicizes the text.normal, italic, obliquefont-style: italic;
line-heightDefines the space between lines of text.normal, numeric, lengthline-height: 1.5;
text-alignAligns the text inside an element.left, right, center, justifytext-align: center;
text-transformControls capitalization of text.uppercase, lowercase, capitalizetext-transform: uppercase;
text-decorationAdds text styling like underline, overline, or line-through.none, underline, overline, line-throughtext-decoration: underline;

4. Box Model: Margins, Padding, and Borders

The CSS box model defines how elements are structured in terms of space (padding, margins, and borders).

PropertyDescriptionValuesExample
marginDefines the space outside an element's border.Length, automargin: 10px;
paddingDefines the space between the content and the border.Length, percentagepadding: 20px;
borderAdds a border around the element.width style colorborder: 1px solid black;
border-radiusRounds the corners of an element's border.Length, percentageborder-radius: 10px;
box-shadowAdds a shadow effect around an element.h-offset v-offset blur colorbox-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);
widthSets the width of an element.Length, percentagewidth: 300px;
heightSets the height of an element.Length, percentageheight: 200px;

5. Layout and Positioning

CSS provides several ways to control the layout and positioning of elements on a webpage. The following properties allow for both static and dynamic positioning.

PropertyDescriptionValuesExample
displaySpecifies how an element is displayed.block, inline, inline-block, nonedisplay: block;
positionSpecifies the type of positioning for an element.static, relative, absolute, fixed, stickyposition: relative;
top, right, bottom, leftDefines the positioning offsets for positioned elements.Length, percentagetop: 10px;
z-indexDefines the stack order of elements.Numeric valuez-index: 10;
floatFloats an element to the left or right.left, right, nonefloat: left;
clearSpecifies whether an element should be moved below floating elements.left, right, both, noneclear: both;

6. Backgrounds and Gradients

CSS enables you to set solid color backgrounds or more advanced gradient backgrounds. These properties help you add depth and interest to web designs.

PropertyDescriptionValuesExample
background-colorSets the background color of an element.Color name, hex, RGB, HSLbackground-color: #f4f4f4;
background-imageSets a background image for an element.url()background-image: url('background.jpg');
background-repeatDefines how a background image is repeated.repeat, no-repeat, repeat-x, repeat-ybackground-repeat: no-repeat;
background-positionSpecifies the position of the background image.left, center, right, top, bottom, lengthbackground-position: center center;
background-sizeSets the size of the background image.cover, contain, lengthbackground-size: cover;
background-clipSpecifies whether the background extends under the border.border-box, padding-box, content-boxbackground-clip: padding-box;
linear-gradientCreates a linear gradient background.Gradient direction, color stopsbackground: linear-gradient(to right, red, yellow);

7. Transitions, Animations, and Transforms

CSS allows for dynamic transitions, animations, and transformations that enhance user experience and interactivity.

PropertyDescriptionValuesExample
transitionCreates smooth transitions between different states of an element.property duration timing-functiontransition: background-color 0.3s ease-in-out;
transition-durationSpecifies the duration of the transition effect.Time (s, ms)transition-duration: 0.5s;
transition-timing-functionSpecifies the speed curve of the transition.ease, linear, ease-in, ease-out, ease-in-outtransition-timing-function: ease-in;
animationDefines a keyframe animation.name duration timing-functionanimation: bounce 2s infinite;
animation-nameSpecifies the name of the @keyframes animation.Keyframe nameanimation-name: slideIn;
animation-durationSpecifies how long an animation takes to complete.Time (s, ms)animation-duration: 1.5s;
transformApplies 2D or 3D transformations to an element.rotate(), scale(), translate(), skew()transform: rotate(45deg);
transform-originDefines the origin for transformations.center, top left, 50% 50%transform-origin: center center;
scale()Resizes the element (scales it).Numbertransform: scale(1.5);
rotate()Rotates the element.Angle (deg, turn)transform: rotate(90deg);
translate()Moves the element along the X and Y axes.Length or percentagetransform: translate(50px, 100px);

8. Flexbox

Flexbox is a powerful layout module that allows for flexible and responsive layouts. It is especially useful for aligning and distributing space among items within a container.

PropertyDescriptionValuesExample
display: flexTurns an element into a flex container.flex, inline-flexdisplay: flex;
flex-directionDefines the direction of the flex items.row, row-reverse, column, column-reverseflex-direction: row;
justify-contentAligns items horizontally in a flex container.flex-start, flex-end, center, space-between, space-aroundjustify-content: space-between;
align-itemsAligns items vertically in a flex container.flex-start, flex-end, center, baseline, stretchalign-items: center;
flex-wrapDefines whether flex items should wrap onto multiple lines.nowrap, wrap, wrap-reverseflex-wrap: wrap;
align-contentAligns the flex container’s lines when there is extra space.stretch, center, flex-start, flex-end, space-between, space-aroundalign-content: stretch;
flex-growSpecifies how much an item will grow relative to the others.Numberflex-grow: 2;
flex-shrinkSpecifies how much an item will shrink relative to the others.Numberflex-shrink: 1;
flex-basisDefines the initial size of a flex item before any remaining space is distributed.auto, length, percentageflex-basis: 200px;

9. Grid Layout

CSS Grid is another powerful layout system that allows for complex, two-dimensional layouts. It provides more control over rows and columns than Flexbox.

PropertyDescriptionValuesExample
display: gridTurns an element into a grid container.grid, inline-griddisplay: grid;
grid-template-columnsDefines the number and size of columns in the grid.Length, percentage, autogrid-template-columns: repeat(3, 1fr);
grid-template-rowsDefines the number and size of rows in the grid.Length, percentage, autogrid-template-rows: 100px auto 100px;
grid-column-gapSpecifies the gap between grid columns.Lengthgrid-column-gap: 20px;
grid-row-gapSpecifies the gap between grid rows.Lengthgrid-row-gap: 20px;
grid-gapShorthand for both grid-column-gap and grid-row-gap.Lengthgrid-gap: 20px 40px;
grid-columnSpecifies how many columns an item should span.span or autogrid-column: span 2;
grid-rowSpecifies how many rows an item should span.span or autogrid-row: span 3;
justify-itemsAligns grid items along the row axis.start, end, center, stretchjustify-items: center;
align-itemsAligns grid items along the column axis.start, end, center, stretchalign-items: stretch;

10. Media Queries

Media queries allow you to apply styles based on the device's characteristics, such as screen width or resolution. This enables responsive design.

PropertyDescriptionValuesExample
@mediaApplies styles based on specified conditions.screen, min-width, max-width@media screen and (min-width: 768px) { ... }
min-widthSets a minimum width for applying styles.Length@media (min-width: 600px) { ... }
max-widthSets a maximum width for applying styles.Length@media (max-width: 1200px) { ... }

This CSS Cheat Sheet 2024 provides a comprehensive reference for core CSS properties, values, and techniques, helping you style and design your web pages effectively. Whether you're working with basic typography or advanced layout systems like Flexbox and Grid, mastering these properties is essential for creating responsive, visually appealing websites.

tools

CSS

Related Articles