EDT
Everyday Dev Tools
Fast tools, no fuss
Home / Tutorials / CSS Tools and Techniques: Formatting, Gradients, and Flexbox
CSS

CSS Tools and Techniques: Formatting, Gradients, and Flexbox

2024-06-20 · 14 min read

A practical guide to CSS formatting, gradient creation, and flexbox layouts with examples.

Why CSS Formatting Matters (And My Preferred Style)

CSS works regardless of how you format it — but your teammates care! Consistent formatting makes CSS easier to read, easier to debug, and easier to maintain.

Here's the formatting style I recommend (and what our CSS Formatter produces):

.selector {
  property: value;
  another-property: value;
}

**The rules:**

- One selector per line

- Opening brace on the same line as selector

- One property per line

- Indent properties (2 or 4 spaces, just be consistent!)

- Space after colon

- Semicolons after every property (even the last one!)

- Closing brace on its own line

This might seem nitpicky, but when you're debugging at 3 AM trying to find a missing semicolon, you'll appreciate consistent formatting!

Organizing Your CSS (How to Keep It Maintainable)

Once your project grows, CSS organization becomes important. Here are approaches I've used successfully:

**Approach 1: Group by category**

Put position first, then box model, then typography, then colors. I like this because related properties are together.

.component {
  /* Position */
  position: absolute;
  top: 0;
  /* Box model */
  display: flex;
  padding: 1rem;
  /* Typography */
  font-size: 1rem;
  line-height: 1.5;
  /* Colors */
  background: white;
  color: #111;
}

**Approach 2: Alphabetical order**

Some people prefer this because you can always find a property quickly.

**Approach 3: Component-based** (with CSS modules or similar)

Modern frameworks often use scoped CSS per component. This is my favorite now — it keeps related CSS together with the component that uses it.

Whichever you choose, be consistent! And don't be afraid to use a tool like Prettier or our CSS Formatter to handle formatting automatically.

CSS Gradients: From Simple to Spectacular

Gradients used to require images — now we can do it all in CSS! Let's break down the types.

**Linear Gradients:**

The basic workhorse. Goes in a straight line.

/* Top to bottom (default) */
background: linear-gradient(red, blue);
/* Left to right */
background: linear-gradient(to right, red, blue);
/* At an angle */
background: linear-gradient(45deg, red, blue);
/* Multiple color stops */
background: linear-gradient(red, yellow, green, blue);

**Radial Gradients:**

Radiates from a center point.

background: radial-gradient(circle, red, blue);

**Conic Gradients:**

Rotates around a center. Great for pie charts and color wheels!

background: conic-gradient(red, yellow, green, blue, red);

Pro tip: You can specify color stop positions! `linear-gradient(red 0%, yellow 50%, blue 100%)` gives you precise control.

Our CSS Gradient Generator lets you experiment visually — it's much faster than typing these by hand!

Gradient Tricks I Actually Use

Here are gradient techniques that make it into almost every project I build:

**Trick 1: Subtle overlays on images**

.hero {
  background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.8)),
              url('hero.jpg');
}

Great for making text readable on top of photos.

**Trick 2: Fade to edge**

.fade-out {
  background: linear-gradient(to right, white, transparent);
}

Good for indicating there's more to scroll.

**Trick 3: Glass/shine effects**

A subtle gradient can give depth to buttons or cards.

**Trick 4: Animated gradients**

With `background-position` and a larger gradient, you can create nice subtle animations.

The best gradients are subtle! If your users notice the gradient itself, it's probably too strong.

Flexbox: Finally, Layouts Make Sense!

I remember the bad old days of CSS layouts: Tables, floats, clearfix hacks. We don't need those anymore — flexbox is here!

The flexbox model has two main parts: The container and the items.

**Container properties:**

- `display: flex` — Turn on flexbox!

- `flex-direction` — Main axis: `row` or `column`

- `justify-content` — Align along main axis: `flex-start`, `flex-end`, `center`, `space-between`, `space-around`, `space-evenly`

- `align-items` — Align along cross axis: `flex-start`, `flex-end`, `center`, `stretch`

- `flex-wrap` — Wrap items to new lines: `wrap` or `nowrap`

- `gap` — Space between items (game changer!)

**Item properties:**

- `flex` — Shorthand for `flex-grow`, `flex-shrink`, `flex-basis`

- `align-self` — Override alignment for one item

- `order` — Change display order (use sparingly!)

Our Flexbox Generator lets you play with all these properties and see the results instantly — it's the fastest way to learn!

Flexbox Patterns I Use Every Day

These are the flexbox patterns that go into almost everything I build:

**Pattern 1: Center something (finally easy!)**

.center {
  display: flex;
  align-items: center;
  justify-content: center;
}

**Pattern 2: Navbar with logo left, links right**

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

**Pattern 3: Equal-width columns**

.columns {
  display: flex;
  gap: 1rem;
}
.column {
  flex: 1; /* Grow equally */
}

**Pattern 4: Card grid that wraps**

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.card {
  flex: 1 1 300px; /* Grow, shrink, 300px basis */
}

**Pattern 5: Sticky footer**

`min-height: 100vh` on body, `flex-direction: column`, and `margin-top: auto` on the footer.

Flexbox is so much simpler than the old ways. If you haven't learned it yet, use our tool to experiment and it will click!

CSS Best Practices (My Rules for a Maintainable Codebase)

Here are the rules I try to follow on every project:

**1. Use consistent naming** — BEM, utility classes, CSS modules, whatever — just be consistent.

**2. Don't use `!important` unless you have to** — It's a sign your specificity is wrong. Sometimes you need it, but think twice.

**3. Prefer descriptive class names** — `.button-primary` is better than `.blue-button` if the color might change.

**4. Use CSS variables for design tokens** — Colors, spacing, fonts should all be variables.

**5. Keep responsive code together** — Put `@media` queries right after the base styles, not at the end of the file.

**6. Don't over-engineer** — Start simple, add complexity only when needed.

**7. Format your CSS!** — Use Prettier or our CSS Formatter. Clean code is happy code.

And remember: CSS is powerful. Take the time to learn it well — it will pay off!

Tools

Try These Tools

Open the tools that go with this tutorial and practice what you learned.

Navigation

Keep Learning