質問

I'm working with a WordPress plugin and I don't want to manually change the plugin code, but I've run into a bit of a problem.

<div id="comic">
 <img src="link.to/image.jpg">
<div>

For some reason any attributes I throw into the div won't center the image (margin: auto; text-align: center;) but I was playing around with the Inspect Element feature of Chrome and can manage to get it centered by tagging img with "margin: auto;".

The problem is, I can't add an id or class to the image because it's embedded into the plugin. Is there any way to add CSS to the undefined tag, like maybe effecting all the images under the comic id?

Edit: I don't want to affect all of my image tags either, just this specific one.

役に立ちましたか?

解決

You can do css mapping. Look at all the parents and find one with a class you can handle, then map it down appropriately For instance:

div class = grandparent
  ul 
    li
      a
        img

can be targetted as

 /*to affect only the one directly under this parent class */
 div.grandparent > ul > li > a > img {
      margin:0 auto;
 }

 /*to affect all under this parent class */
 div.grandparent ul li a img {
      margin:0 auto;
 }

他のヒント

Fo your current code, you could give the same width to the wrapper and the img tag, and center the wrapper: JS Fiddle

#comic {
    width: 200px; 
    margin: auto;
}
#comic img {
    width: 200px;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top