CSS UI Components
CSS UI components like buttons, tooltips, navbars, and dropdowns make websites interactive, user-friendly, and responsive. These components improve usability, SEO (through better UX), and accessibility.
CSS Tooltips
Tooltips are small information boxes that appear when a user hovers, clicks, or focuses on an element. They provide extra context without cluttering the UI.
Types of Tooltips
- Hover Tooltips – Show when the user hovers the mouse.
- Click Tooltips – Appear only when the user clicks.
- Custom Tooltips – Designed with advanced styles/animations.
Tooltip Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tooltip Example</title>
<style>
.tooltip {
position: relative;
display: inline-block;
cursor: pointer;
}
.tooltip .tooltip-text {
visibility: hidden;
width: 140px;
background: #333;
color: #fff;
text-align: center;
padding: 6px;
border-radius: 5px;
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: opacity 0.3s;
}
.tooltip:hover .tooltip-text {
visibility: visible;
opacity: 1;
}
</style>
</head>
<body>
<div class="tooltip">Hover me
<span class="tooltip-text">This is a tooltip!</span>
</div>
</body>
</html>
CSS Button
Buttons are the call-to-action (CTA) elements of a website. They guide users to sign up, purchase, or navigate.
<button class="button">Basic Button</button>
.button {
background: #4CAF50;
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
font-size: 16px;
cursor: pointer;
}
Hover & Active States
Adding hover/active states improves interactivity.
<button class="button-hover">Hover Me</button>
.button-hover {
background: #008CBA;
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
transition: background 0.3s;
}
.button-hover:hover {
background: #005f73;
}
Gradient & Animated Buttons
Gradient buttons stand out and increase click rates.
<button class="button-gradient">Gradient Button</button>
.button-gradient {
background: linear-gradient(45deg, #ff7e5f, #feb47b);
color: white;
padding: 12px 24px;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: transform 0.3s;
}
.button-gradient:hover {
transform: scale(1.05);
}
Icon Buttons Example
<button class="button-icon">🔍 Search</button>
.button-icon {
background: #333;
color: white;
padding: 12px 20px;
border: none;
border-radius: 6px;
cursor: pointer;
display: flex;
align-items: center;
gap: 8px;
}
.button-icon:hover {
background: #555;
}
CSS Pagination
Pagination divides content into multiple pages, improving navigation, readability, and SEO by preventing long scrolling pages.
Simple Pagination Example (HTML + CSS)
<ul class="pagination">
<li><a href="#">«</a></li>
<li><a href="#" class="active">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">»</a></li>
</ul>
.pagination {
display: flex;
list-style: none;
justify-content: center;
gap: 10px;
}
.pagination li {
display: inline;
}
.pagination a {
padding: 8px 14px;
text-decoration: none;
background: #f1f1f1;
color: #333;
border-radius: 5px;
transition: background 0.3s;
}
.pagination a:hover,
.pagination a.active {
background: #4CAF50;
color: white;
}
Active/Disabled States
<a href="#" class="disabled">« Prev</a>
.disabled {
color: #aaa;
pointer-events: none;
background: #eee;
}
CSS Navigation Bars (Navbars)
A navbar usually contains brand/logo, links, and dropdown menus.
Horizontal vs Vertical Navbars
Horizontal Navbar Example
<div class="navbar">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</div>
.navbar {
display: flex;
background: #333;
padding: 10px;
}
.navbar a {
color: white;
text-decoration: none;
padding: 10px 20px;
}
.navbar a:hover {
background: #575757;
}
Vertical Navbar Example
<div class="vertical-navbar">
<a href="#">Dashboard</a>
<a href="#">Profile</a>
<a href="#">Settings</a>
</div>
.vertical-navbar {
width: 200px;
background: #333;
display: flex;
flex-direction: column;
}
.vertical-navbar a {
padding: 12px;
color: white;
text-decoration: none;
}
.vertical-navbar a:hover {
background: #575757;
}
Sticky & Fixed Navbar Example
<div class="sticky-navbar">
<a href="#">Home</a>
<a href="#">Blog</a>
<a href="#">Contact</a>
</div>
.sticky-navbar {
position: fixed;
top: 0;
width: 100%;
background: #444;
padding: 12px;
}
.sticky-navbar a {
color: white;
margin: 0 10px;
text-decoration: none;
}
CSS Dropdowns
Dropdowns allow users to expand hidden options, often used in forms and navigation menus.
Example
<div class="dropdown">
<button>Menu</button>
<div class="dropdown-content">
<a href="#">Profile</a>
<a href="#">Settings</a>
<a href="#">Logout</a>
</div>
</div>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown button {
background: #4CAF50;
color: white;
padding: 12px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.dropdown-content {
display: none;
position: absolute;
background: #f1f1f1;
min-width: 160px;
border-radius: 6px;
box-shadow: 0 8px 12px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
padding: 10px 16px;
display: block;
text-decoration: none;
color: #333;
}
.dropdown-content a:hover {
background: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
Multi-level Dropdowns
<div class="dropdown">
<button>Services</button>
<div class="dropdown-content">
<a href="#">Web Design</a>
<div class="dropdown-submenu">
<a href="#">Development »</a>
<div class="sub-content">
<a href="#">Frontend</a>
<a href="#">Backend</a>
</div>
</div>
</div>
</div>
.dropdown-submenu {
position: relative;
}
.dropdown-submenu .sub-content {
display: none;
position: absolute;
left: 100%;
top: 0;
background: #eee;
min-width: 160px;
}
.dropdown-submenu:hover .sub-content {
display: block;
}