In Firefox or IE, the span cannot determine the width of the solution _ HTML/Xhtml _ webpage Creation

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.