30 Days of JavaScript: Responsive Web Design with JavaScript — Day 7

30 Days of Javascript Series by DopeThemes

Welcome back, passionate web developers! As we continue our journey through the 30 days of JavaScript, we now arrive at Day 7, where we’ll explore Responsive Web Design with JavaScript. Get ready to dive into the mobile-first design approach, CSS media queries, JavaScript techniques, and touch events. Mastering these skills will enable you to create engaging, responsive, and device-friendly websites that delight your users.

Mobile-First Design Approach

The mobile-first design approach emphasizes designing and developing websites for mobile devices before scaling them up to larger screens. This strategy ensures optimal performance and usability on smaller screens, making it easier to adapt your design to desktop and tablet devices.

  1. Start with a Fluid Grid: Use relative units like percentages and viewport units (vw, vh) to create a flexible grid that adapts to different screen sizes. This approach ensures your layout remains consistent across devices.
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;

.column {
  width: 100%;
  padding: 0 15px;

@media (min-width: 768px) {
  .column {
    width: 50%;
  1. Prioritize Mobile Navigation: Create a mobile-friendly navigation menu that can be easily expanded and collapsed on smaller screens. This menu should remain accessible and intuitive as screen size increases.
  2. Optimize Images and Media: Ensure that images and other media elements scale appropriately for different screen sizes. Use the srcset attribute for responsive images and employ CSS techniques like object-fit to maintain aspect ratios.

  3. Utilize Mobile-Friendly Typography: Choose readable and legible fonts for mobile devices, and use relative units like rem or em for font sizes. This approach allows for better scalability and adaptability across different screen sizes.

CSS Media Queries and JavaScript

CSS media queries enable you to apply specific styles based on device characteristics like screen width, height, and resolution. Combined with JavaScript, you can create powerful, dynamic, and responsive websites that cater to various devices.

Here’s an example of how to use media queries in your CSS:

/<em> Mobile styles (default) </em>/
body {
  font-size: 14px;

/<em> Tablet styles </em>/
@media (min-width: 768px) {
  body {
    font-size: 16px;

/<em> Desktop styles </em>/
@media (min-width: 1024px) {
  body {
    font-size: 18px;

In addition to CSS media queries, JavaScript allows you to detect screen size changes and execute specific code based on these changes. The window.matchMedia() method returns a MediaQueryList object, which you can use to determine whether a particular media query matches the current state of the document.

Here’s an example of how to use window.matchMedia() with JavaScript:

const mediaQuery = window.matchMedia("(min-width: 768px)");

function handleScreenChange(e) {
  if (e.matches) {
    console.log("Screen width is at least 768px");
  } else {
    console.log("Screen width is less than 768px");

mediaQuery.addEventListener("change", handleScreenChange);

// Call the function initially to log the current screen state

Working with Touch Events

To create engaging and interactive experiences on touch-enabled devices, it’s crucial to understand and work with touch events. These events provide information about user interactions, such as touch coordinates, the number of touch points, and the duration of a touch.

There are several touch events available in JavaScript:

  • touchstart: Triggered when a user first touches the screen.
  • touchmove: Triggered when a user moves their finger across the screen.
  • touchend: Triggered when a user lifts their finger from the screen.
  • touchcancel: Triggered when a touch event is interrupted, for example, by an incoming phone call or an alert.

Here’s an example of how to work with touch events using JavaScript:

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Touch Events Example</title>
  <div id="touchArea" style="width: 100%; height: 300px; background-color: #f0f0f0;"></div>
    const touchArea = document.getElementById("touchArea");

    touchArea.addEventListener("touchstart", (e) => {
      console.log("Touch started");

    touchArea.addEventListener("touchmove", (e) => {
      console.log("Touch moved");

    touchArea.addEventListener("touchend", (e) => {
      console.log("Touch ended");

    touchArea.addEventListener("touchcancel", (e) => {
      console.log("Touch cancelled");

This example demonstrates how to listen for touch events on a designated area and execute specific code based on the type of event. By understanding and working with touch events, you can create more interactive and engaging experiences for users on touch-enabled devices.


Today’s tutorial covered the essentials of Responsive Web Design with JavaScript. By understanding the mobile-first design approach, utilizing CSS media queries and JavaScript, and working with touch events, you’ll be well-equipped to create responsive, device-friendly websites. As you continue to sharpen your JavaScript skills, remember to stay adaptable and keep up with the ever-evolving landscape of web development.

In the coming days, we’ll explore even more advanced topics that will deepen your knowledge of JavaScript and its applications. Stay tuned, and keep that coding enthusiasm going strong!

Next: 30 Days of JavaScript: Building a Simple ToDo App with JavaScript — Day 8

Stay in the loop with our web development newsletter - no spam, just juicy updates! Join us now. Join our web development newsletter to stay updated on the latest industry news, trends, and tips. No spam, just juicy updates delivered straight to your inbox. Don't miss out - sign up now!

We’ve tried our best to explain everything thoroughly, even though there’s so much information out there. If you found our writing helpful, we’d really appreciate it if you could buy us a coffee as a token of support.

Also, if you’re interested in learning more about WordPress, Javascript, HTML, CSS, and programming in general, you can subscribe to our MailChimp for some extra insights.


This site uses Akismet to reduce spam. Learn how your comment data is processed.