Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Responsive Web Design – Introduction

Responsive Web Design - Introduction


What is responsive web design?

Responsive web design allows your web pages to display well on all devices.

Responsive web design uses only HTML and CSS.

Responsive web design is not a program or a JavaScript script.

Responsive web design is a website design method that can adapt to different devices and screen sizes. It provides a consistent user experience and ensures that website content is fully displayed and easy to use on any device.


Designing the best user experience

Responsive web design is a method for creating websites that adapt to different devices and screen sizes. As the variety of devices and screen sizes continues to grow, such as mobile phones, tablets, and desktop computers, users expect to browse websites normally and get a good user experience on any device.

Traditional web design is usually designed for desktop computers, but when browsed on mobile devices, pages may be displayed incompletely or require horizontal scrolling. Responsive web design uses flexible layouts, CSS media queries, and image processing techniques to enable websites to adapt to different screen sizes and devices.

The key to responsive web design is the use of Fluid Layout, which defines the size of page elements using relative units (such as percentages) rather than fixed units (such as pixels). In addition, Media Queries allow different CSS styles to be applied based on device characteristics (such as screen width, resolution, and orientation). By combining fluid layouts and media queries, the layout and style of a page can be dynamically adjusted according to the screen size to provide the best user experience.

Responsive web design also includes image processing techniques optimized for mobile devices. For example, Responsive Images can be used to load images of different sizes according to screen resolution, reducing page load time and bandwidth usage.

Web pages should automatically adjust their content according to the device size.

Desktop devices

Tablet devices

Mobile devices

The design and development of a page that responds and adjusts according to user behavior and the device environment (system platform, screen size, screen orientation, etc.) is called responsive web design.

Simple Example

The following is a simple example of responsive web design, assuming we want to create a simple navigation bar with different layouts and styles on different screen sizes.

HTML code:

   

   

CSS code

/* Navigation bar styles */

.navbar

{

   

background-color

:

#333

;

   

padding

:

10px

;

   

display

:

flex

;

   

justify-content

:

space-between

;

}

/* Logo styles */

.logo

{

   

color

:

#fff

;

   

font-size

:

20px

;

   

text-decoration

:

none

;

}

/* Menu styles */

.menu

{

   

list-style-type

:

none

;

   

margin

:

0

;

   

padding

:

0

;

   

display

:

flex

;

}

.menu

li

{

   

margin-left

:

10px

;

}

.menu

li a

{

   

color

:

#fff

;

   

text-decoration

:

none

;

}

/* Media query: layout adjustments on small screens */

@media screen and (max-width: 600px) {

   

.navbar

{

       

flex-direction

:

column

;

       

align-items

:

center

;

   

}

   

   

.menu

{

       

margin-top

:

10px

;

   

}

   

   

.menu

li

{

       

margin-left

:

0

;

   

}

}

Try it »

In the example above, we first use thetag to set the viewport to ensure the page is displayed at the correct scale on mobile devices. Then, through thetag, we link the stylesheet to the HTML file.

In CSS, we define the styles of the navigation bar (.navbar) as well as the Logo (.logo) and the menu (.menu). The menu uses Flexbox layout for horizontal arrangement.

Next, we use a media query (@media) to make layout adjustments on small screens. When the screen width is 600 pixels or less, the media query styles will apply. In this case, the navigation bar's direction will change to a vertical arrangement, the menu will be centered, and the left margin of menu items will be set to 0.

Through this simple example, when viewed on different devices, the layout and style of the navigation bar will automatically adjust according to the screen size, providing a better user experience.