Unable to eliminate the right margin, which continues to expand as the width of the div decreases

I'm facing a peculiar issue with my HTML/CSS while working on a profile page for a game with a focus on mobile responsiveness. The right-margin seems to persist even though I've tried various solutions. In portrait mode, everything appears fine but in landscape mode, the excessive width causes a scrollbar to appear.

HTML:

<div class="userProfile" style="display:none">
    <div class="profileTop">
        <div class="profilePicture">
            <img src="somepicture.png"></img>
        </div>
        <div class="profileName"></div>
    </div>
</div>

CSS:

.profileTop {
    position: relative;
    top: 10%;
    left: 15%;
    width: 70%;
    height: 12%;
    margin: 0;
}

.profilePicture {
    display: inline-block;
    width: 12vw;
}

.profilePicture img {
    width: 100%;
    height: auto;
    vertical-align: top;
}

.profileName {
    display: inline-block;
    position: relative;
    font-family:Stencil;
    font-size: 1.3em    ;
    font-weight: lighter;
    color: white;
    left: 20%;
    top: 35%;
}

It's strange that adjusting the width of the "profileTop" class only seems to increase the right margin, maintaining the same overall width. Any suggestions on how to solve this?

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

UPDATE: I managed to find a temporary fix by decreasing the width of "userProfile", but the original issue persists.

UPDATE 2: The margin issue extends to the "userProfile" div as well. It appears that the "profileTop" div is inheriting properties from its parent, even though I've explicitly set the margin. The parent of "userProfile" is the body element.

Answer №1

There seems to be an error in the HTML code - you do not need a closing tag for an image.

Additionally, you can utilize media queries to accomplish this task. Media queries offer an option for landscape orientation which is quite straightforward to implement. Best of luck!

@media (min-width: 700px) and (orientation: landscape) { ... }

By the way, consider using Codepen or jsFiddle for the second attempt, as it will make it much simpler for us to assist you.

UPDATE: Included an example of media queries for your reference.

Answer №2

In order to change the CSS attribute that was initially assigned, you must implement a more precise selector.

@media (max-width: 768px) {
  div.userProfile div.profileTop {
    margin-right:0;
  }
}

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

Tips on handling a JSON string alert

Can someone guide me on how to extract a specific value from a JSON string I received in an alert message? The JSON string looks like this: {"Error":true, "data":["not available","somethinghere"]} The JSON string is obtained from an alert using the follow ...

What is the best way to ensure a grid remains at 100% width when resizing a browser window?

Within the div element, I have two child divs. One has the class col-md-2 and the other has the class col-md-10.Check out a sample view here In the image provided, the div containing hyperlinks (Database edit, invoice, preview) is not taking up 100% width ...

User agreement required for HTML5 geolocation custom notifications

Currently implementing HTML5 geolocation on my mobile website. I am exploring the possibility of displaying a custom notification instead of the default web browser notification to request user consent for sharing their location. This custom notification ...

Is it possible to integrate a standard drop-down menu/style into a MUI Select component?

I am currently working on implementing the default drop-down menu style for my MUI Select Component. Here is the desired menu appearance: https://i.stack.imgur.com/GqfSM.png However, this is what my current Select Component looks like: https://i.stack. ...

The mechanics behind CSS3 sliding gradient animation

Behold the ingenious code that effortlessly creates a sliding gradient animation without the need for even a single line of JavaScript: html { height: 100% } body { height: 100%; margin: 0 } @keyframes loading { from { background-position: ...

Upon including the enctype attribute as "multipart/form-data" in my form and submitting it, the form redirects to a different page

My website has two main pages: The first page is admin.php <?php session_start(); if (!isset($_GET['page'])){ $_GET['page'] = 'home'; }//end if include_once(dirname(__FILE__)."/function.php"); ?> <?ph ...

Achieve the positioning of a Bootstrap navbar within a div without it getting wrapped by following

I have been attempting to overlay my navbar on top of a background image by nesting it within a div and using absolute positioning. However, I've run into an issue where the navbar-header/navbar-brand section causes the rest of the navbar to wrap onto ...

ng-click event not firing after $compile in AngularJS

I'm struggling to get my dynamic HTML elements generated from an AngularJS controller to trigger a function using ng-click. Even after using $compile, the function is not being called. Check out my JS code: var accountApp = angular.module('acco ...

integrating live html content into dompdf with php

Hey there, I'm currently working on a script that dynamically loads HTML using the file_get_contents function and then generates and saves the PDF on the server. However, I'm running into a problem where my PHP file is throwing a 500 internal ser ...

Entire body encompassing table

I am trying to create an HTML page with a table that spans the entire body and displays scrollbars when needed for both horizontal and vertical scrolling. Here is my current styling for the table: table { box-shadow: inset 0 0 10px #000; position: ...

How can I add styling to materializecss chip elements?

When working with tags on a web page, Materializecss makes it easy. However, I have a requirement where the tags need to be of different colors based on their types. Although this is feasible, another complication arises when supporting Materializecss keyb ...

How can I simulate pressing the ENTER key in Selenium without selecting any element?

Having trouble using the firefox selenium plugin and sending the enter key after pasting text? The ENTER key press should not interact with any other element on the page, just a simple 'dumb' ENTER key press. error [error] Element name=code not ...

Limit the display of an element to a single occurrence by utilizing jQuery cookies

I have a unique piece of HTML and JS code that showcases an eye-catching modal box. This remarkable feature is present on every page of my website. However, to enhance the user experience, I would like to ensure that this div element is displayed only once ...

Trouble with triggering HTML5 FileReader() functions

I'm having trouble determining why neither readSuccess() nor readFailure() are being executed in the code snippet below: function readMyFile(){ var reader = new FileReader(); reader.onload = readSuccess; reader.onerror = readFailure; ...

The pair of divs with distinct backgrounds

Looking to customize the navigation on my website by creating a separate background for the list links menu and making sure the left part of the navigation with the logo has its own distinct background that extends until the next div. I've experimente ...

Sending information from ngRoute resolve to the controller

I am currently working on an application that utilizes ngRoute to create a Single Page Application. One of the requirements is to ensure that the view is not loaded until the data has been successfully fetched from the database. While I have managed to ach ...

Utilizing the Invision prototype for embedding

They provided me with this code snippet <iframe width="424" height="916" src="//invis.io/U5574OIA3" frameborder="0" allowfullscreen></iframe> However, I am unsure of how to properly embed it. I attempted to insert the line into my website but ...

What is the best way to align these two divs centrally within a container set to display as inline-block?

I can't seem to get these two floated left divs centered within a container that has a height set to auto and a display of inline-block. The container has a max-width of 1600px, but when the window is stretched wider than that, it stays aligned to the ...

Can Chrome support color management for css colors?

In my current project, we are dealing with designers who work in Adobe RGB and are accustomed to viewing the vibrant colors from that spectrum in Illustrator. However, we are developing an application that will enable them to work in a web browser using a ...

Closing an Electron Dialog triggers a refresh on my current page

I currently have an Electron application in place. It utilizes Bootstrap v4, Electron v2, as well as other node modules. One concern I am facing is related to a button that triggers an Electron dialog box where users can select a folder. Upon selecting th ...