Master advanced border styling and gradient backgrounds with CSS3
CSS3 introduced powerful new features for borders and backgrounds, including border-radius for rounded corners, box-shadow for depth, border-image for custom borders, and gradients for colorful backgrounds without images.
The border-radius property creates rounded corners on elements.
/* All corners equal */
.box {
border-radius: 10px;
}
/* Different corners */
.box {
border-radius: 10px 20px 30px 40px;
/* top-left, top-right, bottom-right, bottom-left */
}
/* Circle */
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
}
/* Ellipse */
.oval {
width: 300px;
height: 200px;
border-radius: 50%;
}
/* Specific corners */
border-top-left-radius: 20px;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
border-bottom-left-radius: 20px;
/* Horizontal / Vertical radius */
border-radius: 50px / 25px; /* Creates elliptical corners */
The box-shadow property adds shadow effects around an element's frame.
/* Syntax: h-offset v-offset blur spread color */
box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 0.3);
/* Multiple shadows */
box-shadow:
2px 2px 5px rgba(0, 0, 0, 0.3),
-2px -2px 5px rgba(255, 255, 255, 0.5);
/* Inset shadow (inner) */
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
/* Subtle shadow */
.card {
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
/* Material Design elevation */
.elevated {
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}
/* Glow effect */
.glow {
box-shadow: 0 0 20px rgba(102, 126, 234, 0.8);
}
/* Neumorphism */
.neumorphic {
box-shadow:
8px 8px 16px rgba(0, 0, 0, 0.2),
-8px -8px 16px rgba(255, 255, 255, 0.7);
}
/* Pressed effect */
.pressed {
box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.3);
}
The border-image property allows you to use an image as the border of an element.
/* Using an image as border */
.fancy-border {
border: 10px solid transparent;
border-image: url('border.png') 30 round;
}
/* Gradient border */
.gradient-border {
border: 5px solid;
border-image: linear-gradient(45deg, #667eea, #764ba2) 1;
}
/* Border image properties */
border-image-source: url('border.png');
border-image-slice: 30;
border-image-width: 10px;
border-image-repeat: round; /* or stretch, repeat, space */
CSS3 allows multiple background images on a single element.
/* Multiple backgrounds (stacked) */
.element {
background:
url('top-pattern.png') top left no-repeat,
url('bottom-pattern.png') bottom right no-repeat,
linear-gradient(135deg, #667eea, #764ba2);
}
/* Each background can have different properties */
.complex {
background-image:
url('overlay.png'),
url('texture.png'),
linear-gradient(to bottom, rgba(0,0,0,0.5), transparent);
background-position:
center,
top left,
center;
background-size:
cover,
200px 200px,
cover;
background-repeat:
no-repeat,
repeat,
no-repeat;
}
Linear gradients create smooth color transitions along a straight line.
/* Top to bottom (default) */
background: linear-gradient(#667eea, #764ba2);
/* Direction keywords */
background: linear-gradient(to right, red, blue);
background: linear-gradient(to bottom right, red, blue);
/* Angle */
background: linear-gradient(45deg, red, blue);
background: linear-gradient(180deg, red, blue);
/* Multiple colors */
background: linear-gradient(to right, red, yellow, green, blue);
/* Color stops */
background: linear-gradient(to right,
red 0%,
yellow 25%,
green 50%,
blue 100%
);
/* Transparent gradient */
background: linear-gradient(to bottom,
rgba(0, 0, 0, 0.8),
transparent
);
/* Striped pattern */
background: linear-gradient(45deg,
#667eea 25%,
#764ba2 25%,
#764ba2 50%,
#667eea 50%,
#667eea 75%,
#764ba2 75%
);
background-size: 40px 40px;
/* Subtle gradient */
background: linear-gradient(to bottom,
#ffffff,
#f5f5f5
);
Radial gradients create circular or elliptical color transitions from a center point.
/* Basic radial gradient */
background: radial-gradient(#667eea, #764ba2);
/* Circle shape */
background: radial-gradient(circle, #667eea, #764ba2);
/* Ellipse (default) */
background: radial-gradient(ellipse, #667eea, #764ba2);
/* Size keywords */
background: radial-gradient(circle closest-side, red, blue);
/* closest-side, farthest-side, closest-corner, farthest-corner */
/* Position */
background: radial-gradient(circle at top right, red, blue);
background: radial-gradient(at 30% 40%, red, blue);
/* Multiple color stops */
background: radial-gradient(circle,
red 0%,
yellow 30%,
green 60%,
blue 100%
);
Conic gradients rotate colors around a center point (like a pie chart).
/* Basic conic gradient */
background: conic-gradient(red, yellow, green, blue, red);
/* From angle */
background: conic-gradient(from 45deg, red, blue);
/* At position */
background: conic-gradient(at 30% 40%, red, blue);
/* Pie chart effect */
background: conic-gradient(
red 0deg 90deg,
blue 90deg 180deg,
green 180deg 270deg,
yellow 270deg 360deg
);
/* Color wheel */
background: conic-gradient(
red, yellow, lime, aqua, blue, magenta, red
);
/* Cover entire container */
background-size: cover;
/* Contain within container */
background-size: contain;
/* Specific size */
background-size: 200px 100px;
/* Responsive */
background-size: 100% auto;
/* Background clips to different areas */
background-clip: border-box; /* Default */
background-clip: padding-box; /* Inside border */
background-clip: content-box; /* Inside padding */
background-clip: text; /* Clips to text (with -webkit-) */
/* Gradient text effect */
.gradient-text {
background: linear-gradient(45deg, #667eea, #764ba2);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.card {
background: white;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
padding: 30px;
transition: transform 0.3s, box-shadow 0.3s;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
}
.gradient-button {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
padding: 15px 40px;
border: none;
border-radius: 50px;
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
cursor: pointer;
transition: all 0.3s;
}
.gradient-button:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
}
.neumorphic {
background: #e0e0e0;
border-radius: 20px;
box-shadow:
10px 10px 20px rgba(0, 0, 0, 0.1),
-10px -10px 20px rgba(255, 255, 255, 0.7);
padding: 30px;
}
.neumorphic:active {
box-shadow:
inset 5px 5px 10px rgba(0, 0, 0, 0.1),
inset -5px -5px 10px rgba(255, 255, 255, 0.7);
}
.glass {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
padding: 30px;
}
.animated-gradient {
background: linear-gradient(
45deg,
#667eea, #764ba2, #f093fb, #4facfe
);
background-size: 400% 400%;
animation: gradientShift 15s ease infinite;
}
@keyframes gradientShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
This card has rounded corners and a subtle shadow for depth.
Q1: What value makes a square element circular?
Q2: What does box-shadow: inset do?
Q3: Which gradient type rotates colors around a center point?
Q4: How do you create gradient text?
Q5: What is the correct syntax for a linear gradient from left to right?