css - Centering hyperlink containing an image -


i have run problem seems rather dull can't seem solve it. have following html cannot change:

<a href="#" class="aligncenter">     <img src="http://placehold.it/240x150" alt=""> </a> 

the width of image not known in advance. css used was

a {     display: block;     margin: 20px auto;     border: 1px solid #d5d5d5;     padding: 2px;     display: block; } img {     display: block; } 

note can't set text-align: center on parent of a!

sometimes image large wrapper (in fiddle below that's body) , there's no problem. however, image smaller wrapping element in case there'll white space fills empty space. see this fiddle.

a possible solution positioning link right-from-center margin-left (using percentual values parent) , reprositioning translate(using percentual values of element). see fiddle. however, can't used in ie8.

margin-left: 50%; transform: translatex(-50%); 

is there easier/better solution overlooking should able handle behaviour cross-browser? interested in hearing ideas not restricted ie8-compatibility. (for instance flex boxes, not experienced.)

try this: demo

img {     display: block;     margin: 0px auto; } 

edit: updated demo

so according requirement changed this:

css:

a img {     display: block;     margin:0 auto; } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -