<br><div class="msgheader"> <div class="right"><span style="CURSOR: pointer" onclick="copycode(getid('phpcode62'));"><u>Copy code</u></span></div>The code is as follows:</div> <div class="msgborder" id="phpcode62"> <br><html xmlns="http ://www. w3.org/1999/xhtml " > <br><head> <br><title>Test Span</title> <br><mce:style type="text/css"><! -- <br />span { <br />background-color:#ffcc00; <br />width:150px ; <br />} <br />--></mce:style><style type="text/ css" mce_bogus="1">span { <br>background-color:#ffcc00; <br>width:150px; <br>}</style> <br></head > <br>< ;body> <br>fixed <span >width</span> span <br></body> <br></html> <br> </div> <br>After passing the test, it was found that it was invalid. It doesn't work either in Firefox or IE. <br><br>By consulting the definition of width in the CSS2 standard, we found that the original width attribute in CSS is not always valid. If the object is an inline object, the width attribute will be ignored. Firefox and IE originally did this by following standards. <br><br><strong>Modifying span to block type and setting float is not a perfect solution</strong> <br><br>Add the display attribute in span’s CSS and set span to block type Element so that the width is correct It works, but it also separates the preceding and following text on different lines. In this way, span actually becomes a div. <br><br><div class="msgheader"> <div class="right"><span style="CURSOR: pointer" onclick="copycode(getid('phpcode63'));"><u>Copy code </u></span></div>The code is as follows:</div> <div class="msgborder" id="phpcode63"> <br>span { background-color: #ffcc00; display:block; width:150px;} <br> </div> <br>Many people will suggest adding another CSS attribute float, which can indeed solve the problem under certain conditions. For example, in our example, if there is no text in front of span, it is indeed feasible. But if there is, the text before and after will be connected together, and span will run to the second line. <br><br><div class="msgheader"> <div class="right"><span style="CURSOR: pointer" onclick="copycode(getid('phpcode64'));"><u>Copy code </u></span></div>The code is as follows:</div> <div class="msgborder" id="phpcode64"> <br>span { background-color: #ffcc00; <br>display:block; float:left; width:150px;} <br> </div> <br><strong>Perfect solution for setting span width</strong> <br><br>The CSS definition of the following code is perfect Solved the problem of span width setting. Since browsers usually ignore unsupported CSS properties, it is best to write the display:inline -block line at the end, so that in Firefox, if the future Firefox 3 is reached, this line will work. The code Can be compatible with various versions at the same time. <br><br><div class="msgheader"> <div class="right"><span style="CURSOR: pointer" onclick="copycode(getid('phpcode65'));"><u>Copy code</u></span></div>The code is as follows:</div> <div class="msgborder" id="phpcode65"> <br><!DOCTYPE html PUBLIC "-/ /W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br><html xmlns="http:// www.w3.org/1999/xhtml" > <br><head><title>Test Span</title> <br><mce:style type="text/css"><!- - <br />span { background-color:#ffcc00; display:-moz-inline-box; display:inline-block; width:150px;} <br />--></mce:style> <br> <style type="text/css" mce_bogus="1">span { background-color:#ffcc00; display:-moz-inline-box; display:inline-block; width:150px;}</style> ; <br /></head> <br><body> <br>fixed <span>width</span> span <br></body> <br></html> <br> </div>