简体   繁体   English

如何将背景图片与h1中居中文字的左侧对齐?

[英]How can I align a background image to the left of centered text in an h1?

I need to align an image just to the left of some text in an h1. 我需要将图像对齐到h1中某些文本的左侧。 So imagine it's something like: 想象一下,就像这样:

[logo image] Company [徽标图片]公司

So it isn't really a "background"--it's more of a foreground image. 因此,它实际上不是“背景”,而是更多是前景图像。

This works if the h1 is left-aligned, using padding to offset the text from the image: 如果h1左对齐(使用填充使图像中的文本偏移),则此方法有效:

h1 {
  background: url("logo.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);
  line-height: 70px;
  padding-left: 80px;
}

However, I also want to center the text. 但是,我也想将文本居中。 I can do that as follows, but the image is still left-aligned: 我可以按照以下步骤进行操作,但是图像仍然是左对齐的:

h1 {
  background: url("logo.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);
  line-height: 70px;
  padding-left: 80px;
  text-align: center;
}

In other words, the text is in the center of the screen, but the image is still on the left margin. 换句话说,文本在屏幕的中央,但是图像仍在左边缘。

How can I keep the image centered, but just to the left of the text? 如何使图像居中,但仅在文本左侧? I don't want to get too tricky with absolute pixel values, or a fixed-width h1, as this all needs to be relative and responsive. 我不想对绝对像素值或固定宽度h1太棘手,因为所有这些都需要相对和响应。

I can't center the background image, as it needs an offset for the h1 text, which needs to scale. 我无法使背景图像居中,因为它需要h1文本的偏移量,该偏移量需要缩放。

Should I just slap an img tag in there and be done with this? 我是否应该在其中打一个img标签并完成此操作?

Another option would be to make the whole thing an image, but I want the text to scale--it's actually more of a caption than a logo. 另一个选择是将整个图片制作成图像,但是我希望文本可以缩放-实际上,它更像是标题而不是徽标。

[Note: I googled a bunch but most of the links deal with the "centered h1 over a large background image" scenario, which this isn't. [注意:我在Google上搜索了一堆,但是大多数链接都涉及“在大背景图像上居中放置h1”的情况,但事实并非如此。 Thanks!] 谢谢!]

UPDATED 更新

h1 {
    background: no-repeat scroll 0 0 rgba(0, 0, 0, 0);
    line-height: 70px;
    text-align: center;
}
h1:before {
    content: url("logo.png");
    float: left;
}

DEMO: http://jsfiddle.net/4HQ8T/3/ 演示: http : //jsfiddle.net/4HQ8T/3/

I learned something new about the content CSS here, thanks! 我在这里学到了一些有关content CSS的新知识,谢谢!

Since you know how big everything is you could try it with background-position porperty and then give % value to x. 由于您知道所有内容的大小,因此可以使用背景位置属性进行尝试,然后将%值赋予x。

h1 {
background: url("logo.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);
line-height: 70px;
padding-left: 80px;
text-align: center;
background-position:20% (e.g.);
}

But to be honest, i would recommend using an image tag. 但老实说,我建议使用图像标签。 You can give that image some responsiveness like done in bootstrap Bootstrap 您可以赋予该图像一些响应性,就像在Bootstrap Bootstrap中完成的一样

Here's my approach: 这是我的方法:

h1 {
    text-align: center;
}
h1::before {
    content: url("http://placekitten.com/200/80");
    display: inline-block;
    vertical-align: middle;
    height: 80px;
    width: 200px;
    margin-left: -200px;
}

Since you know the image dimensions, you can play with negative margin-left . 由于您知道图像的尺寸,因此可以使用负的margin-left进行播放。 As you can see, the text alignment is NOT affected by the image. 如您所见,文本对齐方式不受图像影响。 Is that what you wanted to achieve? 这就是您想要实现的目标吗?

Here's the jsFiddle demo 这是jsFiddle演示

there are several options already, and this one comes with an "IF" but i'm adding it here for variety. 已经有几种选择,并且这个带有“ IF”,但是我在这里添加了多样性。

If you can specify the width of the H1 tag then you can use padding and margin:auto to achieve this. 如果可以指定H1标签的宽度,则可以使用paddingmargin:auto来实现。

h1#elementID {
    background-image: url("img.png");
    background-repeat: no-repeat;
    margin: auto;
    width: 165px;
    padding-left: 45px;
}

adjusting the width / padding to fit your text image. 调整宽度/填充以适合您的文本图像。 It may improve scaling if you use something besides px as your sizing mechanism. 如果您使用除px以外的其他内容作为大小调整机制,则可能会改善缩放比例。

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

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