简体   繁体   English

将标签与背景图像对齐

[英]Aligning label with a background image

I have created a checkbox constructed of a div with checkbox image background and a label. 我创建了一个由div构成的复选框,其中div具有复选框图像背景和标签。

Html: HTML:

<div class="checkbox checked" ></div>
<label class="label">Some text</label>

and css: 和CSS:

.checkbox
{
    height: 16px;
    width: 16px;
    float:left;
    background: url(http://cdn1.iconfinder.com/data/icons/uidesignicons/checkbox_yes.png) no-repeat 0px 0px;
}

The problem is - the label is not aligned with the image, it sits slightly below the baseline of the image div and that looks ugly. 问题是-标签与图像不对齐,标签位于图像div基线的下方,看起来很难看。 You can see the jsfiddle of it here: http://jsfiddle.net/muzzzy/3hUGV/5/ 您可以在这里看到它的jsfiddle: http : //jsfiddle.net/muzzzy/3hUGV/5/

How can I align them, given that I can't specify absolute values for padding or line-height, because this is really a part of a javascript plugin that converts regular checkboxes, so labels can be of different font sizes. 考虑到我无法指定padding或line-height的绝对值,我该如何对齐它们,因为这实际上是javascript插件的一部分,该插件可以转换常规复选框,因此标签可以具有不同的字体大小。

UPDATE: I'm also including the image of how it looks now: 更新:我还包括它现在的外观的图像:

在此处输入图片说明

I'd like the text to be vertically centered with the center of the image 我希望文字以图片的中心垂直居中

If you dont care about older browsers, drop the float: left and try using display: inline-block; 如果您不关心较旧的浏览器,请将float: left然后尝试使用display: inline-block; on the div.checkbox. 在div.checkbox上。 After that, play with the vertical-align attribute. 之后,使用vertical-align属性。

Then do some cross-browser because for some of us it already looks ok! 然后做一些跨浏览器,因为对于我们中的某些人来说,它已经可以了!

Here the live example: http://jsfiddle.net/karameloso/hQ2uK/ 这里是实时示例: http : //jsfiddle.net/karameloso/hQ2uK/

You will be better off swapping out your div.checkbox for a span , and then setting the span to display:inline-block . 您最好将div.checkbox span ,然后将span设置为display:inline-block This way, you can use vertical-align:middle on the elements to get them centered (The only rare cases vertical-align has a use!) 这样,您可以在元素上使用vertical-align:middle来使它们居中(vertical-align很少用!)

JS Fiddle: http://jsfiddle.net/3hUGV/34/ JS小提琴: http : //jsfiddle.net/3hUGV/34/

Well for what you're asking, try a table: http://jsfiddle.net/3rRb6/ 很好,您可以尝试使用以下表格: http : //jsfiddle.net/3rRb6/

(Unless of course, you're one of those omg-im-not-using-a-table-for-layout!@$#?@$!?! type of people) (当然,除非您是omg-im-not-using-table-layout!@ $#?@ $!?!类型的人之一)

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

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