40 CSS Tricks That Will Save You Time and Improve Your Workflow

40 CSS Tricks That Will Save You Time and Improve Your Workflow


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

  1. Full-Screen Section
    Use height: 100vh to create a full-screen section.
   .fullscreen {
     height: 100vh;
     width: 100%;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. CSS Grid Layout
    Automatically adjust grid columns using repeat and minmax.
   .grid {
     display: grid;
     grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
     gap: 1rem;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Hover Effects
    Add a smooth scaling effect to buttons or images.
   .button:hover {
     transform: scale(1.1);
     transition: transform 0.3s ease;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Disable Pointer Events
    Prevent user interaction while showing an element as inactive.
   .disabled {
     pointer-events: none;
     opacity: 0.6;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Gradient Borders
    Apply gradient borders using border-image.
   .gradient-border {
     border: 4px solid;
     border-image: linear-gradient(45deg, red, blue) 1;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Responsive Container
    Center and constrain your content with a utility class.
   .container {
     max-width: 1200px;
     margin: 0 auto;
     padding: 0 1rem;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Hidden Scrollbars
    Remove scrollbars without affecting scroll functionality.
   .no-scrollbar {
     overflow: auto;
     scrollbar-width: none;
   }
   .no-scrollbar::-webkit-scrollbar {
     display: none;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Circular Elements
    Make any element circular with border-radius.
   .circle {
     width: 100px;
     height: 100px;
     background: red;
     border-radius: 50%;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Box Shadow
    Add depth with a subtle shadow effect.
   .shadow {
     box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Z-Index Hierarchy
    Manage overlapping elements using z-index.
   .layer {
     position: relative;
     z-index: 10;
   }
Enter fullscreen mode

Exit fullscreen mode

2. Advanced Styling and Flexbox Tricks

  1. Flexbox Equal Spacing
    Distribute child elements evenly.
   .flex {
     display: flex;
     justify-content: space-between;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Invert Colors
    Quickly invert the colors of an element.
   .invert {
     filter: invert(1);
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Sticky Header
    Fix a header at the top of the viewport.
   .sticky-header {
     position: sticky;
     top: 0;
     background: white;
     z-index: 1000;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. 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;
   }
Enter fullscreen mode

Exit fullscreen mode

  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);
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Card Hover Lift
    Lift cards on hover for a 3D effect.
   .card:hover {
     transform: translateY(-10px);
     transition: transform 0.3s ease;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Gradient Text
    Make text colorful with a gradient effect.
   .gradient-text {
     background: linear-gradient(to right, red, blue);
     -webkit-background-clip: text;
     color: transparent;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Vertical Text
    Rotate text to read vertically.
   .vertical-text {
     writing-mode: vertical-rl;
     text-orientation: upright;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. 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;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Full-Width Button
    Create buttons that span the full width of their container.
   .full-width-btn {
     display: block;
     width: 100%;
   }
Enter fullscreen mode

Exit fullscreen mode

3. Responsive Design and Animations

  1. Media Query for Small Screens
    Adjust styles for mobile devices.
   @media (max-width: 768px) {
     .mobile {
       font-size: 14px;
     }
   }
Enter fullscreen mode

Exit fullscreen mode

  1. 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;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Button Gradient Hover
    Add a gradient background on hover.
   .button-gradient:hover {
     background: linear-gradient(to right, red, blue);
     color: white;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Diagonal Section
    Create diagonal sections with gradients.
   .diagonal {
     background: linear-gradient(135deg, red, blue);
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Responsive Images
    Automatically resize images to fit their container.
   img {
     max-width: 100%;
     height: auto;
   }
Enter fullscreen mode

Exit fullscreen mode

4. Advanced Selectors and Customization

  1. Target First and Last Child
    Style the first and last items in a container.
   .parent > :first-child {
     color: red;
   }
   .parent > :last-child {
     color: blue;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Hover on Parent, Affect Child
    Change a child element’s style when the parent is hovered.
   .parent:hover .child {
     background: yellow;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Autofill Styling
    Style browser autofill inputs.
   input:-webkit-autofill {
     background: yellow;
   }
Enter fullscreen mode

Exit fullscreen mode

  1. Responsive Typography
    Scale font sizes with viewport width.
   .responsive-text {
     font-size: calc(16px + 1vw);
   }
Enter fullscreen mode

Exit fullscreen mode

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!



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.