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
Table of contents 1. React Basic Usage Notable Fe...
This article originated from my complaints about ...
In actual web page development, accordions also a...
Routing configuration commands under Linux 1. Add...
MySQL SQL statement performance tuning simple exa...
A simple cool effect achieved with CSS3 animation...
This article example shares the specific code of ...
This is because the database server is set to aut...
Vue stores storage with Boolean values I encounte...
1. Introduction to KVM The abbreviation of kernel...
Because I want to write a web page myself, I am al...
The image can be saved on hub.docker.com, but the...
introduction Have you ever encountered a situatio...
Table of contents 1. Block scope 1.1. let replace...
Trigger Introduction A trigger is a special store...