Here's some text

I'm trying to get the outer height" />

Here's some text

I'm trying to get the outer height" />

Here's some text

I'm trying to get the outer height"/>

Get height of element and use it in css

1.1k Views Asked by At

I have an div element on my page that contains a p element.

<div class="parent-div">

    <p>Here's some text</p>

</div>

I'm trying to get the outer height of the p element, and then use that number to apply a margin to the parent element.

e.g if the height of the p element is 346px, then I'd like to apply a 346px margin-top to the parent div.

I can get the height using jQuery's outerHeight(), but I have no idea how to use that number to apply a margin to the parent element.

Any advice is much appreciated.

2

There are 2 best solutions below

0
renakre On BEST ANSWER
$('p').each(function(){//for each paragraph
   $(this).parent().css("margin-top", $(this).outerHeight() +"px");
   //change its parent   margin-top   with the current p's outerHeight
})
0
Quintile On
var height = $("#yourelement").outerHeight();
$("#parent").css('margin-top', height);

You can use the 'css' method to apply styles to the elements.