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/
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.
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.
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.
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 ).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With