views:

107

answers:

3

Hi Team,

Here's the html:

<div class="gallerybox">
<a href="/CustomContentRetrieve.aspx?ID=398791">
<img alt="" src="/Utilities/image.jpg" width="400" height="400" />
</a>
</div>

Here's the css:

.gallerybox {width:200px;height:200px;overflow:hidden;}

Can anyone tell me a way to align the linked image inside div.gallerybox middle (vertically) and center (horizontaly)? Maybe using javascript, jQuery or just plain css?

+2  A: 

Maybe something like the following:

<div class="gallerybox"> 
  <a href="/CustomContentRetrieve.aspx?ID=398791"> 
    <img id="imgUtility" class="galleryImage" alt="" src="/Utilities/ShowThumbnail.aspx?USM=1&amp;W=400&amp;H=400&amp;R=1&amp;Img={tag_image_value}" />"  /> 
  </a> 
</div> 

.gallerybox {
  width:200px;
  height:200px;
  overflow:hidden;
}

<!-- Edit Again -->
<script language="javascript">
window.onload = fixImage;

function fixImage() {
  var img = document.getElementById('imgUtility');
  var x = img.offsetWidth;
  var y = img.offsetHeight;

  img.style.left = -(x / 2);
  img.style.top = -(y / 2);
}
</script>
Joel Etherton
Jackson
@Jackson: Ah, ok, I see what you mean.
Joel Etherton
@Jackson: Threw in an edited idea
Joel Etherton
Thanks Joel. I actually was already using the above method, but the issue lies in the images being different formats (landscape/vertical etc). It is very hard to predict how the image will work with the dimensions they have used. That is why I was thinking a javascript method to enable virticle and horizontal center alignment would be the key. Thanks for your effort so far!
Jackson
@Jackson: How about that edit?
Joel Etherton
Hi Joel. Thanks for your time so far! I have been pulled on to another project and will try your code ASAP! Will let you know as soon as it's done.
Jackson
Gave it a go and it doesn't seem to work unfortunately :( Any other ideas?
Jackson
Rereading the question, are you attempting to resize the div to the dimensions of the image or are you attempting to center the image and have the size of the div clip the edges of the image?
Joel Etherton
+1  A: 

Hi, Jackson. I'm going to recommend a bit of an HTML rewrite, which I understand may not be possible, but I think this might be the most effective solution to your problem.

Create a hidden image with that database encoded <img>:

img#imgUtility {
   display: none;
}

(CSS and HTML)

<img id="imgUtility" class="galleryImage" alt=""
src="/Utilities/ShowThumbnail.aspx?USM=1&amp;W=350&amp;H=350&amp;
R=1&amp;Img={tag_image_value}" />

Then after the page has loaded and image has resolved to an actual URL, you can replace the <img> with a <span> in your posted HTML, set the hidden tag src to the background image of the <span> using it's inline style attribute via JavaScript:

// galleryBoxLinkTarget should be a reference to the <a> in a div.galleryBox
function imgReplacement(galleryBoxLinkTarget) {
   var span = document.createElement("span");
   var img = document.getElementById("imgUtility");
   span.style.backgroundImage = "url('" + img.getAttribute("src") + "')";
   span.className = "imgReplacement";
   galleryBoxLinkTarget.appendChild(span);
}

(JavaScript and HTML)

<div class="gallerybox">
   <a href="/CustomContentRetrieve.aspx?ID=398791">
      <!-- <span> goes here -->
   </a>
</div>

And then do a bit of clever CSS'ing:

span.imgReplacement {
   display: block;
   background-position: 50% 50%;
   background-repeat: no-repeat;
   width: 200px;
   height: 200px;
}

This should center the picture regardless of dimension, as well as allow you to remove the inline width and height attributes. Hope this works out for you!

Impirator
Hi Impirator. Thanks for your time so far! I have been pulled on to another project and will try your code ASAP! Will let you know as soon as it's done.
Jackson
Thanks again, but on closer look I have crealised that this particular feature in Adobe Business Catalyst does not enable the image to resolve as an actual url. It keeps the same crap format which means the ; character screwing up the css :( Any other ideas?
Jackson
+1  A: 

Try this:

<style type="text/css">
  .gallerybox {text-align:center;vertical-align:middle;height:400px;line-height:400px;}
  .gallerbox img {display:inline; vertical-align:middle;}
</style>

<div class="gallerybox">
   <a href="/CustomContentRetrieve.aspx?ID=398791">
   <img alt="" src="/Utilities/image.jpg" />
</a>
jerjer