tips for creating responsive css media queries for mobile devices

In my project, I'm attempting to create a container called .data that will display flex on screens with a width of less than 768px. If the screen width is greater than that, it should be displayed as a grid.

Upon testing in Chrome developer settings without using the "Toggle device toolbar" option, the layout works as expected. However, when I utilize the "Toggle device toolbar" option, the grid layout is displayed even if the screen width is less than 768px. Testing on a mobile phone also reveals that it's not functioning correctly.

If you'd like to see the app, it's deployed at this link.

If you want to view the entire code base, it can be found in the repository located here.

https://i.sstatic.net/QIiHu.png

https://i.sstatic.net/d6yrr.png

Below is the CSS and Pug markup:

CSS:

.data{
    display: flex;
    flex-direction: column;
    padding: 10px;  
}

@media only screen and (min-width: 768px ) {
    .data{
        text-align: justify;
        display: grid;
        gap: 20px;
        padding: 10px;
            
        grid-template-columns: repeat(3,1fr);
        grid-template-areas: "heading heading notification";
    }
    .heading{
        grid-area: heading;
        text-align: justify;
    }
    .notification{grid-area: notification;}
}

Pug file:

extends base

block content
    .main
        .home
            .content
                h1.content WELCOME TO WINROBOT
    
    .data
        .heading
            h2 Heading
            p Lorem, ipsum dolor sit amet consectetur adipisicing elit...
        .notifications
            h2 Notifications
            if (user && user.role === 'admin')
                button.btn.btnAddNotification new notification
            .note
                h3.ntitle title 
                p.ndate Date
                p.ncontent notification content
            if (user && user.role === 'admin')
                button.btn.btnDeleteNotification Remove 

Resulting HTML from the browser:

<!DOCTYPE html>
<html>
    <head>
          <title>Winrobot | Home</title>
          <link rel="shortcut icon" type="image/png" href="/img/favicon.png"/>
          <link rel="stylesheet" href="/css/styles.css"/>
    </head>
    <body>
        <nav class="navbar">
            ...
        </nav>
        <div class="main">
           <div class="home">
                ...
            </div>
            <div class="data">
                ...
            </div>
        </div>
            <footer class="footer">
                ...
            </footer>
            ...
        </body>
</html>

Answer №1

The reason for the issue was because I forgot to include

meta(name="viewport", content="width=device-width, initial-scale=1.0")
in my base template.

Answer №2

When it comes to Responsive web design, the best approach is Mobile first for reliability. It's common practice for developers to use the min-width media query, as even the widely-used Responsive CSS Framework "Bootstrap" recommends using this method...

// Extra small devices (portrait phones, less than 576px)
// No media query since this is the default in Bootstrap

// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) { ... }

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) { ... }

// Large devices (desktops, 992px and up)
@media (min-width: 992px) { ... }

// Extra large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }

For further information, you can Visit the Bootstrap Site. I hope this explanation proves helpful to you.

Answer №3

Your approach to CSS appears to align with the mobile-first strategy, where the styling written outside of media queries is what will be displayed on smaller screens like phones.

It's important to prioritize styling for screens ranging from 0 to 768px width before diving into your first media query. This ensures a seamless user experience across different devices.

To fully embrace the mobile-first concept, consider moving CSS rules for elements like .data out of media queries and into the main stylesheet.

Media queries can then be used to cater to larger viewports starting from 768px, gradually adding styles for wider screen sizes such as 960px or 1200px if needed.

If you choose to take a desktop-first approach, begin by targeting larger screens with media queries using @media (max-width: 768px), making adjustments to accommodate smaller screens as necessary.

Regardless of your methodology, make sure to tweak or override initial CSS properties within media queries to ensure responsiveness across various screen sizes. You may even need to utilize multiple media queries to fine-tune styles for specific viewport ranges, such as screens narrower than 380px.

Answer №4

Can you tell me the location for defining mobile phone styles?

Suggest using max-width rather than min-width in your media query.

Answer №5

Consider incorporating a responsive design that utilizes breakpoints. For more information on Breakpoints for creating a responsive web page, you can refer to this resource.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Integrate blogger into my website with automatic height adjustment and scrolling functionality

Check out my website here. I've integrated a blogger component into it. Can anyone provide guidance on creating parallel scroll and automatically resizing sections based on the blog's height? Thanks, M ...

Enhancing React components with ellipsis and tooltips for text longer than two lines

Can a React component be developed to automatically add an ellipsis after two lines and display a tooltip only when the text is wrapped? I attempted to customize the Material UI's Typography component with the "noWrap" property and additional CSS, bu ...

Issue with ISO-8859-1 encoding in Internet Explorer

I have been working on programming in Spanish, and in order to use accent marks, I rely on the # -*- coding: iso-8859-1 -*- and <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> settings in all my programs (Python). I have test ...

The selected image should change its border color, while clicking on another image within the same div should deselect the previous image

https://i.sstatic.net/jp2VF.png I could really use some assistance! I've been working on Angular8 and I came across an image that shows how all the div elements are being selected when clicking on an image. Instead of just removing the border effect f ...

Angular: Display an element above and in relation to a button

Before I dive in, let me just mention that I have searched extensively for a solution to my problem without much luck. The issue is describing exactly what I'm trying to accomplish in a way that yields relevant search results. If you're interest ...

Transferring Information from EventSource Server Sent Event (SSE) to a Different Variable

I have a webpage that updates latitude and longitude values from the database every few seconds and displays them. I am successfully using Server-Sent Events to achieve this functionality. However, I'm facing an issue with passing these latitude and l ...

Check if a specific condition is satisfied in Django models.TextChoices and then extract the relevant data

Current Program Versions: Django 3.1.13 Python 3.8.10 My models.py: # Defining models here. class Odorant(models.Model): Pubchem_ID = models.IntegerField(primary_key = True) Name = models.CharField(max_length =50) Ruletype = models.TextCh ...

Can a client-side React component or application be hosted on a server-side Express route?

Currently, I have a Node/Express backend that utilizes Pug.js to render various server-side routes for a basic, static website. In React.js, I have developed an interactive "builder" component through CRA, enabling visitors to configure products interacti ...

Trouble with CSS Vertical Alignment: Solutions to Fix it

Link to a JSFiddle for reference: http://jsfiddle.net/STmwz/4/ Initially, there is only the top div displayed. Upon clicking the edit button, the JavaScript code replaces the top div with the bottom div. However, during this transition, there is a slight ...

Could someone shed some light on why hjk fails to print whenever I click?

Why is the code not printing 'hgk' every time I click? The debounce function should run and print 'hgk' each time the button is clicked. Can someone please explain why this is not happening? const debounce=(fn,delay)=>{ ...

Unable to access and saving html5 video/audio in IE and FireFox browsers

On my website, I have mp3 and mp4 files embedded using audio/video HTML5 tags. Users can easily download these files in Chrome by clicking on the download button that appears. However, some users who use Internet Explorer and Firefox do not see this downl ...

What is the best way to incorporate progressive JPEG images onto a website?

I am currently working on a website called winni.in that is built using Java, Html, and Javascript. I would like to incorporate progressive image rendering upon page load, but I lack the necessary knowledge. I have come across and explored the following re ...

The search function is currently limited to only searching the first column of the HTML table instead of searching the entire table

Having some issue with the search and filter options on my table. It seems like the search function only works for the first column "ID". I would like it to work for all columns, especially the names and pincode columns. Any help would be appreciated. Than ...

Learn how to retrieve data using the $.ajax() function in jQuery and effectively showcase it on your HTML page

Can someone assist me with extracting data from https://jsonplaceholder.typicode.com/? Below is the AJAX call I'm using: $.ajax({ url: root + '/posts/', data: { userId: 1 }, type: "GET", dataType: "json", success: function(data) { ...

Getting access to the CSS class selector within the HTML document in the application

Upon examining this snippet of HTML: <html> ... <iframe> #document <html> ... <div className='change-me'></div> ... </html> </iframe> </html> This iframe has be ...

Struggling with Implementing a Hollow Button in Bootstrap 3

Could you please review This Demo and provide guidance on how to remove the grey background color when clicking on the button? I attempted the following: .btn-hallow:hover{ background: none; color: aliceblue; } .btn-hallow.active:focus{ backg ...

Unraveling HTML in TypeScript with Angular 2

After receiving encoded HTML from the back-end, I am struggling to decode it. I have attempted to use decodeURIComponent and decodeURI. The server sent me: "<table style="background-color: white;"> <tbody> <tr> ...

Refresh the current page and log out the authenticated user

One issue I am facing is with a button on my website that is supposed to reload the page. However, when this button is pressed, it unexpectedly logs out the user and redirects them to the homepage. I am implementing passport-local for user authentication ...

Python Automation with Selenium Web Scraping

I attempted to scrape the content of Crédit Suisse's page as an exercise. After creating this script, I encountered difficulties in retrieving the data. Initially, I suspected it may be due to an iframe issue or an AngularJS website structure, but u ...

How to style a div for printing using CSS

Currently, I am working on a project that has already been completed but now requires some enhancements. To give you an overview, the project includes a search functionality that displays additional details upon clicking on the displayed name in the result ...