Are you tired of writing the same old CSS and JavaScript? Want to level up your frontend game with some killer tricks? Here are 10 mind-blowing hacks that will save you hours and make your code look like magic! β¨
1. CSS Grid + Flexbox = π₯ Layout Superpowers
Stop fighting with floats! Combine Grid
for overall structure and Flexbox
for micro-alignments.
.parent {
display: grid;
grid-template-columns: 1fr 2fr;
}
.child {
display: flex;
justify-content: center;
}
2. One-Line Dark Mode π
Forget complex themesβuse filter
for a quick dark mode!
.dark-mode {
filter: invert(1) hue-rotate(180deg);
}
(Works best for simple sites!)
3. console.log
Like a Pro π΅οΈββοΈ
Use styled logs to debug in style!
console.log(
"%cπ Success!",
"color: green; font-size: 18px; font-weight: bold;"
);
4. The Ultimate Centering Trick π―
No more margin: 0 auto
strugglesβjust use:
.center-me {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
5. Smooth Scrolling in 1 Line π
No JavaScript needed!
html {
scroll-behavior: smooth;
}
6. CSS Variables for Theming π¨
Change your entire color scheme in seconds!
:root {
--primary: #0066ff;
}
button {
background: var(--primary);
}
7. Prevent Image Distortion πΌοΈ
Always maintain aspect ratio with object-fit
:
img {
width: 100%;
height: 300px;
object-fit: cover;
}
8. The Magic of clamp()
π
Responsive font sizes without media queries!
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
}
9. Detect Clicks Outside an Element π±οΈ
Perfect for dropdowns & modals!
document.addEventListener("click", (e) => {
if (!e.target.closest(".dropdown")) {
closeDropdown();
}
});
10. Supercharge Your :hover
Effects β¨
Add smooth transitions for a pro feel:
button {
transition: all 0.3s ease;
}
button:hover {
transform: scale(1.05);
}
π BONUS: Want More?
Drop a β€οΈ if you found this helpful and comment with your favorite hack!
Follow me for more frontend gems! π
Top comments (0)