Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an image in span without converting it to a block level element

I am a little confused over here. I have a txtbox and to the right of the txtbox, i want to insert a help icon. i have inserted that into a span. the problem is that i dont want to make this span a block because it goes down then. is there any other way to display this image without turning this into a block level element?

The Code goes here:

<input name="firm" type="text" id="firm" size="20" /><span id="helpico"></span>

The CSS:

#helpico{ background:url(images/question.png) left top; width:16px; height:16px;}
like image 915
themajiks Avatar asked Feb 22 '11 11:02

themajiks


People also ask

Can you put an image in a span?

Usually span element is used to group inline elements together to implement style (using id or class attributes) or language information or JavaScript DOM(using id or class attributes), when no other element is found suitable to form that group. 3. The element can contain a piece of text or an image directly.

Is span tag a block element?

<span> is very much like a <div> element, but <div> is a block-level element whereas a <span> is an inline element.

What is the default display type for a span element?

Since span elements are rendered using display: inline by default, the two width CSS properties are ignored.

How do you change inline to block element?

You can change the visual presentation of an element using the CSS display property. For example, by changing the value of display from "inline" to "block" , you can tell the browser to render the inline element in a block box rather than an inline box, and vice versa.


1 Answers

You can use this CSS:

#firm 
{
   float: left;
}
#helpico
{ 
   background:url(images/question.png) left top; 
   width:16px; 
   height:16px;
   display: block;
   float: left;
}

You have to use display block to enable widths and heights and to accept other styles. But to counter your issue of it "going down" you can set both the elements to float left and they will remain inline within the parent element.

Be aware that if the icon is relative to the content, it should be included with <img> tag and an alt attribute.


After seeing that you don't want to use floats OR display block, there is only one way left, use display: inline-block;.

Example for you here. :)

like image 50
Kyle Avatar answered Oct 11 '22 23:10

Kyle