L4 CSS Responsive Design
L4 CSS Responsive Design
Web Engineering
188.951 2VU SS20
Jürgen Cito
L4: CSS Responsive Design
• Media Queries
• Grid Layouts
Learning Goals
Graceful
degradation
Interface A Interface B
Mobile-first /
Progressive
enhancement
Interface A Interface B
Responsive
design
Interface A Interface B
Slides by Michael Nebeling and the work by Michael Nebeling and Moira C. Norrie: Responsive Design and Development: Methods,
Technologies and Current Issues, In: Proc. 13th Intl. Conf. on Web Engineering (ICWE 2013), LNCS, vol. 7977, pp. 510-513. Springer, 2013.
Responsive Design
Let content fill the container and define min/max constraints
Use relative units to specify position and size of text and media
Techniques
▪ Media Queries
▪ Fluid, Grid-based Layout
▪ Responsive Images
▪ Font Scaling
▪ …
Required Reading:
http://alistapart.com/article/responsive-web-design/
Media Queries
▪ Previously only media types (screen, print, braille, handheld …)
▪ @media rule
▪ Additional features
▪ color
▪ aspect-ratio
▪ max-width
▪ orientation
▪ resolution
▪ scan
▪ …
▪ Build complex queries using logical operators (not, and, only)
▪ Consider mobile
▪ Sometimes narrower/wider
▪ Prevent default shrinking
https://developer.apple.com/library/safari/documentation/
AppleApplications/Reference/SafariWebContent/UsingtheViewport/
Media Queries
@media screen and (max-width: 768px) {
#header {
width: 80px;
height: 120px;
display: inline;
}
#navigation {
width: 560px;
height: 120px;
}
#content {
#header { width: 640px;
height: 200px; height: 760px;
} }
#navigation { #footer {
width: 300px; height: 80px;
}
} }
#content { @media screen and (max-width: 1024px) {
width: 900px; ...
}
#footer {
height: 200px;
}
8
Media Queries and Fluid Layouts
Use of CSS3 media queries for defining breakpoints and style switches
@media screen and (max-width: 680px) { ... }
However…
▪ No linear progression between fix-sized designs
▪ Snaps at break points
▪ Horizontal scrolling may be required in-between
▪ Doesn't allow styling for future or unknown devices
https://html.spec.whatwg.org/multipage/embedded-content.html#adaptive-images
Responsive and Adaptive Images
▪ Detect visitor screen size, resolution, and pixel density
▪ Fetch respective image, size or version (additional download!)
▪ Use JavaScript, CSS media queries, and/or HTML5 markup
<picture>
<source src="pic-mobile.jpg" media="(max-width: 720px)" />
<source src="pic-tablet.jpg" media="(max-width: 1280px)" />
<source src="pic-desktop.jpg" />
<img src="default.png" /> <!–- User Agent not supporting picture element -->
</picture>
https://html.spec.whatwg.org/multipage/embedded-content.html#adaptive-images
Scaling Fonts
Scaling Fonts
▪ Use media queries and relative values for your fonts
▪ Font scales according to viewport and stays readable
W3C: https://www.w3.org/TR/css3-flexbox/
Image credit: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes
Layout Modes - Flexbox
▪ Container Properties
▪ flex-direction, flex-wrap, flex-flow, justify-content, align-items, align-content
▪ Item Properties
▪ order, flex-grow, flex-shrink, flex-basis, align-self
“Holy Grail Layout” with Flexbox
Holy Grail Layout <header>
https://www.w3.org/TR/css3-flexbox/#order-accessibility
Layout Modes - Grid
display: grid;
W3C: https://drafts.csswg.org/css-grid/
Image credit: https://webkit.org/blog/7434/css-grid-layout-a-new-layout-module-for-the-web/
Layout Modes - Grid
Placement of child elements in the grid
▪ grid-row, grid-column grid-row: 3;
▪ Element in one particular grid cell
grid-row: 2 / 5;
▪ Element spanning a grid area grid-column: 3 / span 2;
▪ 3rd column has fixed size and 4th column is relative to container size
grid-template-columns: 1fr 1fr 20px 20%;
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Relationship_of_Grid_Layout