Eugene Zolotuhin
Eugene Zolotuhin

Reputation: 111

Image cannot get parent attributes

*Sorry for formatting: typing from the phone.

Hello guys!

Help me please with a problem: embedded img inside div not changing it's width in respect to it's parent.

<div display="block" position="absolute" width="608.5px" height="480.5px" text-align="center" top="20%" left="50%">
    <img src="here_is_source" width="100%" height="auto">
</div>

So, I can't use CSS because I'm appending it using JS (element.appendChild), as well as creating properties of the element using element.setAttribute.
Thank you for suggestions, guys!

Upvotes: 1

Views: 42

Answers (1)

Coffeeholic
Coffeeholic

Reputation: 378

You can not set the props you are setting directly to a div. Use style, or insert a CSS style in the JS you describe.

Below solution solves your issue as stated, however also consider using

element.classList.add("my-class");
<div style="display:block;position:absolute; width:608.5px;height:480.5px;text-align:center;top:20%;left:50%;">
    <img src="https://via.placeholder.com/400" width="100%" height="auto"/>
</div>

Upvotes: 1

Related Questions