Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
Git Mistakes a Developer Should Avoid
Next article icon

7 CSS Hacks Every Developer Should Know

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When it comes to web development, the user interface (UI) plays a crucial role. A well-designed layout not only attracts users but also sets the tone for their overall experience. While JavaScript often takes the spotlight, CSS remains a fundamental language, powering 96% of all websites. As a developer, knowing CSS shortcuts and hacks can significantly enhance your workflow, improve design elements, and save valuable time. These hacks will help you in writing efficient code for your user interface of the website. Let's get started.

7 CSS Hacks Every Developer Should Know

1. Hovering Effect Delays With CSS:

To create the hovering effect you can use a hover selector. To delay the hovering effect you can use a transition element to delay the hovering effect. It looks so clean that it draws the user's attention to the element.

An example of the code snippet is given below.

HTML
<!DOCTYPE html> <html> <head>     <style>         h1 {             color: green;             position: relative;             animation: lit 2s;             animation-delay: 2s;         }            @keyframes lit {             from {                 left: 0px;             }                to {                 left: 200px;             }         }     </style> </head> <body>     <h3>How to use animation-delay in CSS?</h3>     <p>Animation will start after 2 sec.</p>     <h1>GeeksforGeeks</h1> </body> </html> 

Output:

a1

2. Position Content in Center With CSS:

Another tricky thing UI developers struggle in doing is positioning the content in the center. Setting the property position: absolute resolve this issue. Content will be positioned in the center. Setting this property is going to work on all the devices. 

An example of the code snippet is given below.

HTML
<!DOCTYPE html> <html> <head>     <title>Page Title</title>     <style>         .parent {             position: relative;             height: 400px;             width: 400px;             background-color: red;         }          .child {             position: absolute;             top: 50%;             left: 50%;             transform: translate(-50%, -50%);             height: 100px;             width: 100px;             background-color: blue;             display: flex;             align-items: center;             justify-content: center;             color: white;             text-align: center;         }     </style> </head> <body>     <div class="parent">         <div class="child">             This element is centered         </div>     </div> </body> </html> 

Output:

a1

3. Fix The Element’s Position in CSS:

While working on the code in CSS, you may find the elements to fix at a certain position. This can be the trickiest thing as a beginner. To get the desired result you can set the property position: absolute. Make sure that this property doesn't break the responsiveness of your site. Check your site appearance on every screen size and resolution. Doing this ensures that your design fits in all screen resolutions or devices. Elements should remain in the same position for all users.  

An example of the code snippet is given below.

HTML
<!DOCTYPE html> <html>  <head>     <style>         div.fixed {             position: fixed;             bottom: 0;             right: 0;             width: 300px;             border: 3px solid #73AD21;         }          div.absolute {             position: absolute;             top: 150px;             right: 80;             width: 200px;             height: 100px;             border: 3px solid #73AD21;         }     </style> </head>  <body>     <h1>position: absolute;</h1>     <h2>position: fixed;</h2>     <div class="absolute">         This element has position: absolute;     </div> </body>  </html> 

Output:

4. Fit Images to The Page in CSS:

If images can make your website beautiful then also it can make your website ugly. Images look very bad on a website when it spills all over the screen. This issue is common in web development. It creates a bad impression on users who visit the website. So what's the solution for it?

An example of the code snippet is given below.

HTML
<!DOCTYPE html> <html>  <head>     <style>         .geeks {             width: 60%;             height: 300px;         }          img {             width: 100%;             height: 100%;         }     </style> </head>  <body>     <div class="geeks">         <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png"             alt="Geeks Image" />     </div> </body>  </html> 

Output:

5. Visited Link Styling in CSS:

You can customize the styling of your link when a user visits it. You might have noticed that once a link is clicked the styling or color of the link is changed. You can customize it as you need, and you can code to tweak how the links look before and after clicking on them. 

An example of the code snippet is given below.

HTML
<!DOCTYPE html> <html lang="en">    <head>     <!--Meta data-->     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">        <style>         h1 {             color: #006600;             text-align: center;         }            /* If the link is unvisited you see this color*/         a:link {             color: #006600;             text-decoration: none;         }            /* If the link is visited you see this color*/         a:visited {             color: rgb(255, 105, 223);         }            /* On placing mouse over the link */         a:hover {             color: rgb(128, 105, 255);             text-decoration: underline;         }            /* If the click the link,  you see this color*/         a:active {             color: rgb(255, 105, 138);         }     </style> </head>    <body>     <h1>GeeksforGeeks</h1>     <p>Click the links</p>     <ul>         <li><a href= "https://www.geeksforgeeks.org/dbms/">            DBMS         </a>         </li>         <li><a href= "https://www.geeksforgeeks.org/computer-network-tutorials/">           Computer Networks</a>         </li>         <li> <a href= "https://www.geeksforgeeks.org/operating-systems/">           Operating Systems</a>         </li>         <li><a href= "https://www.geeksforgeeks.org/data-structures/">           Data Structures</a>         </li>         <li><a href="https://www.geeksforgeeks.org/">             And many more</a>         </li>     </ul> </body>    </html> 

Output:

file

6. Consolidate Styling in CSS:

In your webpage, if you need to repeat the same styling multiple times then writing that piece of code at several places takes a lot of time. You need to optimize your CSS code here and you need to write code in fewer lines you can separate the items with commas, and you can write the style into it. It will be added to all of them. 

An example of the code snippet is given below.

HTML
<!DOCTYPE html> <html>  <head>     <style>         .geeks,         img {             width: 70%;             height: 70%;         }     </style> </head>  <body>     <div class="geeks">         <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png"             alt="Geeks Image" />     </div> </body>  </html> 

Output:

7. Reset and Unset CSS Styles:

There can be a situation when you need to override all the default styling attributes for different browsers. Default styling doesn't guarantee that it will work on all browsers. Every browser has its own style sheet with default built-in styles. If the design won't work in the browser you're using then this can be a problem for you especially when you will have to make your website consistent throughout all the browsers. Below is the solution for it.

An example of the code snippet is given below.

HTML
<!DOCTYPE html> <html>    <head>     <title>         How to reset/remove CSS         styles for element ?     </title>            <style>         body {             display: grid;             min-height: 100vh;                    }         .gfg {             all: unset;         }         .geeks {             color: green;             font-size: 3rem;         }     </style> </head>    <body>     <center>         <div class="geeks">             <button class="gfg">                 GeeksforGeeks             </button>         </div>                    <p>             Here the GeeksforGeeks button is             attached with the unset property         </p>         <br>         <button class="GFG">             A Online Computer Secience Portal         </button>     </center> </body>    </html> 

Output:

Conclusion

We have shared some common hacks of CSS, but these are not limited here. As you will progress in the journey of frontend development, you will learn a lot of things in CSS. Reading the other developer's code helps a lot in creating a beautiful user interface. In UI development it is very important to check the responsiveness of your web application across all the browsers. A lot of developers end up writing long code which is not good. We highly recommend them to check the other developer's code and check the projects that use fewer lines of code and still achieve the same things.


Next Article
Git Mistakes a Developer Should Avoid
author
anuupadhyay
Improve
Article Tags :
  • GBlog
  • Web Technologies
  • CSS
  • CSS-Advanced
  • CSS-Questions

Similar Reads

  • 10 CSS functions every Front End developer Should Know
    CSS functions are powerful tools that help you style websites more efficiently. They allow you to dynamically adjust values like colors, sizes, and positions, making your code cleaner and more maintainable. Here are 10 essential CSS functions every front-end developer should know: 1. url() FunctionT
    3 min read
  • Top 10 Tools That Every Java Developer Should Know
    Hey there, Java lovers! If you're someone who enjoys coding in Java or you're just starting out, you probably know that having the right tools can make a big difference. In this article, we're going to talk about 10 tools that every Java developer should know about. Whether you're new to Java or a p
    15+ min read
  • 12 Web Development Checklists Every Team Should Keep Handy
    A website when build should be attractive, meet the goals of businesses, and be easy to handle. There are various factors that help in keeping it successful. A comprehensive web development checklist can help in delivering a better final product. A checklist is a list that keeps track of items requi
    6 min read
  • Top 10 JavaScript Fundamentals That Every Developer Should Know
    Javascript is an object-oriented language that is lightweight and used for web development, web applications, game development, etc. It enables dynamic interactivity on static web pages, which cannot be done with HTML, and CSS only. Javascript is so vast that even mid-level professionals find it dif
    8 min read
  • Git Mistakes a Developer Should Avoid
    Git is an important tool for developers, allowing for efficient version control, collaboration, and management of codebases. However, even experienced developers can make mistakes that lead to errors, data loss, or workflow disruptions. Avoiding common Git mistakes can save time, reduce errors, and
    6 min read
  • 12 JavaScript Code Snippets That Every Developer Must Know
    JavaScript is by far the most popular language when it comes to web development. It is being used on both the client side and the server side. To improve coding efficiency, every developer should know essential JavaScript code snippets to make their development fast and easy. 1. Sorting an ArraySort
    4 min read
  • Top JavaScript Playgrounds every developer should try!
    Javascript has seen significant advancements in recent years, allowing developers to create, edit, and run code using an online environment called “Playground”. These playgrounds offer several features over the traditional offline code editor that runs on a development environment. Just like in a re
    10 min read
  • Top 12 Git Commands for Every Developer
    Git is a distributed version control system and open-source software. It enables developers to manage many versions of a source code with ease. You can use it to figure out who did what, when, and why. Git has become a must-have tool for any developer nowadays, and knowing Git commands is required f
    9 min read
  • 9 Features of Chrome Developer Tools That You Must Know
    We all know the popularity of the Chrome browser. This browser has made life easier for developers and being a developer using its built-in developer tool is nothing new for you. The chrome developer tool has made debugging a lot easier. The built-in developer tool allows you to edit the page, debug
    6 min read
  • Top 7 Code Sharing Website For Developers
    Building an application is quite challenging for developers especially when you are a new coder or you are a solo coder. Quite often developers get stuck in their projects due to some errors. Sometimes it's also difficult to build some specific features and there you just want readymade code snippet
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences