Combine several divs into a block of individual divs

As part of my project, I am working on creating a number board where specific numbers need to merge into a single cell. Please see the picture for reference https://i.sstatic.net/99WJq.png

Below is the HTML code snippet that I have tried. I used margin for non-merge cells and border for merged cells, but unfortunately, the alignment of the cells got distorted. Can anyone suggest a different approach or provide any leads or runnable links that might be helpful?

<!DOCTYPE html>
<html>
<body>
<div class="parent">
    <div class="child">14</div>
    <div class="child">15</div>
    <div class="child">16</div>
    <div class="child">17</div>
    <div class="child merged">18</div>
    <div class="child merged">19</div>
    <div class="child merged">20</div>
    <div class="child">34</div>
    <div class="child">35</div>
    <div class="child">36</div>
    <div class="child">37</div>
    <div class="child">38</div>
    <div class="child">39</div>
    <div class="child">40</div>
</div>
</body>
</html>

Here is what I have attempted so far https://i.sstatic.net/e6OXk.png

.parent {
    display: flex;        
    flex-wrap: wrap;
    align-content: flex-start;
 }
 .child {
    margin: 4px;
    padding: 0;
    height: 35px;
    width: 35px;
    background: #FFFFFF;
    border: 1px solid #C0C0C0;
 }
 .merged{
    margin: 0;
    padding: 4px;
    border-color: #1792E5;
    background-color: #1792E5;
    color: #FFFFFF;
}

Answer №1

My assistance should prove beneficial

var x = document.getElementsByClassName('child');
var y = 'child merged';
var c, d, e;
for (var i = 1; i < x.length - 1; i++) {

    c = x[i - 1].getAttribute('class');
    d = x[i + 1].getAttribute('class');
    e = x[i].getAttribute('class');

    if (c === y && e === y && d === y) {
        x[i].setAttribute('style', 'margin: 5px 0px 5px 0px;');
    }
    else if (c !== y && e === y && d !== y) {
        x[i].setAttribute('style', 'margin: 5px 5px 5px 5px;');
    }
    else if (c !== y && e === y && d === y) {
        x[i].setAttribute('style', 'margin: 5px 0px 5px 5px;');
    }
    else if (c === y && e === y && d !== y) {
        x[i].setAttribute('style', 'margin: 5px 5px 5px 0px;');
    }
    else if (c === y && x[i - 2] === undefined && e !== y) {
        x[i - 1].setAttribute('style', 'margin: 5px 5px 5px 5px;');
    }
    else if (i === x.length - 2 && d === y) {
        x[i + 1].setAttribute('style', 'margin: 5px 5px 5px 5px;');
    } else { }

    if (x[i + 2] === undefined && e === y && d === y) {
        x[i + 1].setAttribute('style', 'margin: 5px 5px 5px 0px;');
    }

    if (c === y && x[i - 2] === undefined && e === y) {
        x[i - 1].setAttribute('style', 'margin: 5px 0px 5px 5px;');
    }

    if (i === 7 || i === 14 || i === 21) {
        x[i - 1].style.marginRight = '5px';
        x[i].style.marginLeft = '5px';
    }

}
.parent {
    display: grid;
    grid-template-columns: 50px 50px 50px 50px 50px 50px 50px;
    grid-template-rows: 50px 50px 50px 50px 50px 50px 50px;
    text-align: center;
}

.child {
    border: 1px solid #ccc;
    margin: 5px 5px 5px 5px;
}

.merged {
    margin: 5px 0px 5px 0px;
    padding: 0px;
    border: 0px;
    background: #1792E5;
}
<div class="parent">
    <div class="child merged">14</div>
    <div class="child merged">15</div>
    <div class="child">16</div>
    <div class="child">17</div>
    <div class="child merged">18</div>
    <div class="child merged">19</div>
    <div class="child merged">20</div>
    <div class="child merged">34</div>
    <div class="child">35</div>
    <div class="child">36</div>
    <div class="child merged">37</div>
    <div class="child merged">38</div>
    <div class="child">39</div>
    <div class="child merged">40</div>
    <div class="child merged">41</div>
    <div class="child merged">42</div>
    <div class="child">43</div>
    <div class="child merged">44</div>
    <div class="child">45</div>
    <div class="child ">46</div>
    <div class="child merged">47</div>
</div>

Answer №2

The problem was resolved by making adjustments to the CSS file. Feel free to reach out if you need further clarification.

.parent {
    display: grid;
    grid-template-columns: 100px 100px 100px 100px 100px 100px 100px;
    grid-template-rows: 100px 100px 100px 100px 100px 100px 100px;
}

.child {
    border: 1px solid #ccc;
    margin: 10px 10px 10px 10px;
}

.middle {
  margin: 10px 0px 10px 0px;
  background: blue;
  border:10px;
}

.end {
    margin: 10px 10px 10px 0px;
    border: 10px;
    background: blue;
}

.start {
    margin: 10px 0px 10px 10px;
    border: 10px;
    background: blue;
}
<!DOCTYPE html>
<html>
<body>
<div class="parent">
    <div class="child">14</div>
    <div class="child">15</div>
    <div class="child">16</div>
    <div class="child">17</div>
    <div class="child merged start">18</div>
    <div class="child merged middle">19</div>
    <div class="child merged end">20</div>
    <div class="child">34</div>
    <div class="child">35</div>
    <div class="child">36</div>
    <div class="child">37</div>
    <div class="child">38</div>
    <div class="child">39</div>
    <div class="child">40</div>
</div>
</body>
</html>

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 iPad screen displays the image in a rotated position while it remains

Recently, I developed a mini test website that enables users to upload pictures and immediately see them without navigating back to the server. It seemed quite simple at first. $('input').on('change', function () { var file = this. ...

Error in jQuery ajax request when passing multiple variables to php script

When I use ajax to send two variables to PHP, the second variable is received correctly, but the first variable is always reported as NULL by PHP. I checked the JavaScript variable before sending it and confirmed that it contains an array of strings. I&apo ...

Can an element be positioned in relation to the border-box of its parent element?

Check out this jsfiddle for an example: http://jsfiddle.net/apmmw2ma/ <div class='outer'> <div class='inner'> Inner. </div> Outer. </div> div.outer { position: absolute; left: 10px ...

Mastering the art of horizontal alignment for button and ul elements

I aim to create a unique layout that appears as follows: [the button][world1][world2] The code provided is in HTML format: <div id='container'> <button id='my-button'>the button</button> <ul id='my-ul&a ...

Tips on obtaining checkbox values other than "true"

Having trouble retrieving the values of selected checkboxes instead of displaying "Custom Category"? I've attempted to access the values and attributes with no success. I'm aiming to display the values of the selected checkbox. app.component.ht ...

Is there a way to clear a @font-face downloaded font from the browser cache?

Recently, I realized that I made an error in my webapp release by using a limited version of "Droid Sans" with @font-face for Latin characters. The font files are hosted on my server. To rectify this issue, I have now updated the font to the full version s ...

Ensure that radio buttons are evenly aligned both vertically and horizontally within a flexbox layout

I am attempting to align Radio buttons both vertically and horizontally within a flexbox layout. I have made an attempt so far, but unfortunately, the buttons are not aligning properly. Here is the code snippet: <div class="d-flex flex-column justify-c ...

Avoid making numerous http requests by making use of the fetch API

Currently, I am in the process of developing a Chrome extension that collects user input using prompts and sends it to the server via an HTTP request. However, I'm encountering a problem where the data is being duplicated, leading to multiple requests ...

What is the best way to design a regular expression that will only match up to 12 numbers?

Is there a way to construct a regular expression that will validate a string containing up to 12 digits only? Thank you! ...

Using SVG as an inline background image for a Div element is not functioning correctly

I'm facing a challenge with my Jquery-UI based dialog where I want to use an SVG image as the background. Initially, I attempted to make this work in a simple test file, but unfortunately, it was not successful even though the SVG itself worked perfec ...

MongoDB's Data-Driven Dropdown

I have a database named Staff in mongoDB, which includes a collection called records. Within this collection, there is a field named workgroup that contains entries such as "management", "union", "support staff", and more. My goal is to populate a dropdow ...

Struggling to maintain data consistency among controllers in Angular by utilizing the rootScope, only to encounter persistent issues with

I am facing an issue with displaying the admin status at the top of all pages for a user who successfully logs in as an admin. Here is my code snippet: <!-- nav bar --> <div> <span ng-show="$root.isAdmin">(ADMIN)</span> </di ...

Transmitting JSON data from Angular to a PHP script

Utilizing Angular for sending JSON data to the server, I'm encountering an issue where the entire PHP file is being returned as data. Within my Bootstrap modal, I have ng-submit=createNewProgram(). $scope.createNewProgram = function() { var data ...

Encountered an unhandled rejection error: TypeError: Unable to destructure the property 'protocol' of 'window.location' because it is undefined

I integrated the react-tradingview-widget into my nextjs project and it was working perfectly on version 10.2.3. However, after upgrading to version 12.1.4, I encountered an error when trying to reload the tradingview component. The error message was: unh ...

How to place the loading component in the centre of the image using React?

Check out the code and demo below: https://codesandbox.io/s/sparkling-silence-7cv3l I recently implemented the react-image-enlarger component to enable zoom functionality for images, similar to Medium. This component offers an API called renderLoading wh ...

What are the steps for creating a dropdown menu that allows for easy selection of the correct item

I am dealing with a dropdown menu as shown below. <select name="slt"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="3">Three +</option&g ...

Saving a dynamically generated table in a PHP session to display every time the current user logs in

This specific table is generated based on the user's selection of values in the drop-down menus. How can we store this dynamically generated table in a session so that it appears every time the current user logs in? <table border="0" class="table ...

"Approach for appending an additional element to an array item within a child component

I'm currently working on implementing two components, one acting as the parent and the other as the child. In the parent component, I have set up the data attribute like this... data () { return { users: [], } } The users array within ...

What is the best way to extract the JSON value from my API website and assign it to a new variable

Currently, I am in need of a way to convert a username into an ID. To accomplish this task, I will utilize the following API link: (where "username" is replaced by the variable name.) The main objective here is to extract the ID value from the provided li ...

Building a custom dialog box using Angular Material with HTML and CSS

I've been exploring different approaches to create a customized angular material dialog with a unique header using CSS/SCSS and the TailwindCSS framework. I am aiming for a specific visual effect, similar to what is shown in the figure below. desired ...