Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS fixed width in a span

Tags:

html

css

Within an unordered list:

<li><span></span> The lazy dog.</li> <li><span>AND</span> The lazy cat.</li> <li><span>OR</span> The active goldfish.</li> 

Adding a class or style attribute is permitted but padding the text and adding or changing tags is not allowed.

The page is rendering with Courier New.

Goal is to have text after span lined up.

    The lazy dog. AND The lazy cat. OR  The active goldfish. 

Justification of the "OR" is unimportant.

The lazy animal text may be wrapped in an additional element but I'll have to double check.

like image 728
jason saldo Avatar asked Nov 02 '08 22:11

jason saldo


People also ask

Can you set width on a span?

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.

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.

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.

Can a span have padding?

span won't have padding because it is an inline element by default, so set inline-block or block.


1 Answers

In an ideal world you'd achieve this simply using the following css

<style type="text/css">  span {   display: inline-block;   width: 50px; }  </style> 

This works on all browsers apart from FF2 and below.

Firefox 2 and lower don't support this value. You can use -moz-inline-box, but be aware that it's not the same as inline-block, and it may not work as you expect in some situations.

Quote taken from quirksmode

like image 58
codeinthehole Avatar answered Sep 23 '22 04:09

codeinthehole