Two divs with floated attributes and equal heights

My unique HTML is below:

<div class="container"> 
    <div class="box">
        <div class="float">
            <img src='http://media-cdn.tripadvisor.com/media/photo-s/03/9b/2f/db/miami-beach.jpg' />
        </div>
        <div class="float float_txt">
            text here!
        <p class"a_p">a</p>
        <p class"b_p">b</p>
        <p class"c_p">c</p>
        </div>
    </div>
</div>

Furthermore, I have included some CSS:

.container{width:400px}
.box{display:inline-block}
.float{width:50%; float:left}
.float img{width: 100%; height:auto;}
.float_txt{background:red}

You can view a live example here.

The .container element has a flexible width for responsive design and the image will adjust its size accordingly.

To ensure .float_txt is the same height as the image, and to divide elements a, b, and c in percentage, you may use the following CSS properties:

.a_p, .b_p{height: 20%}
.c_p{height:60%}

If you need assistance achieving this with only CSS and without JavaScript, please let me know!

Answer №1

Have you thought about solving it using JQuery? An example of how to do this with JQuery is by calculating the height of .float img and adding it to the height of float_txt.

$(".float_txt").css({
    'height': $('.float img').height()
});

This is just one way to solve the problem using JQuery, which can be much easier than relying solely on CSS.

Check out the Jsfiddle example here

Answer №2

Let me share with you one possible solution.

While not claiming to be the ultimate answer or the most elegant fix, this workaround satisfies the main requirement - it works (albeit with certain limitations).

Check out the fiddle here

Firstly, we need to ensure that all content within the .float_txt is encompassed by these three paragraphs, which are designated to occupy 20%, 20%, and 60% respectively, totaling 100% and leaving no room for additional elements. Then, we enclose these paragraphs in a div and position a duplicate image beside it, assigning the id="speculate" to this new image.
The HTML structure will resemble the following:

<div class="container"> 
    <div class="box">
        <div class="float">
            <img src='http://media-cdn.tripadvisor.com/media/photo-s/03/9b/2f/db/miami-beach.jpg' />
        </div><div class="float float_txt">
            <img id='speculate' src='http://media-cdn.tripadvisor.com/media/photo-s/03/9b/2f/db/miami-beach.jpg' />
            <div class='content'>
                <p class="a-p">a</p>
                <p class="b-p">b</p>
                <p class="c-p">c</p>
            </div>
        </div>
    </div>
</div>

We utilize the #speculate image to dictate the height of the .float_txt div. This image remains hidden with visibility: hidden, still occupying space within its container. The .content div is absolutely positioned to fill the entire area of .float_txt using top:0; right:0; bottom:0; left:0.
Each paragraph is also placed absolutely based on their set percentage heights. For example, to position the second paragraph, we use top: 20% because the first paragraph spans height: 20%. I hope this explanation clarifies things.

The complete CSS implementation appears as follows:

.box {
    display: inline-block;
}
.float {
    display: inline-block;
    width:50%;
}
.float img {
    width: 100%;
    height: auto;
}
.float_txt {
    background: red;
    position: relative;
}

#speculate {
    width: 100%;
    visibility: hidden;
    display: block;
}

.content {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
.content p {
    margin: 0;
    position: absolute;
}
.content .static {
    position: static;
}
.a-p {
    height: calc(20% + 20px);
    top: 20px;
}
.b-p {
    height: 20%;
    top: calc(20% + 20px);
}
.c-p {
    height: 60%;
    top: calc(40% + 20px);
}

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

Exciting effects activated by hovering on a button

I'm attempting to create an animated effect by having a rectangle expand over the button when hovered by the user. However, I am unsure how to achieve this and have hit a roadblock. Here's what I want: There's a button. There's a rect ...

Is there a way to customize the background color when the Bootstrap 4 toggle switch is in the "checked" position?

I'm struggling to find a way to adjust the background color of the "checked" state for this toggle switch in Bootstrap 4. It utilizes an additional library for toggling between dark and light modes - you can find it here on github. While that function ...

Nest a Div inside another Div with a precise margin of 10 pixels

Recently, I attempted to create a fullscreen webpage like this one: My goal was to have the color of each element change randomly every second. Here is the JavaScript code I implemented: <script> var tid = setInterval(chngColor, 1000); ...

Preventing the dropdown options from appearing in Angular when clearing the input field

In a reusable component, I implemented some simple logic to clear the value of a select input when the 'x' button is clicked. select.component.ts @Input() selectFormControlName: string; @Input() selectAbstractControl: AbstractControl; ... ...

The login page allows entry of any password

I'm running a xamp-based webserver with an attendance system installed. I have 10 registered users who are supposed to log in individually to enter their attendance. However, there seems to be an issue on the login page where any password is accepted ...

Struggling to align the Bootstrap list-group container in the middle

Good day to you! I'm currently working on a sidebar layout using Bootstrap's list-group and I need to set a specific width to ensure it aligns properly with the other elements above and below it. However, when I try setting the width to 300px, t ...

Show image details on hover?

I have six images displayed full width using a flex grid. I would like the image information (along with a visit button) to appear on hover, and the brightness of the image should decrease. I am struggling to position the information in the center of the i ...

Utilizing Material UI tabs panel with customized CSS styling

Having trouble changing colors and CSS in the TAB PANEL using material-ui. Any tips or suggestions? :( It seems like useStyle and theme functions are not working as expected. I was able to modify other properties like scrollable, but colors seem to be fix ...

Ensure that a div remains active even after it has been selected through AJAX using jQuery

I am currently utilizing ajax to display information from a database. The application I am working on is a chat app, where clicking on a specific conversation will append the data to a view. The structure of my conversation div is as follows: <div clas ...

Encountering issue when deploying multiple HTML files using Parcel

Whenever I deploy a website with multiple entry points and many HTML files, and the host uses the build command: parcel build index.html aboutme.html, the deployed website gives me a 404 error. However, if I manually enter /aboutme.html or /index.html in t ...

Select a CSS design with C# coding in ASP.net

I have defined two classes in my embedded CSS on the Default.aspx page. If necessary, I can move the code to an external CSS file. Is it possible to write C# code that allows me to select between these two styles for my table after clicking a button? I ha ...

What is the best way to pass an array to a child component in React?

I am having an issue where only the first element of inputArrival and inputBurst is being sent to the child component Barchart.js, instead of all elements. My goal is for the data to be instantly reflected as it is entered into Entrytable.js. EntryTable.js ...

Upon including the enctype attribute as "multipart/form-data" in my form and submitting it, the form redirects to a different page

My website has two main pages: The first page is admin.php <?php session_start(); if (!isset($_GET['page'])){ $_GET['page'] = 'home'; }//end if include_once(dirname(__FILE__)."/function.php"); ?> <?ph ...

Is there a way to showcase a block of Python code using a combination of HTML, CSS, and Javascript to enhance its

On my website, I want to display code blocks similar to how StackOverflow does it. The code block should be properly colored, formatted, and spaced out for better readability. All the code blocks on my site will be in python. def func(A): result = 0 ...

Is it possible for me to generate HTML using JavaScript?

Below is the javascript code I have written, saved as create.js: var stuff = document.querySelector(".stuff"); var item = document.createElement('div'); item.className = 'item'; stuff.appendChild(item); This is the corresponding HT ...

Add JavaScript and CSS files from the node_modules folder to enhance a static HTML webpage

Currently, my development server is set up using node's live-server. However, for production, I plan to use a LAMP server. Within my node_modules directory, I have the normalize.css file. In my index.html, I currently link to it like this: <link ...

Retrieve CSS content from a file hosted on the local server

Is it feasible to use a locally hosted CSS file for styling a website managed by a server-based CMS? I am interested in utilizing SASS for local CSS development while retrieving the HTML content from a centrally hosted CMS server. ...

Should you create an archive - Retain outcomes or retrieve them whenever needed?

I am currently developing a project that allows users to input SQL queries with parameters. These queries will be executed at specified intervals determined by the user (e.g., every 2 hours for 6 months), and the results will be sent to their email address ...

React Application - The division is positioned centrally when run on local host, however, it does not retain its center alignment

I'm encountering an issue with deploying my create-react-app on github pages. The first image showcases my deployed app on github pages, but the image is not properly centered on the page as intended. https://i.stack.imgur.com/rOciT.jpg On the othe ...

Webpack is notorious for creating multiple copies of images

Having an issue with Webpack where it's generating duplicate images, one of which is broken. I have an original image image, and after running Webpack, two duplicates are created. One works fine: image, but the other one is broken: image. I'm us ...