Is there a way to ensure that all items have the same height and designate the column they belong to using

I'm working with 2 columns and I want to indicate which column items belong to, while also ensuring that they have the same height.

Here's what I currently have:

https://codepen.io/anon/pen/KyGewd

* {
  box-sizing: border-box;
}
.cont {
  margin: auto;
  width: 500px;
  display: flex;
}
.row {
  flex-basis: 50%;
  align-self: top;
  border: 1px solid blue;
}
.item {
  background: grey;
  padding: 10px;  
  border: 1px solid green;
}
<div class="cont">
  <div class="row">
<div class="item">
  <h2>Item 1</h2>
  <p>Here is short text</p>
</div>
<div class="item">
  <h2>Item 2</h2>
  <p>Here is short text</p>
</div>
  </div>
  <div class="row">
<div class="item">
 <h2>Item 3</h2>
  <p>Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. </p>
</div>
  </div>
</div>

https://i.sstatic.net/8Ms96.jpg

But what I need now is this:

https://i.sstatic.net/qso69.jpg

I don't want to specify a fixed height in pixels since my text content varies and the layout should be responsive (the 500px width set in the code is just for demonstration).

I could use JavaScript to calculate heights and assign a min-height accordingly, but it feels like a hack. Is there a CSS-based solution available?

UPDATE - The provided code example is simplified. In reality, I will have numerous items to arrange into "right" or "left" columns dynamically based on their properties.

Answer №1

If you have the ability to modify html code, one way to achieve a flexible layout is by using flexbox. You can place all elements in the same row and use wrap to control wrapping. To adjust the positioning, you can utilize the order property as shown below :

* {
  box-sizing: border-box;
}

.cont {
  margin: auto;
  display: flex;
  width: 500px;
}

.row {
  display: flex;
  flex-wrap: wrap;
  border: 1px solid blue;
}

.item {
  background: grey;
  padding: 10px;
  border: 1px solid green;
  flex: 0 0 50%;
}

.item:nth-child(1) {
  order: 1;
}

.item:nth-child(2) {
  order: 3;
}

.item:nth-child(3) {
  order: 2;
}
<div class="cont">
  <div class="row">
    <div class="item">
      <h2>Item 1</h2>
      <p>Here is short text</p>
    </div>

    <div class="item">
      <h2>Item 2</h2>
      <p>Here is short text</p>
    </div>
    <div class="item">
      <h2>Item 3</h2>
      <p>Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. Here is Longer text. </p>
    </div>
  </div>
</div>

Answer №2

To ensure that all items have the same height, it will be necessary to utilize javascript since there is no CSS technique available to achieve this specific requirement.

However, if the goal is for the "rows" to contain items of equal height, this can be accomplished using CSS Grid along with grid-auto-flow:column.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

::before,
::after {
  box-sizing: inherit;
}

.container {
  display: grid;
  grid-auto-flow: column;
}

.item {
  border: 1px solid grey;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
}

.left {
  background: rebeccapurple;
  color: white;
  grid-column: 1;
}

.right {
  background: orange;
  grid-column: 2;
}

.small {
  min-height: 32px;
}

.medium {
  min-height: 60px;
}

.large {
  min-height: 120px;
}
<div class="container">
  <div class="item left small">L1</div>
  <div class="item left medium">L2</div>
  <div class="item right large">R1</div>
  <div class="item left large">L3</div>
  <div class="item right small">R2</div>
  <div class="item right medium">R3</div>
  <div class="item left small">L4</div>
</div>

It should be noted that regardless of the specified min-heights (which are included solely for demonstration purposes), each "row" will adjust its item height to match the tallest item in the row.

Answer №3

* {
  margin:0px;
 padding:0px;
}
html,body{
        width:100%;
}
.cont {
    width: 500px;
     display: table;
    border-spacing: 2px;
    border:1px solid blue;
       border-collapse: collapse;
}
.row {
        display: table-row;
    vertical-align: inherit;
    border-color: inherit;
}
.item{
    display: table-cell;
    vertical-align: inherit;
    vertical-align: middle;
    padding:10px;
    word-break:break-all;
}
.item:nth-child(even){
   border:1px solid blue;
   border-bottom:1px solid green;
}
.item:nth-child(odd){
   border:1px solid blue;
   border-bottom:1px solid green;
}
<html>
<body>
<div class="cont">
  <div class="row">
        <div class="item">
          <h2>Item 1</h2>
          <p>Here is some short text</p>
        </div>
        <div class="item">
         <h2>Item 2</h2>
          <p>This text is longer than the previous one, but still manageable to read.</p>
        </div>
  </div>
  <div class="row">
        <div class="item">
          <h2>Item 3</h2>
          <p>Another piece of short content here.</p>
        </div>
        <div class="item">
          <h2>Item 4</h2>
          <p>Yet another brief message for this section.</p>
        </div>
  </div>
  <div class="row">
        <div class="item">
          <h2>Item 5</h2>
          <p>A quick snippet of text right here.</p>
        </div>
        <div class="item">
          <h2>Item 6</h2>
          <p>Keeping it short and sweet in this part.</p>
        </div>
  </div>
  <div class="row">
        <div class="item">
         <h2>Item 7</h2>
          <p>Now we're entering a longer text territory, so watch out!</p>
        </div>
        <div class="item">
          <h2>Item 8</h2>
          <p>Back to shorter messages as we close this grid layout.</p>
        </div>
   </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

How come my navigation item isn't positioned at the top of the window?

I'm facing an issue while trying to add a navbar with a navbar-brand to my website. It seems like the image I have included is causing the nav-items to not align properly at the top of the window. Here is a snapshot of the problem: https://i.sstatic.n ...

What are the pros and cons of embedding background image data into CSS as Base64?

While examining the source code of a greasemonkey userscript, I came across some interesting CSS: .even { background: #fff url(data:image/gif;base64,R0lGODlhBgASALMAAOfn5+rq6uvr6+zs7O7u7vHx8fPz8/b29vj4+P39/f///wAAAAAAAAAAAAAAAAAAACwAAAAABgASAAAIMAAVCBxIsK ...

CSS malfunctioning on a single box display

Our team has been successfully maintaining a client's website for several years without any issues. Recently, we encountered a problem where one block is not displaying correctly, but strangely it only seems to affect Google Chrome: We suspect that t ...

Switch out the content within a div upon selection

I'm currently working on a palette board project and facing some challenges when switching to a different theme. The initial page is set to have a Warm color palette, but I intend to alter this once the user clicks on the All theme option. Users wil ...

Tips for ensuring v-tabs-items and v-tab-item fill height

I found an example that I am trying to follow at the following link: https://vuetifyjs.com/en/components/tabs#content <v-tabs-items v-model="model"> <v-tab-item v-for="i in 3" :key="i" :value="`tab-${i}`" > < ...

Dealing with a jQuery/JavaScript issue involving thumbnail images

I am facing an issue with smooth transitions between thumbnail images in HTML list tags. I have Jquery listeners set up for mouseenter and mouseleave events that update a parent image when a thumbnail is hovered over. The problem arises when scrolling fro ...

Guide to creating a tab dropdown in Bootstrap version 2

Can someone guide me on how to make my Bootstrap tab dropdown? Here is the code snippet I am currently using in MVC. HTML <div id="tabs" style="border: 0px; width:100%;"> <ul id="uTabs"> <li><a href="#tabs-1">< ...

What is the process for selecting a radio button using Selenium WebDriver?

I am trying to select the radio button in this HTML code, but I'm having trouble. Here is the snippet of my HTML: <div class="appendContent"> <div> id="contentContainer" class="grid_list_template"> <div id="routeMonitor ...

What are the steps for utilizing CSS Global Variables?

Hey there, thank you for taking the time to help me out. I'm having a simple doubt that I just can't seem to figure out. So... here's my '/pages/_app.js' file: import '../public/styles/global.css'; export default funct ...

Adjust the button's hue upon clicking it

The current function is operational. Upon pressing the button, it toggles between displaying "click me" and "click me again". However, I desire the button to appear blue when showing "click me" and red when displaying "click me again". <!DOCTYPE html ...

What steps should I take to ensure proper rendering of CSS in Django?

Hey there! I'm new to Django and I'm struggling to get my css to display properly. I'm trying to create a red notification, similar to Facebook, for my unread messages. But for some reason, my css isn't rendering. Can anyone point out w ...

Utilizing interpolation in Angular to apply CSS styling to specific sections of a TypeScript variable

Suppose I have a variable called data in the app.component.ts file of type :string. In the app.component.html file, I am displaying the value of data on the UI using string interpolation like {{data}}. My question is, how can I apply some css to specific ...

Unique CSS Styles for Border Radius

Is there a way to achieve rounded corners on only the top left and top right, but not on the bottom two corners of a div using a specific value for -webkit-border-radius? ...

Scrolling to zoom in on the div content

I need the ability to resize the content within a div without changing the size of the div itself when the user scrolls. The function I currently have is as follows: var zoomable = document.getElementById('zoomable'), zX = 1; window.addEvent ...

Explore through the absolute object in the console by scrolling down, similar to a

I have a collection of commands that are displayed in a console-like format using Angular. When a new command is added, it appears at the bottom and the overflow moves upwards to accommodate the new content. However, I am facing an issue where I can no lon ...

Utilize jQuery to convert text to lowercase before adding Capitalize CSS styling

I have encountered a situation where I need to change the HTML link values from UPPERCASE to LOWERCASE and then apply a capitalization style. The problem lies in the fact that the links arrive in uppercase, so I had to come up with a workaround: The given ...

Can a JavaScript function be used with images to operate across multiple elements?

How can I make a function work on multiple elements? let image = document.getElementById("opacityLink"); image.onmouseover = function() { image.style = "opacity:0.9"; }; image.onmouseout = function() { image.style = "opacity:1"; }; <div class=" ...

javascript if condition not executing properly

I am struggling with my random number generator that should generate numbers between 5 and 15. I am trying to change the position of the 'chest' div based on the number generated by the computer, but for some reason it is not working as expected. ...

Is there a way for me to manage the appearance of the printed document's layout?

I have successfully added 3 print buttons to my website, each one designed to print a specific portion (div) of the webpage. Here is the code snippet for this functionality: function printPage(id) { var html="<html>"; //html+="<link rel="st ...

Is it possible for a dropdown to span 100% of the width

http://jsfiddle.net/gL1xynzr/ Is there a way to style a dropdown menu with the following properties: width: 100%; max-width: 440px; I am planning to include 4 of these dropdowns in one CSS table row to ensure they fit horizontally on mobile phone portra ...