David Alsbright
David Alsbright

Reputation: 1565

Get height of element and use it in css

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

Answers (2)

renakre
renakre

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

Quintile
Quintile

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

Related Questions