简体   繁体   English

将背景图像居中对齐

[英]Align background image middle and center

Here's the html: 这是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: 这是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)? 谁能告诉我在div.gallerybox中(垂直)和中心(水平)内对齐链接图像的方法吗? Maybe using javascript, jQuery or just plain css? 也许使用javascript,jQuery或仅使用CSS?

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>

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. 我将建议您进行一些HTML重写,虽然我知道这不可能,但是我认为这可能是解决问题的最有效方法。

Create a hidden image with that database encoded <img> : 用该数据库编码的<img>创建一个隐藏图像:

img#imgUtility {
   display: none;
}

(CSS and HTML) (CSS和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: 已加载网页和图像解决了一个实际的URL在此之后,你可以替换<img><span>在您发布的HTML,设置隐藏标签src的背景图像<span>使用它的内嵌style通过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) (JavaScript和HTML)

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

And then do a bit of clever CSS'ing: 然后执行一些巧妙的CSS编写:

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. 无论尺寸如何,这都应使图片居中,并允许您删除内联widthheight属性。 Hope this works out for you! 希望这对您有用!

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>

Use a simple JQuery fn called center ( Usage: $(element).center() ): 使用一个名为center的简单JQuery fn(用法:$(element).center()):

     jQuery.fn.center = function() {
        this.css("position","absolute");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
        this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
        this.css("bottom", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
        return this;
      }
    $(document).ready(function(){
$(#imgUtility).center();
)};

With the HTML: 使用HTML:

<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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM