Reputation: 1565
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.
Upvotes: 0
Views: 1158
Reputation: 8291
$('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
})
Upvotes: 2
Reputation: 343
var height = $("#yourelement").outerHeight();
$("#parent").css('margin-top', height);
You can use the 'css' method to apply styles to the elements.
Upvotes: 1