Ways to make the height of two div elements the same using CSS

I need to ensure that the height of two div's is equal, with one stretching to match the height of the other if necessary. I am looking for a solution using only CSS. Here is the code I have so far (Fiddle Link):

HTML:

<div class="common">
    lorem ipusm random text just for check
</div>
<div class="common">
    lorem ipusm random text just for check lorem ipusm random text just for check
</div>

CSS:

.common{
  border:1px solid red;
  float:left;
  margin-left:5px;
  width:20%;
  padding:5px;
}

Check out the fiddle for more details.

Answer №1

To achieve a responsive layout, consider using the powerful flexbox model. Simply create a container for the divs and apply display: flex to the parent element, along with flex: 1 for the children.

Check out the updated code on jsfiddle

For more in-depth information on flexbox, visit http://css-tricks.com/snippets/css/a-guide-to-flexbox/

Answer №2

In my opinion, you have two options to choose from: either utilize a table structure or implement JavaScript for equalizing heights.

.wrapper{
    display:table;
    border: 1px solid blue;
    width: 250px;
}
.item{
    display: table-cell;
    border: 1px dashed orange;
    padding: 10px;
}

table example

Alternatively, you can achieve the same result using the following JavaScript code:

var maxHeight = 0;
$('.item').each(function(){
    var height = $(this).height();
    if(height > maxHeight){
        maxHeight = height;
    }
})

$('.item').each(function(){
    $(this).height(maxHeight);
})

JavaScript solution

Answer №3

The CSS attribute responsible for determining the vertical size of elements is known as height.

Negative values are not accepted by the height property. When a percentage is used, it references the height of the parent element. If the parent element does not have a specific height defined, it defaults to auto height.

The value 'inherit' means that the element's height is inherited from its parent. By using the 'auto' value, the browser will automatically calculate the element's height based on its content and available space on the page.

In addition to height, CSS also includes min-height and max-height properties for controlling element sizes.

.common{
  border:1px solid blue;
  float:right;
  margin-right:10px;
  min-height: 150px;
  width:25%;
  padding:10px;
}

For further details, visit Height

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

Utilizing a Text Box, HTML, and Javascript to Showcase a PDF within an iFrame

Is there a way to add a form on a Sharepoint page that prompts users to input the URL of a PDF document, which will then be displayed in an iFrame on the same page? I've come across some code that achieves this with an image, but I'm struggling ...

Phone website doesn't display properly on mobile device (CSS and HTML)

I am experiencing an issue where my website is not loading on mobile devices, but it functions properly when I adjust the size of my Chrome window on my laptop. @media screen { body{ background-image: url(background.jpg); backgroun ...

Set the input of a component in Angular to determine its width

I am working on a basic angular component. Here is the code snippet: <div class="k-v-c" fxFlex fxLayout = "row" > <div class="k-c" fxFlex = "widthOfTable" > {{ key | translate }} </div> < div class="separator" > ...

What is the best way to center the startIcon material ui icon?

I'm having trouble keeping the icon button centered on my page. When I add left: "0.5rem" to the sx prop, it ends up pushing the button icon even further away from center. I'd prefer not to use makeStyles to manually override the position. Any ad ...

Tips for arranging buttons horizontally in Angular

After adding a third button, I noticed that the buttons were now displaying in a column instead of a row. html: <div class="creator-button-container" *ngIf="isCurrentUserTheChannelCreator"> <div *ngIf="isChannelE ...

Attempting to load the parent window of a framed page from a separate domain results in a permission denial issue in Internet

Having an issue with a login page that is hosted within an iframe on a different domain. After a successful login, I am attempting to load the following page: <html> <head> </head> <body onload="parent.window.loca ...

Limit the y-axis on CanvasJS visualization

Is it possible to adjust the minimum and maximum values on the y-axis of the graph below: new CanvasJS.Chart(this.chartDivId, { zoomEnabled: true, //Interactive viewing: false for better performance animatio ...

When using NextJs, running the commands 'npm build' and 'npm start' can sometimes cause style inconsistencies

Currently working on a project with NextJs (sorry, no screenshots allowed). Running 'npm run dev' for development works perfectly fine, but when I switch to 'npm run build' and then 'npm start', the website displays overlappin ...

Tips for populating a row in the Bootstrap grid with the assistance of the mr or ml classes

I've come across a roadblock here. The issue arises when attempting to fill the Bootstrap row while also utilizing mr (or ml). Initially, I attempted to calculate the numbers so that they add up to 12. In my 3-column layout, I have a column of 5, fol ...

Utilizing React Material UI to divide the screen in half with one half being scrollable and the other half remaining fixed using UI Grid

My setup involves two Grid components, with one structured like this: <Grid container alignItems="stretch" spacing={3}> <Grid className="left-pane" item md={4} xs={12}> <Navigation /> </Grid> <Grid className="right-pan ...

What methods are most effective for designing a static side panel featuring a personalized tree-style list?

I am currently working on a sidebar design which is not rendering correctly, displaying a lot of flicker when expanded. I opted for b-nav instead of a traditional sidebar due to the interference it caused with my horizontal navigation bar. Here are my code ...

Navigating to the following webpage with Selenium using Python

url_main = "https://comic.naver.com/webtoon/detail.nhn?titleId=131385&no=292" This particular website features a comment pagination at the bottom of the page. Upon inspecting the page, I noticed that the buttons were structured like this: &l ...

I am currently facing an issue with validating forms in JavaScript Document Object Model (DOM)

Whenever I click on a field and move to another one, the span tag turns red. Then, after pressing the submit button, an alert message pops up. However, if I turn the span red, fill in the field, and press the submit button, it shows success even if other f ...

Function is raising an error with the wrong value or text

I am currently testing a form that I am in the process of developing. My goal is to have different values returned each time I click on a different item from the dropdown menu. If this explanation isn't clear, please take a quick look at my pen for cl ...

Having trouble showing an image with jQuery after it has been uploaded

Currently, I have a URL that shows an image upon loading. However, I want to provide the option for users to replace this image using a form input. My goal is to display the uploaded image as soon as it's selected so users can evaluate it. I'm a ...

What is the best way to use CSS to evenly lay out a group of dynamically generated buttons?

My Vue-generated buttons need to be evenly laid out on the bottom of the page, with equal space on the left and right. The mdui button style I am using has fixed width and height, so I have to decide between a single row for fewer buttons (less than three ...

Interactively scroll through div content using both horizontal and vertical movements with mouse wheel

I've created a div called 'demo' and I want it to start scrolling when it exceeds its defined size. Additionally, I would like to track the dimensions of the div so that next time I open my project, it automatically assigns those dimensions ...

Steps to modify the background-color of an iFrame using CSS

I am attempting to extract HTML code from an iframe and apply a background-color to the class: class="body" The structure of my HTML source is as follows: <iframe id="sc_ext_XT29EK" class="sc_ext" width="100%" height="1300px" frameborder="0" src="some ...

Having trouble invoking PHP functions from AngularJS

Hey there, I'm a complete beginner in web development. I have an HTML file with AngularJS code that calls a .php file, both located in my local folder. I've tried looking online, but: Installing a server is not possible as this will be used ...

Difficulty with the D3.js grid and tick functionality

Dealing with x-axis grid line formatting issues in a D3.js graph that I've created. var data = [{ x: '12-May-12', y: 5 }, { x: '30-Apr-12', y: 28 } // more data points listed here var margin = { top: 30, ...