CSS3背景图像定位

时间:2012-03-17 05:46:43

标签: css css3

我决定玩一些CSS3并遇到一些背景图片的问题。我的背景图像(六边形和箭头)显示不正确。六边形没有显示在父元素中,我无法使箭头向右移动。

我正在使用边框图像,以便我可以水平调整元素的大小。

任何建议?

这应该是它的样子 - > Finished Navigation Example

这是六边形 - > Hexagon

这是箭头 - > Arrow

这是锚元素背景 - > Anchor Background

这是列表项元素背景 - > List Item Background

JS Fiddle

HTML

<nav>
    <ul>
        <li>
            <a href="">Examples</a>
        </li>
    </ul>
</nav>

CSS

nav {
    font-family: sans-serif;
    font-weight: 700;
    font-size: 24px
}
li {
    display: inline-block;
    height: 107px;
    border-width: 1px 32px 1px 32px;
    -moz-border-image: url('http://markschamel.com/graphics/themes/misc/nav.li.background.png') 1 32 1 32 stretch stretch;
    -webkit-border-image: url('http://markschamel.com/graphics/themes/misc/nav.li.background.png') 1 32 1 32 stretch stretch;
    -o-border-image: url('http://markschamel.com/graphics/themes/misc/nav.li.background.png') 1 32 1 32 stretch stretch;
    border-image: url('http://markschamel.com/graphics/themes/misc/nav.li.background.png') 1 32 1 32 stretch stretch;
}
a {
    display: inline-block;
    height: 45px;
    border-width: 1px 14px 1px 14px;
    -moz-border-image: url('http://markschamel.com/graphics/themes/misc/nav.a.background.png') 1 14 1 14 stretch stretch;
    -webkit-border-image: url('http://markschamel.com/graphics/themes/misc/nav.a.background.png') 1 14 1 14 stretch stretch;
    -o-border-image: url('http://markschamel.com/graphics/themes/misc/nav.a.background.png') 1 14 1 14 stretch stretch;
    border-image: url('http://markschamel.com/graphics/themes/misc/nav.a.background.png') 1 14 1 14 stretch stretch;
    text-decoration: none;
    line-height: 45px;
    padding: 0 10px;
    margin: 31px 0 0 17px;
    background: url('http://markschamel.com/graphics/themes/misc/nav.hex.background.png') no-repeat -50px center,
    url('http://markschamel.com/graphics/themes/misc/nav.arrow.background.png') no-repeat right center;
    color: #d92701;
}

2 个答案:

答案 0 :(得分:1)

我已编辑过我的CSS。看看这个http://jsfiddle.net/8y6XL/1/

CSS

 nav {
    font-family: sans-serif;
    font-weight: 700;
    font-size: 24px
}
li {
    background: url("http://markschamel.com/graphics/themes/misc/nav.hex.background.png") no-repeat scroll 6px center, url("http://markschamel.com/graphics/themes/misc/nav.arrow.background.png") no-repeat scroll 223px center, url("http://markschamel.com/graphics/themes/misc/nav.li.background.png") no-repeat scroll left top transparent;
    border-width: 1px 32px;
    display: inline-block;
    height: 107px;
    text-align: center;
    width: 271px;
}
a {
    display: inline-block;
    height: 45px;
    border-width: 1px 14px 1px 14px;

    -webkit-border-image: url('http://markschamel.com/graphics/themes/misc/nav.a.background.png') 1 14 1 14 stretch stretch;
    -o-border-image: url('http://markschamel.com/graphics/themes/misc/nav.a.background.png') 1 14 1 14 stretch stretch;
    border-image: url('http://markschamel.com/graphics/themes/misc/nav.a.background.png') 1 14 1 14 stretch stretch;
    -moz-border-image: url("http://markschamel.com/graphics/themes/misc/nav.a.background.png") 1 14 1 14 stretch stretch;
    border-width: 1px 14px;
    color: #D92701;
    display: inline-block;
    height: 45px;
    line-height: 45px;
    margin: 31px 0 0 8px;
    padding: 0 10px;
    text-decoration: none;
    width: 138px;
}

如果你想要一个可拉伸的li元素,那么拉伸图像将会很混乱。我建议你把图像分成3部分。 1.左半部分包括左箭头角和六角形2.右部分包括小箭头和右角部分3.a 1px乘1px瓷砖图像。

答案 1 :(得分:0)

JS Fiddle

在阅读sliding doors上的A List Apart文章后,我研究了这个问题的解决方案。我使用Abhidev的建议将图像分成三部分。

此解决方案还允许我缩小每个列表项的宽度(例如,向 anchor 元素提供除auto之外的值)。

<强> CSS

nav {
    font-family: sans-serif;
    font-size: 24px;
    font-weight: 700;
}
nav ul {
    height: 107px;
}
nav li {
    display: inline-block;
    height: 107px;
    position: relative;
}
nav a {
    display: inline-block;
    color: #d92701;
    text-decoration: none;
    line-height: 4.5;
    overflow: hidden;
    height: 107px; width: auto;
    background: transparent url('nav.background.expand.middle.png') repeat-x left top;
    position: relative;
    top: 0;
    white-space:nowrap;
}
nav li:before {
    content: "";
    float: left;
    width: 64px;
    height: 107px;
    margin: 0;
    background: transparent url('nav.background.expand.begin.png') no-repeat left top;
}
nav li:after {
    content: "";
    float: right;
    width: 58px;
    height: 107px;
    margin: 0;
    background: transparent url('nav.background.expand.end.png') no-repeat right top;
}

<强> HTML

<nav><ul><li><a href="">Hello World</a></li><li><a href="">Hello</a></li><li><a href="">Hello World Hello World</a></li></ul></nav>