Blog

How to Design a Responsive Web Layout using @Media Rule

responsive web layout using @media rule

We took quite a considerable amount of time to discuss two display property values which include flex and grid. We also learned of their importance in creating flexible and responsive web layouts.

In this course, we will look at how to ensure that our website is suitable for all kinds of devices. We will take time to look at how to design a responsive web layout using @media rule.

In recent times, people connect to the internet with different kinds of devices, including phones, watches, tablets, etc. It is important that you make your website display appropriately in all these devices.

A website is described as responsive if it could automatically adjust its content for optimal viewing in all devices. In this case, the size of the device used to access it would not be a barrier.

Hence, it does not matter what the size of the device used in accessing the website.

How do I know my website is responsive?

People connect to the internet with different devices. This reason implores us to test and see how our website displays on different devices. Our experience in this testing tells us how others feel when they access our site.

There are different ways to carry out this test. Let’s look at 3 of them.

Resizing the browser

The easiest way to test a website for responsiveness is to resize the browser window to different sizes to match different viewports.

RD checker: full screen mode
FullScreen Mode before Browser Resize

tablet: creating responsive web layout using @media rule

Tablet Mode after Adjusting the Browser a Little

RD checker: Browser adjustment
Mobile Phone Mode after Full Adjustment

When doing this, we observe if there are rendering problems, and correct them at once.

Test in Different Mobile Phones

Though the browser resizing method helps, it will be better to test the site on real devices. You can use different mobile devices where you can perform different tasks.

creating responsive web layout using @media rule 2

creating responsive web layout using @media rule

I advise that you test your site using friend’s and family’s devices and see how your site renders on their devices. Also, perform different tasks and note how people who browse your site feel.

There is yet another method.

Use appropriate testing URL

These days, it’s easy to test your website for responsiveness. There are different websites that can help you do this, some of them include:

 

  1. First, visit any of the websites above;
  2. Enter the URL of your website.
  3. RD checker: creating responsive web layout using @media rule
  4. Test using different viewports.

RD checker: device selection

Among these 3 methods, it will be best to test your sites using real devices. Do not rely on only the first and last methods.

Welcome to the last of the 3-course series in CSS (Cascading Stylesheets). In this course, we will discuss the following:

  • how to work with generic and web fonts
  • how to add animations to elements, and
  • how to create responsive websites (web layouts)

Let’s get running, I can’t wait to start.

Responsive Web Layout using @Media Rule

As defined at the beginning of this course, a website is responsive if it adjusts perfectly to any device used to access it.

Remember that the flex, multiple columns and grid properties make building a responsive website easier these days. This is because they are responsive by default.

However, when using floats, and when we want to create breakpoints for different devices in our layout, we use the @media at-rule to make a website responsive.

Hence, we can wave flexbox and grid layouts and still create a responsive web layout using @media ruleset.

The viewport

The viewport is your screen size or the size of your browsing window. Viewport depends on the screen size of a device. Different devices have different screen sizes. Mobile phones have smaller screen sizes or viewport while laptops and desktops have larger viewports.

Viewports can be basically divided into 3:

  • mobile phones (max-width: 768px)
  • tablets and netbooks (max-width: 1024px)
  • laptops and desktops (min-width: 1024px)

Traditionally, the iPhone and iPad view ports are used where:

iPhone has (min-width: 320px; orientation: portrait) and (max-width: 480px; orientation: landscape).

iPad has (min-width: 768px; orientation: portrait) and (max-width: 1024px; orientation: landscape).

Because devices have different viewports, it is important for us to ensure that our website adjusts to users’ viewport to create a good user experience.

For us to achieve this, we need to include a <meta> element in the <head> section of our HTML document with the name and content attributes as follow:

<meta name = “viewport” content = “width=device-width, initial-scale=1.0”>

The above code will tell the browser how to adjust the webpages depending on the user’s device-width.

Haven understood the basics; we shall begin our journey towards building a responsive web layout using @media ruleset. We shall bring the detail of @media rule in our next post.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Advertisements
0
Would love your thoughts, please comment.x
()
x

Fatal error: Uncaught ErrorException: md5_file(/home/kitlosco/kmacims.com.ng/wp-content/litespeed/js/501e3682940581a90fd46db519acb048.js.tmp): Failed to open stream: No such file or directory in /home/kitlosco/kmacims.com.ng/wp-content/plugins/litespeed-cache/src/optimizer.cls.php:140 Stack trace: #0 [internal function]: litespeed_exception_handler(2, 'md5_file(/home/...', '/home/kitlosco/...', 140) #1 /home/kitlosco/kmacims.com.ng/wp-content/plugins/litespeed-cache/src/optimizer.cls.php(140): md5_file('/home/kitlosco/...') #2 /home/kitlosco/kmacims.com.ng/wp-content/plugins/litespeed-cache/src/optimize.cls.php(837): LiteSpeed\Optimizer->serve('https://www.kma...', 'js', true, Array) #3 /home/kitlosco/kmacims.com.ng/wp-content/plugins/litespeed-cache/src/optimize.cls.php(382): LiteSpeed\Optimize->_build_hash_url(Array, 'js') #4 /home/kitlosco/kmacims.com.ng/wp-content/plugins/litespeed-cache/src/optimize.cls.php(264): LiteSpeed\Optimize->_optimize() #5 /home/kitlosco/kmacims.com.ng/wp-includes/class-wp-hook.php(324): LiteSpeed\Optimize->finalize('<!DOCTYPE html>...') #6 /home/kitlosco/kmacims.com.ng/wp-includes/plugin.php(205): WP_Hook->apply_filters('<!DOCTYPE html>...', Array) #7 /home/kitlosco/kmacims.com.ng/wp-content/plugins/litespeed-cache/src/core.cls.php(477): apply_filters('litespeed_buffe...', '<!DOCTYPE html>...') #8 [internal function]: LiteSpeed\Core->send_headers_force('<!DOCTYPE html>...', 9) #9 /home/kitlosco/kmacims.com.ng/wp-includes/functions.php(5427): ob_end_flush() #10 /home/kitlosco/kmacims.com.ng/wp-includes/class-wp-hook.php(324): wp_ob_end_flush_all('') #11 /home/kitlosco/kmacims.com.ng/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #12 /home/kitlosco/kmacims.com.ng/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #13 /home/kitlosco/kmacims.com.ng/wp-includes/load.php(1280): do_action('shutdown') #14 [internal function]: shutdown_action_hook() #15 {main} thrown in /home/kitlosco/kmacims.com.ng/wp-content/plugins/litespeed-cache/src/optimizer.cls.php on line 140