I’ve been going crazy with this: I have a banner which is structured like this:
<div id="header-image-wrapper">
<a href="#content-title" id="header-link">
<img id="bannerimage" src="image source" />
</a>
</div>
The images won’t always be the same width, and my client wants the image to be horizontally centered within the div. The div should have a maximum width of 1200px with overflow:hidden, so that if the image is larger than that, it will be centered and the excess on the left and right side will be cut off.
The only way I could think this would work was using javascript:
var imgWidth = document.getElementById('bannerimage').style.width/ 2;
document.getElementById('bannerimage').style.marginLeft = imgWidth;
But when I refresh the page, I get this error:
[cycle] terminating; zero elements found by selector
Does anyone know what that means, and why it’s happening? Also, if someone has a better solution to this problem, please share it as I’m at my wit’s end. 🙂
Edit: (bottom one won’t work if the image is too big, like you said, but this one should) Put the image as the background of the a tag and position it center.
I would just use CSS here. Give the div an ID and then style it accordingly with “text-align: center”