Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a background image to display in a blank div element

Tags:

css

I'm looking to use a blank div element to display a background image but nothing is displaying unless I put something (i.e., some text) in the div element.

I need to do this because I want to display a graphic next to some text but have the graphic vertically aligned with the text.

How do you get a background image to display in a blank div?

My Google-fu has failed me.

<div class="photoInfo">
lorem ipsum | 
<div style="background:url('foo.gif') no-repeat;display:inline;"></div>
</div>

Thanks!

edit: The code below actually displays the image but I just need to nick it down like 2 or 3 pixels to have it perfectly aligned.

That's what I'm trying to achieve, any ideas?

<div class="photoInfo">
    Added<span class="spacer">•</span>Wed Apr 01, 2009 1:01 pm<span class="spacer">•</span>64.10 KB<span class="spacer">•</span>659x878 pixels
    <span class="spacer">•</span>
    <img src="/_assets/img/icons/new-photo-small.gif" />
</div>
<!--<div style="height:14px;width:31px;background:url('') no-repeat 0px 5px;display:inline-block;"></div>-->

.photoInfo
{
font-size:0.6em;
color:#b0bad9;
padding-bottom:15px;
text-align:center;
}
like image 519
Tom Avatar asked Apr 02 '09 19:04

Tom


People also ask

How to add a background image to an empty Div?

Since the div is empty, there's no content to push it "open" leaving the div to be 0px tall. Set explicit dimensions on the div and you should see the background image.

How to center a background image inside a <div> element?

If you want to center your background image inside a <div> element and display the whole image without any of its part being clipped or stretched, try the following example, where we set the background-size property to its "contain" value.

How to add a background image using jQuery?

In simple words, we need to create an empty div and on click event, the empty div will get the image as a background. CSS (): This is an inbuilt method in jQuery to add the CSS property but in this case, we need to provide complete property using the URL () method.

What does the <Div> background image property do?

This property applies one or more background images to an element, like a <div>, as the documentation explains. Use it for aesthetic reasons, such as adding a textured background to your webpage.


1 Answers

Give this a shot!

one | <span style="padding-left: 20px; background: url('16x16-image.gif') no-repeat; height: 16px">two</span> | three

If you use a div instead of span, you'll need to set display: inline in the style as well.

like image 148
jyustman Avatar answered Oct 15 '22 20:10

jyustman