简体   繁体   English

具有两行的h1和h1之前和之后的图像

[英]h1 with two lines and image before and after h1

I have <h1> with image before and after it. 我在图像之前和之后都有<h1>

It works fine within all webpages, but now I have some additional headings with two lines. 在所有网页上都可以正常工作,但是现在我有一些额外的标题,包括两行。 So, I need to style <h1> with two lines. 因此,我需要用两行设置<h1>样式。

If I use the existing style with image before and after then text align is a problem. 如果我在图像前后使用现有样式,则文本对齐是一个问题。

You can test it in jsfiddle: http://jsfiddle.net/kw3KX/ Or see it in realtime webpage (a bit modified): http://modeles-de-lettres.org/test/ 您可以测试它的jsfiddle: http://jsfiddle.net/kw3KX/还是看实时网页(有点修改): http://modeles-de-lettres.org/test/

You can see that <h1> with two lines contains text "Some text here" and "about this site" and alignment is a problem. 您可以看到两行的<h1>包含文本“此处有文本”和“关于此站点”,并且对齐方式是个问题。

HTML: HTML:

<div id="content-wrapper">
     <h1>
            Some text here
        </h1>

</div>

CSS: CSS:

h1 {
    text-align: center;
    font-size: 22px;
    font-weight: bold;
    color: #5d5d5d;
    margin: 41px 0 32px 0;
}
h1:before {
    background-image: url('http://modeles-de-lettres.org/test/images/h_ltr.png');
    background-repeat: no-repeat;
    padding: 0 152px 0 0;
}
#content-wrapper {
    width: 1000px;
    margin: 0px auto;
    position: relative;
}

So is it possible to fix it somehow with <h1> tag? 那么是否可以通过<h1>标签对其进行修复?

This is beeing caused by the float you apply to the :before and :after elements. 这是由于您对:before:after元素应用了float引起的。

I would advise you to postion the images absolute instead. 我建议您将图像绝对放置。 This lifts the images out of the document flow, and they can no longer influence the other elements (and mess up your text). 这将图像从文档流中移出,它们将不再影响其他元素(并弄乱了您的文本)。 This way your layout will keep working, no matter the number of lines in your h1. 这样,无论h1中的行数如何,您的布局都将继续工作。

I updated your fiddle: http://jsfiddle.net/kw3KX/2/ 我更新了您的小提琴: http : //jsfiddle.net/kw3KX/2/

and the relevant css: 和相关的CSS:

h1 {
    text-align: center;
    font-size: 22px;
    font-weight: bold;
    color: #5d5d5d;
    margin: 41px 0 32px 0;
    position: relative; /* added so the position absolute will work */
}
h1:before {
    background-image: url('http://modeles-de-lettres.org/test/images/h_ltr.png');
    background-repeat: no-repeat;
    background-position: center left;
    padding: 0 317px 0 0;
    content:"\00a0"; 
    /* added */
    position: absolute;
    left: 0;
    top: 50%;
    margin-top: -7px; /* half the height of the image, to center it */
}
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 317px 0 0;
    content:"\00a0"; 
    /* added */
    position: absolute;
    right: 0;
    top: 50%;
    margin-top: -7px;  /* half the height of the image, to center it */
}

you may wrap the text in a span and do display: inline-block; 您可以将文本包裹在一个spandisplay: inline-block; for that span , and do some margin-top to adjust the alignment with the images: 对于该span ,并做一些页margin-top以调整与图像的对齐方式:

<h1>
        <span style="display:inline-block; margin-top:-16px;">Some text here<br>
        about this site</span>
</h1>

Use the white-space property: white-space:pre; 使用空白属性: white-space:pre;

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM