CSS Advanced Notes
A comprehensive guide to advanced CSS concepts and techniques
Table of Contents
- CSS Border
- CSS Outline
- CSS Shadow
- CSS Background
- CSS Gradient
- CSS Object
- CSS Shape
- CSS Filter and Blend
- SVG
- CSS Interaction
- CSS Scrolling
- Media Query
- Container Query
- Feature Query
- CSS Accessibility
- CSS Performance
- CSS Hacks
- CSS Tools
- CSS Style Guide
CSS Border
Border Radius
Border radius creates rounded corners for elements. It can be applied uniformly or to individual corners.
/* Uniform border radius */
.element {
border-radius: 10px;
}
/* Individual corners */
.element {
border-top-left-radius: 10px;
border-top-right-radius: 20px;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 40px;
}
/* Elliptical corners */
.element {
border-radius: 50px / 25px;
}
Border Color
Defines the color of element borders using various color formats.
.element {
/* Named color */
border-color: red;
/* Hex color */
border-color: #ff0000;
/* RGB */
border-color: rgb(255, 0, 0);
/* RGBA with transparency */
border-color: rgba(255, 0, 0, 0.5);
}
Border Style
Controls the appearance of border lines.
.element {
border-style: solid; /* Solid line */
border-style: dashed; /* Dashed line */
border-style: dotted; /* Dotted line */
border-style: double; /* Double line */
border-style: groove; /* 3D grooved effect */
border-style: ridge; /* 3D ridged effect */
border-style: inset; /* 3D inset effect */
border-style: outset; /* 3D outset effect */
border-style: none; /* No border */
border-style: hidden; /* Hidden border */
}
Border Image
Uses images for element borders instead of solid colors.
.element {
border-image-source: url('border.png');
border-image-slice: 30;
border-image-width: 10px;
border-image-repeat: round;
/* Shorthand */
border-image: url('border.png') 30 / 10px round;
}
Border Collapse
Controls how table borders are rendered.
table {
/* Merge adjacent borders */
border-collapse: collapse;
/* Keep borders separate */
border-collapse: separate;
/* Control spacing between separate borders */
border-spacing: 2px;
}
Box Decoration Break
Determines how element fragments are rendered when broken across lines or pages.
.element {
/* Clone decorations for each fragment */
box-decoration-break: clone;
/* Slice decorations at break points */
box-decoration-break: slice;
}
Multiple Borders
Techniques for creating multiple borders around elements.
/* Using outline */
.multiple-borders {
border: 5px solid red;
outline: 5px solid blue;
outline-offset: 5px;
}
/* Using box-shadow */
.multiple-borders {
border: 5px solid red;
box-shadow:
0 0 0 5px blue,
0 0 0 10px green;
}
CSS Outline
Outlines differ from borders as they don't affect layout and can be non-rectangular.
.element {
outline: 2px solid red;
outline-offset: 5px;
/* Auto-color high contrast outline */
outline: 2px auto -webkit-focus-ring-color;
}
CSS Shadow
Text Shadow
Adds shadow effects to text content.
.text {
/* Basic shadow */
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
/* Multiple shadows */
text-shadow:
2px 2px 4px rgba(0, 0, 0, 0.5),
-2px -2px 4px rgba(255, 0, 0, 0.5);
}
Box Shadow
Adds shadow effects to elements.
.element {
/* Basic shadow */
box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.3);
/* Inner shadow */
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.3);
/* Multiple shadows */
box-shadow:
5px 5px 10px rgba(0, 0, 0, 0.3),
inset 0 0 10px rgba(0, 0, 0, 0.3);
}
CSS Background
Background Color
Sets the background color of elements.
.element {
/* Solid color */
background-color: #ff0000;
/* Transparent color */
background-color: rgba(255, 0, 0, 0.5);
}
Background Image
Adds images as element backgrounds.
.element {
/* Single image */
background-image: url('image.jpg');
/* Multiple images */
background-image:
url('overlay.png'),
url('background.jpg');
}
Background Repeat
Controls how background images repeat.
.element {
background-repeat: repeat; /* Default */
background-repeat: no-repeat; /* Single instance */
background-repeat: repeat-x; /* Repeat horizontally */
background-repeat: repeat-y; /* Repeat vertically */
background-repeat: space; /* Evenly spaced */
background-repeat: round; /* Scaled to fit */
}
Background Position
Controls the starting position of background images.
.element {
/* Keywords */
background-position: top left;
background-position: center center;
/* Percentages */
background-position: 50% 50%;
/* Length values */
background-position: 20px 50px;
}
Background Clip
Determines the painting area of the background.
.element {
background-clip: border-box; /* Default */
background-clip: padding-box; /* Inside padding */
background-clip: content-box; /* Inside content */
background-clip: text; /* Clips to text */
}
Background Origin
Sets the background positioning area.
.element {
background-origin: border-box; /* From border edge */
background-origin: padding-box; /* From padding edge */
background-origin: content-box; /* From content edge */
}
Background Size
Controls the size of background images.
.element {
background-size: auto; /* Default */
background-size: cover; /* Cover container */
background-size: contain; /* Fit within container */
background-size: 100% 100%; /* Stretch to fit */
background-size: 50px 50px; /* Specific size */
}
Background Attachment
Determines how backgrounds scroll with content.
.element {
background-attachment: scroll; /* Scroll with content */
background-attachment: fixed; /* Fixed to viewport */
background-attachment: local; /* Scroll with element */
}
CSS Gradient
Color Stop List
Defines color transitions in gradients.
.element {
background: linear-gradient(
90deg,
red 0%,
yellow 50%,
blue 100%
);
}
Linear Gradient
Creates a gradient in a single direction.
.element {
/* Directional */
background: linear-gradient(to right, red, blue);
/* Angle */
background: linear-gradient(45deg, red, blue);
/* Multiple color stops */
background: linear-gradient(
to bottom,
red,
orange,
yellow,
green,
blue,
indigo,
violet
);
}
Radial Gradient
Creates a gradient that radiates from a center point.
.element {
/* Basic */
background: radial-gradient(red, blue);
/* Shaped */
background: radial-gradient(circle, red, blue);
/* Positioned */
background: radial-gradient(
circle at center,
red,
blue
);
/* Sized */
background: radial-gradient(
circle closest-corner at center,
red,
blue
);
}
Conic Gradient
Creates a gradient that rotates around a center point.
.element {
/* Basic */
background: conic-gradient(red, blue);
/* From angle */
background: conic-gradient(from 45deg, red, blue);
/* At position */
background: conic-gradient(at 50% 50%, red, blue);
/* Color wheel */
background: conic-gradient(
from 0deg,
red,
yellow,
lime,
aqua,
blue,
magenta,
red
);
}
Gradient Reference
Common gradient patterns and techniques.
/* Rainbow gradient */
.rainbow {
background: linear-gradient(
to right,
red,
orange,
yellow,
green,
blue,
indigo,
violet
);
}
/* Metallic effect */
.metallic {
background: linear-gradient(
170deg,
#C0C0C0 0%,
#FFFFFF 47%,
#C0C0C0 100%
);
}
/* Glass effect */
.glass {
background: linear-gradient(
135deg,
rgba(255,255,255,0.1) 0%,
rgba(255,255,255,0.4) 100%
);
}