CSS UI Components
A navbar usually has a logo, links, and maybe a button. Flexbox makes it easy to spread items left and right and wrap on small screens.
Navigation Bars
A navbar usually has a logo, links, and maybe a button. Flexbox makes it easy to spread items left and right and wrap on small screens.
Real-life example: A navbar is like the main menu board at a restaurant — logo on one side, dish names in the middle, order button on the other side.
.navbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding: 1rem 1.25rem;
background: #0f172a;
color: white;
}
.navbar a {
color: white;
text-decoration: none;
}
.navbar nav {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}Dropdowns
Dropdowns show extra links when you hover or click. Hide the menu by default, then show it when the parent is hovered or has a .open class (JavaScript can toggle .open).
Real-life example: A dropdown is like a folder tab — closed normally, opens to show more papers inside.
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
min-width: 180px;
background: white;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 0.5rem 0;
display: none;
box-shadow: 0 10px 25px rgba(15, 23, 42, 0.12);
}
.dropdown:hover .dropdown-menu {
display: block;
}
.dropdown-menu a {
display: block;
padding: 0.5rem 1rem;
color: #0f172a;
}Image Gallery
A gallery shows many images in a grid. Use CSS Grid or Flexbox with gap. Add hover effects for a polished feel.
Real-life example: An image gallery is like a photo wall at a wedding — many frames arranged neatly on one wall.
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 1rem;
}
.gallery img {
width: 100%;
height: 160px;
object-fit: cover;
border-radius: 10px;
transition: transform 0.2s ease;
}
.gallery img:hover {
transform: scale(1.03);
}Image Sprites
A sprite is one image file with many icons. background-position shows only one part. This was popular before SVG icons — still useful to understand old code.
Real-life example: A sprite sheet is like a stamp sheet — you peel one stamp by showing only that corner of the sheet.
.icon-home {
width: 24px;
height: 24px;
background: url("icons-sprite.png") no-repeat 0 0;
}
.icon-user {
width: 24px;
height: 24px;
background: url("icons-sprite.png") no-repeat -24px 0;
}Forms & Buttons
Forms need clear labels, comfortable input size, and visible focus states. Buttons should look clickable with padding, color, and hover feedback.
Real-life example: A styled form is like a clean government form — big boxes to write in, clear labels, and a bright submit button.
label {
display: block;
font-weight: 600;
margin-bottom: 0.35rem;
}
input, textarea, select {
width: 100%;
padding: 0.65rem 0.75rem;
border: 1px solid #cbd5e1;
border-radius: 8px;
font: inherit;
}
input:focus, textarea:focus, select:focus {
outline: 2px solid #0891b2;
border-color: #0891b2;
}
.btn {
display: inline-block;
padding: 0.7rem 1.25rem;
background: #0891b2;
color: white;
border: none;
border-radius: 999px;
font-weight: 600;
cursor: pointer;
}
.btn:hover {
background: #0e7490;
}Tooltips & Pagination
Tooltips are small hint boxes on hover. Pagination splits long content into numbered pages. Both are common UI patterns built mostly with CSS and a little HTML structure.
Real-life example: A tooltip is like a whispered hint when you point at something. Pagination is like page numbers at the bottom of a notebook.
.tooltip {
position: relative;
cursor: help;
}
.tooltip::after {
content: attr(data-tip);
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
background: #0f172a;
color: white;
padding: 0.35rem 0.6rem;
border-radius: 6px;
font-size: 0.8rem;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s;
}
.tooltip:hover::after {
opacity: 1;
}
.pagination {
display: flex;
gap: 0.5rem;
list-style: none;
padding: 0;
}
.pagination a {
display: grid;
place-items: center;
width: 2.25rem;
height: 2.25rem;
border: 1px solid #e2e8f0;
border-radius: 8px;
text-decoration: none;
color: #0f172a;
}
.pagination a.active {
background: #0891b2;
color: white;
border-color: #0891b2;
}