2012-07-01 43 views
0

我创建了一个tumblr主题,其中的所有内容都以660px为中心。中心对齐我的大照片,没有负边距

但是,我也发布了940像素宽的大图像,并且通过给它一个-140像素(940-660/2)的负边距来对中,但这不是理想的,因为我必须全部发布图像作为这个尺寸,或者他们只是左对齐。

滚动到我的网站底部看到没有正确对齐的图像:http://seans.ws

的CSS:

 section {display: block; clear: both; margin: 0 auto;width: 660px;} 

     article img {clear: both; max-width: 940px; margin-left: -140px;} 

感谢您的帮助!

+0

研究使用javascript来动态定位元素。 –

回答

1

可以这两种解决方案之间进行选择:

标记:

<div id="content"> 
    <div class="a"><div class="b"> 
    <img src="http://placekitten.com/100/100"> 
    </div></div> 
    <div class="a"><div class="b"> 
    <img src="http://placekitten.com/2000/100"> 
    </div></div> 

常见的CSS:

#content { 
    width: 300px; 
    margin: 0 auto; 
    border: 1px solid blue; 
} 
.a { 
    /* extend image area */ 
    margin-left :-9999px; 
    margin-right:-9999px; 
    /* but without scrollbars */ 
    position: relative; 
    left: -9999px; 
} 
.a .b { 
    /* undo scrollbar-removing positioning */ 
    position: relative; 
    left: 9999px; 
} 

display: table方式:

http://jsfiddle.net/ZhEku/3/

.a .b { 
    display: table; /* shrink-wrap to content (= the image) */ 
    width: 300px; /* content width, acts as min-width when display:table */ 
    margin: 0 auto; /* center inside the (2*9999+300)px area */ 
} 

display: inline-block方式:

http://jsfiddle.net/ZhEku/4/

.a { 
    /* center content (= the image wrapped into .b) */ 
    text-align: center; 
} 
.a .b { 
    display: inline-block; /* shrink-wrap to content (= the image) */ 
    min-width: 300px;  /* content width */ 
    text-align: left;   /* if image is smaller than the content */ 
} 

享受:)

+0

好吧,它并不完美,因为它在页面上添加了一个水平滚动条。 – biziclop

+0

是的,然后它不会对齐小于#content中指定宽度的图像(就像200像素的图像不会与我在每个帖子/图形设计器怪胎开头处的红线齐平;) 谢谢Biziclop! –

+0

为了达到这个效果,你准备好容忍多少“不必要”的标记? :) – biziclop

0

这里的无限滚动JS:http://static.tumblr.com/q0etgkr/ytzm5f1ke/infinitescrolling.js

这里是较大的图像我利润率左脚本比容器的默认宽度:

<!--Dynamicaly center big images--> 
    <script> 
    $(window).load(function() { 
     $(function() { 
      $('img').css('marginLeft', function(index, value){ 
       if($(this).width() > 660) { 
        return -($(this).width() - 660)/2; 
       } 
       return value; 
      }); 
     }); 
    }); 
    </script> 

我唯一需要弄清楚的是如何在动态加载的图像上执行相同的功能,因为我有无限滚动(就像底部图像在下载页面之前不加载)。

+0

如果您修复了图片,请为其添加一些类,如'.fixed-margin'。当一堆新的图像加载时,只需修复'img:not(.fixed-margin)'。或者你可以听'DOMNodeInserted'或类似的事件:http://stackoverflow.com/questions/2143929/domnodeinserted-equivalent-in-ie – biziclop