Why does the img ID not work in this CSS code?

  • Context: HTML/CSS 
  • Thread starter Thread starter kolleamm
  • Start date Start date
  • Tags Tags
    Css Element
Click For Summary

Discussion Overview

The discussion revolves around the issue of centering an image using CSS, specifically why using an ID for the image does not yield the expected results compared to using a general selector. Participants explore the nuances of CSS selectors, precedence, and the implications of using IDs versus classes.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes that using the CSS rule for the image tag works for centering, while using the ID for the containing div does not, raising confusion about the necessity of the img selector.
  • Another participant suggests that the issue may relate to CSS precedence, indicating that certain CSS attributes may have priority over others.
  • It is mentioned that centering the image directly relates to the img tag itself, implying that the focus should be on the image rather than the container.
  • A suggestion is made to visually inspect the div's size by adding a border, which may help understand how CSS rules are applied and how they interact with the image.
  • One participant discusses the role of CSS in creating structured containers for HTML elements, contrasting it with older methods of using tables for layout.
  • Concerns are raised about centering a specific image using an ID, as it could unintentionally affect multiple images if not managed properly.
  • There is a discussion about the functionality of the ID and class attributes in CSS, with some participants expressing uncertainty about their properties and behavior.
  • Another participant questions the understanding of how the div element is referenced in CSS, suggesting that similar principles apply to the img element.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness and implications of using IDs versus classes in CSS, as well as the understanding of CSS precedence. The discussion remains unresolved with multiple competing perspectives on the best approach to center an image.

Contextual Notes

There are limitations in understanding how CSS rules cascade and interact, particularly regarding the specificity of selectors and the behavior of IDs versus classes. Some assumptions about CSS behavior may not be universally applicable.

kolleamm
Messages
476
Reaction score
44
The following code works successfully and centers my image on the page (CSS)
#box {
display: block;
margin: 0 auto;
}

This one does not (CSS)
img {
display: block;
margin: 0 auto;
}

HTML
<div id="box">
<img src="title.png" style="width:40%">
</div>

I'm confused why it doesn't work. The HTML code stays the same in both cases. Why do I have to use img and not use the box id in the CSS code?

Thanks in advance!
 
Technology news on Phys.org
kolleamm said:
The following code works successfully and centers my image on the page (CSS)
Why do I have to use img and not use the box id in the CSS code?
Because it is the img that you want to center.
 
Draw a border around your div using
Code:
#box {
border: 2px solid black;
}

It's possible you'll see that the div's size is adapted to the image. Is this part of a larger page? Because CSS can become a tangled mess real quick especially with the cascading rules that aren't very clear unless you specifically look into them.

Oddly this does seem to work in my browsers (firefox 55 and chromium) :S
 
The important role of CSS is to create containers of HTML elements, give them the properties we need and follow inheritance rules, from outer to inner space of a region of a webpage - that eventually extends through different regions to the whole webpage, in order to give the look / embellishments to the HTML elements we want. So, you first create a skeleton i.e. an outer container and you gradually include other containers, depending on the depth level you want to give and the idea you want to accomplish.

In the old pre - CSS era, we were doing this using tables for the alignment of HTML elements - a poor hack, browser dependent and non portable solution. Now, thanks to CSS, there's absolutely no need for HTML to mess with the appearance of a site.
 
I don't mind using the img ID, however if I wanted to center a single specific image there might be a problem doing it directly from CSS since the img ID would center all of them.
 
I thought CSS could handle one specific tag via the ID attribute and multiple tags via the class attribute.
 
jedishrfu said:
I thought CSS could handle one specific tag via the ID attribute and multiple tags via the class attribute.
It can but I'm thinking the problem is that the img ID is not really a true ID attribute and has some different properties.
 
kolleamm said:
It can but I'm thinking the problem is that the img ID is not really a true ID attribute and has some different properties.
Do you understand how your div element is referenced from the CSS ? You can do the same with the img element.
Not sure if it makes sense in your case though.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K