Stephen Tang wrote:

> I was recently taking a CSS online exam, and I ran into a question I
> have never encountered before.

This list is devoted to discussions on practical use of CSS, but let's 
imagine that long lines become a practical issue. Rare, but possible. A very 
long URL, used for background-image, comes into my mind.

> The multiple choice question question basically asked, "If a CSS rule
> is too long on a single line, what character do you use to tell the
> style sheet processors that the rule is continued on the next line?"
> The possible answers were -, >, \, /, \\.
>
> I couldn't find anything the explicitly answer this question in the
> W3C CSS spec, but I am wondering if the question was not quite
> detailed enough.

I think you found the right answer in a reasonable explicit formulation:

> I found these two links, which makes me think the answer is backslash
> (\), used on long strings in CSS rules.
> http://www.w3.org/TR/CSS21/syndata.html#characters
> http://www.w3.org/TR/CSS21/syndata.html#strings

The first one says, among other things:
"First, inside a string, a backslash followed by a newline is ignored (i.e., 
the string is deemed not to contain either the backslash or the newline)."
So yes, the backslash "\" - or "reverse solidus" to use its Unicode name - 
is the answer.

> Does this agree with any of your experiences?

Yes, though I've never really needed the backslash in CSS. But I've tested 
that the example I give of its use in my CSS book actually works in 
browsers.

> Originally, I kept
> thinking the question was about a series of long CSS rules on one line
> (i.e. display: inline; padding-top: 2px;), but a semicolon would work
> there, and it was not one of the answer choices.

The semicolon is not special as regards to line breaks. In CSS, a line break 
(when not preceded by "\") is a whitespace character, hence equivalent to a 
space. That is, you can break a line wherever you could use a space. A space 
is allowed, for example, before or after a semicolon. Basic symbols such as 
"display", "inline", "padding-top", and "2px" must not contain a space, 
however.

So the exam question was misleading, if not incorrect. However, it makes 
sense if we add the word "immediately". When you have "\" at the end of a 
line, then the style sheet continues at the start of the next line as if the 
"\" and the line break were not there. So it allows you to break even basic 
symbols and, more realistically, URLs in property values.

CSS as such allows indefinitely long lines, and I don't think browsers have 
problems with them either. The reason for using line breaks is readability. 
For a long URL, for example, the practical problem might be that the program 
you use for writing CSS does not handle long lines well and might even have 
a line length limit.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/ 

______________________________________________________________________
css-discuss [[email protected]]
http://www.css-discuss.org/mailman/listinfo/css-d
List wiki/FAQ -- http://css-discuss.incutio.com/
List policies -- http://css-discuss.org/policies.html
Supported by evolt.org -- http://www.evolt.org/help_support_evolt/

Reply via email to