Overview of Responsive Design in CSS
This article explains the overview of responsive design in CSS.
We will give an overview of the basic concept of responsive design and the role of CSS in implementing it.
YouTube Video
HTML for Preview
css-responsive-overview.html
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>CSS Properties Example</title>
7 <link rel="stylesheet" href="css-base.css">
8 <link rel="stylesheet" href="css-responsive-overview.css">
9</head>
10<body>
11 <!-- Header -->
12 <header>
13 <h1>CSS Properties For Responsive Design</h1>
14 </header>
15
16 <!-- Main content -->
17 <main>
18 <header>
19 <h2>Responsive Design Related Properties</h2>
20 </header>
21 <article>
22 <h3>Media Queries</h3>
23 <section>
24 <header><h4>CSS</h4></header>
25<pre class="sample">
26/* When the screen width is 600px or less */
27@media (max-width: 600px) {
28 body {
29 background-color: lightblue;
30 }
31}
32/* When the screen width is between 601px and 1200px */
33@media (min-width: 601px) and (max-width: 1200px) {
34 body {
35 background-color: lightgreen;
36 }
37}
38/* When the screen width is 1201px or more */
39@media (min-width: 1201px) {
40 body {
41 background-color: lightyellow;
42 }
43}
44</pre>
45 </section>
46 </article>
47 <article>
48 <h3>Fluid Layouts</h3>
49 <section>
50 <header><h4>CSS</h4></header>
51<pre class="sample">
52.container {
53 width: 80%; /* 80% width relative to the parent element */
54 margin: 0 auto; /* Center the element */
55}
56</pre>
57 </section>
58 </article>
59 <article>
60 <h3>Responsive Images</h3>
61 <section>
62 <header><h4>CSS</h4></header>
63<pre class="sample">
64img {
65 max-width: 100%;
66 height: auto;
67}
68</pre>
69 </section>
70 </article>
71 <article>
72 <h3>Viewport</h3>
73 <section>
74 <header><h4>HTML</h4></header>
75 <pre><meta name="viewport" content="width=device-width, initial-scale=1.0"></pre>
76 </section>
77 </article>
78 <article>
79 <h3>CSS Frameworks</h3>
80 <section>
81 <header><h4>HTML</h4></header>
82<pre>
83<div class="container">
84 <div class="row">
85 <div class="col-md-6">Left content</div>
86 <div class="col-md-6">Right content</div>
87 </div>
88</div>
89</pre>
90 </section>
91 </article>
92 <article>
93 <h3>Flexbox & Grid Layout</h3>
94 <section>
95 <header><h4>CSS</h4></header>
96<pre class="sample">
97/* Flex Example */
98.container {
99 display: flex;
100 flex-wrap: wrap; /* Allow elements to wrap */
101}
102
103.item {
104 flex: 1 1 200px; /* Each item has a minimum width of 200px */
105}
106
107/* Grid Example */
108.container {
109 display: grid;
110 grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
111 gap: 16px;
112}
113</pre>
114 </section>
115 </article>
116 </main>
117</body>
118</html>
Overview of Responsive Design in CSS
In CSS, responsive design is a technique for appropriately displaying the layout and content of a webpage on a variety of devices and screen sizes, such as smartphones, tablets, and desktops. To achieve responsive design, mainly the following techniques and methods are used.
Media Queries
1/* When the screen width is 600px or less */
2@media (max-width: 600px) {
3 body {
4 background-color: lightblue;
5 }
6}
7/* When the screen width is between 601px and 1200px */
8@media (min-width: 601px) and (max-width: 1200px) {
9 body {
10 background-color: lightgreen;
11 }
12}
13/* When the screen width is 1201px or more */
14@media (min-width: 1201px) {
15 body {
16 background-color: lightyellow;
17 }
18}
Media queries are a feature that allows you to apply different CSS rules based on device width, height, resolution, and more. This allows different styles to be applied depending on the screen size.
In this example, the background color changes according to the screen width.
Fluid Layouts
1.container {
2 width: 80%; /* 80% width relative to the parent element */
3 margin: 0 auto; /* Center the element */
4}
In responsive design, the width and height of elements are specified using relative units like percentages or em
instead of absolute pixel values. This allows the layout to flexibly change according to screen size.
Responsive Images
1img {
2 max-width: 100%;
3 height: auto;
4}
Images are also set to scale according to screen size. For example, using max-width
can adjust images so they do not exceed the width of their parent element.
Viewport Settings
1<meta name="viewport" content="width=device-width, initial-scale=1.0">
Use the <meta>
tag in HTML to ensure the browser correctly interprets the screen width of the device. Without this, responsive design will not function correctly, especially on mobile devices.
Utilization of CSS Frameworks
CSS frameworks like Bootstrap and Tailwind CSS are often used to efficiently achieve responsive design. These frameworks are designed to easily create responsive layouts using pre-defined classes.
Example: Bootstrap's Grid System
1<div class="container">
2 <div class="row">
3 <div class="col-md-6">Left content</div>
4 <div class="col-md-6">Right content</div>
5 </div>
6</div>
In the above example, when the screen width is md
(medium size, generally 768px or more), two columns are arranged side by side, but on narrower screens, they are arranged vertically.
Flexbox and Grid Layout
1/* Flex Example */
2.container {
3 display: flex;
4 flex-wrap: wrap; /* Allow elements to wrap */
5}
6
7.item {
8 flex: 1 1 200px; /* Each item has a minimum width of 200px */
9}
10
11/* Grid Example */
12.container {
13 display: grid;
14 grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
15 gap: 16px;
16}
Using CSS flexbox
or grid
allows for flexible layout adjustments. These layout techniques are well-suited for responsive design as they make it easy to automatically adjust layouts according to screen size.
Summary
Responsive design is an important method to ensure users can comfortably view websites regardless of the device they use. By combining media queries, flexible layouts, frameworks, and new layout technologies like Flexbox and Grid, it is possible to create more flexible and adaptive web pages.
You can follow along with the above article using Visual Studio Code on our YouTube channel. Please also check out the YouTube channel.