Ways to compel divs underneath aligned divs

There are six divs numbered 1 to 6. I want divs 2, 4, and 6 to be positioned below divs 1, 3, and 5 respectively. Is it possible to achieve this layout? Sorry if my explanation is not very clear. Thank you.

http://jsfiddle.net/LkGV8/

<body>


<div class="contain">

    <div class="square">
    </div>
    <div class="rec01">
    </div>
    <div class="square">
    </div>
    <div class="rec02">
    </div>
    <div class="square">
    </div>
    <div class="rec03">
    </div>

</div>

</body>



  .contain {
    border:1px solid;
    width:639px;
    height:900px;
    background-color:;
    }

    .square {
    width:33%;
    height:50px;
    float:left;
    background-color:grey;
    }

    .rec01 {
    width:100%;
    height:50px;
    float:right;
    background-color:black;
    }

   .rec02 {
    width:100%;
    height:50px;
    float:right;
    background-color:black;
    }

    .rec03 {
    width:100%;
    height:50px;
    float:right;
    background-color:black;
    }

(add more details, don't pay attention this.)

Answer №1

SEE IT IN ACTION

Not sure if this is the solution you are looking for.

To achieve the desired result, it is recommended to remove the float property from the .square class and instead add the display:inline-block property to it:

.square {
    width:32%;
    height:50px;
    display:inline-block;
    background-color:grey;
}

Answer №2

Have you tried rearranging the elements in the HTML code to achieve the desired layout? You can also utilize jQuery to help with this task, as outlined in this Stack Overflow post.

// While not the most elegant solution, the following code snippet should do the trick...
$( ".rec01" ).insertBefore( ".square:eq(0)" );
$( ".rec02" ).insertBefore( ".square:eq(1)" );
$( ".rec03" ).insertBefore( ".square:eq(2)" );

If possible, consider providing an image illustrating how you envision the final layout. Based on your description, it seems like you want elements 2, 4, and 6 positioned beneath elements 1, 3, and 5 respectively.

Answer №3

To solve this issue, consider implementing a grid system.

<h3>Method 1</h3>
<div class="grid">
    <div>
        <div class="grid">
            <div>1</div>
            <div>2</div>
            <div>3</div>
        </div>
        <div class="grid">
            <div>4</div>
            <div>5</div>
            <div>6</div>
        </div>
    </div>
</div>
<h3>Method 2</h3>
<div class="grid">
    <div>
        <div class="grid">
            <div>1</div>
            <div>3</div>
            <div>5</div>
        </div>
        <div class="grid">
            <div>2</div>
            <div>4</div>
            <div>6</div>
        </div>
    </div>
</div>

You can find the CSS and visual representation on JSFiddle.

Answer №4

If you want to style odd and even children differently, you can utilize the :nth-child selector with this example in a JSFiddle link. Here's an illustration:

.contain div:nth-child(odd) {
position:relative;
top:0;
}
.contain div:nth-child(even) {
position:relative;
top:220px;
}

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

Extracting a value from one HTML element and transferring it to another HTML element's attribute

Is there a way to retrieve the value of an "alt" attribute from an HTML <img> element and then assign it as the value for the "title" attribute in an enclosing <a> tag using PHP? <a title="" href ="page.html"><img src="image.jpg" al ...

Contenteditable feature dynamically styling text upon input

Is it possible to prevent CSS properties like text-transform from affecting text entered via contenteditable? On my webpage, "CERTIFICATE PROGRAM" is shown in uppercase due to text-transform settings. However, the original input was: Certificate Program. ...

What is the correct way to superimpose an image with a transparent background?

I am currently working on a mini game that involves overlaying images on camera views. The goal is to have the gun displayed without the background rectangle, as shown in this image: https://i.stack.imgur.com/yU4A2.jpg The code snippet below is relevant ...

Guide to creating a dynamic column layout with minimum height divs

I am facing an issue with the variable height of my div, causing it to position incorrectly. To demonstrate the problem, I have included a code snippet below. Here is the solution I am seeking: https://i.sstatic.net/XMAMr.png Note: Since the div has a ...

Creating a form in PHP with the power of JavaScript, AJAX, and jQuery

I have successfully created a registration form using HTML, processed the data with PHP, and utilized javascript, ajax, and jquery. However, I am facing an issue where I want to display a notification stating whether the operation was "inserted/failed" on ...

Tips for creating a clickable A href link in the menu bar that triggers an accordion to open in the body when clicked - html

Is there a way to open the first accordion when clicking on the "open 1st accordion" link, and do the same for the second link? The accordions themselves work perfectly fine, I just need a way to trigger them from outside their scope by clicking on links i ...

Issue: $injector:unpr Unrecognized Provider: itemslistProvider <-

I've spent several days debugging the code, but I can't seem to find a solution. I've gone through the AngularJS documentation and numerous Stack Overflow questions related to the error, yet I'm still unable to identify what's caus ...

Issue with Bootstrap 4 navbar functionality on smaller screens

I'm currently in the process of integrating a navbar that transforms the navbar choices into a dropdown menu on smaller screens (as illustrated in the documentation available here). However, I am encountering an issue where the button responsible for ...

defined dimension of table cell

<table class="data-table"> <tr> <th style="width: 200px;"> DescriptionDescription <%--in this case <td> is resized--%> </th> </tr> <% foreach (var item in Model) { %> ...

What is the preferred method for writing `*zoom: 1;` in CSS?

Trying to create my code, I decided to borrow some CSS files from older code. However, upon running yarn build I encountered several errors like: ▲ [WARNING] Expected identifier but found "*" [css-syntax-error] <stdin>:122:2: 122 │ * ...

Error message in Angular js: Unable to load file using XMLHttpRequest

Encountering an error while debugging an Angular JS application in the WebStorm IDE. The error message states: "XMLHttpRequest cannot load file:///C:/Users/../list.html. Cross origin requests are only supported for HTTP." Provided Javascript code : var a ...

"Animating a card to slide in from the left side upon clicking a button in a React app

How can we create a feature where, upon clicking "Apply Coupon" in Image 1, a window slides in from the left just above the webpage (as shown in Image 2)? Additionally, in Image 2, there is a blue transparent color on the webpage adjacent to this sliding w ...

What is the best way to ensure that the size and maximum length of a text input box are properly aligned?

Is there a way to ensure that the size and maxlength attributes of a text input box align correctly? It can be frustrating when these attributes don't match up due to font differences. Example Input: <input type="text" size="4" maxlength="4" /> ...

Packaging an Angular project without the need for compiling

In order to enhance reusability, I structured my Angular 6 Project into multiple modules. These modules have a reference to a ui module that contains all the style sheets (SASS) as values such as: $primary-text-color: #dde7ff !default; Although this app ...

Material-UI: Eliminating linkOperator functionality in data-grid

Is there a way to completely eliminate the linkOperator UI from the filter panel? I managed to move pagination to the top of the table using Material-UI, but can I achieve the same for the "Operators" menu programmatically? ".MuiDataGridFilterForm-linkOpe ...

Troubleshooting CSS Text Display on HTML5 Video for Mobile Devices

I am currently working on positioning text and a button over an HTML5 video without using media queries to address mobile display issues. The problem arises when using the car-image-overlay class from Bootstrap 4 to show the div containing the text over th ...

Use JQuery to gradually decrease the opacity of divs individually

I am currently working on a function that fades out all divs except the one that has been clicked on simultaneously. However, I want them to fade out one by one instead. Additionally, I would like the divs to fade out in a random order. If anyone knows ho ...

Continuous loop of images displayed in a slideshow (HTML/JavaScript)

Greetings everyone, I am currently working on creating an endless loop slideshow of images for my website. Despite researching several resources, I am still struggling to get the code right. The issue I am facing is that only the image set in the HTML code ...

encountering a Zend Framework error

Looking for help with printing data after an inner join of two tables in table format. I encountered the following error in the code below: Parse error: syntax error, unexpected end of file in /home/bina/public_html/studentzend/application/views/scri ...

Background wrapper does not reach full height of page

I am currently dealing with a website template that has a background image in the wrapper. However, when I view the site on a 13" laptop screen, the text extends below the fold because the wrapper only covers above the fold. This results in my content not ...