Are you using tables for layouts?
Well, first I want to ask whether this is inside a table because you're using a table for layout.
If so, you shouldn't.
1. Sizing
I would suggest that you rather set the size of the anchor than the cell (see the snippet below).
2. Vertical Centering
The old hack to get vertical centering was to space elements with known heights 50% from the top and then use a negative margin of half the height to correct:
margin-top: -35px;
top: 50%;
Now there's a great feature called calc
which is even pretty well supported and much nicer (thanks @billynoah for reminding me):
top: calc(50% - 35px);
3. Use the :after element
There's also a wonderful pseudo element called :after
. You don't need to nest an img in every anchor tag; you can just do this:
a:after {
background-image: url("https://i.stack.imgur.com/5b8Bn.png");
/* You need a few more styles, see the snippet */
}
Try the snippet...
a {
display: block;
width: 244px;
height: 186px;
background: url("https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg/1024px-Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg") no-repeat center;
background-size: 100%;
text-align: center;
}
a:after {
display: block;
width: 70px;
height: 70px;
position: relative;
top: calc(50% - 35px);
left: calc(50% - 35px);
content: " ";
background-image: url("https://i.stack.imgur.com/5b8Bn.png");
}
<table>
<tbody>
<tr>
<td>
<a href="#">
</a>
</td>
</tr>
</tbody>
</table>
Even better: At this point I would recommend that you consider using the :after
trick and then using an actual <img>
for your background:
a {
display: inline-block;
position: relative;
}
img {
display: block;
width: 244px;
height: 186px;
}
a:after {
display: block;
width: 70px;
height: 70px;
content: " ";
position: absolute;
top: calc(50% - 35px);
left: calc(50% - 35px);
background-image: url("https://i.stack.imgur.com/5b8Bn.png");
}
<a>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg/1024px-Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg" />
</a>