loader image
How to make your HTML responsive with CSS (1)
Web Development

How to make your HTML responsive with CSS

The trends in responsive design are going crazy! Every web application built today is responsive to diversified devices to be accessed by the end-users. As you are already here, you must have known the importance of Responsive HTML of a website or application.

We will be working in CSS, not in HTML. The HTML code remains the same, the CSS styles will present the HTML code in different layouts for mobiles, tablets, desktops. This is one of the major reasons to separate content from presentation. 

Nowadays, we have got many CSS frameworks to make our website responsive at ease. These frameworks use pure CSS to achieve the required level responsiveness. So, exposure with CSS for responsive design makes it easy to understand any CSS frameworks such as Bootstrap.

Let’s discuss how to make HTML content responsive to CSS styles.

What is Responsive?

responsive html

A web page design that adapts to different screen sizes is called ‘responsive’. A desktop layout cannot fit smaller screen sizes like mobile or tablets. In the early days, the desktop site as shown in the mobiles,  the user has to zoom in, scroll in X and Y direction to read the content on the webpage, which led to poor user experience. This is where a responsive design gives in.  The layouts are made to adapt to different screen sizes, making the website attractive and convenient for the user in different devices. 

Here well will focus on three areas in responsive design. 

  • Layouts
  • Images
  • Text

Media Queries

Media Query is provided by CSS to achieve the concept of responsiveness. This is a way to conditionally apply CSS rules.

The syntax goes like this,

@media rule indicates the media query using which different styles can be applied based on media types, screen sizes, orientation.

Media queries can be used to specify certain media types such as screen, speech, print and certain media features such as min-width, max-width, min-height, max-height, orientation and more.

The CSS rules are defined by specifying a media type and media features only if those specified criteria are met with the CSS which was applied.

The above sample illustrates the element with class content, which will be applied with the style width 100% only when the type is a screen and the browser width is less than or equal to 768px.

To combine more than one media feature the not keyword is used.

This example applies styles to the browser width 480px to 768px.

Separate stylesheets can be used for specific media query like,

Related: How To Host A Simple Static Website On AWS S3

Viewport

The viewport is the actual area in the rendering surface where the web page is displayed. The width or height constraint specified in the media query refers to the viewport width or height usually on the browser. 

To make an HTML page to be responsive, the viewport meta tag has to be included.

This sets the page width to device-width and initial zoom to 1. If the meta tag is not included the mobile or tablet will try to fit the desktop layout but, it might not fit properly.

Breakpoints

Breakpoints in CSS are the places where different styles are applied, specified in the media query. For example, up to 480px styles for mobile devices get applied at the breakpoint. A standard breakpoint cannot be provided for each device. But if you want to know the commonly used breakpoints you can navigate to this link.

Responsive Layout

The most important part of the responsive design is making the page layout align the best according to the screen size. For desktops, contents can be shown in three columns; for tablets two columns and for mobile devices laying out content in one column is preferred for better user experience.

There are different layouts that refer to design patterns such as fluid, column drop, layout shifter, tiny tweaks, off-canvas. To understand these layouts in details you can navigate to this link.

Fixed layout – Fixed width layout remains the same in all devices. The elements are fixed with absolute units such as pixels, inches, centimeters …

Fluid layout – Fluid layout stretches or shrinks with device width. The element’s properties are specified with relative units such as %, em, vw, vh …

Let’s take a look at the example of how media queries conditionally apply CSS styles. Here, the flexbox is used to create a responsive layout at ease.

Mobile-First Strategy

In a mobile-first strategy, a web page layout is created for small devices at first since there won’t be much work just stacking every element vertically. Then progress to medium devices and then to large-screen devices. This makes development easier and faster.

mobile responsive Design

For medium devices,

Medium size - responsive design

For large-screen devices,

large - responsive design

Responsive Image

To make an image responsive, set width to 100% and height to auto. This will automatically resize the image based on the browser width.

Image - responsive design

Responsive Text

Responsive text can be achieved by using viewport units such as vw (viewport width), vh (viewport height), vmin, vmax. Viewport units indicate the browser’s viewport, 1vw = 1% of viewport width.

Text - responsive design

Also Read: How To Convert HTML To PDF Using Node.js?

EndNote

Making an HTML code responsive is no more arduous. With the help of CSS, anyone can fit the content on all the devices just as the native requires a proper understanding of the rendering surface. I hope this tutorial helps you understand the concept of responsive HTML and areas which are indispensable for real-time projects and expertise.  Have any questions? Leave it in the comments, we are happy to help. 

Don’t forget to subscribe to the weekly newsletter for exclusive updates on web development!

Looking to build a highly responsive web application? Discuss with our technical experts and get your requirements met on the time to market.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
The following two tabs change content below.
Gokulaselvan

Gokulaselvan

Gokulaselvan is a software developer. He is specializing in front end frameworks such as Angular, and React. He is a passionate techie with great fondness towards problem-solving in reactive programming. You can find him browsing and exploring different trends or either searching for easier ways to solve a problem. Gokul loves to share his new learning with others.

Leave a Reply

Your email address will not be published. Required fields are marked *

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.