html - 居中对齐我的大照片,没有负边距

标签 html css tumblr

我创建了一个 tumblr 主题,其中所有内容都居中且宽度为 660 像素。

但是,我也发布了 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;}

感谢您的帮助!

最佳答案

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

标记:

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

显示:表格方式:

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 */
}

享受 :)

关于html - 居中对齐我的大照片,没有负边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285021/

相关文章:

javascript - 通过 ID 获取动态创建的元素 (Jquery)

javascript - 如何禁用输入字段的默认最大长度?

javascript - 如何在JQuery中设置链接访问过的颜色

ruby-on-rails - Rails 可以充当在 mysite 上托管域的反向代理吗?

html - 如何在控制高度的同时将 <li> 元素扩展到 <ul> 容器的整个宽度?

javascript - 将 <div> 的背景颜色设置为里面的十六进制文本

CSS :Select all elements descendent of X but not descendents of Y

javascript - 在鼠标上使用 jQuery 从左到右背景位置输入幻灯片

html - 当某个元素在 Tumblr 主题中不可用时从该元素中删除 CSS?

html - Tumblr 中的水平导航栏问题 (CSS/HTML)