Back
Unveil a vibrant mosaic-inspired toggle button that dynamically transforms with geometric elegance and colorful flair while ensuring fun and function.
Welcome to this tutorial on creating a playful and vibrant toggle button inspired by the geometric elegance of mosaic tiles. This component offers a delightful user experience, combining functionality with a touch of artistry. Let's dive into the world of crafting this unique UI element.
Mosaics, with their intricate patterns and vibrant colors, present a perfect metaphor for an engaging toggle button. Each state of the button will capture the dynamic essence of mosaic art through animated transitions and soft gradients.
We begin with a simple HTML structure that includes a button within a container. This button will transform its appearance when toggled, shifting colors and updating text to reflect its state. We leverage semantic HTML to ensure accessibility.
<div class="mosaic-tiles-container">
<button onclick="toggleMosaic()" class="toggle-mosaic-button" aria-label="Toggle Mosaic">
<span class="toggle-indicator">Inactive</span>
</button>
</div>The container centers the button on the page using flexbox, providing a balanced visual backdrop.
JavaScript handles the interactivity of the button. We maintain a state variable (isActive) that tracks the current state of the button. The function toggleMosaic toggles this state and updates the button's appearance accordingly.
let isActive = false;
function toggleMosaic() {
let button = document.querySelector('.toggle-mosaic-button');
let indicatorText = document.querySelector('.toggle-indicator');
isActive = !isActive;
if (isActive) {
button.style.background = "linear-gradient(45deg, #ff9a9e, #fad0c4 50%, #fad0c4)";
indicatorText.textContent = "Active";
button.classList.add('mosaic-active');
} else {
button.style.background = "linear-gradient(45deg, #a18cd1, #fbc2eb 50%, #fbc2eb)";
indicatorText.textContent = "Inactive";
button.classList.remove('mosaic-active');
}
}Through JavaScript, we can dynamically alter the button's gradient and toggle class for sophisticated transition effects.
The CSS is where the visual magic unfolds. We start with styling the container for full-screen height alignment and flex centering. The button itself combines vibrant gradients and subtle shadows to mimic the visual depth of a mosaic tile.
.mosaic-tiles-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.toggle-mosaic-button {
border: none;
padding: 20px 40px;
border-radius: 12px;
color: #fff;
font-size: 20px;
font-weight: bold;
transition: background 0.5s, box-shadow 0.3s;
cursor: pointer;
background: linear-gradient(45deg, #a18cd1, #fbc2eb 50%, #fbc2eb);
box-shadow: 5px 5px 10px rgba(0,0,0,0.2);
}
.toggle-mosaic-button:focus {
outline: 2px solid #ff9a9e;
}
.mosaic-active {
box-shadow: 10px 10px 20px rgba(255,154,158,0.7);
}We've designed the CSS to incorporate visual effects that are both aesthetic and user-friendly. Focus states and outlines ensure keyboard navigability, upholding accessibility best practices.
We integrate several key accessibility features: descriptive aria-label for buttons and clear visible focus styles. These elements make the component user-friendly for audiences with diverse needs.
Additionally, semantic HTML, combined with meaningful heading levels, enhances the component's SEO, making it discoverable and efficient for search engines.
The Mosaic Tiles Toggle Button exemplifies how UI components can transcend mere functionality to offer a delightful user experience. By drawing on mosaic art's rich tradition, this button encapsulates a blend of elegance, playfulness, and accessibility that enriches any interface.
Through this tutorial, you've learned how to craft a toggle button that not only meets technical requirements but also elevates the aesthetic and user experience of your project. Embrace creativity in UI design and let your components shimmer with innovative potential!
<div class="mosaic-tiles-container">
<button onclick="toggleMosaic()" class="toggle-mosaic-button" aria-label="Toggle Mosaic">
<span class="toggle-indicator">Inactive</span>
</button>
</div>
<script>
let isActive = false;
function toggleMosaic() {
let button = document.querySelector('.toggle-mosaic-button');
let indicatorText = document.querySelector('.toggle-indicator');
isActive = !isActive;
if (isActive) {
button.style.background = "linear-gradient(45deg, #ff9a9e, #fad0c4 50%, #fad0c4)";
indicatorText.textContent = "Active";
button.classList.add('mosaic-active');
} else {
button.style.background = "linear-gradient(45deg, #a18cd1, #fbc2eb 50%, #fbc2eb)";
indicatorText.textContent = "Inactive";
button.classList.remove('mosaic-active');
}
}
</script>
<style>
.mosaic-tiles-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.toggle-mosaic-button {
border: none;
padding: 20px 40px;
border-radius: 12px;
color: #fff;
font-size: 20px;
font-weight: bold;
transition: background 0.5s, box-shadow 0.3s;
cursor: pointer;
background: linear-gradient(45deg, #a18cd1, #fbc2eb 50%, #fbc2eb);
box-shadow: 5px 5px 10px rgba(0,0,0,0.2);
}
.toggle-mosaic-button:focus {
outline: 2px solid #ff9a9e;
}
.mosaic-active {
box-shadow: 10px 10px 20px rgba(255,154,158,0.7);
}
</style>
Thank you for reading this article.
Comments