Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS Why does the class work inside the img tag but not inside div?

hhaarr

New Coder
Hello everyone

why does it work here

[CODE lang="html" title="html"]<div>
<img src="pix.jpeg" alt="Eastern Port" class="A5">
</div>[/CODE]

but not here

HTML:
<div class="A5" >
      <img src="pix.jpeg" alt="Eastern Port">
</div>

this is as far as it goes for the class in CSS
CSS:
.A5 {
        width: 50%;
}


Thanks for any help
 
Last edited:
Hi there,

Welcome to Code Forum!

I will test this, but you could tell us what your experiencing? And what you’re trying to achieve so that we can get a better solution for you?
 
Hello!
As @Malcolm said, you need to be more specific!
However, I think what you expect is the two codes to produce the same result, right? The thing is the size of an <img>, by default, does not depend on its parent, so changing the size of the <div> will, by default, no affect the <img>.
If you want the picture to be the same width as the <div>, you must give a relative width (in %) to the <img>:
HTML:
<div class="A5" >
      <img src="pix.jpeg" alt="Eastern Port">
</div>
CSS:
A5{

    width: 50%;
}

A5 img{
    width: 100%;
}
Above, the <div> element takes 50% of the with of its parent and the <img> takes 100% of the width of the <div>.
 

New Threads

Buy us a coffee!

Back
Top Bottom