CSS is an essential tool for web developers, but mastering it can take years. Here are 40 CSS tricks that will save you time, simplify your code, and make your designs more efficient. These tricks are grouped into layout, styling, animations, and advanced customization categories for easy navigation.
1. Basic Layout and Utility Tricks
-
Full-Screen Section
Use
height: 100vh
to create a full-screen section.
.fullscreen {
height: 100vh;
width: 100%;
}
-
CSS Grid Layout
Automatically adjust grid columns using
repeat
andminmax
.
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
- Hover Effects Add a smooth scaling effect to buttons or images.
.button:hover {
transform: scale(1.1);
transition: transform 0.3s ease;
}
- Disable Pointer Events Prevent user interaction while showing an element as inactive.
.disabled {
pointer-events: none;
opacity: 0.6;
}
-
Gradient Borders
Apply gradient borders using
border-image
.
.gradient-border {
border: 4px solid;
border-image: linear-gradient(45deg, red, blue) 1;
}
- Responsive Container Center and constrain your content with a utility class.
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 1rem;
}
- Hidden Scrollbars Remove scrollbars without affecting scroll functionality.
.no-scrollbar {
overflow: auto;
scrollbar-width: none;
}
.no-scrollbar::-webkit-scrollbar {
display: none;
}
-
Circular Elements
Make any element circular with
border-radius
.
.circle {
width: 100px;
height: 100px;
background: red;
border-radius: 50%;
}
- Box Shadow Add depth with a subtle shadow effect.
.shadow {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
-
Z-Index Hierarchy
Manage overlapping elements using
z-index
.
.layer {
position: relative;
z-index: 10;
}
2. Advanced Styling and Flexbox Tricks
- Flexbox Equal Spacing Distribute child elements evenly.
.flex {
display: flex;
justify-content: space-between;
}
- Invert Colors Quickly invert the colors of an element.
.invert {
filter: invert(1);
}
- Sticky Header Fix a header at the top of the viewport.
.sticky-header {
position: sticky;
top: 0;
background: white;
z-index: 1000;
}
-
CSS Tooltip
Create a tooltip with
::after
.
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
top: -30px;
background: black;
color: white;
padding: 5px;
border-radius: 4px;
opacity: 0;
transition: opacity 0.3s ease;
}
.tooltip:hover::after {
opacity: 1;
}
- Overlay Effect Add a semi-transparent overlay for modals.
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
}
- Card Hover Lift Lift cards on hover for a 3D effect.
.card:hover {
transform: translateY(-10px);
transition: transform 0.3s ease;
}
- Gradient Text Make text colorful with a gradient effect.
.gradient-text {
background: linear-gradient(to right, red, blue);
-webkit-background-clip: text;
color: transparent;
}
- Vertical Text Rotate text to read vertically.
.vertical-text {
writing-mode: vertical-rl;
text-orientation: upright;
}
-
Truncate Multiline Text
Limit lines of text with
-webkit-line-clamp
.
.multiline {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
- Full-Width Button Create buttons that span the full width of their container.
.full-width-btn {
display: block;
width: 100%;
}
3. Responsive Design and Animations
- Media Query for Small Screens Adjust styles for mobile devices.
@media (max-width: 768px) {
.mobile {
font-size: 14px;
}
}
- Keyframe Animations Fade elements in or out using animations.
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade-in {
animation: fadeIn 2s ease-in-out;
}
- Button Gradient Hover Add a gradient background on hover.
.button-gradient:hover {
background: linear-gradient(to right, red, blue);
color: white;
}
- Diagonal Section Create diagonal sections with gradients.
.diagonal {
background: linear-gradient(135deg, red, blue);
}
- Responsive Images Automatically resize images to fit their container.
img {
max-width: 100%;
height: auto;
}
4. Advanced Selectors and Customization
- Target First and Last Child Style the first and last items in a container.
.parent > :first-child {
color: red;
}
.parent > :last-child {
color: blue;
}
- Hover on Parent, Affect Child Change a child element’s style when the parent is hovered.
.parent:hover .child {
background: yellow;
}
- Autofill Styling Style browser autofill inputs.
input:-webkit-autofill {
background: yellow;
}
- Responsive Typography Scale font sizes with viewport width.
.responsive-text {
font-size: calc(16px + 1vw);
}
These tricks cover a wide range of CSS techniques, from basic layout to advanced styling, to help you write better, cleaner, and more effective CSS. Use them to enhance your designs and save time on repetitive tasks!
Top comments (0)