代码之家  ›  专栏  ›  技术社区  ›  Tomas.R

垂直居中背景图像

  •  0
  • Tomas.R  · 技术社区  · 12 年前

    我不知道如何使图像居中。当我使用“中心-中心”作为背景位置时,女孩的头部没有显示出来。嗯。

    body {
        background: url(http://media.silabg.com/uf/common/catch-the-sun01.jpg) no-repeat center center;
    }
    

    http://jsfiddle.net/TomasRR/xvjdow6j/

    有什么想法吗?

    3 回复  |  直到 12 年前
        1
  •  1
  •   Thomas    12 年前

    定义一些高度,以便图像知道应该如何/在何处居中:

    html {
        height: 500px;
    }
    
    body {
        background: url("http://media.silabg.com/uf/common/catch-the-sun01.jpg") no-repeat center;
        height: 100%;
        width: 100%;
    }
    

    http://jsfiddle.net/az6x908x/

        2
  •  0
  •   NanoCat    12 年前

    如果您尝试将图像水平和垂直居中,可以尝试:

    body {
        background: url(http://media.silabg.com/uf/common/catch-the-sun01.jpg) no-repeat center center fixed;
    }
    

    这就是你要找的吗?

        3
  •  0
  •   Kheema Pandey    12 年前

    这将使背景图像垂直和水平居中。检查 DEMO .

    body {
        background: url(http://media.silabg.com/uf/common/catch-the-sun01.jpg) no-repeat 50% 50% fixed;
    }