Responsive Design Flashcards
What are the 3 key priciples of Responsive Design?
- A mobile first approach to design. This means you build the mobile version before you construct the desktop layout.
- The
@media
at-rule syntax (also called media queries) lets you write styles that only apply under certain conditions.
Note: You should only apply media queries when the design breaks - The use of fluid layouts. This approach allows containers to scale to different sizes based on the width of the viewport.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is progressive enhancement?
Progressive enhancement is a design philosophy that provides a baseline of essential content and functionality to as many users as possible, while delivering the best possible experience only to users of the most modern browsers that can run all the required code.
Source MDN
What is graceful degradation?
Graceful degradation is a design philosophy that centers around trying to build a modern web site/application that will work in the newest browsers, but falls back to an experience that while not as good still delivers essential content and functionality in older browsers.
Source MDN
When should you use progressive enhancement and when graceful degradatation?
Both progressive enhancement and graceful degradation try to do the same thing: keep our products useful to every user.
Progressive enhancement is a more stable way of assuring that but it takes more time and effort. Graceful degradation can be used more easily as a patch for an already existing product; it means harder maintenance later on, but requires less initial work.
Source w3.org
What does the viewport
HTML metatag do?
It tells mobile devices you’ve intentionally designed for small screens. Without it, a mobile browser assumes your page is not responsive, and it will attempt to emulate a desktop browser.
Example:
<meta name="viewport" content="width=device-width, initial-scale=1">
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
List the most common viewport
HTML metatag settings
<meta name="viewport" content="width=device-width, initial-scale=1">
-
width=device-width
tells the browser to use the device width as the assumed width when interpreting the CSS -
initial-scale=1
set the zoom level at 100% when the page loads.
There is another common setting user-scalable=no
, which prohibits the user from using two fingers to zoom in and out on their mobile device. While it is used. Be aware that is very annoying for the users to not be able to zoom images and the like.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What considerations should you take for print styles?
To help with printing, there are some common steps you can take. It’ll be helpful to apply them inside of a @media print {…} media query
.
- Use
display: none
to hide non-essential parts of the page, such as navigational menus and footers. - Globally change font colors to black and remove all background images and colors behind blocks of text. In many cases, a universal selector does the job for this. I use
!important
here so I don’t need to worry about the cascade overriding it:
@media print { * { color: black !important; background: none !important; } }
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What does CSS property aspect-ratio
do?
The CSS property aspect-ratio
lets you create boxes that maintain proportional dimensions where the height
and width
of a box are calculated automatically as a ratio.
In other words, this property helps us to size elements consistently, so the ratio of an element stays the same as it grows or shrinks.
Example:
.element { aspect-ratio: 2 / 1; /* ↔️ is double the ↕️ */ } .element { aspect-ratio: 1 / 1; /* ⏹ a perfect square */ }
Source css-tricks
CSS property aspect-ratio
syntax and values
aspect-ratio: auto || <ratio>;
Values:
-
auto
: The default value, Specifies that the element has no preferred aspect ratio and should size itself as it normally would. -
<ratio>
: Two positive numeric values separated by a forward slash (/
) , targeting the width and height of the element. In the case of a single value, the second value is considered to be1
. Size calculations involving preferred aspect ratio work with the dimensions of the box specified bybox-sizing
. -
initial
: Applies the property’s default setting, which is auto. -
inherit
: Adopts the aspect-ratio value of the parent. -
unset
: Removes the current aspect ratio from the element.
Note: it is not inherited
Source css-tricks
What does the following CSS ruleset
.element { aspect-ratio: auto 1 / 1; }
do?
If both auto
and a <ratio>
are specified together, the preferred aspect ratio is the specified ratio of width
divided by height
, unless it is a replaced element with an intrinsic aspect ratio, in which case that aspect ratio is used instead.
Source css-tricks
When does aspect-ratio
property gets ignored?
- When both
width
andheight
are declared on the element - When content breaks out of the ratio
- When it “loses” to
min-*
andmax-*
properties
Source css-tricks
Common use cases of aspect-ratio
property?
- Responsive iFrames that show youtube videos
iframe { aspect-ratio: 16 / 9; width: 100%; height: auto; }
- Hero images
.hero { aspect-ratio: 4 / 3; background: url(background.png); }
- Layout consistency: In a flexbox or a grid layout with
auto-fill
mechanism, you may want items to stay square, but items width and height can shrink or grow based on their content or their parent’s size and as a result it’s most likely that items don’t stay square.
Setting aspect-ratio
to 1/1
changes the height dynamically while your item’s width scales:
grid-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); } .grid-item { aspect-ratio: 1 / 1; }
Source css-tricks
What is a fluid layout?
a fluid layout (sometimes called liquid layout) refers to the use of containers that grow and shrink according to the width of the viewport.
A fluid layout can be slightly narrower than the viewport, but never wider.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What’s the problem with table
s and fluid layouts?
Tables are particularly problematic for fluid layout on mobile devices. If a table has more than a handful of columns, it can easily overflow the screen width.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What can you do when a table
’s width
breaks a fluid layout design?
One approach could be to force the table to display as normal block elements.
table { width: 100%; } @media (max-width: 30em) { table, thead, tbody, tr, th, td { /* Makes all table elements block display */ display: block; } thead tr { /* Hides the headings row by moving it off the screen */ position: absolute; top: -9999px; left: -9999px; } tr { /* Adds a little space between each set of table data */ margin-bottom: 1em; } }
The above styles causes each cell to stack atop one another, then adds a margin between each <tr>
. This approach makes the <thead>
row no longer lineup with columns beneath it, so we use some absolute positioning to remove the header row from view. We avoid display: none
for accessibility
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.