CSS Fundamentals and Syntax
CSS stylesheets can be integrated into HTML documents using three primary methods: inline styles via the style attribute, embedded styles within the <style> tag in the <head>, and external stylesheets linked using the <link> tag or imported via @import.
Basic Styling Properties
Essential properties for visual formatting include color (supporting RGB, HEX, HSL), font (shorthand for size, family, weight, style), and background attributes. Backgrounds support complex configurations such as background-size (with values like cover or contain), background-clip, and background-origin.
Text manipulation relies on properties like text-decoration (line, color, style), white-space, text-indent, and text-transform. Alignment is handled via text-align and vertical-align, while line-height controls vertical spacing.
Selectors and Specificity
The specificity hierarchy determines which styles are applied. The order of precedence, from highest to lowest, is:
!important- Inline styles
- ID selectors
- Class selectors, attribute selectors, and pseudo-classes
- Element selectors
- Universal selectors (
*) - Inherited styles
Combinators
- Descendant:
ul liselects allliinsideul. - Child:
ul > liselects direct children only. - Adjacent Sibling:
h2 + pselects thepimmediately followingh2. - General Sibling:
h2 ~ pselects allpelements followingh2.
The Box Model and Layout Contexts
The CSS box model consists of content, padding, border, and margin. Padding cannot accept negative values, whereas margins can.
Clearing Floats and BFC
To handle layout issues caused by floating elements (parent collapse), several methods exist:
- Using
overflow: hiddenon the parent. - Adding a clear element (or pseudo-element) with
clear: both. - Creating a Block Formatting Context (BFC). BFCs are triggered by
float,position: absolute/fixed,display: inline-block, oroverflowvalues other than visible.
Positioning Schemes
static: Default flow.relative: Positioned relative to its normal position.absolute: Positioned relative to the nearest positioned ancestor.fixed: Positioned relative to the browser window.sticky: Toggles between relative and fixed based on scroll position.
Modern CSS3 Features
Pointer Events
The pointer-events property controls interaction with the mouse. Setting it to none effectively disables click, hover, and active states on an element, passing events through to layers below.
.disabled-layer {
pointer-events: none;
opacity: 0.5;
cursor: not-allowed;
}
Image Scaling and Object Fit
To prevent image distortion when dimensions change, use object-fit:
.responsive-img {
width: 100%;
height: 100%;
object-fit: cover; /* Ensures the image covers the area without stretching */
}
Link State Management
CSS pseudo-classes define link states. The correct order for the LVHA rule is: link, visited, hover, active. Additionally, focus styles accessibility.
Advanced Shadow Effects
Shadows can be layered to create depth. The following example demonstrates a "curled" effect using pseudo-elements and rotated shadows:
.curved-card {
position: relative;
width: 300px;
height: 120px;
background: #ffffff;
border-radius: 8px;
margin: 40px auto;
box-shadow: 0 1px 4px rgba(0,0,0,0.2), 0 0 20px rgba(0,0,0,0.05) inset;
}
.curved-card::before,
.curved-card::after {
content: "";
position: absolute;
z-index: -1;
bottom: 15px;
width: 50%;
height: 20%;
box-shadow: 0 15px 12px rgba(0, 0, 0, 0.5);
}
.curved-card::before {
left: 10px;
transform: rotate(-4deg);
}
.curved-card::after {
right: 10px;
transform: rotate(4deg);
}
Transparency Techniques
Unlike opacity, which affects an element and all its children, rgba or hsla colors allow for semi-transparent backgrounds while keeping child content fully opaque.
.frosted-glass {
/* Child elements remain opaque */
background: rgba(255, 255, 255, 0.9);
}
Pseudo-classes and Pseudo-elements
Pseudo-classes target specific states (e.g., :focus, :checked, :disabled, :nth-child(n)). Pseudo-elements target parts of an element (e.g., ::before, ::after, ::first-letter).
Animations and Transitions
Transitions
Transitions allow smooth changes between property states. Key properties include transition-property, transition-duration, transition-timing-function (e.g., ease-in-out), and transition-delay.
Keyframe Animations
Animations offer more control using @keyframes. Properties include animation-name, animation-duration, animation-iteration-count (e.g., infinite), and animation-direction (e.g., alternate).
Flexible Box Layout (Flexbox)
Flexbox provides efficient one-dimensional layout management.
- Container properties:
display: flex,flex-direction(row/column),justify-content(main-axis alignment),align-items(cross-axis alignment),flex-wrap. - Item properties:
flex-grow(expansion factor),flex-shrink(shrink factor),flex-basis(default size),align-self.
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
.flex-item {
flex: 1 1 auto; /* grow, shrink, basis */
}
Responsive Design Strategies
Media Queries
Media queries apply styles based on device characteristics.
/* Applies to screens wider than 768px */
@media screen and (min-width: 768px) {
.container {
max-width: 960px;
margin: 0 auto;
}
}
Viewport Units (vw/vh)
Viewport units (vw, vh, vmin, vmax) relate to the browser window size. For robust mobile layouts, vw is often combined with rem.
/* Setting root font size relative to viewport width */
html {
font-size: calc(100vw / 37.5); /* 1rem = 10px at 375px width */
}
/* Constraining extremes */
@media screen and (max-width: 320px) {
html { font-size: 64px; }
}
@media screen and (min-width: 540px) {
html { font-size: 108px; }
}