【问题标题】:css background image positioning (negative position?)css背景图片定位(负位置?)
【发布时间】:2009-06-28 00:44:58
【问题描述】:

我正在尝试添加一个位于边框顶部的图标,将其分成两半。

这是我目前所拥有的:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <style type="text/css">
        body {
            background-color:#26140C;
        }

        .box {
            width: 800px;
            margin: 0 auto;
            margin-top: 40px;
            padding: 10px;
            border: 3px solid #A5927C;

            background-color: #3D2216;
            background-image: url(Contents/img/icon_neutral.png);
            background-repeat: no-repeat;
            background-position:10px -20px;
        }
    </style>
</head>
<body>
    <div class="box">
        <h1>This is a test!</h1>
    </div>
</body>

图像不是像我希望的那样超出边界,而是在边界之下。

【问题讨论】:

    标签: html css


    【解决方案1】:

    另一种方法是使用伪类 :after 在你的框之后注入一个元素。

    CSS

      .box{
        position:relative;
      }
      .box:after{
           content:url(icon_neutral.png);
           display:block;
           position:relative;
           top: -30px;
      }
    

    HTML

    <body>
        <div class="box">
            <h1>This is a test!</h1>
         </div>
    </body>
    

    【讨论】:

    • 在某些情况下它会破坏现有的 UI,然后尝试将位置更改为“绝对”
    【解决方案2】:

    背景图片在框内,因此将其移到外面是不可行的。您可以做的是将您的图像放在框外并将其移入其中。

    您可以尝试这样的事情,它不是万无一失的,但可以帮助您实现一些目标。

    <html>
    <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Untitled Document</title>
            <style type="text/css">
                    body {
                            background-color:#26140C;
                    }
    
                    .box {
                            width: 800px;
                            margin: 0 auto;
                            margin-top: 40px;
                            padding: 10px;
                            border: 3px solid #A5927C;
    
                            background-color: #3D2216;
                    }
    
                    .image {
                        float: left;
                        position: relative;
                        top: -30px;
                    }
            </style>
    </head>
    <body>
            <div class="box">
                <img src='icon_neutral.png' class="image" />
                    <h1>This is a test!</h1>
            </div>
    </body>
    

    【讨论】:

      【解决方案3】:

      还有另一种只使用 CSS3 的方法。

      首先设置border-top: transparentbackground-clip: border-box,然后负背景位置是可能的。

      .box {
         border-top: 8px solid transparent;
         background-clip: border-box;
         background-position: 0 -8px;
      
         background-image: url(image.png);
         background-repeat: repeat-x;
         /* ... */
      }
      

      获得相同效果的另一种方法是添加额外的background-origin: border-box,则不再需要负背景位置

      .box {
         border-top: 8px solid transparent;
         background-clip: border-box;
         background-origin: border-box;
         background-position: 0 0px;
      
         background-image: url(image.png);
         background-repeat: repeat-x;
         /* ... */
      }
      

      更多信息:

      http://www.css3.info/preview/background-origin-and-background-clip/

      【讨论】:

        【解决方案4】:
        #box {
          position:relative;
        }
        #shield {
          width:41px;
          height:41px;
          position:absolute;
          top:-25px;
          left:25px;
        }
        
        <div id="box">
        
          <div id="shield">
            <img src="shield.png" />
          </div>
        
          <h1>Site Title</h1>
        
        </div>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-12
          • 1970-01-01
          • 2015-03-10
          • 2014-08-23
          • 1970-01-01
          • 2022-01-19
          • 2012-09-10
          相关资源
          最近更新 更多