Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active February 13, 2024 23:43
Show Gist options
  • Save ross-u/c5a461b80d8375f22543510f8cc169ac to your computer and use it in GitHub Desktop.
Save ross-u/c5a461b80d8375f22543510f8cc169ac to your computer and use it in GitHub Desktop.
CSS | Responsive Web Design - Summary

CSS | Responsive Web Design - Summary


Responsive website examples:


Guidelines / main ideas

  • We don’t focus on the device. Instead, we have to think of the viewport size.

  • Responsive design focus is on mobile first. This means that when creating our applications and webpages we start by writting css for mobile viewports, and move up to target the bigger screen sizes with media-query rules.

  • Instead of making an app for desktop and then trimming it to fit the mobile, we instead make it for mobile first with most essential features and then work up from there when creating a desktop version.


Viewport

  • The viewport is the user's visible area of a web page.

  • To determine the viewport size we can use the Chrome DevTools - Responsive Device Bar


Setting The Viewport

<meta name="viewport" content="width=device-width, initial-scale=1.0">

The content="width=device-width property instructs the page to match the screen's width.

The initial-scale property controls the zoom level when the page is first loaded.

  • Without a viewport meta tag, mobile devices render pages at typical desktop screen widths, and then zoom out the page in order to fit the mobile screens.

Media Queries

Media query @media rules determine when another CSS style starts applying. We call this a breakpoint.


Responsive Font Size Units

CSS Viewport Units

CSS responsive units are units that are relative to the viewport size.

  • vw. - Viewport width. 1 vw equals 1% of the viewport width.
  • vh - Viewport height. 1 vh equals 1% of the viewport height.

Relative unites - pixels, ems, rems

  • px - specifies the height of the letters in CSS pixels

  • em - Is relative to the element's parent font size.

  • rem - is relative to the root (<html>) element's font size.


Loading custom stylesheets for different viewport sizes

We can load different stylesheets for different viewport sizes, using the <link> tag:

Example - Add to the previous code

<!-- Add to the <head> -->
<link
   rel="stylesheet" 
   href="loaded.css"
   media="screen and (min-width:1050px) and (max-width:1150px)"
>
<!-- Stylesheet `loaded.css` will be loaded by the browser only for the screens
between 1050px (minimum) and 1150px (maximum)-->
/* loaded.css */
body {
  background-color: pink;
}

p {
  font-size: 40px;
  color: red;
}

Responsive images

  <h1>Responsive images</h1>
  <div class="container">
    <img src="./logo-ironhack.png" alt="">
  </div>
/* MAKING THE IMAGE RESPONSIVE */
img {
  width: 100%;
  height: auto;
}

Responsive div background

  <h1>Responsive div background</h1>
  <div class="image-div"></div>
/* SETTING THE DIV BACKGROUND IMAGE AND SIZING */
.image-div {
  margin: 0 auto;
  width: 100%;
  height: 500px;
  border: 2px solid navy;
  background-image: url("./river_canyon.jpg"); /* Link to the background image */
  background-repeat: no-repeat;  /* don't repeat the image if there is remaining space */
  background-size: cover; /* Resize the background image to cover the entire container */
  background-position: center; /* Center the image */
}

Other Resources

@ahtashamilyas
Copy link

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment