A Developer's Quick Guide to CSS
This is CSS in just five minutes. In this article, I'll explain CSS how I understand it and the parts of it that I use the most as a full-stack developer.
You can write CSS directly in HTML, such as with inline styles or internal stylesheets, but it's best to use a dedicated CSS file.
Inline Style Example:
html
<h1 style="color:blue;">Styled Heading</h1>
Internal Stylesheet Example:
html
<head>
<style>
h1 {
color: blue;
}
</style>
</head>
CSS has two fundamental components: selectors and attributes.
Selectors: Targeting HTML Elements
Selectors are patterns that search your HTML to find the elements you want to style. There are numerous ways to select elements:
- By element type: h1
- By class: .my-class
- By ID: #unique-id
- By chaining multiple selectors:
- A comma (,
) selects different elements: h1, .my-class
selects both the h1
and elements with my-class
.
- A space selects elements inside another: .container h1
selects h1
elements anywhere inside .container
.
- A >
selects direct children: .container > h1
selects h1
elements that are only one level deep inside .container
.
For maintainability, keeping your selectors as simple as possible, like using a single class name, is often the best approach.
Attributes: Applying the Styles
Once you've selected elements, you apply styles using attributes. These styles are applied sequentially from the top to the bottom of your CSS file.
However, conflicts can arise if multiple styles target the same element. This is resolved through specificity. More specific selectors take priority. The general order of precedence is: ID > Class > Element. Chaining selectors increases specificity, which can get complicated, but you'll develop a feel for it over time.
The attributes themselves fall into two major categories, color and layout, along with many other miscellaneous properties.
Styling with Color
Two of the most common elements to color are backgrounds and text.
- background-color
sets the background.
- color
sets the text color.
You can use simple color words like red
, cornflowerblue
, or skyblue
, but these are limited. More often, you'll use a hex code (e.g., #FF5733
). You can find colors you like online and use a color picker browser extension to get their hex codes.
Structuring with the Box Model
Layout in CSS is based on the box model. Every element on a page is a box, which is itself composed of several nested boxes: content, padding, border, and margin.
You can inspect the box model for any element using your browser's developer tools, which is invaluable for debugging layout issues.
Content: This is the core of the box, where your text and images appear. Its dimensions are set with
width
andheight
. It's often best to use percentages for these values to create responsive layouts that adapt to the parent container and avoid content overflowing. You can align content within this box usingtext-align: center | left | right;
.Padding: This is the space between the content and the border. The
padding
attribute can take one, two, or four values to set the top, right, bottom, and left padding.padding: 10px;
(all four sides)padding: 10px 20px;
(top/bottom, left/right)padding: 10px 20px 15px 5px;
(top, right, bottom, left - clockwise) You can also set sides individually (e.g.,padding-left: 10px;
). An element's background color extends into its padding.
Border: This is the line between the padding and the margin. It has a three-part shorthand syntax:
border: size type color;
(e.g.,border: 1px solid black;
). In 99% of cases, you'll usesolid
for the type.Margin: This is the transparent space outside the border, separating the element from others. It works just like padding, with the same one-to-four value syntax. An element's background color does not extend into the margin.
For sizing padding and margin, you can use pixels (px
) or relative units like rem
, which is relative to the root font size. Using rem
is great for responsive design, as changing the base font size will scale all spacing consistently.
The Display Property
The display
property controls how an element is rendered.
- inline
: Elements sit in a continuous line and do not respect top/bottom margins or padding.
- block
: Elements start on a new line and take up the full available width.
- inline-block
: A hybrid that allows elements to sit on the same line while still respecting width, height, and top/bottom padding/margins.
For more advanced layouts, display: flex
and display: grid
are essential.
- Grid: Excellent for building layouts based on a precise design or wireframe.
- Flex: Amazing for general-purpose spacing and alignment. It's my go-to for easily centering content both horizontally (justify-content: center;
) and vertically (align-items: center;
).
Positioning Elements
You can place elements in custom locations by setting position: relative;
on a parent container and position: absolute;
on a child. Then, you can use top
, right
, bottom
, and left
(with pixel or percentage values) to position the child precisely within the parent.
Interactive Styling with Pseudo-Classes
Pseudo-classes allow you to style elements based on their state. The most common is :hover
, which applies styles when a user's mouse is over an element.
.my-button {
transition: all 0.3s ease-in-out;
}
.my-button:hover {
transform: translateY(-5px);
background-color: blue;
}
To make the change smooth, use the transition
property. You can also create cool effects like making a button move on hover using the transform
property.
Other pseudo-classes, like :first-child
or :nth-child()
, allow you to select elements based on their position among siblings, but it's best to keep selectors simpler whenever possible.
Other Useful Properties
- Fonts:
font-family
andfont-size
are self-explanatory. It's a good practice to provide multiple font families infont-family
as a fallback in case the primary font fails to load. - Backgrounds: Beyond
background-color
, you can setbackground-image
and use thebackground
shorthand to combine multiple properties in one line. - Shadows:
box-shadow
anddrop-shadow
add depth. I typically use an online generator to configure the options and then copy the resulting CSS.
When you look at generated CSS, you might notice vendor prefixes (e.g., -webkit-box-shadow
). These are used for new or experimental features to ensure compatibility with older browsers. My rule of thumb is to use them if they are included in the official documentation for an attribute; otherwise, leave them out.
Animations and Media Queries
You can create complex animations with @keyframes
and the animation
property. It's often easier to search for a pre-made CSS animation for what you need and adapt it.
Finally, media queries are essential for responsive design, allowing you to apply different styles for mobile devices and various screen sizes. They act like if-statements based on screen width.
/* Base styles */
.container {
width: 90%;
}
/* Styles for screens wider than 768px */
@media (min-width: 768px) {
.container {
width: 700px;
}
}
You can use your browser's developer tools to simulate different screen sizes and test your media queries.
Advanced CSS Concepts
- Preprocessors: Tools like Sass/SCSS enhance CSS with features like variables and nested properties, but they don't do anything that can't be done with regular CSS.
- CSS-in-JS: In frameworks like React, you'll encounter CSS-in-JS, often through styled-components. This pattern blurs the lines between JavaScript, CSS, and HTML. These styles are very similar to regular CSS and are converted to standard CSS at compile time.
Join the 10xdev Community
Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.