Can you make two columns in CSS that are left floated and maintain their original order?

While the title may not provide much clarity, I am struggling to put into words exactly what I am trying to achieve. I have created a layout in Photoshop which I have shared below to help illustrate my goal.

Essentially, I have a blog that displays my stories sequentially from #1 to #5. I can designate a unique div class/id for each story based on how I want it to be displayed. For example, .small and .large.

Now, here's the challenge. WordPress will output the stories in order from #1-5, but I want to showcase all the .small stories in a left floated column and all the .large stories in another column.

Initially, I attempted to programmatically alter the ordering of the stories being generated by WordPress to fit my desired columns, but this approach proved to be too server-intensive. Then, I explored a more complex rearrangement using jQuery to reorder elements in the user's browser. While this second method is making progress, it still contains numerous bugs. It occurred to me that perhaps CSS could offer a solution to this problem. I have been experimenting with various CSS techniques all day, yet I have been unable to achieve the desired result.

I was hoping someone else might have insight or ideas on how to accomplish such a challenging task?

To skip the explanation and view the attached picture, please scroll down :)

Answer №1

If you have a situation where you need to display squares in pairs within a container and there is always one more large block than small blocks, then this CSS solution might be helpful. You can see it in action in this fiddle: http://jsfiddle.net/uPzh6/2/.

Remember, you can adjust the spacing between the blocks by using margins effectively. Check out another example in this fiddle: http://jsfiddle.net/VEEE6/1/.

This solution will accommodate any order of large and small blocks.

Here is the HTML markup:

<div id = "wrapper">
    <div class="small">1</div>
    <div class="large">2</div>
    <div class="large">3</div>
    <div class="small">4</div>
    <div class="small">5</div>
    <div class="small">6</div>
    <div class="large">7</div>
    <div class="small">8</div>
    <div class="large">9</div>
</div>

And here is the accompanying CSS:

* {
    margin: 0;
    padding: 0;
}
#wrapper {
    width: 210px;
    text-align: right;
}

#wrapper > div {
    width: 100px;
    height: 100px;
    background-color: rgba(100, 100, 100, 1);
    border: 1px solid rgba(30, 30, 30, 1);
    text-align: center;
    margin-bottom: 5px;
}

#wrapper > div.small {
    float: left;
    clear: left;
    line-height: 100px;
    color: yellow;
}

#wrapper > div.small:before {
    content: "small: ";
}

#wrapper > div.large {
    display: inline-block;
    color: #fff;
    line-height: 100px;
}

#wrapper > div.large:before {
    content: "LARGE: ";
}

Answer №2

Based on the images you provided, it seems like what you are looking for is shown in the following link:

http://jsfiddle.net/JYdB9/4/

@media (min-width: 1px) {
    .wrapper {
        text-align: right;
    }
    .small {
        float: left;
        width: 50%;
        text-align: left;
    }
    .large {
        display: inline-block;
        width: 50%;
        text-align: left;
    }
    .small + .small {
        clear: left;
    }
}

Keep in mind a couple of things:

  1. You must have a wrapper to align the inline-block elements to the right;
  2. Avoid any whitespace between the <div>s (to eliminate the whitespace issue)

Answer №3

To optimize the layout, my recommendation is to create two separate divs (columns): one for elements with the class .small and another for elements with the class .large. Then, using jQuery's .appendTo() function, you can move the .large items to the right column and the .small items to the left column. While this approach may not rely solely on CSS styling, it offers a practical solution since simply floating .small elements to the left and .large elements to the right won't suffice in this scenario.

This example illustrates why this method will not be effective:

<body style="width: 300px">
    <div style="float: left; clear: left; width: 100px; height: 100px; background: #ccc;">left</div>
    <div style="float: right; clear: right; width: 200px; height: 200px; background: #ccc;">right</div>
    <div style="float: left; clear: left; width: 100px; height: 100px; background: #ccc;">left</div>
    <div style="float: right; clear: right; width: 200px; height: 200px; background: #ccc;">right</div>
    <div style="float: right; clear: right; width: 200px; height: 200px; background: #ccc;">right</div>
    <div style="float: right; clear: right; width: 200px; height: 200px; background: #ccc;">right</div>
    <div style="float: right; clear: right; width: 200px; height: 200px; background: #ccc;">right</div>
    <div style="float: left; clear: left; width: 100px; height: 100px; background: #ccc;">left</div>
   <div style="float: left; clear: left; width: 100px; height: 100px; background: #ccc;">left</div>

In essence, the key here is to utilize two distinct columns for improved organization.

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

Periodically transmit information to a Google Script web application

I am currently working on a Google Script web app to automatically update data from a Google Sheet every 30 minutes. I initially attempted using the page refresh method, but encountered an issue where the web app would display a blank page upon refreshin ...

I noticed a change in the state between dispatches, but I did not make any alterations to the state

Although this question has been previously raised, most of the discussions focus on the OP directly mutating the state. I have taken precautions to avoid this by using techniques like the spread operator with objects and arrays. However, despite these effo ...

Utilizing turbolinks enables the page to be reloaded upon form submission

Currently, I have implemented turbolinks in my Rails application. However, every time I submit a form, the page reloads and the form is submitted. Is there a way to prevent the page from reloading and also submit the form seamlessly? ...

Stop the inclusion of the scrollbar in the mat-drawer-inner-container within the Angular mat-drawer Component

Background Story: Working on designing a screen layout that includes the use of a mat-drawer to display a custom component. The challenge arises when the custom component gets nested inside a div (with class="mat-drawer-inner-container") automatically adde ...

What is the best way to incorporate the value of a textbox into a list?

I currently have a list structured like this: <p>Please choose from the following options:</p> <script type="text/javascript"></script> <select id='pre-selected-options1' multiple='multiple'> <opt ...

Exploring VueJs 3's Composition API with Jest: Testing the emission of input component events

I need help testing the event emitting functionality of a VueJs 3 input component. Below is my current code: TextInput <template> <input v-model="input" /> </template> <script> import { watch } from '@vue/composition-api&ap ...

Margin discrepancies when using em units

Currently, I am utilizing an em-based margin for items within a menu that are homogeneous in terms of markup, class, and id. It appears that the margins for each of these items should be rendered the same. However, some are displaying as 1px while others a ...

Visibility of the br tag is not detected

I need to keep one specific div visible on the page while hiding all other content. However, I am facing an issue where the <br> tag inside that div is not showing up. <div id='min320'>min 320px<br>screen width required</div ...

When using jQuery, the script will execute successfully only when running it chunk by chunk in the console, rather than running the

As I tidy up an html page, my main task is to remove anchor tags and keep the text nodes. To achieve this, I am enclosing all text nodes (without any surrounding elements) within the <asdf> tag. Additionally, I am deleting empty elements such as < ...

Sweet treats, items, and data interchange format

Can an object be converted to a string, stored in a cookie, retrieved, and then parsed back to its original form when the user logs on again? Here's a concise example of what I'm asking: var myObject = { prop1: "hello", prop2: 42 }; va ...

Add a new row to the table when a dropdown option is selected, and remove the row when deleted. Ensure that the row is only added

Here is my specific requirement: I need a table with a default row containing a dropdown menu in the first column. When an option is selected from the dropdown, a new table row should be added with the same content as the main row and a delete button for ...

Having trouble getting the jQuery click event to work on iPhone browsers? Despite already trying the cursor:pointer; method

I am facing an issue with setting a click event on anchor tags in my WordPress site. Surprisingly, the event works perfectly fine on all of my devices including PC, Android phone, and tablet except for my iPhone. Despite trying to set the 'Cursor&apo ...

The error notification is not appearing in the correct location

I've been troubleshooting my jQuery error function for hours now (including the success function). I'm struggling to figure out how to display an error message only below the button that I click. To help clarify my issue, I've created a JSFi ...

Tips on leveraging state values within the makeStyles function in Material UI React

I'm in the process of building a Webpage and incorporating Material UI for the Components. Here's the code: import { makeStyles, Typography } from "@material-ui/core"; const useStyles = makeStyles((theme) => ({ container: { ...

Have you not heard of the greatness of Selenium before?

I've been trying to automate the process of selecting my shoe size, adding it to the cart, and checking out whenever I visit a sneaker page like FootLocker or FootAction. However, each time I attempt to run the script, I encounter the following error: ...

What is the best way to target specific elements within my array using Jquery?

I have a Wordpress website where the posts contain images with the .aligncenter class. I want to style the images that are less than 400px in width by adding the display:inline-block; property and reducing their size to 40%. Here is the approach I tried: ...

If I include beforeRouteEnter in one component, the this.$route property may become undefined in another component

I seem to be encountering an issue. After implementing a beforeRouteEnter method in one component, I am unable to access this.$route in another component. Here is the structure of my app: <div id="app"> <settings-modal></settings-modal ...

What is the accurate Scrapy XPath for identifying <p> elements that are mistakenly nested within <h> tags?

I am currently in the process of setting up my initial Scrapy Spider, and I'm encountering some challenges with utilizing xpath to extract specific elements. My focus lies on (which is a Chinese website akin to Box Office Mojo). Extracting the Chine ...

Guide on toggling the button by clicking on the icon to turn it on or off

Within my angular application, I have implemented font icons alongside a toggle switch. Initially, the switch is in the ON state. The specific functionality desired is for clicking on an icon to change its color from white to red (this has been achieved) ...

I seem to be encountering a z-index dilemma

Is there a way to make my slogan float above an image on a wide screen? I've tried adjusting the z-index without success. You can find the site here: Here is the CSS code: The #Slogan contains the words: Fitness Bike Guides - Top Models - Discount ...