INFO 153A/253A Front End Web Architecture
Kay Ashaolu
1: Inline CSS (avoid)
<h1 style="color: red;">Heading</h1>
2: Internal CSS
<style>
h1 { color: red; }
</style>
3: External CSS (preferred)
<link rel="stylesheet" href="styles.css">
p
, div
, h1
.classname
#idname
[attribute="value"]
:hover
, :first-child
+-----------------------------------+
| Margin |
| +---------------------------+ |
| | Border | |
| | +-------------------+ | |
| | | Padding | | |
| | | +-----------+ | | |
| | | | Content | | | |
| | | +-----------+ | | |
| | +-------------------+ | |
| +---------------------------+ |
+-----------------------------------+
Float-based layouts (older method)
.sidebar { float: left; width: 30%; }
.main-content { float: right; width: 70%; }
Flexbox (modern, one-dimensional)
.container {
display: flex;
justify-content: space-between;
}
CSS Grid (modern, two-dimensional)
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Media queries
@media (max-width: 600px) {
.container { flex-direction: column; }
}
Fluid grids using percentages
Flexible images: max-width: 100%;
Mobile-first approach
Viewport meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1">
Example (Sass):
$primary-color: #3498db;
.button {
background-color: $primary-color;
&:hover {
background-color: darken($primary-color, 10%);
}
}
BEM (Block Element Modifier)
.block__element--modifier { }
Example (styled-components):
const Button = styled.button`
background-color: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'black'};
`;
:root {
--primary-color: #3498db;
}
.button {
background-color: var(--primary-color);
}
Feature queries:
@supports (display: grid) {
.container { display: grid; }
}
Note: Responsive design is crucial in today's multi-device world
@media (max-width: 600px) {
.container {
width: 100%;
padding: 10px;
}
}
@media [type] and (condition) { ... }
Note: Media queries are the cornerstone of responsive design
Unit | Relative To | Use Case |
---|---|---|
em | Parent element's font size | Component-specific sizing |
rem | Root element's font size | Global, consistent sizing |
html { font-size: 16px; }
.container { font-size: 1.5rem; } /* 24px */
.container p { font-size: 1em; } /* 24px */
Note: Relative units provide flexibility and maintain proportions across devices
.hero {
height: 100vh;
width: 50vw;
}
Note: Viewport units offer unique solutions for responsive design challenges
body {
font-size: calc(16px + 0.5vw);
}
Note: Combining these techniques creates robust, adaptable layouts