Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline CSS formatting best practices - Two questions

Tags:

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> 
like image 942
webworm Avatar asked Apr 20 '11 15:04

webworm


People also ask

Is it good practice to use inline CSS?

Inline styles, while they have a purpose, generally are not the best way to maintain your website. They go against every one of the best practices: Inline styles don't separate content from design: Inline styles are exactly the same as embedded font and other clunky design tags that modern developers rail against.

Which is the correct format for inline CSS?

Usually, CSS is written in a separate CSS file (with file extension . css ) or in a <style> tag inside of the <head> tag, but there is a third place which is also valid.

Why is it best practice to avoid the use of important and inline styles?

Inline styles don't separate content from design. Inline styles cause more maintenance headaches. Inline styles are not as accessible. Inline styles make your pages bigger.

What is inline CSS disadvantages?

Disadvantages of Inline CSSThese styles cannot be reused anywhere else. These styles are tough to be edited because they are not stored at a single place. It is not possible to style pseudo-codes and pseudo-classes with inline CSS. Inline CSS does not provide browser cache advantages.


1 Answers

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.

like image 153
Šime Vidas Avatar answered Nov 04 '22 23:11

Šime Vidas