Some Important Tips & Tricks in CSS
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
position≠static).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
relativeuntil a threshold is reached, then becomesfixedwithin 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 = verticaljustify-content→ aligns along main axisalign-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:
item1shrinks by ~33px (1 part)item2shrinks 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
| Value | Effect |
auto | Inherits from align-items |
flex-start | Aligns to top / start of cross axis |
flex-end | Aligns to bottom / end of cross axis |
center | Centers along cross axis |
stretch | Fills container height/width |
baseline | Aligns based on text baseline |
Tailwind Equivalents
| CSS Property | Tailwind Class |
align-self: flex-start | self-start |
align-self: flex-end | self-end |
align-self: center | self-center |
align-self: stretch | self-stretch |
align-self: auto | self-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
| Concept | Tailwind Utility |
| Flex container | flex |
| Direction | flex-row, flex-col |
| Wrapping | flex-wrap |
| Spacing | gap-x-4, gap-y-2, gap-3 |
| Centering | justify-center items-center |
| Shrink/Grow | shrink, shrink-0, grow, grow-0 |
| Basis | basis-1/2, basis-48, basis-[200px] |
| Alignment | items-start, items-end, self-center, self-stretch |
| Positioning | relative, absolute, sticky, top-0, right-0 |