Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get image true css display property

i got a piece of html

<img style="cursor: pointer; width: auto; height: auto; display: inline;" src="http://www.kidsgen.com/fables_and_fairytales/images/rapunzel.gif" alt="rapunzel" title="rapunzel" align="right">

and even if i set display: inline; in its style, when i'm trying to get its css display property like this:

alert($('img:first').css('display'))

or

var el=document.getElementsByTagName('img')[0]
alert(document.defaultView.getComputedStyle(el,null)['display'])

it always gives me value block.

what's wrong?

like image 678
SuperYegorius Avatar asked Apr 13 '13 23:04

SuperYegorius


People also ask

How do I find the CSS of an image?

For example with Google Chrome, you can right-click on any element, such as an image, and choose "Inspect Element". That will open the Developer Tools window. From there, click on the tab called "Resources". That will show a complete list of files, including images, that the page is using.

What is a valid CSS display property?

The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex. Formally, the display property sets an element's inner and outer display types.

Do images with display none get loaded?

Images or wrapper elements that have display: none set, will still get loaded. So if you want to eg. load a different image for desktop and mobile, you can either: use display: none , but with the loading="lazy" parameter set on img .

Is display property inherited CSS?

Some CSS properties, such as color or font-family , are automatically inherited, but others, such as display or margin , aren't.


1 Answers

The align='right' property assignment is causing the img element to have its display property set to 'block'. Your code without the align='right' propery will alert 'inline' on jsFiddle.

<body>
    <img style="cursor: pointer; width: auto; height: auto; display: inline;" src="http://www.kidsgen.com/fables_and_fairytales/images/rapunzel.gif" alt="rapunzel" title="rapunzel" />
</body>

alert($('img:first').css('display')); // alerts 'inline'

A relevant piece of extra information is img tags are in fact inline elements by default. However, with align='right' set inside the img tag, I was unable to set the display property back to inline even by inserting this line of code:

$('img:first').css('display', 'inline');
like image 103
orb Avatar answered Oct 17 '22 08:10

orb