After this lesson, you will be able to:
font-size
and line-height
propertiesLe CSS est élastique par nature : une div occupera toute la largeur de l'écran quelque soit l'écran.
=> le montrer sur un ex
Cependant, si notre écran devient trop étroit, il se peut que nous ne puissions plus faire tenir nos éléments de notre navbar les uns à côté des autres !
We all have a computer. We all have a cell phone. Some of us also have a tablet. Maybe someone has an smart TV. What does this mean for us? It means that when a user visits our website, we won't know which device will she be using? How will it adapt the appearance to show nicely on different devices? Responsive Web Design to the rescue!
Nowadays, the number of users that will visit our website from a mobile device is greater than the number of visitors from other devices. Since there are many types of device resolutions, we will have to adapt our design depending on which device visits our site. This is the goal of Responsive Web Design (RWD).
RWD is the practice of building a website that is suitable to work on every device and every screen size, no matter how large or small, mobile or desktop. It's focused around providing an intuitive and gratifying User Experience for everyone.
We know the design should change depending on the device, but we don't have to be focused on the device. Instead, we have to think in the viewport size.
We don't have to create a specific design for the last iPhone or Samsung device. You have to create a design for a viewport with a width of 680px. Why?
The goal we want to achieve is not to have a perfect website in every possible single device (that would take super long and would make development so complex!). Instead, we will group all the devices with a similar screen size into one viewport.
We will see this deeply in the Media Queries section.
Approche bottom-to-top : plus facile de rajouter que d'enlever.
We could think that the normal workflow when creating a website is:
We avoid this concern with the Mobile First approach.
We will not create websites from large screens (computer) and then translate them into smaller screens (mobile). We will do the opposite: we will design the mobile view first and then add more elements incrementally for larger resolutions.
This will help us keep just the necessary elements for our mobile version. The mobile visitors don't have to see anything but the necessary information. This technique is very useful to ensure a good UI/UX experience. It's the best way to keep the user focused on what is really important.
Another reason to do Mobile First is avoid loading unnecessary resources and assets. We add an image to the mobile version just if it's necessary and it adds some value to the design. We also have to add just the necessary scripts to work in mobile.
A media query is an optional media type with zero or more expressions that limit the style sheets' scope by using media features: width, height, etc.
In other words: we use media query to set the viewport sizes where different styles will be applied. We can consider each one like an step in the design. While we resize the browser, we see the different styles depending on the current resolution.
Exercice 1
Sur un écran de plus de 500px, rendre le fond de page en vert
Exercice 2
Sur un écran de plus de 500px, faire en sorte que la div ne prenne plus 100% de large mais seulement 50%
The syntax to create a media query is the following:
CSS media query within a stylesheet:
CSS media query within a stylesheet:
The [media-features]
are optional, and they will specify where to apply the styles indicated in the CSS or the specified file.
When a media query is true, the corresponding style sheet or style rules are applied, following the normal cascading rules. Style sheets with media queries attached to their <link>
tags will still download even if the media queries would return false.
We will work over this codepen to learn how to apply media queries to our design:
As you can see, we just have a div
with a class. In the CSS we set the width and height with a fixed value and a background color. We will add different media queries and see how they change the styles by resizing the browser window.
First of all we will add a media query to change the background color when the resolution is bigger than 1000px:
Our <div>
will change the background color. This is because the browser (that in this case is our viewport) size is bigger than 1000px. If we resize the window, we will see how it changes its color when the resolution becomes smaller than 1000px.
We can compose complex media queries using logical operators, including not
, and
and only
. The and
operator is used for combining multiple media features together into a single media query.
Let's create a media query to change the background color when the resolutions is smaller than 1000px and bigger than 650px:
Now, the <div>
color will be:
We can set up a lot of different options. Check out the MDN article "Using media queries" to see them all.
Historiquement, pour des raisons de retro-compatibilité, il fallait zoomer sur mobile.
Introduction de cette balise.
Viewport meta tag allows us to control the viewport's size and scale. We must to add this tag if we want to load correctly the styles in different devices. Just add it in the <head>
section, at the top of the page:
Pas spécialement lié au responsive, mais on en profite pour voir différentes unités qui vont nous servir dans le cas d'intégrations responsives.
Having a fixed font-size accross devices is not always a good idea. You should make your font-size respond to viewport or container to complete your responsive designs. CSS viewport units and media queries provide the means to implement a responsive font size.
de base : 16px
=> le montrer dans devTools
Most websites today use font sizes ranging between 14 and 18 pixels for body text. Usability experts recommend a font-size of at least 16px.
We should also consider how many words per line appear. The recommendation is to have about 18 words. As a general rule, use the largest font-size that doesn't look disproportionate and results in lines with 20 words or less.
Refaire voir ce qu'est le line-height
Another measurement to take in account is line height or line spacing. Line height doesn't involve a compromise because it only affects the page height. Most sources recommend a line-height of 1.2 to 1.45em for good readibility and aesthetics.
There are two main ways to implement a responsive font size: viewport units and media queries along with good old pixels and ems.
vw
, vh
, vmin
, vmax
Le montrer sur codepen
CSS viewport units were specially created to allow measurements in CSS (including font-size
) to be relative to the viewport size. They allow a truly responsive font-size
, by defining the size as a percentage of the viewport width or height. We have four possible values:
The problem with viewport units
When we try to set responsive font size in viewport units, we can find two different problems:
font-size: 1vw
will produce 19px letters in 1920px wide displays, but 6px letters in 640px wide viewports.max-width
and vw
doesn't care. The text will get larger while the rest of the layout stays the same. The same occurs for min-width
.unités absolues VS. unité relatives
=> Sur codepen, montrer un carré de 1em puis augmenter la font-size.
Expliquer l'héritage de certaines propriétés et notamment la font-size.
=> si:
=> cumul !
=> Montrer la computed-value
dans devTools
Expliquer maintenant le rem
(seulement dépendant de la font-size de :root
ou html
)
The other way to implement a responsive font size is to use media queries along with fixed-size text. For fixed-size text we can use:
font-size
is 10px.html
font size. i.e. 1rem = 10px if html element's font-size
is 10px.The best way to use ems and pixels is to specify a pixel font-size
for the html element and use ems and rems for the rest. The reasons to do that are:
font-size
will change all the fonts of the website.em
valuesrem
valuesfont-size
to 10px. It will be much easier to calculate the size of the elements multiplying them by 10.
i.e. if we want to have a 15px font-size, we will set the element's font-size to 1.5rem
CSS Units/ Box Dimensions (Video):
We have seen that resizing our browser we can check out if the responsive design is working or not. We can do the same with Google Chrome Developer Tools:
With the Responsive Inspector extension allows us to:
In the following video you can see how it works.
Responsive Web Design is the practice of building a website suitable to work on every device and every screen size. Mobile first helps you to design the design from bottom to top, avoiding the use of useless information on the mobile versions.
With media queries you can change the design of the website depending on the viewport size, and you can check out the different viewport versions with the Chrome Dev Tools. Finally, you will be able to control the font size and the line height in a Responsive website.