Skip to main content

Command Palette

Search for a command to run...

Some Important Tips & Tricks in CSS

Published
5 min read

CSS :

  • 🌊 The “C” in CSS: Cascade & Specificity **CSS** stands for **Cascading Style Sheets**. When multiple rules target the same element, the browser decides which one wins using **specificity** and **source order**.

  • 🔸 Specificity hierarchy: - Inline styles > IDs > Classes/Attributes/Pseudo-classes > Elements/Pseudo-elements - Later rules (within the same specificity) override earlier ones. Quick mental model (bigger number = stronger):

Inline (1000) > #id (100) > .class, [attr], :hover (10) > h1, ::before (1)


💡 **Tip:** Keep specificity low and predictable. Prefer reusable class-based selectors instead of ID-heavy rules.

---

## 📍 Positioning Cheatsheet

### `position: static` (default)
- Element stays in the **normal flow**.  
- Positioned naturally based on the document order.

### `position: relative`
- Keeps its place in flow but allows **offsets** (top/right/bottom/left).  
- The element visually moves, but the original space remains.

```css
.badge {
  position: relative;
  top: -4px;
  left: 6px;
}

position: absolute

  • Removed from the normal flow and positioned relative to the nearest positioned ancestor (first ancestor with positionstatic).

  • Once absolute, width and height no longer depend on surrounding content.
    You control them manually.

.card {
  position: relative;
}
.ribbon {
  position: absolute;
  top: 0;
  right: 0;
  width: 120px;
}

position: fixed

  • Stays anchored to the viewport (useful for sticky headers, toolbars, or back-to-top buttons).

position: sticky

  • Acts relative until a threshold is reached, then becomes fixed within its parent container.
.toc {
  position: sticky;
  top: 1rem;
}

🧭 Flexbox: 1D Layout Superpower

Make a flex container

.container {
  display: flex;        /* default direction: row */
  gap: 12px;            /* spacing without margins */
}

Main-axis vs Cross-axis

  • flex-direction: row → main axis = horizontal, cross axis = vertical

  • justify-content → aligns along main axis

  • align-items → aligns along cross axis

.container {
  justify-content: space-between; /* horizontal alignment */
  align-items: center;            /* vertical alignment */
}

Wrapping

.container {
  flex-wrap: wrap;
}

Centering (the classic)

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

📦 The Flex Sizing Trio

flex-basis

Defines the starting size of an item before extra space is distributed.

.item {
  flex-basis: 200px;
}

flex-grow

Controls how much an item expands when extra space is available.

.a { flex-grow: 2; } /* grows twice as much as .b */
.b { flex-grow: 1; }

flex-shrink

Controls how much an item shrinks when there isn’t enough space.

  • Shrinkage is proportional to flex-shrink × flex-basis.
.container { display: flex; width: 400px; }

.item1 { flex: 0 1 200px; } /* shrink 1 */
.item2 { flex: 0 2 200px; } /* shrink 2 */

If the container width = 300px → total 100px needs to be removed:

  • item1 shrinks by ~33px (1 part)

  • item2 shrinks by ~67px (2 parts)

Final widths: 167px and 133px

Prevent shrinking

.no-shrink {
  flex-shrink: 0; /* item won’t shrink even if overflow */
}

Common shorthand

.item {
  flex: 1 1 240px; /* grow | shrink | basis */
}

🎯 align-self — Override Alignment for a Single Item

While align-items aligns all flex children together,
align-self lets one specific child override that rule.

.container {
  display: flex;
  align-items: center; /* all children vertically centered */
}

.child-1 {
  align-self: flex-start; /* moves to top */
}

.child-2 {
  align-self: stretch; /* fills container height */
}

🧠 Quick Reference

ValueEffect
autoInherits from align-items
flex-startAligns to top / start of cross axis
flex-endAligns to bottom / end of cross axis
centerCenters along cross axis
stretchFills container height/width
baselineAligns based on text baseline

Tailwind Equivalents

CSS PropertyTailwind Class
align-self: flex-startself-start
align-self: flex-endself-end
align-self: centerself-center
align-self: stretchself-stretch
align-self: autoself-auto

🪄 Practical Flex Tips & Patterns

1️⃣ Use gap for clean spacing

.toolbar {
  display: flex;
  gap: 0.75rem;
}

2️⃣ Absolute child inside relative parent

Perfect for badges, ribbons, or overlays.

.card { position: relative; }
.badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

3️⃣ Constrain overflowing content

.sidebar {
  flex: 0 0 clamp(220px, 25vw, 320px);
}

4️⃣ Logical properties for better responsiveness

.section {
  padding-block: 2rem;  /* top & bottom */
  padding-inline: 1rem; /* left & right */
}

5️⃣ Equal-height cards in one line

.cards {
  display: flex;
  align-items: stretch;
  gap: 1rem;
}
.card {
  flex: 1 1 280px;
}

🧰 Bonus: Tailwind Quick Reference

ConceptTailwind Utility
Flex containerflex
Directionflex-row, flex-col
Wrappingflex-wrap
Spacinggap-x-4, gap-y-2, gap-3
Centeringjustify-center items-center
Shrink/Growshrink, shrink-0, grow, grow-0
Basisbasis-1/2, basis-48, basis-[200px]
Alignmentitems-start, items-end, self-center, self-stretch
Positioningrelative, absolute, sticky, top-0, right-0