Inline CSS formatting best practices - Two questions - css

Question #1 - When specifying an inline style in an HTML element, is it necessary to include a trailing semi-colon? For example ...
<div style="padding:10px;">content</div>
Question #2 - When specifying an inline style should a space be inserted after the colon separating attribute name from attribute value?
<div style="padding: 10px;">content</div>
vs.
<div style="padding:10px;">content</div>

Answer #1: No.
Semi-colons are required only between declarations.
A declaration-block (also called a
{}-block in the following text) starts
with a left curly brace ({) and ends
with the matching right curly brace
(}). In between there must be a list
of zero or more semicolon-separated
(;) declarations.
Source: http://www.w3.org/TR/css3-syntax/#rule-sets
The value of the style attribute must
match the syntax of the contents of a
CSS declaration block (excluding the
delimiting braces)
Source: http://www.w3.org/TR/css-style-attr/#syntax
Since you have only one declaration, there is nothing to separate, so no semicolons are needed.
However, the CSS syntax allows for empty declarations, which means that you can add leading and trailing semicolons as you like. For instance, this is valid CSS:
.foo { ;;;display:none;;;color:black;;; }
and is equivalent to this:
.foo { display:none;color:black }
Answer #2: No.
A declaration is either empty or
consists of a property, followed by a
colon (:), followed by a value. Around
each of these there may be whitespace.
Source: http://www.w3.org/TR/css3-syntax/#declarations
You can add spaces in order to improve readability, but they have no relevance.

Q1: No, but I always include a trailing semicolon. Some years ago that semicolon could be a reason to a wrong render (or lack of) by some browsers. I guess nowadays is not a problem.
Q2: No, both ways means the same. Your election to include an space after the colon should be based on personal preferences for legibility.

Question 1: Yes (if you have more than one inline-style specified. Even it's not required for the last one, it's a good practice to append ; after each one).
Quote:
The normal rules of CSS apply inside
the style attribute. Each CSS
statement must be separated with a
semicolon ";" and colons appear
between the CSS property and its
value.
Question 2: No, but you can add it to be easier to read. For instance, Eclipse formatting automatically adds this space.

Question 1: Not required for your first question as written, but you would need to have the semi-colon if multiple definitions were present.
Question 2: Spaces are not required unless you are separating values in a specific property, such as: box-shadow:0 0 5px 0 #000;
One reason that you may want to add them in anyway, at least in a CSS-file context, would be that if you ever needed to run the CSS through a post-processor, like Sass, not having the semi-colons at the end of the line will cause the compiler to fail.
In summary, then: For inline styles, the above answers apply, but for CSS in separate files on the file system, I would always add the extra semi-colons and spaces to make it easier to read. You can always run your CSS through a compressor when you are ready for production.

Related

CSS Rule Set Selector

I recently completed an assignment that had this question:
Within a CSS rule set, a selector includes
a selector and a declaration block
a selector and a value
a property and a selector
a property and a value
The correct answer given was option 4.
I don't quite agree with the answer. In fact, none of the answers seem correct to me, but I wanted to make sure I wasn't misunderstanding the question.
I would appreciate if someone could confirm if the question is flawed or explain the answer if it was indeed correct.
As W3C (World Wide Web Consortium says, a selectors are:
patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in an XML document.
Mozilla Developer Network describes CSS rules here.
Indeed, a CSS rule includes a selector, a property and a value.
Personally, the sintax of the question is a bit awkward, but it is not flawed.
In CSS A "rule set" (or just "rule") consist of a "selector" and a "declaration block".
A "declaration block" consists of "declarations" between curly brackets.
Each "declaration" contains a "property", a colon and a "value" separated from each other within the "declaration block" by a semi-colon.
So you are right. None of the options match, and option 4 is the definition of a "declaration", not a "selector".
See 4.1.7 Rule sets, declaration blocks, and selectors and 4.1.8 Declarations and properties
It's correct:
div { margin: 0; }
The whole line is a rule set
div is a selector
margin is a property
0 is a value

Hanging soft hyphens

I found this article about hanging punctuation:
http://webdesign.tutsplus.com/articles/getting-the-hang-of-hanging-punctuation--cms-19890
and this link in the comments: http://dabblet.com/gist/9623025
It's a really nice idea, and it would be an awesome way to hang soft hyphens generated by some tool like Hyphenator or Hypher. I started playing around with Hypher and encapsulated the hyphens in a span...
Then I realized I don't know how to style soft hyphens.
This code should make an hyphen red, but of course the one in span is not breaking a word, so the browser doesn't show it:
<style>
.box{
width:40px;
background-color:yellow;
}
.hyphen{
color:red;
}
</style>
<div class="box">
<p>aaaa­bbbb<span class="hyphen">­</span>cccc</p>
</div>
http://jsfiddle.net/5LazU/
Is there any way to do it?
It is somewhat unclear what the question is about, so I will address different interpretations of it.
The hyphens introduced by a browser’s automatic hyphenation (due to the use of the hyphens property) cannot be styled as separate entities (i.e., in a manner that differs from the style of the surrounding text), since they do not constitute elements or pseudo-elements.
Styling SOFT HYPHEN characters in HTML is a different issue, with browser-dependent answers. The answer does not depend on the method of entering SOFT HYPHEN (as a raw character vs. the ­ reference vs. a numeric reference), since they result in the same data in the DOM: But it depends on wrapping the character in an element, as in the question.
The code in the question causes a red hyphen to be displayed on Firefox and on IE, but on Chrome, there is no hyphen after “bbbb”, so I guess the code was tested on Chrome. This appears to be a bug: the element is treated as allowing a direct line break without inserting a hyphen.
The issue of “hanging punctuation” is something completely different. The reason why hanging punctuation techniques word for quotation marks (which is what the linked documents contain) is that the marks appear at the start and at the end of a segment of text. Browser-inserted hyphens are different, and the key thing there is that the browser analyzes how many characters will fit on a line before it inserts a hyphen (due to automatic hyphenation or as a result of rendering SOFT HYPHEN as a visible hyphen). I don’t see how there could be a way to intercept this unless the browser provides an API for the purpose.

Why does Notepad++ show some (valid?) CSS in black?

When I write CSS in Notepad++, the color coding sometimes seems inconsistent. Normally, selectors are shown in light purple but sometimes they are black for 1 or more lines consecutively. I don't see anything wrong with such lines. Why are they black? What am I missing here?
i'm not sure why that happened to you!?
but you can add keywords to notepad++ :
Setting => Style Configurator ..
Select your language and Style.
Add your keyword like color and etc , separated by space :
Usually, that sort of coloring indicates that the CSS rule immediately preceding the affected one hasn't been closed. Here's an example where I remove the closing brace from a rule in normalize.css, which affects the one that immediately follows in exactly the same way (ignoring the comment and the lack of bold type, of course):
Presumably then, the reason why the "first" declaration after that selector is affected but the subsequent ones are not is because the semicolon from the first declaration tells the syntax coloring parser to terminate the nonsensical statement which is formed by the selector. But I'm just blindly guessing.
If you're sure that the preceding rule has been closed properly, then the syntax coloring parser may have been confused. Try simply highlighting the rule, deleting it, and undoing; that usually works for me.
Since Notepad++ recognizes color of codes based on the language type, you can't able to view multiple languages with color codes in a same file. Even though CSS is a part of web designing, it is still a unique language. If you want to display the CSS codings inside the HTML to color, just change the language type to CSS (only for temporarily purpose). But, don't forget to revert the language conversion back to HTML before saving the file.
Steps: Language -> C -> CSS

Fix a CSS file: space after colon on rules but not selectors (Sublime Text 2)

OK--I have a HUGE css file, inherited from another designer. I'm in the process of cleaning it. One thing I need to do is add a space after colon for rules but not selectors. i.e.:
.selector:after {
foo: bar;
}
Where I'm adding a space after foo: but NOT after .selector (between selector: and after).
I'm assuming there's an easier way to do this than find/replace and manually/visually skipping the selector colons, but any advice is appreciated.
Why creating a regular expression would work very well. I think the easiest and quickest way would be to simple use your IDE's find and replace to add a space after each colon and then use find and replace again to remove the space from your pseudo classes. Since there are probably only around 8 different pseudo classes used it shouldn't take to long to prform a find a replace on each.
I ended up using ProCssor, this is so very cool, thanks SteveAx.
This would take about 5 seconds in Sublime text, I would do this:
Highlight : with the cursor
alt+f3 to select all instances of the colon
hit the right arrow key to jump to the right of the colon
add a space
If you want to "normalize" and make sure there are no double spaces after the colon, just add two spaces after one of the colons, and do alt+f3 to select all instances of it and correct it.

How can I select links that lead to urls ending with "/foo-bar"?

I want to style some tags on SE with custom colors based on their tag, using CSS only.
This shouldn't be too hard, for:
foo-bar
...should be easily matchable with a selector such as:
a.post-tag[href$=/foo-bar]
...where the slash would be required to match foo-bar but not baz-foo-bar.
However, it isn't that simple. Both the dash and the slash seem to prevent the selector from working. Luckily I can work around that by escaping characters, so / becomes \5c and - becomes \2d.
This doesn't seem to match anything either, however:
a.post-tag[href$=\5cfoo\2dbar]
...whereas a.post-tag[href$=foo\2dbar] does have some matches, but again too many of them.
How can I select an href ending in "/foo-bar" using CSS?
Simply encase the value in the attribute selector in quotes so you don't need to escape anything, just like how you quote attribute values in HTML:
a.post-tag[href$="/foo-bar"]
jsFiddle preview

Resources