Learning Outcomes
On completion of this page you will know how to control the width of a box object using border, padding and margin
Go to topOn completion of this page you will know how to control the width of a box object using border, padding and margin
Go to topDue to the internet computer data can now be displayed on 400 pixel cellphones, on 5120 pixel super monitors and any number of other monitors and tablets with sizes between these two extremes.
Due to it's small screen size there is a limit to how we can design our web pages to suit a cellphone. As we get to the larger monitors, however, we can be more inventive about how we lay out both our text and images.
Although as the size of monitors gets bigger bigger we can be more inventive with our designs. Once we reach the 1000px limit, however, then one should stop further designing. Why is ths? Have you tried reading a large block of text across a window that is 1000 px wide? It is a frustrating and exhausting experience.
There are two ways of creating a responsive design:
Up until now we have used two measuring units: px for determining the width of elements and rem for measuring text. Here we are going to introduce another measurement - the percentage.
The percentage simply measures the width of an element relevant to its immediate container.
In Fig 1 above if the width of the article element is 600px and the width of the img element is 80% then the physical width of the img element 480px, i.e. 80% of 600. Similarly if the width of the article element was reduced to 400px then the width of the img element would be 320px.
There is nothing to explain in Listing 1. It simply consists of a heading, an image and a block of text inside the main element.
This is the CSS code for styling the HTML code in Listing 1. At first glance there is nothing unusual about it apart from line 10. Here the width of the img element is set to 30%.
Since the body and main elements have no width setting, they will take up the entire width of the browser window. This will result in the img element having 30% of the width of the browser window.
As the width of the browser window increases or decreases the width of the img element increases or decreases accordingly.
The above video shows the img element contracting along with the browser window
Go to topThe above example of responsive design works fine as long as all of the elements have a display with a value of block and are therefore placed neatly underneath each other.
Due to the huge variety of sytling facilities that CSS provides us with, web pages are rarely as simply laid out as the as the percentages application. One example of this is the Grid layout, which will be the basis of of our next example of Responsive Design.
The above code contains nothing new apart from the fact that each img element is assigned a class. We shall discuss those later when we discuss the CSS code.
It contains six images that we will try to arrange on a screen. How we arrange them depends on the width of our screen. Clearly we cannot arrange six images across a cellphone screen that has a width of 400 pixels.
How we arrange our images will be:
If there are more images and you have access to a very large screen you might add another media query to accommodate four columns.
We now look at how we will use CSS to style our layouts for us.
The above code shows the first third of the CSS file. It is similar to the CSS code that we have been using so far. There are a few items of interest that we need to point out however:
Below we see the images arranged according the the instructions in Listing 4-1
The width of the window in this case is 495px. After another 5 pixels it should tip over to a rearranged layout that will allow a two column display. This is what we are going to look at next.
We have allowed the single column display to be applied to browser windows as far as 499 pixels wide. This is in order to accommodate the larger cellphones that may have a width greater than 400 pixels.
By the time the browser window reaches a width of 500 pixels it is time to look at accommodating the layout to a two column display. Enter the Grid layout!
All of this code should be familiar to you apart from line 23. This is our first encounter with a media query and thus we need to go into some details about its conmponents.
The first component is the @rule which is @media. This is followed by the media-type which will be screen in our case. This means thar we shall be dealing with what appears on the computer monitor. (Another common media type is print)
The third component is media feature. In our case this is (min-width:500px). This means that the minumum width of the element in question is 500 pixels. In other words this applies to elements that are 500 pixels or more in width.
So if our window is 500 pixels or more in width, what happens to it? We shall explain below.
At line 25 the display property of the article element from Listing 3 is set to grid.
Line 26 specifies that the grid consist of two columns, each 48% of the width of the article element.
Lines 29 - 52 consist of six classes, each one specifying where each image from Listing 3 is to be placed on the grid.
Here we look at the final stage of rearranging the the images for a larger screen. The code below kicks in once the width of the window reaches 900 pixels.
The code here is very similar to that in Listing 4-2, with only very small changes. We will only discuss those same changes.
In line 54 the media feature has changed to (min-width:900px). This applies to screen widths of 900 pixels and upwards.
The next difference is that at line 57 the horizontal space is divided into three columns of equal width. This is also reflected in the classes that determine which item goes into what cell. Notice that at lines 69 and 81 the column count goes up as far as three while the row count goes only as far as 2.
Below is a demonstration of the number of columns increasing as the containing window gets wider.
Here we looked at two different ways of making the same web page adapt itself to different screen widths.
The simplest method was using percentages where all of the elements of a window would be scaled in proportion to the width of the same window. It has, however, a drawback that it can only be applied to web pages where all elements have a display value of block. It is not able to work in situations where elements have to be rearranged, depending on the width of the containing window.
The other option is to use media queries. This works differently to the percentage method. It selects a number of descrete widths from small cellphones to the large monitors and gives each width a different layout where each layout is the most appropriate for that particular screen width.
Go to top