【问题标题】:Centering a background image, using CSS使用 CSS 将背景图像居中
【发布时间】:2011-02-08 06:07:43
【问题描述】:

我想将背景图片居中。没有使用 div,这是CSS 样式:

body{
    background-position:center;
    background-image:url(../images/images2.jpg) no-repeat;
}

上面的 CSS 瓦片全部居中,但一半的图像看不到,它只是向上移动。我想做的是使图像居中。我可以采用图像在 21 英寸屏幕上查看吗?

【问题讨论】:

  • 现在这将取决于图像的大小和浏览器的大小,不是吗?
  • @nikc +1,我知道,但需要知道是否有解决方法

标签: css


【解决方案1】:
background-position: center center;

没有...对我不起作用

background-attachment: fixed;

同时使用我的图像在 x 和 y 轴上居中

background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;

【讨论】:

    【解决方案2】:

    我认为this 是我们想要的:

    body
    {
        background-image:url('smiley.gif');
        background-repeat:no-repeat;
        background-attachment:fixed;
        background-position:center;
    } 
    

    【讨论】:

    • 后台附件为我解决了这个问题;)
    【解决方案3】:

    像这样:

    background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
    background-repeat:no-repeat;
    background-position: center;
    background-size: cover;
    

    html{
    height:100%
    }
    
    body{
    background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
    background-repeat:no-repeat;
    background-position: center;
    background-size: cover;
    }

    【讨论】:

      【解决方案4】:

      简单替换

      background-image:url(../images/images2.jpg) no-repeat;
      

      background:url(../images/images2.jpg)  center;
      

      【讨论】:

        【解决方案5】:

        遇到了同样的问题。使用了 display 和 margin 属性,它起作用了。

        .background-image {
          background: url('yourimage.jpg') no-repeat;
          display: block;
          margin-left: auto;
          margin-right: auto;
          height: whateveryouwantpx;
          width: whateveryouwantpx;
        }
        

        【讨论】:

          【解决方案6】:

          唯一对我有用的是..

          margin: 0 auto
          

          【讨论】:

            【解决方案7】:

            如果您对以上答案不满意,请使用此

                * {margin: 0;padding: 0;}
            
                html
                {
                    height: 100%;
                }
            
                body
                {
                    background-image: url("background.png");
                    background-repeat: no-repeat;
                    background-position: center center;
                    background-size: 80%;
                }
            

            【讨论】:

              【解决方案8】:
              background-repeat:no-repeat;
              background-position:center center;
              

              当使用 html 4.01 'STRICT' doctype 时,背景图像不垂直居中。

              添加:

              background-attachment: fixed;
              

              应该解决问题

              (所以亚历山大是对的)

              【讨论】:

                【解决方案9】:

                试试这个background-position: center top;

                这将为您解决问题。

                【讨论】:

                  【解决方案10】:

                  我用这个代码,效果很好

                  html, body {
                    height: 100%;
                    width: 100%;
                    padding: 0;
                    margin: 0;
                    background: black url(back2.png) center center no-repeat;;
                  -webkit-background-size: cover;
                  -moz-background-size: cover;
                  -o-background-size: cover;
                  background-size: cover;
                  }
                  

                  【讨论】:

                  • 谢谢!我什至不知道 webkit 有不同的声明等......
                  • 嘿维克多,我经常使用你的代码,但现在看起来它不起作用。我添加了背景附件:固定;但它不适用于我的 iPad 浏览器...
                  • 只有-webkit-background-size 存在于特殊声明中。
                  【解决方案11】:
                  background-image: url(path-to-file/img.jpg);
                  background-repeat:no-repeat;
                  background-position: center center;
                  

                  应该可以的。

                  如果没有,为什么不使用图像制作div 并使用z-index 将其作为背景?这比在正文上的背景图像更容易居中。

                  除此之外的尝试:

                  background-position: 0 100px;/*use a pixel value that will center it*/ 或者我认为如果你将身体 min-height 设置为 100%,你可以使用 50%。

                  body{
                  
                      background-repeat:no-repeat;
                      background-position: center center;
                      background-image:url(../images/images2.jpg);
                      color:#FFF;
                      font-family:Arial, Helvetica, sans-serif;
                      min-height:100%;
                  }
                  

                  【讨论】:

                  • 它确实集中,但只显示一半屏幕,我希望 stackoverflow 可以选择附加屏幕截图
                  • 您可以制作屏幕截图并将其上传到您的服务器或 photobucket.com 等免费图片托管网站,或者在 www.jsfiddle.net 上制作一个临时示例并为我们发布链接。根据您所说,图像是 1600x1200,大多数屏幕分辨率不会那么高,请尝试调整图像大小以适应。例如,我使用 1440x900 的宽屏显示器。
                  • screenshot - imagevat.com/picview.php?ig=6179 我不知道如何将图像上传到 jsfiddle,如果你能帮助我jsfiddle.net/yWrQP
                  • 要将图像添加到 jsfiddle,只需使用完整的 http 路径 EX:background-image:url(http://www.myurl.com/images/image.jpg); 将其放入 CSS 中。
                  • 我没有服务器,但使用了一个图像托管站点,但现在仍然检查它
                  【解决方案12】:

                  您的代码中有错误。您在 background-image 属性上使用了完整语法和速记符号的混合。这会导致 no-repeat 被忽略,因为它不是 background-image 属性的有效值。

                  body{   
                      background-position:center;
                      background-image:url(../images/images2.jpg) no-repeat;
                  }
                  

                  应该成为以下之一:

                  body{
                      background:url(../images/images2.jpg) center center no-repeat;
                  }
                  

                  body
                  {
                      background-image: url(path-to-file/img.jpg);
                      background-repeat:no-repeat;
                      background-position: center center;
                  }
                  

                  编辑:对于您的图像“缩放”问题,您可能需要查看this question's answer

                  【讨论】:

                  • 'background:url(../images/images2.jpg) center center no-repeat;'不起作用,因为语法错误而且其他的也不起作用
                  【解决方案13】:

                  试试

                  background-position: center center;
                  

                  【讨论】:

                  • 双中心是怎么工作的?无论如何它并没有解决问题,还是一样
                  • @Jean:看看规格,背景位置实际上可以取 2 个值(水平和垂直)。 w3.org/TR/CSS2/colors.html#propdef-background-position
                  • @rob 它不工作,图像平铺。图片是 1600x1200,我的屏幕尺寸是 1280x800
                  • @Jean 你还不能调整背景图片的大小。这在 CSS 3 中是可能的,但还没有得到足够广泛的支持。 center centerbackground-repeat: no-repeat 配合使用应该满足您的要求 - 将图像居中。
                  • @Jean ahh,我现在从你的屏幕截图中看到了。您需要将body 指定为min-height100%,以便它扩展到整个屏幕。这可能会对它进行排序。
                  猜你喜欢
                  • 2023-04-01
                  • 2017-02-24
                  • 2022-01-25
                  • 1970-01-01
                  • 2011-05-04
                  • 2016-01-12
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多