
Dalam membuat website atau aplikasi berbasis web sebaiknya menggunakan disain web responsif (Responsive Web Design / RWD). Dengan RWD website atau aplikasi web kita dapat ditampilkan dengan baik di berbagai ukuran (diagonal) layar. Misalnya di TV dengan ukuran layar besar > 20 inch, di PC laptop/desktop 10-20 inch, atau di tablet / smartphone < 10 inch.
RWD terkait resolusi layar seperti UHD (3840 x 2160), FHD (1360px X 768px), HD (1024px X 768px), ataupun nHD (360p× X 640px).
Tampilan website atau aplikasi berbasis web yang responsif akan meningkatkan aksesibilitas (accessibility), usabilitas (usability) dan kepuasan pengguna (user satisfaction). Disamping itu juga akan mengurangi biaya pengembangan (Reduced development costs) karena tidak perlu membuat kode terpisah untuk device yang berbeda, dan dapat meningkatkan level SEO (Search Engine Optimization) karena lebih user-friendly dan mudah diakses.
Untuk membuat tampilan website atau aplikasi web dengan RWD, pertama kita perlu menambahkan dulu tag <meta> pada bagian HTML <head>, berikut ini.
Setelah itu kita bisa membuat layout web menggunakan CSS dengan 3 cara:
- CSS Layout Float
- CSS Layout Flexbox
- CSS Layout Grid
CSS memiliki @media rule yang diperkenalkan pada CSS2. @media rule ini digunakan untuk membuat style yang berbeda pada setiap tipe media. Misalnya: membuat rule style untuk layar PC, untuk printers, untuk smartphone, untuk TV, dan sebagainya.
Contoh kode CSS Media Queries untuk membuat style warna background biru pada device layar kecil (< 480px):
@media screen and (min-width: 480px) {
body {
background-color: #0000ff;
}
}
Berikut ini kode HTML5 dengan CSS Layout Float.
<!DOCTYPE html>
<html lang="en">
<head>
<title>RWD with CSS float</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Style the header */
header {
background-color: #666;
padding: 10px;
text-align: center;
font-size: 35px;
color: white;
}
/* Create two columns that floats next to each other */
nav {
float: left;
width: 15%;
height: 300px;
background: #ccc;
padding: 20px;
}
/* Style the list inside the menu */
nav ul {
list-style-type: none;
padding: 10px;
}
article {
float: left;
padding: 20px;
width: 85%;
background-color: #f1f1f1;
height: 300px;
}
/* Clear floats after the columns */
section::after {
content: "";
display: table;
clear: both;
}
/* Style the footer */
footer {
background-color: #777;
padding: 10px;
text-align: center;
color: white;
}
/* Responsive layout on small screens
makes two columns stack on top of each other instead of next to each other*/
@media (max-width: 600px) {
nav,
article {
width: 100%;
height: auto;
}
}
</style>
</head>
<body>
<header>
<p>R W D</p>
</header>
<section>
<nav>
<ul>
<li><a href="#">HTML5</a></li>
<li><a href="#">CSS3</a></li>
<li><a href="#">JavaScript ES6</a></li>
</ul>
</nav>
<article>
<h1>Responsive Web Design (RWD)</h1>
<p>
This website is using CSS Layout Float. We create a header, two
columns/boxes and a footer. On smaller screens, the columns will stack
on top of each other.
</p>
<p>Resize the browser window to see the responsive effect.</p>
<p>
You can learn more about this in
<a href="https://www.w3schools.com/html/html_responsive.asp"
>HTML Responsive.</a
>
</p>
</article>
</section>
<footer>
<p>© 2023</p>
</footer>
</body>
</html>Berikut ini kode HTML5 dengan CSS Layout Flex.
<!DOCTYPE html>
<html lang="en">
<head>
<title>RWD with CSS Flexbox</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Style the header */
header {
background-color: #666;
padding: 10px;
text-align: center;
font-size: 35px;
color: #fff;
}
/* Container for flexboxes */
section {
display: flex;
}
/* Style the navigation menu */
nav {
flex: 1;
background: #ccc;
padding: 20px;
height: 300px;
}
/* Style the list inside the menu */
nav ul {
list-style-type: none;
padding: 0;
}
/* Style the content */
article {
flex: 6;
background-color: #f1f1f1;
padding: 10px;
}
/* Style the footer */
footer {
background-color: #777;
padding: 10px;
text-align: center;
color: white;
}
/* Responsive layout
makes the menu and the content (inside the section) sit on top of each other
instead of next to each other */
@media (max-width: 600px) {
section {
flex-direction: column;
}
}
</style>
</head>
<body>
<header>
<p>R W D</p>
</header>
<section>
<nav>
<ul>
<li><a href="#">HTML5</a></li>
<li><a href="#">CSS3</a></li>
<li><a href="#">JavaScript ES6</a></li>
</ul>
</nav>
<article>
<h1>Responsive Web Design (RWD)</h1>
<p>
This website is using CSS Layout Flexbox. We create a header, two
columns/boxes and a footer. On smaller screens, the columns will stack
on top of each other.
</p>
<p>Resize the browser window to see the responsive effect.</p>
<p>
<strong>Note:</strong> Flexbox is not supported in Internet Explorer
10 and earlier versions.
</p>
<p>
You can learn more about this in
<a href="https://www.w3schools.com/html/html_responsive.asp"
>HTML Responsive.</a
>
</p>
</article>
</section>
<footer>
<p>© 2023</p>
</footer>
</body>
</html>
Berikut ini kode HTML5 dengan CSS Layout Grid.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>RWD with CSS Grid</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
.wrapper {
display: grid;
grid-gap: 0.1em;
grid-template-areas: "header" "sidebar" "content" "sidebar2" "footer";
}
header {
grid-area: header;
padding: 10px;
background-color: #666;
color: #fff;
text-align: center;
font-size: 35px;
}
nav {
grid-area: sidebar;
padding: 20px;
background-color: #ccc;
color: #444;
width: 180px;
height: 300px;
}
aside {
grid-area: sidebar2;
padding: 10px;
background-color: #ccc;
color: #444;
}
section {
grid-area: content;
padding: 10px;
}
footer {
grid-area: footer;
padding: 10px;
background-color: #666;
color: #fff;
text-align: center;
}
@media (min-width: 600px) {
.wrapper {
grid-gap: 5px;
grid-template-columns: 220px auto 200px;
grid-template-areas: "header header header" "sidebar content sidebar2" "footer footer footer";
}
}
</style>
</head>
<body>
<div class="wrapper">
<header>R W D</header>
<nav>
<ul>
<li><a href="#">HTML5</a></li>
<li><a href="#">CSS3</a></li>
<li><a href="#">JavaScript ES6</a></li>
</ul>
</nav>
<aside>Right Sidebar</aside>
<section>
<h1>Responsive Web Design (RWD)</h1>
<p>
The CSS Grid Layout Module offers a grid-based layout system, with
rows and columns, making it easier to design web pages without having
to use floats and positioning.
</p>
<p>Resize the browser window to see the responsive effect.</p>
<p>
You can learn more about this in
<a href="https://www.w3schools.com/html/html_responsive.asp"
>HTML Responsive.</a
>
</p>
</section>
<footer>© 2023</footer>
</div>
</body>
</html>
Silakan Anda coba ketiga teknik tersebut untuk membuat layout website atau aplikasi web menjadi responsif.











































