Responsive Web Design
Principle
RWD (Responsive Web Design) is an art of adjusting the size of elements on different screen.
The focus of RWD is designing on mobile layoutting first, then expand to desktop layouyt.
Steps of making basic layoutting
- Set the body CSS as below
body {
height: 100vh;
width: 100vw;
background-color: black; /* Optional */
margin: 0rem;
overflow: hidden; /* Optional */
}
Make a container. Ensure it inherit the width of the body parent, overflow can be scroll starting at this section.
Flexbox inside container
CSS Flex
Flexbox controls the size of your web elements dynamically
.divContainerClassName {
display: flex; /* Enable flexbox */
flex: 1; /* Elements ratio in flexbox */
flex-wrap: wrap; /* Elements wrap when there is not enough space */
flex-direction: row;
align-items: center; /* Arrange elements along vertical axis */
justify-content: center; /* Arrange elements along horizontal axis */
}
Bootstrap Layoutting
Breakpoint
Breakpoint is an important reference to adjust the layout based on device screen width
Breakpoint | Class infix | Dimensions |
---|---|---|
Extra small | None | <576px |
Small | sm | ≥576px |
Medium | md | ≥768px |
Large | lg | ≥992px |
Extra large | xl | ≥1200px |
Extra extra large | xxl | ≥1400px |
To operate with breakpoints, include the mixins from bootstrap source Sass files
@include media-breakpoint-between(md, xl) { ... }
/* which results in */
@media (min-width: 768px) and (max-width: 1199.98px) { ... }
Containers
Containers are the most basic layout element in Bootstrap and are required when using the default grid system
.container
, which sets a max-width at each responsive breakpoint.container-{breakpoint}
, which is width: 100% until the specified breakpoint.container-fluid
, which is width: 100% at all breakpoints
For example, try to adjust the window width to see the changes below
<div class="custom-box container">container</div>
<div class="custom-box container-md">container-md</div>
<div class="custom-box container-fluid">container-fluid</div>
Grid
Grid layout divides the screen into 12 equal column. Row must be declared under to provide grid layout, row is a flex.
col
, equal column widthcol-{number}
, number of column takencol-offset-{number}
, offset the col- `col-{breakpoint}-{number}, use the number of columns after the breakpoint
<div class="container-fluid">
<div class="row">
<div class="custom-box col-sm-6 col-lg-8 flex-fill">
col-sm-6 col-lg-8
</div>
<div class="custom-box col-6 col-lg-4 flex-fill">
col-6 col-lg-4
</div>
</div>
<div class="row">
<div class="custom-box col-6 col-sm-4 flex-fill">
col-6 col-sm-4
</div>
<div class="custom-box col-6 col-sm-4 flex-fill">
col-6 col-sm-4
</div>
<div class="custom-box col-6 col-sm-4 flex-fill">
col-6 col-sm-4
</div>
</div>
</div>
Alignment
<div class="container-fluid">
<div class="row justify-content-around">
<div class="custom-box col-3">col-3</div>
<div class="custom-box col-3">col-3</div>
<div class="custom-box col-3">col-3</div>
</div>
</div>
Self alignment
<div class="container">
<div class="row" style="height: 20vh;">
<div class="custom-box col align-self-start">One of three columns</div>
<div class="custom-box col align-self-center">One of three columns</div>
<div class="custom-box col align-self-end">One of three columns</div>
</div>
</div>
Display
- Inline: display new elements in one line
<div style="display: inline;">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<span>Spans are inline</span>
<div class="d-inline">Child elements are inline</div>
- Block: display new elements in next line
<div style="display: block;">
<span>1</span>
<span>2</span>
<span>3</span>
</div>
<div>divs are inline</div>
<div class="d-inline">Child elements are blocks</div>
- Flexbox
<div style="display: flex;">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<span class="d-flex">Display elements in flexbox</span>
Adjust flex items horizontally
<div class="d-flex justify-content-start">...</div>
<div class="d-flex justify-content-end">...</div>
<div class="d-flex justify-content-center">...</div>
<div class="d-flex justify-content-between">...</div>
<div class="d-flex justify-content-around">...</div>
<div class="d-flex justify-content-evenly">...</div>
Adjust flex items vertically
<div class="d-flex align-items-start">...</div>
<div class="d-flex align-items-end">...</div>
<div class="d-flex align-items-center">...</div>
<div class="d-flex align-items-baseline">...</div>
<div class="d-flex align-items-stretch">...</div>
Expand one item only
<div class="d-flex">
<div class="flex-grow-1">
<!-- This div grows to take up remaining space in the flex container -->
</div>
<div>
<!-- Fixed-width div that does not expand -->
</div>
</div>
Expand flex items
<div class="d-flex">
<div class="flex-fill bg-success text-white p-3">Item 1</div>
<div class="flex-fill bg-primary text-white p-3">Item 2</div>
<div class="flex-fill bg-warning text-dark p-3">Item 3</div>
</div>
Bootstrap Utilities
- Visibility keep the invisible element on the space, but did not show it out
<div class="visible">...</div>
<div class="invisible">...</div>
visibility: visible;
visibility: hidden;
- Margin and padding
<div class="p-1">Sets the padding to 1rem</div>
<div class="m-1">Sets the margin to 1rem</div>
- Size
w-100
takes 100% of parent’s width
Conclusion of using Bootstrap
- Pick a container
- Put a row and define the column. Commonly use justify, align, w-100
- Define the child elements of row. Commonly use col-{}, flex-fill, flex-grow