Achieving equal height for two divs and centering them

Is it possible to align the height of two floated divs so that the second div slips down, and the container width automatically adjusts to fit the child width while centering the divs? I apologize for any language errors. :(

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title></title>
    <style type="text/css">
.container{
text-align:center;
display:block;
background-color:grey;
}

.left {
    background-color:#EFF5FB;
    width: 510px;
    float: left;
    min-height:50px;

}

.right {
    float: left;
    width: calc(100% - 520px);
    width: -webkit-calc(100% - 520px);
    width: -moz-calc(100% - 520px);
    width: -o-calc (100% - 520px);
    min-width:510px;
    background-color: #00f;
    height:100%;
    //min-height:50px; (edited)
}
.clear{clear:both;}

</style>
</head>
<body>
<div class="container">
<div class="left"><p>paragraph 1</p><p>paragraph 2</p></div>
<div class="right"><p>paragraph 1</p></div>
<div class="clear"></div>
</div>  
</body>
</html>

[Here is the visual representation of what I would like to achieve] https://i.sstatic.net/56gwt.jpg

http://jsfiddle.net/SpSjL/3268/ (edited again!)

Answer №1

To optimize the layout, it is recommended to adjust the CSS for .left and .right elements by removing the floating property and setting width: auto; when the width is reduced. Additionally, center align the .container by specifying a max-width value and using margin-left: auto; and margin-right: auto;.

Here is an example media query:

@media (max-width: 600px){
    .container{
        max-width: 400px;
        margin: 0 auto;
    }
     .left, .right{
         float: none;
         width: auto;
         padding: 10px;
     }
     .left p{
         padding-bottom: 10px;
     }
}

Adding padding bottom to the <p> tag helps create spacing between elements.

You can view a live demo here: http://jsfiddle.net/MasoomS/3xw2qpb8/

Answer №2

To achieve this effect using CSS, you can use a basic class with properties such as width: 50%, margin: 0, and padding: 0. Apply this class to the two div elements that you wish to combine. Then, create a media query like the following example:

@media screen and (max-width: 500px) {
    .customClass { 
        width: 100%; 
    }
} 

Answer №3

To create two divs of equal height, you can utilize a table element.

<table style="width:100%">
  <tr>
    <td style="float:right";>Jill</td>
    <td style="float:left";>Smith</td>      
  </tr>
</table>

By using the <td> element within a table, which has a default display property of table-cell, the two divs will automatically be displayed at the same height.

If there are any changes in the layout due to window resizing, you can implement media queries to adjust the styling as needed.

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

Error with setting innerHTML property of a null nested div

This issue arises when the element is referenced before the DOM has completely loaded. To combat this, I have ensured that the necessary script runs only after the window has finished loading. Interestingly, if the getElementById() call is for a standalon ...

Having trouble with images not displaying in IE10 on HTML5 canvas?

I recently delved into learning about html5 and the canvas element, attempting to create a basic webpage that rotates an image clockwise and counterclockwise upon button clicks. Although I managed to get it working, when I tested it on ie10, only the blank ...

Overflowing CSS grid issue

I've been working on this for a couple of hours, but I haven't been able to get it right. I want to add labels in a grid layout with a maximum of 3 columns. If the items don't fit, they should wrap to the next row. It would be great if the i ...

Increase the parent height by 100% when the child element expands

I am attempting to create a full-screen website with 100% height and no scrollbar. I have a div that takes up 90% of the page height and I want to dynamically add elements inside it. However, when there are too many elements inside this div, they expand th ...

"Mastering the art of text animation: A beginner's guide

If you want to see a cool moving text animation, simply head over to this link: . The text will automatically type out and then clear just like the example below: https://i.sstatic.net/dAZW6.png Ever wondered how to create this kind of text animation? A ...

Automatically submit upon selecting an option

Encountering some difficulties with the code provided below. Purpose of this code I have multiple list items (shown here is just a snippet) made up of select elements, options, and one submit input field. When an option is chosen, the user can click on ...

Is there a definitive way to distinguish between scrollTop and scrollHeight in web development?

For instance, function checkingScroll(){ var newHeight = element.scrollHeight; var scrollTopValue = element.scrollTop; alert("The scrollHeight property is: " + newHeight + "px"); alert("The scrollTop property is: " + scrollTopValue ...

Designing a captivating loading screen in Sencha Touch optimized for various mobile device resolutions

I am currently working with Sencha Touch 1.1 and I need my application to be compatible across various mobile platforms such as Android, iPhone, iPad, and Blackberry. One of the requirements is to have a splash screen at startup, which I have tried impleme ...

The layout of my website is perfect when my laptop's zoom is set to 90%, but at 100% it starts overflowing

Currently, I am in the process of building a website on my laptop while following an instructor's video tutorial. It has been important for me to set the same pixel measurements as the instructor in order to replicate the design they provided. However ...

Retrieving Data from Web using Python Selenium

I've been trying to extract information from a website (snippet provided below) The process involves entering data, moving to another page for more inputs, and eventually displaying a table. However, I'm encountering an issue at this stage: dri ...

Angular template src variable issue with no solution in sight

The videoSrc variable is not evaluating correctly images/{{videoSrc}}.mp4 When I write just videoSrc, it works fine. But when I concatenate it with other strings, it doesn't work. Check out this jsfiddle ...

Tips for effectively sizing a logo within a Bootstrap navbar

<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <link ...

Upon selecting a checkbox, I desire for a corresponding checkbox to also be marked

I am looking to enhance my current project by incorporating a feature that allows the user to simply check a checkbox with specific content once. For example, I have a recipes page where users can select ingredients they need for each recipe while planning ...

Update the font URL in Bootstrap 5

I am looking to update the URL that Bootstrap uses to access fonts, as I want to host them on my server instead of relying on users to fetch them from Google with each page load. However, despite my efforts, I cannot locate the specific URLs mentioned when ...

Adding data from PHP into a designated div element

update: After some trial and error, I managed to find a solution using jQuery and append() methods. From my PHP code, I created an array containing all the necessary information that I wanted to insert into the tab divs. I then encoded this array into JSON ...

Is it necessary for a click handler to be triggered when clicking on a scrollbar?

Check out these HTML snippets: Jsfiddle <style> div { margin:20px; border: 30px red solid; padding: 20px; background-color:green; overflow-y:scroll; } </style> <div onclick="alert('div clicked');"> ...

Are background images covering hidden text detrimental to accessibility?

Is there a way to display images of text instead of actual text for my menu? I have implemented spans within each menu link. These spans have specific dimensions, block display properties, the text is indented to be hidden, and the background image is set ...

What happens when browsers encounter unfamiliar at-rules?

CSS at-rules have been part of CSS since the days of CSS2. As CSS evolves with new specifications like @supports, it's interesting to see how different browsers handle unsupported rules. Do major browsers simply ignore unrecognized rules, or do they f ...

stop the menu component from triggering a re-render

I am facing an issue where the menu opens and closes briefly when I refresh the page. I want to find a way to prevent this re-rendering process. Every time I refresh, my console.log shows: false 'open' navigation.jsx:23 item navigation.jsx: ...

Linking an image to a database-stored value through a hyperlink

My goal is to give users the option to link their social media accounts such as Facebook and Twitter, with each link displaying an image that corresponds to the platform. The intention is for these images to direct them to the links stored in the database. ...