简体   繁体   English

对齐文字和背景图片

[英]Aligning text and background image

Please could someone help diagnose this tiny problem that shouldn't be one - trying to center-bottom align text below a background image which I've done many times, for some reason can't after hours of trying.. 请有人帮助诊断这个不应该是的小问题-尝试使文本在背景图像下方居中对齐,这是我已经做过很多次的尝试了,由于某种原因,经过数小时的尝试后还是无法解决。

It's the logos floated right in the footer here: http://www.inside-guides.co.uk/brentwood/pages/index.html 徽标浮在此处的页脚中: http : //www.inside-guides.co.uk/brentwood/pages/index.html

As you can see 'Member Of' is to the left of the image, just below. 如您所见,“ Member Of”位于图像的左侧,正下方。 here's the inline code for testing: 这是用于测试的内联代码:

     <div style="margin-right:15px;position:relative;float:right;width:88px;height:100px;background:url(/images/Structure/chamber-logo.png) no-repeat;background-position:50% 0"><span style="font-size:12px;position:absolute;bottom:0;margin:0 auto;text-align:center;">Member Of</span></div>

text-align:center does nothing on the span because its width is the same as it's text. text-align:center在跨度上不执行任何操作,因为其宽度与文本相同。 put that style on the outer div instead. 将该样式放在外部div上。

您可以将跨度更改为div:

<div style="font-size:12px; position:absolute;bottom:0; width:100%; text-align: center;">Member Of</div></div>

Here is my version of this pattern: 这是此模式的我的版本:

http://jsfiddle.net/audetwebdesign/svXkt/ http://jsfiddle.net/audetwebdesign/svXkt/

I added some background colors, some borders and exaggerated the width of the element containing the logo so as to show that it is centered. 我添加了一些背景颜色,一些边框,并夸大了包含徽标的元素的宽度,以表明其居中。

(1) You can center the background image by using the center value for the background-position property. (1)您可以通过使用background-position属性的center值来使background-position图像center This is good trick to avoid having to size your container to match the size of the particular logo. 这是一个好技巧,可以避免必须调整容器大小以匹配特定徽标的大小。

(2) The text label/caption is easily centered using text-align: center; (2)使用text-align: center;可以轻松地使文本标签/标题居中text-align: center; but this works only on a block level element, hence, use display: block when styling the span element. 但这仅适用于块级元素,因此,在对span元素进行样式设置时,请使用display: block

(3) Inherit the width of the span from the logo's parent container, you need to specify a width. (3)从徽标的父容器Inherit跨度的宽度,您需要指定一个宽度。

Of course, as suggested by SpliFF, you could add some padding to the top to move the text inline element down, and then use text-align. 当然,如SpliFF所建议,您可以在顶部添加一些填充以将文本内联元素向下移动,然后使用text-align。

I tend to use this pattern when I have a series of logos on a page, for example, a sponsors page. 当页面上有一系列徽标(例如,赞助者页面)时,我倾向于使用这种模式。

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

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