Solution to span width not being determined in Firefox or IE

Solution to span width not being determined in Firefox or IE

Copy code
The code is as follows:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test Span</title>
<mce:style type="text/css"><!--
span {
background-color:#ffcc00;
width:150px;
}
--></mce:style><style type="text/css" mce_bogus="1">span {
background-color:#ffcc00;
width:150px;
}</style>
</head>
<body>
fixed <span >width</span> span
</body>
</html>

After testing, I found that it is invalid, whether in Firefox or IE.

By consulting the definition of width in the CSS2 standard, it is found that the 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 to follow the standards.

Changing span to block type and setting float is not a perfect solution

Add the display attribute to the span's CSS and set the span to a block-type element. This will indeed change the width, but will also put the preceding and following text in different lines. In this way, span actually becomes div completely.

Copy code
The code is as follows:

span { background-color:#ffcc00; display:block; width:150px;}

Many people would suggest adding a CSS property float, which can indeed solve the problem under certain conditions. For example, in our example, if there is no text before the span, it is indeed feasible. But if it is there, the text before and after will be connected together, and the span will run to the second line.

Copy code
The code is as follows:

span { background-color:#ffcc00;
display:block; float:left; width:150px;}

The perfect solution for setting span width

The CSS definition in the following code perfectly solves the problem of setting the span width. 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 Firefox 3 is released in the future, this line will work and the code can be compatible with various versions at the same time.

Copy code
The code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>Test Span</title>
<mce:style type="text/css"><!--
span { background-color:#ffcc00; display:-moz-inline-box; display:inline-block; width:150px;}
--></mce:style>
<style type="text/css" mce_bogus="1">span { background-color:#ffcc00; display:-moz-inline-box; display:inline-block; width:150px;}</style>
</head>
<body>
fixed <span>width</span> span
</body>
</html>

<<:  Detailed process of getting started with docker compose helloworld

>>:  Common DIV tasks (Part 2) — Transform into editors and various DIY applications of DIV

Recommend

Linux bridge method steps to bridge two VirtualBox virtual networks

This article originated from my complaints about ...

Native js to achieve accordion effect

In actual web page development, accordions also a...

MySQL SQL statement performance tuning simple example

MySQL SQL statement performance tuning simple exa...

Sample code for cool breathing effect using CSS3+JavaScript

A simple cool effect achieved with CSS3 animation...

Vue+Element realizes paging effect

This article example shares the specific code of ...

Vue storage contains a solution for Boolean values

Vue stores storage with Boolean values I encounte...

Use a table to adjust the format of the form controls to make them look better

Because I want to write a web page myself, I am al...

Detailed explanation of the construction and use of Docker private warehouse

The image can be saved on hub.docker.com, but the...

Briefly understand the MYSQL database optimization stage

introduction Have you ever encountered a situatio...

Detailed explanation of JS ES6 coding standards

Table of contents 1. Block scope 1.1. let replace...

Introduction and use of triggers and cursors in MySQL

Trigger Introduction A trigger is a special store...