Modifying body width for optimal display on both desktop and mobile devices

I've been experiencing difficulties in getting my pages to utilize 100% width. While it displays properly on desktop, occupying the entire page width, a strange issue arises on mobile devices. I am utilizing NextJS and Material-UI, in case that information is relevant. Despite attempting to set the width to 100%, there has been no change.

Here is my CSS:

global.css:

    html,
body {
    padding: 0;
    margin: 0;
    font-family: 'Roboto', sans-serif;
}

a {
    color: inherit;
    text-decoration: none;
}

* {
    box-sizing: border-box;
}

_app.js

<Head>
        <title>E-commerce</title>
        <meta
            name="viewport"
            content="initial-scale=1.0, width=device-width"
                                />
        </Head>

This screenshot shows the body width on mobile. The gray area represents the HTML and body width.

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

Answer №1

If you're unsure about what you need, consider utilizing width: 100vw. This approach may work for your situation. It's advisable to refrain from specifying width on the <body> element and opt for a container <div> instead.
Based on the image provided, it appears that your body content is spilling out of the body tag. Addressing this issue early on can prevent potential complications as your project grows and includes more elements.

Answer №2

If the CSS code you provided is the only styling present, then the preview will appear as expected. However, it is recommended to include @media queries in your CSS for responsive design. Additionally, make sure to add "box-sizing" to both the HTML and body tags.

html, body, *{
 box-sizing: border-box;
}

While your CSS functions well for desktop display, there may be elements within the <body> tag that exceed the viewport size. Utilize the browser developer tool to identify these oversized elements.

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

The desktop version features a lighter font compared to the mobile version

One of my texts has the class .description {font-weight: lighter;font-size: 22px; } Strangely, the font-weight attribute seems to work on desktop but not on mobile devices. Why might this be happening? When I scale Chrome on my desktop to mobile size, ev ...

Tips on deactivating or disregarding a click() event

I have created a card with an anchor tag and added an onclick() event to the card using its id. The click is working fine, but the issue is that even if the checkbox, image, or button inside the card is clicked, the click event of the card is triggered. Ho ...

Tips for utilizing navigator.getDisplayMedia with automatic screen selection:

navigator.mediaDevices.getDisplayMedia({ audio: false, video: true }).then(gotMedia).catch(function(e) { console.log('getDisplayMedia() error: ', e); }); Triggering the above code will result in a popup like this. There is anoth ...

The X-axis remains visible even after being hidden and still functions properly in mobile view

I've been encountering some minor bugs while working on a website recently. One particular issue I'm struggling to fix involves the x-axis not hiding despite attempts in CSS, especially on mobile view. body { overflow-x: hidden; overflo ...

The dialog box fails to respond to the onClick event

Below is the code snippet I am currently working with: CommentSend.js import React, { useState } from "react"; import Dialog from "@material-ui/core/Dialog"; import FormControl from "@material-ui/core/FormControl"; import Button from "@material-ui/core/B ...

Wrapper for box placement

Just starting out in web development and I'm eager to create my first webpage. I'd like to have three boxes neatly placed within my wrapper, something similar to this sketch: https://ibb.co/xGDCv2f The three boxes I want to include are "Registre ...

"Encountering an issue with Next.js due to npm dependencies importing CSS files

I am working on a next.js project and I am trying to incorporate a component from npm that internally imports typeface-montserrat. However, when trying to do so, Next.js is throwing an error: <path>/node_modules/typeface-montserrate/index.css:2 @fo ...

There was a dependency resolution error encountered when resolving the following: [email protected] 12:15:56 AM: npm ERR! Discovered: [email protected] . The Netlify deploy log is provided below for reference

5:27:42 PM: Installing npm packages using npm version 8.19.3 5:27:44 PM: npm ERR! code ERESOLVE 5:27:44 PM: npm ERR! ERESOLVE could not resolve 5:27:44 PM: npm ERR! 5:27:44 PM: npm ERR! While resolving: [email protected] 5:27:44 PM: npm ERR! Foun ...

Tips for linking newly generated elements post-page rendering to events using jQuery?

Utilizing Jquery to insert <select> elements into a <div>. These <select> elements should trigger a change() event with a specific class (e.g. <select class="trigger"> -> $(.trigger).change(...)). The issue arises when adding t ...

Creating an HTML dynamic table with fixed height to prevent expanding when adding text

I have a dynamic table with fixed width and height. By using a random function, I determine the number of cells (ranging from 3x3 to 7x7) in the table. The table renders correctly, but when clicking on a cell to write a letter, the row expands in height. H ...

What is the method for displaying data from a JSON file that is associated with the wallet address of the authenticated user in next.js?

I am currently working on a claim page using next.js, where logged in Metamask addresses can perform the following tasks: Access data from a local .json file to check eligibility for claiming an item and display the claim page. Submitting a form to update ...

What is the best way to position an image at the center in Bootstrap?

I'm attempting to horizontally center an image within the Bootstrap 4 Alpha 6 carousel. Despite my efforts, including utilizing center-block, I have been unable to achieve the desired result. Here's a link to what I've attempted so far: ht ...

Utilizing a media query to execute a <script> exclusively on desktop devices

I am attempting to only run <script type="text/javascript" src="#" data-cfasync="false" async="async"></script> on desktop devices. I experimented with <style> @media screen and (max-width: 600px) { ...

What is the best way to style an icon in CSS?

I am facing an issue with the following code snippet: <span class="stars">★★☆☆☆ </span> My objective is to style the empty stars differently, but I am unsure how to specifically target them. The following CSS rule does not produc ...

My website's performance suffered due to a failure in Core Web Vitals CLS (Cumulative Layout Shift)

Core Web Vitals CLS (Cumulative Layout Shift) on my website failed to meet the standards when analyzed from the Core Web Vitals metrics during the most recent 28-day data collection period in Google Page Speed Insights at . Despite running a performance d ...

What is the best method to encourage users to download the app instead of the mobile web by displaying a banner?

I have built my website using React. I am looking to display a banner when users access the site on an iPhone or Android phone, encouraging them to download and use the dedicated iOS or Android app instead of browsing on their mobile browser. How can I ach ...

Passing props to another component using the <Link> element in React Router

I'm working on a project where I need to display search results using an array. When a user clicks on an item, I want to pass that item as props rather than as parameters. Below is the code snippet: { this.props.results.map((result) => { ...

Manage a large quantity of inputs effortlessly within an accordion using React-Native with just one click from the parent component

I recently started exploring the world of React. In my current project, I have multiple custom accordion components with lists of text inputs. I'm looking for a way to handle these input systems with just one button click from the parent component of ...

Balancing scrolling status in sibling components within React/Redux

Is it possible for a React component to control the scroll position of another sibling component? The main component, known as Parent, includes two child components: List (which contains a scrollable div) and Actions with a button that is meant to control ...

It appears that the Next.js environment variables are not defined

Upon setting up a fresh next.js project using npx create-next-app@latest and configuring some environment variables in the .env.local file, I encountered an error when attempting to run the server. "Failed to load env from .env.local TypeError: Cannot ...