Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center an image with a link on it

Is it possible to center an image only trough setting a class to the img tag without side effects? The problem is the following: I have an anchor around an image. If I use the following CSS

.aligncenter {
    clear: both;
    display: block;
    margin-left: auto;
    margin-right: auto;
}

And this (stripped down) HTML:

<a href="/path/to/image.jpg" class="fancybox" rel="fancybox" title="System">
    <img src="/path/to/image.jpg" title="System" class="aligncenter">
</a>

the link takes the whole width of the main div. This means not only the image is clickable - also the space around the image (actually the whole width) is clickable. This is through the CSS display: block. enter image description here

How can I center an image without using a parent div? I don't want the whole area clickable.

Background: You can read this topic. It is about Wordpress and the built in editor. He automatically generates the class aligncenter on an image (if the user pressed the center button). I need this for my own theme. According to the moderators there this should be only a CSS question and doesn't have to do with changing code in Wordpress.

like image 913
testing Avatar asked May 01 '12 10:05

testing


People also ask

How do I align an image with a link in HTML?

HTML | <img> align Attribute. The <img> align attribute is used to set the alignment of an image. It is an inline element. It is used to specify the alignment of the image according to surrounding elements.

How do you put a link in the center?

Similarly, when we add a link, it is displayed at its default position, but you can center align them using the CSS properties. There are two methods to center the link: Center links in CSS using the “text-align” property. Center links in CSS using the “margin” property.

How do you center an image in a cell HTML?

To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid . Then set the place-items property to center.


2 Answers

in aligncenter class add text-align:center

.aligncenter {
    clear: both;
    display: block;
    margin-left: auto;
    margin-right: auto;
    text-align:center;
}
like image 56
Enve Avatar answered Sep 20 '22 11:09

Enve


I'm not familiar with wordpress, but you might want to try setting the image's and the anchors's css 'display' property to 'inline-block'.

If you are limited in changing the document's DOM, another option is adding an 'onClick' attribute to the image.
This will allow you to run some function once the image is clicked.
So, for example, if you want to redirect to another page:

<img src='myImg.png' onclick='myRedirect()' style='cursor:pointer'/>

And in the page's header:

<script type='text/JavaScript'>
    var myRedirect = function(){
        window.location.href = 'Some other location';
    }
</script>

Notice the style='cursor:pointer', which changes the mouse's cursor to a 'hand' cursor.

like image 30
EyalAr Avatar answered Sep 19 '22 11:09

EyalAr