在背景之前和之后对齐h1

时间:2013-06-28 16:13:17

标签: html css

我在h1之前和之后都有图像。

但是,我需要将其定位如下:

  • h1文字应位于中心
  • h1之前,图片应从容器的左侧开始
  • h1图像应该在容器右侧结束

我无法得到所有这些。例如,我可以居中h1文本,但之后是h1之前和h1之后图像未正确对齐。这是jsfiddle示例:http://jsfiddle.net/wK8ve/

此外还有Html和CSS:

CSS:

.test {
    border: 1px solid black;
    width: 1000px;
    height: 200px;
}
h1 {
    text-align: center;
    font-size: 22px;
    font-weight: bold;
    color: #5d5d5d;
    margin: 0 0 32px 0;        
}

h1:before {
    text-align: left;
    background-image: url('http://modeles-de-lettres.org/test/images/h_ltr.png');
    background-repeat: no-repeat;     
    background-position: center left;   
    padding: 0 352px 0 0;
    content: "\00a0";
}

h1:after {
    background-image: url('http://modeles-de-lettres.org/test/images/h_rtl.png');
    background-repeat: no-repeat;    
    background-position: center right;
    padding: 0 180px;
    content: "\00a0";
}

HTML:

<div class="test">
    <h1>Test</h1>
</div>

这是现场示例:http://modeles-de-lettres.org/test/index.php?p=about_us

2 个答案:

答案 0 :(得分:1)

使用float

在您之前和之后的部分中,您需要

http://jsfiddle.net/wK8ve/1/

h1:before {
    text-align: left;
    background-image: url('http://modeles-de-lettres.org/test/images/h_ltr.png');
    background-repeat: no-repeat;     
    background-position: center left;   
    padding: 0 352px 0 0;
    content: "\00a0";
}

h1:after {
    float: right;
    background-image: url('http://modeles-de-lettres.org/test/images/h_rtl.png');
    background-repeat: no-repeat;    
    background-position: center right;
    padding: 0 180px;
    content: "\00a0";
}

我假设这是你想要的,如果不澄清的话。

答案 1 :(得分:1)

我想这就是你想要的。我使用img代替:before:after

http://jsfiddle.net/wK8ve/2/

相关问题