Creating Page Layouts w/CSS: Flex
CSCI 185, Spring 2024�Intro to Computer Programming for the Web
1
Outline
2
CSS & Layouts
Layouts are the hardest thing about CSS for many reasons:
3
Layout Tips
Outline
5
CSS Units: Absolute Units
6
Units | Example |
Pixels | width: 300px; |
Points | font-size: 12pt; |
CSS Units: Relative Units
7
Units | Description | Example |
em | Relative to the size of the parent element (2em means 2 times the parent element’s font size). | font-size: 2em; |
rem | Relative to size of the root html element (2rem means 2 times the html element’s font size).. See em v. rem CodePen. | font-size: 2rem; |
fr | “Fractional units” of the available space (within the parent element). | grid-template-columns: 1fr 1fr 40px 20%; |
vw | Relative to 1% of the width of the viewport (size of browser window) | width: 30vw; |
vh | Relative to 1% of the height of the viewport (size of browser window) | width: 10vw; |
% | Relative to the parent element | width: 100%; |
Outline
8
Using Media Queries
Media queries allow you to set CSS style rules based on the type of media and the device dimensions of the viewport. Example:
“Turning On” Media Queries in your HTML
In order to make sure that media queries are honored, you need to include a meta tag in the <head></head> of your html file:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Practice: 00-media-query
11
Tablet or Mobile
Desktop
Using Flexbox: Suggested Strategy
Parent Container�Put the display into “flex mode” display: flex and then use the following properties to align the child items:
Child Container
Apply sizing to the child container as needed, using the box model (padding, margin, width, height, border).
13
14
Flexbox: Practice 2
Open 02-flexbox-wrapping. Can you create the following layouts?
1
2
3
Flexbox: Practice 1 Cont’d – Using Media Queries
Make it so that #1 is for Desktop screens, #2 is for Tablet, and #3 is for Mobile
1
2
3
Flexbox: Practice 2 (on your own)
Open 03-flexbox-media-queries and create these screens:
Desktop
Tablet
Mobile
Flexbox: Navigation Bar
Open 04-nav-bar, and try to make the following: