Responsive design breaks down on smaller devices

How can I ensure that the following CSS only applies to small devices (mobile) and not on tablets as well?

@media (max-width: 980px) {
  h2 {
    font-size: 3em;
  }

  p {
    font-size: 2.3em;
    line-height: 1.7em;
  }

  button {
    width: 400px;
    height: 80px;
    font-size: 30px;
  }
}

I've tried using postcss with @media (max-width: 30em), but it still doesn't work as intended for mobile devices. Any suggestions on how to fix this issue would be greatly appreciated.

Thank you in advance.

Answer №1

When working with CSS units, ems are based on the inherited font-size, making them a relative size. As a beginner, it might be best to stick with px values for now. Following @MichaelSorensen's advice, consider setting a lower value initially. Your media query specifies that "all windows with a max-width of 980px should apply these values." If you want this to only apply to smaller devices, adjust the value accordingly.

Check out standard device widths [CSS Tricks].

It's important to understand the concept of "mobile first," which involves starting with media queries that apply to all devices and then adding breakpoints for specific sizes going upwards. Here's a simplified example.

/* Applies to .my-class regardless of size until overridden */
.my-class {
    background-color: #f00; /* red */
}

/* Applies from 768px onwards, targeting many tablets */
@media screen and (min-width: 768px) {
    .my-class {
        background-color: #0f0; /* green */
    }
}

/* Applies from 1200px onwards, targeting larger devices */
@media screen and (min-width: 1200px) {
    .my-class {
        background-color: #00f; /* blue */
    }
}

This is a basic explanation, but if you're struggling with understanding media queries from blog posts, I hope this clears things up a bit.

Answer №2

Even though @mccambridge's answer is accurate, I believe @Liz Parody may be facing a different challenge. When teaching students, I noticed that many of them struggled with Media Queries due to some simple things that are easy to overlook.

  1. It is important to remember that Media Queries work similarly to if statements, where multiple queries can evaluate as true. Refer to the image below for an example.

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

  1. CSS renders from top to bottom, so in the example below, if you maintain the same 200px screen size, a red box with blue text will be displayed because the last media query will overwrite the previous ones. You can observe this behavior using your browser's developer tools:

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

However, by reordering the queries, the outcome will change. This situation can become particularly complex when dealing with multiple style sheets. There are various effective strategies to handle this, but I advise selecting one and ensuring consistency within your team.

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

Ways to soften the edges of a picture by utilizing CSS

I am trying to achieve a feathered effect on the edges of an image within an unordered list that contains 3 list items. Each list item has an image and 3 text-containing divs. I am looking for a CSS-only solution to achieve this effect. Currently, my webs ...

What is the best way to ensure a table is responsive while maintaining a fixed header? This would involve the table scrolling when it reaches the maximum viewpoint, while also keeping

Can anyone help me create a responsive table with a fixed header that scrolls when it reaches the maximum viewpoint without scrolling the entire page? I've tried using box-sizing: border-box; and overflow-x:scroll; but it didn't work. Any suggest ...

Effortlessly switch between CSS animation styles while adjusting animation settings

My HTML element with animation is defined as follows: <div id="box"></div> The box starts by moving 200 pixels to the right, taking 4 seconds to complete. .anim { animation-name: anim; animation-duration: 4s; animation-t ...

Guide on achieving mirror effect with cursor on a webpage

Is it feasible to achieve a cursor mirroring effect on a website using Javascript, or similar technology? Imagine a scenario where the line dividing the black and white sections of the page serves as the "reflection line." In this setup, moving the cursor ...

Front-end webpage presenting coding malfunction - Written in HTML/CSS

After completing the development of a website at , I tested it on my PC and everything looked good. However, when I asked a friend to review the site, she pointed out that on mobile devices, the section with 4 boxes was appearing squeezed together despite ...

Experiencing problems with website loading due to jquery?

Recently, I've been experiencing slow loading times on my website . It's taking about a minute for the site to load properly, but strangely, if I click on the stop loading button, the site loads instantly. Does anyone have any insight into what ...

Changing the image's flex-grow property will take precedence over the flex settings of other child

This is the error code I am encountering, where "text1" seems to be overridden <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <!--mobile friendly--> <meta name="view ...

Issue encountering with flex-wrap to shift to the next row once reaching the 5th element

Recently, I've been experimenting with creating an image gallery using flexbox and incorporating a flex-basis transition to control the growth and shrinkage of elements upon hover. However, I'm encountering difficulties in adjusting the gallery t ...

Center Button Horizontally and Vertically with CSS Code

I really need some assistance with getting this button I made to be perfectly centered on both the vertical and horizontal axes of the page. So far, I've managed to center it horizontally, but not vertically. Any advice or guidance would be greatly ap ...

Troubleshooting CSS Code Issues on Myspace

Greetings! I am currently working on enhancing a friend's profile on myspace.com. I have two divs on each side of the screen that I want to align next to each other. I have tried using float: left; and float: right;, as well as experimenting with marg ...

Just started using Bootstrap and in need of help with a layout problem

Bootstrap is known for its grid-based system. My goal is to create a category page for products that can adapt to the screen size, allowing for as many DIVs as the screen will accommodate. Essentially, I want the number of DIVs to increase as the screen si ...

Toggling the visibility of a div using JavaScript

When I click the button, I want to show and then hide a div. However, it doesn't work on the first click - only on the second click. How can I make it work on the first click? HTML <p>Click the button</p> <button onclick="myFu ...

What could be causing the .remove() method to malfunction in my current environment?

I'm new to jQuery and struggling with the .remove() method. Despite finding similar questions, I'm still unable to solve my issue. Within an HTML file, there's a form alongside a div acting as an alert box, indicating whether the form was v ...

HTML generates areas of blank space

Currently, I am in the process of developing a web page using Angular 2 and PrimeNG Design Framework. The layout is simple, with a menu bar at the top and content below it to fill the remaining space. However, there seems to be an unexplained gap at the bo ...

CSS mismatch detected between production and development modes

Currently, I am utilizing the Vuetify framework coupled with custom CSS for a project developed using a webpack template. During development mode, my custom CSS modifications are successfully applied to HTML elements; however, in Production mode, these cha ...

Letters are frolicking in varying sizes and designs

Currently, I am encountering a strange issue with the text on my webpage. Some fonts are displaying with completely different shapes and sizes despite using <meta charset="UTF-8"> to ensure font consistency. Unfortunately, this did not resolve the pr ...

The jQuery slideToggle() function causes motion

One of my web elements utilizes slideToggle to display/hide a table. This functionality is achieved using jQuery $("#featMoreInfo").click(function() { $("#featben2").slideToggle('slow'); var txt = $(this).text() == '+ More Info ...

Mixin for conditional input styles in Sass

I am interested in developing a conditional sass class that can be customized based on an argument provided. I am unsure about the terminology for this feature. For instance: <img class="width-200" .../> I would like to create a sass mixin that tak ...

Explaining the precise measurements of font size in CSS

I recently started diving into the world of CSS and picked up a copy of Eric Meyer's 3rd edition of CSS: The Definitive Guide. While reading through his section on font sizes (page 107), I came across an interesting concept - that the font size deter ...

Adjusting the color of a div based on the selection of a radio button contained within it

Looking for a way to change the color of a div when a radio button inside it is selected. I have been searching for a solution and haven't found anything that works yet. I want to display two divs side by side, each saying "choose this plan", with a c ...