How to Create a CSS Toggle Button With an Icon
Posté 2025-02-15 10:58:27
0
447

Creating a CSS toggle button with an icon is simple and effective for various interactive web features. Below is an example of a toggle button that changes its icon when toggled.
Example: Toggle Button with CSS and Icons
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Icon Toggle Button</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
}
.toggle-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 60px;
height: 60px;
border: none;
border-radius: 50%;
background-color: #007BFF;
color: white;
font-size: 24px;
cursor: pointer;
transition: background-color 0.3s, transform 0.2s;
}
.toggle-btn:active {
transform: scale(0.95);
}
.toggle-btn.toggled {
background-color: #28A745;
}
</style>
</head>
<body>
<button id="toggleButton" class="toggle-btn">
<span id="icon">âïž</span>
</button>
<script>
const toggleButton = document.getElementById('toggleButton');
const icon = document.getElementById('icon');
toggleButton.addEventListener('click', () => {
toggleButton.classList.toggle('toggled');
icon.textContent = toggleButton.classList.contains('toggled') ? 'đ' : 'âïž';
});
</script>
</body>
</html>
How it works:
- CSS Styling: The
.toggle-btn
class styles the button. Thetoggled
class applies a different background color for the active state. - Icons: Unicode icons (âïž for sun and đ for moon) change when the button is toggled.
- JavaScript: Adds an event listener to toggle the
toggled
class and swap the icon when clicked.
Rechercher
Sellect from all CatĂ©gories bellow â
- Artificial Intelligence (AI)
- Cybersecurity
- Blockchain & Cryptocurrencies
- Internet of Things
- Cloud Computing
- Big Data & Analytics
- Virtual Reality
- 5G & Future Connectivity
- Robotics & Automation
- Software Development & Programming
- Tech Hardware & Devices
- Tech in Healthcare
- Tech in Business
- Gaming Technologies
- Tech in Education
- Machine Learning (ML)
- Blogging
- Affiliate Marketing
- Make Money
- Digital Marketing
- Product Review
- Social Media
- Excel
- Graphics design
- Freelancing/Consulting
- FinTech (Financial Technology)
- E-commerce and Digital Marketing
- Business
- Sport
- Self Development
- Tips to Success
- Video Editing
- Photo Editing
- Website Promotion
- YouTube
- Lifestyle
- Health
- Computer
- Téléphone
- Music
- Accounting
- Causes
- Networking
Lire la suite
How Do Cryptocurrencies Work? A Step-by-Step Explanation
Here’s a detailed explanation of How...
How to Generate More Traffic From Social Media
Generating more traffic from social media...
Introduction to Computer Science
Computer Science is a vast and dynamic field...
Steps on how to create telegram bot
Creating a Telegram bot involves several steps....
Backend Validation For Facebook Auto-login
For backend validation of the Facebook...