How can you modify the styling of a single child element within a grandparent container and then revert it back to its original state

My goal is for #homeContainer to occupy the entire width of the page, but a higher-level element is restricting its width and margin.

Below you can see the DOM structure. By removing margin: 0 auto, I am able to achieve full width. How can I specifically target the code within .p-body-inner that is associated with #homeContainer?

I attempted to use this line of JavaScript. It does remove the margin for .p-body-inner, but it affects the whole page rather than just the section pertaining to `#homeContainer`. My intention is to modify only that section and then revert back to the original styling.

If you wish to view this issue in action, visit the actual site at:

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

document.querySelector('#homeContainer').closest('.p-body-inner').style.margin = '0';
.p-body
{
    display: flex;
    align-items: stretch;
    flex-grow: 1;
    min-height: 1px;
    background: gray;
}
.p-body-inner
{
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 1200px;
    padding: 0 10px;
    margin: 0 auto;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 10px;
    padding-bottom: 20px;
}
.p-body-main
{
    display: table;
    table-layout: fixed;
    width: 100%;
    margin-bottom: auto;
    min-height: 1px; // IE11 workaround - related to #139187
}
#homeContainer {
    width: 100vw;
    height: 100px;
    background: red;
    border: 5px solid green;
}
#reset {
    width: 300px;
    border: 5px solid blue;
}
<div class="p-body">
    <div class="p-body-inner">
        <div class="p-body-main">
            <section id="homeContainer">
                Need full width of .p-body
            </section>
            <section id="reset">
                This should be centered and inherit the margin: 0 auto again.
            </section>
        </div>
    </div>
</div>

Answer №1

It seems like the margin: 0 auto property is being added to the wrong element...

Additionally, when using display: flex;, the children will automatically fill the width so there is no need to specify width: 100%

.p-body
{
    display: flex;
    align-items: stretch;
    flex-grow: 1;
    min-height: 1px;
  background: gray;
}
.p-body-inner
{
    display: flex;
    flex-direction: column;
    width: 100%;
  max-width: 1200px;
  padding: 0 10px;
  margin: 0 auto;
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 10px;
  padding-bottom: 20px;
}
.p-body-main
{
    display: table;
    table-layout: fixed;
    width: 100%;
    margin-bottom: auto;
    min-height: 1px; // IE11 workaround - related to #139187
}
#homeContainer {
  height: 100px;
  background: red;
  border: 5px solid green;
}
#reset {
  width: 300px;
  border: 5px solid blue;
  margin: 0 auto;
}
<div class="p-body">
    <div class="p-body-inner">
    <div class="p-body-main">
      <section id="homeContainer">
      Need full width of .p-body
      </section>
      <section id="reset">
      This should be centered and inherit the margin: 0 auto again.
      </section>
    </div>
  </div>
</div>

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

Randomly reorganize elements in a JavaScript array

I am facing an unusual issue while shuffling an array in JavaScript and I am unable to identify the root cause. Can someone provide assistance? When attempting to shuffle an array, the output I receive is unexpected: [1,2,3,4,5,6,7,8,9,10] Instead of ...

How can I connect a click event on one div to trigger another using jquery?

Could someone assist me with the proper terminology? I am interested in clicking one div, which would then automatically trigger the click on another div. For example, if I click on div 1, I want div 2 to be clicked by JavaScript without user interaction ...

Encountering an issue with React JS Array Filtering: running into the error message "

I am encountering an error stating that includes is not a function, and the record.id is not being recognized in VS Code. I'm not sure where the problem lies? import React, { Component } from 'react' import axios from "axios" export de ...

The max-width attribute is failing to apply to the form within the Bootstrap framework

login.html file: {% extends "base.html" %} {% block content %} <div class="container login-page-form"> <form id="login-form" action="/auth/login" method="post"> <div class="f ...

What is the best way to send a string value from HTML to a JavaScript function as a parameter?

When embedding HTML codes in Java, I encountered an issue where I needed to pass a string value from HTML to a JavaScript function. Initially, I tried using the following code: out.print("<script>init("+macId+")</script>"); However, this meth ...

Having trouble incorporating autocomplete search results into an HTML table using Jquery, Ajax, and JSP

My current project involves an App that utilizes AJAX with jQuery to communicate with a Spring-boot REST controller. While the app is functional, I am facing difficulty in displaying the search results neatly within HTML tables. https://i.sstatic.net/7sw8 ...

Enhance the elastic layout to ensure full compatibility with Internet Explorer

My elastic layout functions perfectly in Chrome and other major browsers, except for IE which seems to be ignoring the @media query. http://jsfiddle.net/U2W72/17/embedded/result/ * {margin: 0px; padding 0px'} .thumb { float: left; width:16. ...

Exploring the search functionality within Meteor JS

As a newcomer to databases and search functionality, I am exploring how to implement a search feature in my Meteor app. After browsing through atmosphere, I found these 4 options: Mattodem easy search Search Source Elastic search package on Atmosphere (h ...

"Manipulating a drop-down menu in HTML with data from a MySQL database using PHP

Hello there, I could use some assistance. I am working on a straightforward web application and require help with a specific task. Here's the scenario: I have an HTML form with a single combo box. I would like to populate this combo box with data fro ...

observing the value of the parent controller from the UI router state's resolve function

I am facing an issue in my AngularJS application while using ui-router. There are three states set up - the parent state controller resolves a promise upon a successful request, and then executes the necessary code. In the child state portfolio.modal.pate ...

What is the best way to confirm checkbox selection based on MySQL data?

Writing this question feels challenging, but I have a collection of checkboxes with their data stored as JSON in my PHP database. What I'm trying to achieve now is to dynamically load the JSON data on the page without refreshing it, checking specific ...

Is there a way for me to automatically close the navbar by clicking anywhere on the body of

Here is the structure of my navigation bar: <div id="mySidenav" class="sidenav"> <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a> <span ...

"Troubleshooting a Responsive Design Problem: Horizontal Scroll Bar Appears on Low Res

My website is experiencing an odd issue with responsiveness. When viewed on desktop resolution in Chrome, there is no horizontal scroll. However, when I resize the browser to lower resolutions (400px width and less), the horizontal scroll appears. It see ...

jQuery script unable to alter img src in Firefox and IE browsers

I am working with an image and a jQuery script to change the captcha image. <img src="captcha.jpg" id="my-image"/> The jQuery script is as follows: <script> $(document).ready($("#my-image").click(function(){ $('#my-image&ap ...

Monitor modifications to an <input type="text"> element as its value is updated dynamically through JQuery from the server side

I am struggling with detecting when the value of an input field changes after it has been set by clicking a button. I have tried various events but can't seem to find the right one. <!DOCTYPE html> <html> <head> <script src=" ...

using `clear: both` does not stop text from wrapping

Currently, I am facing an issue with positioning 3 divs in my layout. Two of the divs are floating to the left and right respectively, while the third one should be placed under the two others. I tried using clear: both but it doesn't seem to work as ...

A guide to setting up checkbox input in Vue 3 so that it toggles between checked and unchecked states when an

I'm in the process of creating a div container that will contain both an image and a checkbox input element. The checkbox input has a click handler that passes a value to a function, as well as a :value binding which sends the value to an array named ...

Encountered an error with Aurelia webpack 4 when trying to load a necessary CSS file during runtime

I encountered a unique issue with webpack and aurelia that I can't seem to figure out. After creating a new webpack configuration based on online resources and official documentation, the compilation goes smoothly without any errors. However, during r ...

Drag and drop a Jquery image onto the div with its top left corner

Drop targets: <div id="targetContainer" style="width:100px; height:100px;> <div id="tar_0-0" class="target" style="position: relative; float:left; width:50px; height:50px; background-color: #fff;"></div> <div id="tar_1-0" class="t ...

Unable to access filteredItems from custom popup in uib-typeahead retrieval process

Issue with Retrieving Filtered Items from ng-repeat in Controller using $scope Despite trying to fetch filtered items from ng-repeat, the value of $scope.filteredItems appears as undefined when I log it to the console. I attempted to implement the solutio ...