How to detect whether a DOM element is block or inline with javascript?
For example, is there a function/property which returns ‘inline’ for a ‘<a>‘ tag (or ‘block’ for a ‘<p>‘ tag)?
Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can go with
getComputedStyle()andcurrentStyleto get the calculated styles for an element. This should do it:To be a little clearer, computed styles contain values for every style property, even for those that don’t have a style property set. Those values will be the default value so in the case of an unstyled
<a>element,displaywill returninline:Tested in latest Firefox, Chrome and IE7/IE8.
Results:
Update: edited to give preference to standards compliance/
getComputedStyle()in IE9, which supports both methods.