This was the beginning of making the page look good in any given screen size. So my journey in Responsive web design began:
This is the first question that would come to anyone’s mind when they encounter a new thing.
Responsive web design is the use of HTML/CSS to have a good design or layout in any screen size either by shrinking, enlarging, shrinking, hiding or moving its contents.
Do you ever realize that when you open most sites the design outlay looks good despite the screen size you are using? Try it with your Twitter or Instagram accounts and check it out. And just like that, that is what responsiveness is. But how do we make a page responsive?
There are several ways to make the web page responsive, these include:
1. Use of Media queries
A media query is a CSS feature used to create new rules or overwrite existing rules for the web page contents styling in various screen sizes. The rules can be written in a different file and a link attached to the head tag to link the HTML file to the file with media queries. This can be done this link:
<link rel=”stylesheet” type=”text/CSS” media=”screen
and (max-device-width: 480px)” href=”style.css” />
Media queries can as well be written in the main CSS file. Since CSS follows the order made in styling, it is always wise to write the media queries below the main styling you have.
So how are they written? Here are a few examples of ways to write media queries:
@media only screen and (min-width: 768px) and (max-width: 1200px) {
.home-page {
padding: 2.5em;
font-size: 1em;
width: 100%;
}
.featured-speakers {
width: 100%;
font-size: smaller;
grid-template-columns:auto auto;
}
}
The styling above will be applied when the screen has a width of 768px and 1200px, but on the other screen sizes, it applies the style given.
@media screen and print (max-width: 576px) {
.main-page {
display: flex;
flex-direction: column;
}
footer {
background-color: red;
color: #fff
}
}
With the above media query, the style in it is applied to a maximum width of 576px both on-screen and print
@media only screen and (orientation: landscape;){
.home-page {
font-size: 2em;
color: #fff;
}
}
With the orientation, the style in the media query is applied in the given orientation, either portrait or landscape.
2. Using CSS grids and flexboxes
CSS grid and flexbox are used in CSS to determine how elements are positioned on a webpage screen. Using either of the two enables content to be spread across the width of a screen. An example of flexbox is:
.home-page {
display: flex;
justify-content: space-
between;
}
With this, despite the screen size, the content will fill the page with some space between each of them.
An example of a grid would be:
.home-page{
dispay: grid;
grid-template-column: auto auto;}
This gives chance to the content to fill the page with two columns in each screen size.
3. Using responsive units of measurement
After spending a few months learning web development, I came across several CSS units and my curiosity drove to me to make research on the right CSS unit to use for a responsive design. A list of responsive units to use include:
We can use the measurements to set the sizes of everything responsively including font-size.
*You can read more of this at W3Schools.
4. Using frameworks:
Frameworks such as the bootstrap make responsiveness effective. Bootstrap uses the grid system in that it can tell how many columns should be applied in various screen sizes from the extra-large to the extra small screens. This styling is applied in HTML through bootstrap classes.
Congratulations! You too can make your website beautiful in any screen size by trying any of the tips above! All the best!