CSS背景图片定位(负位置?)

6
我正在尝试添加一个图标,它位于边框的顶部并将其分成两半。
以下是我目前的代码:
<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>

与我希望的不同,图像位于边框下方而不是上方。

4个回答

15

另一种方法是使用伪类 :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>

在某些情况下,它会破坏现有的用户界面,那么请尝试将位置更改为“绝对”。 - didxga

7

背景图像位于框内,因此无法像这样将其移到外部。您可以将图像定位到框外并将其移动到框内。

您可以尝试以下方法,虽然不是万无一失的,但可以帮助您实现部分目标。

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

6

还有一种只使用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/

这是一个关于CSS3中背景原点和背景裁剪的预览页面。

1
#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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接