Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set div's width equal to another div [duplicate]

Tags:

html

jquery

css

I want to give my .second_div the width of first_div, but the problem is that the first div does not have a width value given in css and I need to find his width using jQuery.

HTML:

<div class="first_div">First Div</div>
<div class="second_div">Second.width == First.width</div>

CSS:

.second_div,
.first_div {
    border: 1px solid black;
    padding: 2px 4px;
    display:inline-block;
}

jQuery:

$(document).ready(function(){
    $(".second_div").css({'width':($(".first_div").width()+'px')});
});

jsfiddle: https://jsfiddle.net/ekqecjb8/1/

like image 736
Valip Avatar asked Jul 30 '15 10:07

Valip


People also ask

How do I keep two side by side div elements the same width?

The two or more different div of same height can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format.

How do I make two divs the same size?

You can use Jquery's Equal Heights Plugin to accomplish, this plugins makes all the div of exact same height as other. If one of them grows and other will also grow.

How do I make div width auto adjust?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

How do I make another div overlap?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).


1 Answers

Missing jQuery library in your fiddle, there is no error in your code . Just include jQuery library in your code

$(document).ready(function() {
  $(".second_div").css({
    'width': ($(".first_div").width() + 'px')
  });
});
.second_div,
.first_div {
  border: 1px solid black;
  padding: 2px 4px;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="first_div">First Div</div>
<div class="second_div">Second.width == First.width</div>
like image 124
Pranav C Balan Avatar answered Oct 23 '22 09:10

Pranav C Balan