Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - width not honored on span tag

I have a issue with one of my spans. Please consider the following styles:

.form_container .col1che
{
float: left; 
width: 4%; 
text-align: left;    
}

.form_container .col2che
{
float: left; 
width: 80%; 
text-align:left;
}

.form_container .col3che
{
float: right; 
width: 13%; 
text-align:right;
}

These 3 spans in my code:

<div class="row"><!-- start: "row" -->
     <span class="col1che">        
          <?php  //some db feeds    ?>        
     </span>
     <span class="col2che">
          <?php  //some db feeds    ?>
     </span>
     <span class="col3che">
          <?php  //some db feeds    ?>
     </span>
     <div class="clear"></div>
</div><!-- end: "row" -->

The problem occurs when there is no data to be displayed in "COL1CHE" (or maybe even in any of them although I'm not sure) when there is no data this span "collapses" and its width is not honored. This happens in Firefox, in IE it doesn't "collapse".

I included a "clear" class to see if this helps but to no avail.

Any ideas?

like image 214
chicane Avatar asked Jul 21 '09 20:07

chicane


People also ask

Can we add width to span tag?

The main thing to remember about a span element is that it is an inline element, which means that you will not be able to set the width or height of it as is.

Can we apply CSS on span?

The <span> tag is an inline container used to mark up a part of a text, or a part of a document. The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.

How do I fix the width in CSS?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.


3 Answers

Span is an inline element and you can therefore not set a width. To set a width you must first set it to a block element with

display:block; 

After that you can set a width. Since span is a native inline element, you can use inline-block too and it will even work in IE:

display:inline-block; 
like image 159
richard Avatar answered Sep 17 '22 13:09

richard


Width is not honored because span is an inline element. To fix this, add:

display: inline-block;

Depending on your situation, display: inline-block may be better than display: block because any content after the span won't be forced onto a new line.

like image 44
jimyi Avatar answered Sep 21 '22 13:09

jimyi


display: block will not help you here because when float is activated, the elements is automatically switched to block mode, whatever its initial mode, so your width should work.

The problem may be with the empty <span> tag which might not be rendered because of some obscure rule I cannot remember. You should try preventing your span from being empty, maybe by adding a &nbsp; if the content is empty.

like image 40
Vincent Robert Avatar answered Sep 20 '22 13:09

Vincent Robert