A string is any sequence of characters. For example, “Hello, World” is a string. In most programming languages and in CSS, strings are enclosed within either single or double quotation marks. A string is what is known as a data type. Data types are used to classify information. Integers, real numbers, and strings are examples of data types. Strings may contain text, numbers, symbols - any type of character. An integer can be a number that has a positive or negative value, and can only be a whole number, no decimals. A real number can have decimal places. These data types are made to conform to their defined rules by the language. Whereas a string can contain any character, real numbers are expected to be whole numbers or decimals; a string cannot appear where a real number is expected, and a real number cannot appear where an integer is expected, and so on.
One use of strings in CSS is to specify a font that contains spaces in its name.
font-family: "Times New Roman", Times, serif;
Font faces with spaces in the name are enclosed with quotations to keep the program that interprets CSS from getting confused. The quotes act as marking posts for where the font face’s name begins and ends.
Strings may also be used to include content in an HTML document from a style sheet. Try including content from a style sheet for yourself.
Example 2-4. To include content from a style sheet, follow these steps.
- Type in the following document:
- Save the document as Example_2-4.html.
- Open the example with Safari, Firefox, or Opera - IE 6 and IE 7 don’t support this feature. Figure 2-15 shows that the string “I said, “Hello, world!”” is inserted into the element using the content property.
How It Works
You included the string “I said, “Hello, world!”” in the HTML document by using the CSS content property.
Strings may contain any sequence of characters of any length (at least up to whatever arbitrary limit a browser may have defined) - even quotation marks are allowed. However, strings may contain quotation marks only if they’re escaped using another special character, the backslash character. When you escape quotation marks, you tell the browser: “Ignore the quotation mark; it is part of the string.” The backslash is used to quote Foghorn Leghorn in the following code:
the single quotes do not have to be escaped because double quotes enclose the string.