After altering the display properties of the divs within a parent div using JavaScript, the background of the parent div no longer fills the entire space as expected

I have successfully developed a unique landing page layout consisting of 14 distinct groups.

Within the third group of this layout, there are three columns labeled as divs. Each column contains an image, heading (h3), and accompanying text. The display of these columns is adjusted based on the screen size - for screens wider than 768px, the columns are arranged horizontally, while for smaller screens they are displayed vertically in descending order.

Currently, everything functions correctly with regard to the columns, especially after some JavaScript modifications. The background color of the third group is set to pink, covering the entire area where the columns are positioned, whether vertically or horizontally. However, an issue arises when the screen width exceeds 768px, causing the bottom part of the necessary div within the pink background not to be filled. Strangely, this problem does not occur on phone devices where the content displays properly.

let width = screen.width;
    var x = document.getElementsByClassName("column-G3");
    var y = document.getElementsByClassName("column-G3-outside");
    if(width<768)
    {
        for(var i = 0; i < x.length; i++)
        {
            y[i].style.width = "100%";
            x[i].style.width = "100%";
        }
    }
    else if(width>=768)
    {
        width*=0.85;
        width_block = Math.floor(width*0.3);
        margin_block = (width - width_block*3);
        for(var i = 0; i < x.length; i++)
        {
            x[i].style.width = width_block + 'px';
            x[i].style.display = "block";
            x[i].style.marginLeft = "auto";
            x[i].style.marginRight = "auto";
            y[i].style.float = "left";
            y[i].style.width = "33%";
        }
    }
.G3
{
background: #a914b5;
background-size: cover;
font-size: 2vh;
padding-bottom: 5vw;
padding-top: 5vw;
}

.G3 p
{
font-family: 'Raleway', sans-serif;
font-weight: 500;
}

.center-in-G3
{
width: 100%;
}

.center-in-G3 img
{
display: block;
margin-left: auto;
margin-right: auto;
}

.center-in-G3 h3
{
text-align: center;
}

.column-G3
{
width: 100%;
}
<div class="G3">
    <div class="align-center">
        <div class="column-G3-outside">
            <div class="column-G3">
                <div class="center-in-G3">
                    <img src="./images/3/Icon.png">
                    <h3>New Music</h3>
                </div>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat </p>
            </div>
        </div>
        <div class="column-G3-outside">
            <div class="column-G3">
                <div class="center-in-G3">
                    <img src="./images/3/Icon.png">
                    <h3>New Group</h3>
                </div>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat </p>
            </div>
        </div>
        <div class="column-G3-outside">
            <div class="column-G3">
                <div class="center-in-G3">
                    <img src="./images/3/Icon.png">
                    <h3>New Themes</h3>
                </div>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat </p>
            </div>
        </div>
    </div>
</div>

Answer №1

Have you considered utilizing display: grid;? It can significantly simplify and enhance your layout. There are various resources available online to help you generate and implement grid layouts effortlessly.

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

Utilizing JavaScript to create multiple HTML tables from JSON data

Is it necessary to create separate tables in HTML for each set of JSON data when all tables have the same number of columns (2 columns)? I am looking to minimize the JavaScript function that displays the table with the current JSON data. Can I use one tabl ...

Aurelia encountering an error when attempting to load Bootstrap v4 during runtime

In my aurelia.json file, I have the following configuration for bundling vendor scripts. It was directly copied from a working reference implementation and has been functioning properly. { 'build': { 'bundles': [ 'name ...

Stopping observers in Observable.merge can be achieved by unsubscribing from the merged

In my code, I have a method that sets up a timer: create(options: any) { return timer(0, 1000).pipe( map(() => { return options; }), takeWhile(() => new Date() < options.finishDate) ); } After creating the ti ...

Trouble with sending semicolons in Node.js MSSQL/MSNodesqlv8 database queries

Trying to create a simple API for interacting with a MSSQL v12 database using Nodejs. Successfully connected to the database with the mssql/msnodesqlv8 package, but encountering issues with parameterized queries. code: 'EREQUEST', number: 102 ...

What steps should I take to ensure that my Material Design Icon Fonts are appearing correctly on the screen?

I can't seem to get my icons to display properly. I have included the path like this: <link href="material/vendors/material-icons/material-design-iconic-font.min.css" rel="stylesheet"> This is what I currently see: But thi ...

Switch Hidden Input Value When UserControl is in Focus

This task seems deceptively simple, but I'm encountering difficulties trying to make it happen. Within ResidentAddress.aspx, there are two user controls (AppName.ascx and NavButtons.ascx). My goal is to update a hidden input field on NavButtons.ascx ...

Ways to determine if a specific text in HTML is already assigned a class name?

How can I modify my function to highlight selected text with the mark tag, and then un-highlight it if clicked again? highLightText() { var selection = window.getSelection(); console.log('this is the selection: ', selection); var ...

How can I perform a MySQL UPDATE using 2 parameters received from an AJAX request ($_POST)?

I am currently working on an AJAX call that updates a row in the database based on the ID (pid=38) and changes the value of a column to NULL depending on the button clicked (taskimg0). I have managed to make it work by hardcoding the value $_POST['dtf ...

What is the best way to extract parameters from an HTTP request while performing unit tests in AngularJS?

I have created a method in my controller that looks like this: $scope.submitForm = ( username, password ) =>{ $http({ method: 'post', url: 'balabala', params: {username, password} }).success( res =&g ...

Achieving Efficiency with Handlebars: Streamlining Remote Template Organization and

I am looking for a way to better organize my HB template by splitting it into different HTML files. In this case, I have created a file called helpers.html. This file contains two script tags: <script id='alert' type='text/template>... ...

Embed your JavaScript code within the header tags of the WooCommerce order confirmation page

Seeking to enhance my thank you page with Google tracking script, I have developed code that successfully injects the tracker within the of the page, incorporating dynamic values. However, my goal is to embed it within the tags instead. function mv_goog ...

Collapsed Bootstrap 5 select drop-down featuring the multiple attribute

I am facing a challenge with using the select element along with the 'multiple' attribute. My aim is to have it collapsed by default and expand only when the user clicks on it. Unfortunately, Bootstrap 5 no longer supports the multiselect feature ...

How to insert an array into another array in JavaScript

Using Node, express, and mongoose to work on a project. I am attempting to include an Array as an element within another Array through a callback function. app.get('/view', function(req, res){ var csvRows = []; Invitation.find({}, func ...

Styling the background of the endAdornment in a material-ui textfield using React

I tried following the instructions from this source: Unfortunately, the example code provided doesn't seem to be functioning properly now. Is there a way to achieve the same result without having that right margin so that it aligns better with the r ...

Is there a similar feature to RxJs version 4's ofArrayChanges in RxJs version 5?

Currently utilizing Angular2 and attempting to monitor changes in an array. The issue lies with only having RxJs5 available, which appears to lack this specific functionality. ...

I attempted to retrieve a PHP file in my HTML document using AJAX, however, the scripts embedded in the file are not functioning as expected

I have successfully integrated a php file into my HTML document using AJAX. However, I am encountering an issue with the script tags within the PHP file. These scripts work perfectly when the PHP file is viewed individually, but when called using AJAX, the ...

When invoking a JavaScript method, the context variable 'this' is lost

I have developed a basic pointer to a method like this: export class SmbwaService { getExistingArsByLab(labId: number): Observable<SmwbaAr[]> { this.otherMethod(); } otherMethod(): void { } } let method: (x: number) => ...

Is it possible to submit a form using both ajax and normal POST simultaneously?

I have a form that submits data to an external URL and at the same time I want to save it in my database. I attempted using Ajax to submit and then try to submit a normal POST request in the Ajax success function. Here is my code: <form action="http:/ ...

Why are these two characters £ and ¬ not being decoded properly?

Exploring an external api, I decided to test its handling of special characters. For the experiment, I sent the following value: !"£$%^&*()_+-=[]{},./;'[]#?:@~{};:\|¬` Upon inspection in the external system, it was saved as: !"%u00a3$%^& ...

The Power of JQuery in Dynamically Adding Script Tags and Executing Code

The objective is to dynamically load script tags via ajax, execute the scripts, and display the content within the script tag (an iframe with a video). Here's the scenario: Imagine a page dedicated to videos. Upon clicking on "video-text," the corres ...