A floated div containing three rows of varying sizes

I have a single div element containing two child div elements. These children are both 50% in width, with one floated left and the other floated right. The right-floated div contains various tall images, while the left-floated div contains text separated into three different rows of varying heights. I want the entire left div to match the height of the right div. How can I achieve this using only CSS? Here is an example of my code:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
body {
    margin: 0;
}
.container {
    width: 100%;
    height: 100%;
    overflow: auto;
    background: #FF0;
}
.left {
    float: left;
    width: 50%;
    background: #F0F;
}
.left .first {
    height: 20%;
}
.left .second {
    height: 50%;
}
.left .third {
    height: 30%;
}
.right {
    float: right;
    width: 50%;
}
.right img {
    display: block;
    max-width: 100%;
}
p {
    margin: 0;
}
</style>
</head>
<body>          
    <div class="container">
        <div class="left">
            <div class="first">
                <p>First</p>
            </div>
            <div class="second">
                <p>Second</p>
            </div>
            <div class="third">
                <p>Third</p>
            </div>
        </div>
        <div class="right">
            <img src="http://upload.wikimedia.org/wikipedia/commons/3/3a/Centara_Grand_Hotel.jpg" alt="" />
        </div>
    </div>
</body>
</html>

Answer №1

In short, it is possible to achieve this effect to some extent, but the outcome may not meet your expectations.

To make two <div> elements match in height, you would need to specify explicit heights for both:

.left, .right {
   height: 100px /*or adjust as needed*/;     
}

If the content on the page is static and the image dimensions remain constant, you can manually input the pixel values.

However, if the image changes dynamically and its height varies, it is challenging to synchronize the heights of the two divs using only CSS.

Although techniques like faux columns exist, manipulating one div's height to match another purely with CSS is not straightforward.

While JavaScript offers solutions for achieving this effect, I prefer avoiding it due to reliability issues when altering layout through scripting.

Additionally, in cases where the containing div (.container) collapses, floating it or utilizing a clearfix technique is necessary to maintain the desired layout.

Answer №2

Here are the steps you need to follow:

First, make sure to use the float property for your containers.

Next, create an additional container and structure the divs in this order:

<div class="container2">
  <div class="container">
    <div class="left">
      <div class="first">
        <p>First</p>
      </div>
      <div class="second">
        <p>Second</p>
      </div>
      <div class="third">
        <p>Third</p>
      </div>
    </div>
    <div class="right">
      <img src="http://upload.wikimedia.org/wikipedia/commons/3/3a/Centara_Grand_Hotel.jpg" alt="" />
    </div>
  </div>
</div>

Then, apply a relative position to your containers and align them to the right. Finally, shift your content divs from the left side.

When it comes to your CSS styling:

.container {
  width: 100%;
  float: left;
  position: relative;
  right: 50%;
}
.container2 {
  width: 100%;
  float: left;
  overflow:hidden;
  position:relative;
}
.left {
  float: left;
  width: 50%;
  left: 50%;
  position: relative;
  background: #F0F;
}
.right {
  float: left;
  width: 50%;
  left: 50%;
  position: relative;
}

If you encounter any challenges, refer to this resource for assistance.

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

Having issues with jQuery's .hover function in Chrome

<div class="front_hover"> <img class="bg_img" src="image.jpg" width="320px" height="400px"/> <div class="front_roll" style="display:none;"> <!-- CONTENT HERE --> </div> </div> <script language="Java ...

Deactivate hover effects and media queries for Material UI controls in all states

Since I am using a touch-capable monitor, I noticed that hover styles are not showing up on any controls. Specifically, when I hover over a Material UI button, I see the following styles: https://i.stack.imgur.com/uwBpt.png Is there a way to disable all ...

What could be causing the JSON content to not display in an AJAX request?

I am facing an issue while trying to retrieve JSON data from one page to another. js_page.php <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> ...

Define the number of columns in CSS Grid and ensure that rows wrap accordingly

tag, I need to utilize CSS grid with 12 columns while ensuring that the tables stack in a column layout when the viewport size decreases. Is it possible to accomplish this using CSS grid? My research suggests that for row wrapping, I must use either ' ...

Ensure that selected options are unique across multiple selections

Could you help me with a question that involves matching pairs of words in both Russian and English? <div class="form-group" id="question4"> <label for="q4FirstSelectEN">4</label> <div class="row"> <div class="col-lg ...

Facing difficulties updating certain product and theme options in WordPress

I am experiencing difficulty in making updates to certain products, such as those published in October 2020, and I am unable to update the theme options. Despite performing all upgrades, checking my PHP version, and generating a new .htaccess file, I con ...

How to position text in the center of a video using CSS

My attempt to center the name of my blog on top of a video background was not successful, even though I tried positioning it absolutely with 50% from the top. Can someone assist me in vertically and horizontally centering the text over the video? Here is ...

The initial item in the Materializecss slider is failing to display

Currently, I am attempting to implement materialize slider with skrollr. The setup is functional; however, only the first item of the slider is set to opacity: 0 initially. https://i.stack.imgur.com/urSww.jpg After a brief delay, the second item becomes ...

Choosing a specific column in an HTML table using jQuery based on the text within it

I have a table with a similar structure as shown below: <table> <c:forEach ...> <tr> <td>test</td> // this is the initial value <td>random value</td> <td>random value</td&g ...

Stop the automatic resizing of a bootstrap card containing an image when the text within the card is lengthy

How can I keep the height of a card containing an image fixed, while allowing the text inside to be wrapped and shortened if necessary? https://i.sstatic.net/r5HNG.png <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/ ...

Is there a way to create a collapsible menu in Semantic UI?

This is my customized menu using Semantic UI, which is stackable for mobile devices. I am looking to make it collapsible on low resolutions such as mobile devices. Here is an example of what I'm aiming for: https://i.sstatic.net/6qHuc.png Could anyon ...

Challenges encountered during the integration of jQuery UI datepicker

Recently, I added a datepicker to one of my pages and encountered some unexpected behavior when hovering over the corresponding div. Here's what I observed: In addition, I came across the following errors: <link rel="stylesheet" href="//code.jq ...

Using CSS to target the last child and hover states of an anchor link within an unordered list

I am currently adding styles to the last child of a navigation menu, and I have been successful with this code: .aston-menu-light ul > li:last-child { border:2px solid blue; border-radius: 50px; padding:0 20px 0 20px; } .aston-menu-light ...

Ways to position one card to the lower left corner of another card on separate screens

Hello, I am at the beginning of my journey into Web development, and I have successfully created a basic HTML webpage using bootstrap-4. My goal is to have 2 cards displayed horizontally, with the image card on the left and the text card on the right. Whe ...

looping through to obtain images

I am currently working on a school project that requires creating a website to display images from a database. I am attempting to achieve this using the following PHP code snippet: <?php $table_name = "image"; // variable with the database ...

Ensure the footer remains fixed while scrolling

For the past day, I've been tackling a challenge with my online shop project. On the specific item's page, I want to include a fixed [add to cart] button as a footer initially, which then becomes sticky when scrolling to a certain point. You can ...

Can a variable Angular component be embedded within another component and have data passed to it?

What I desire If we consider component2 as the component in which I want to utilize variable components, let's take component1 as an example. component1.html: <div>Hello, I'm comp 1. Data: {{data}}</div> component2.html:<div> ...

How can I make a clickable <div> easily navigable with tab functionality?

I am currently tackling a project that involves implementing an onclick event within a <div>. While I've successfully set up the primary functionality, there is one issue at hand. Specifically, I need the onclick event to trigger when a user nav ...

Manipulate an image by hiding it, altering its contents, and then displaying it

I am seeking the most effective method to accomplish the following task: $("#some-image").fadeOut(); $("#some-image").attr("src", "new-src.png"); $("#some-image").fadeIn(); For timing purposes, the code below is a step closer, but it still needs improvem ...

Retrieve HTML classes within JavaScript textual templates

<!-- TEMPLATE UPLOAD --> <script id="template-upload" type="text/x-tmpl"> {% for (var i=0, file; file=o.files[i]; i++) { %} <tr class="template-upload fade"> <td class="name"><span>{%=file.name%}</span></td> <td ...