After setting the width of span, it is found that it is invalid after the experiment. Both Firefox and IE are invalid because the width attribute in CSS is not always valid. You may find a solution after reading this article.
The Code is as follows:
Test Span
Fixed width span
After the test, it is found that the content is invalid, both in Firefox and IE.
By looking at the width definition in the CSS2 standard, we found that the width attribute in the original CSS is not always valid. if the object is an inline object, the width attribute will be ignored. Firefox and IE follow the standard.
Changing span to block type and setting float is not perfect
Add the display attribute to the CSS of span and set span to block-type Element. The width is indeed valid, but the text before and after is separated into different rows. In this case, the span is actually changed to p.
The Code is as follows:
Span {background-color: # ffcc00; display: block; width: 150px ;}
Many people will suggest adding another CSS attribute float, which can solve the problem under certain conditions. For example, in our example, if there is no text before span, it is indeed feasible. But if there is, the front and back texts will be connected together, And span runs to the second line.
The Code is as follows:
Span {background-color: # ffcc00;
Display: block; float: left; width: 150px ;}
Perfect solution for setting span width
The CSS definition in the following code perfectly solves the problem of span width setting. Because browsers usually ignore unsupported CSS attributes, it is best to write the display: inline-block row in the back, so that in Firefox, if Firefox 3 is in the future, this line works, and the code can be compatible with various versions at the same time.
The Code is as follows:
Test Span
Fixed width span