How do I have a non-tiling (non-repeating) background image?
With CSS, you can use the background-repeat property. The background repeat can be included in the shorthand background property, as in this example:
body {
background: white url(example.gif) no-repeat ;
color: black ;
CSS is clearly very useful for separating style from content. But apparently people tend to have problems when using it for layouts. Would you say this is because people have not yet understood how to properly do layout in CSS, or is it CSS that is lacking in this area? What can be done to improve the situation? --- Would the web benefit from HTML and CSS being complemented with some kind of "layout language"?
Layout and style should be tackled by the same language and the two are intertwined. Trying to split the two is like splitting the HTML specification in two, one specification describing inline elements and the other describing block elements. It's not worth the effort. CSS is capable of describing beautiful and scalable layouts. The CSS Zen Garden has been a eye-opening showcase of what is possible today. If MS IE had supported CSS tables, another set of layouts would have been possible. So, there is still lots of potential in the existing CSS specifications which should be the next milestone.
I always wanted to have "included" substyles or "aliases" in my CSS definition, to save redundancy.
(For includes)
.class1 { color:#ff0000; }
.class2 { background-color:#ffffff; }
.class3 { include:class1,class2;font-weight:bold; }
(For aliases)
@alias color1 #ff0000;
@alias color2 #ffffff;
@alias default_image url('/img/image1.jpg');
.class1 { color:color1; }
.class2 { background-image:default_image;background-color:co lor2; }
This way we could change colors or images for a whole webpage
by editing a reduced number of lines.
Had you considered any of these ideas in the past? If so, why were they rejected?
Yes, aliases and constants have been considered. CSS is already an indirection. Instead of putting properties and values directly on elements, it associates properties and values with selectors. What you (and others) are proposing is to add another layer of indirection. By doing so, one could possible write shorter, more manageable style sheets. However, there are also some downsides. It requires a new syntactic construct (@alias) and implementations must be able to remember a list of aliases. What if aliases are defined in one style sheet and referenced in another -- should that work? If so, what if the first style sheet isn't available?..
Styles not showing?
There are different ways to apply CSS to a HTML document with a stylesheet, and these different ways can be combined:
* inline (internal) (Deprecated for XHTML)
* embedded (internal)
* linked (external) and
* @import (external)
Note: An external stylesheet is a text file that contains only CSS Styles. HTML comments are not supposed to be in there and can lead to misinterpretation (> is the CSS "Child" selector!).
How do I quote font names in quoted values of the style attribute?
The attribute values can contain both single quotes and double quotes as long as they come in matching pairs. If two pair of quotes are required include single quotes in double ones or vice versa:
<P STYLE="font-family: 'New Times Roman'; font-size: 90%">
<P STYLE='font-family: "New Times Roman"; font-size: 90%'>
It's been reported the latter method doesn't work very well in some browsers, therefore the first one should be used.
Why is my external stylesheet not working ?
There may be several different reasons behind that, but one very common mistake is to have an external stylesheet that contains HTML markup in some form.
An external stylesheet must contain only CSS rules, and if required, correctly formed CSS comments; never include any HTML syntax, such as <style type="text/css">…
CSS comments are defined as anything that is placed between
/* (the comment start mark) and
*/ (the comment end mark). I.e. as follows…
/* This text right here is a correct CSS comment */
CSS comments may span multiple lines in the stylesheet. Nesting of CSS comments is not allowed.
Another reason for external stylesheets (and even embedded and inline stylerules) not to function as expected may be that you have tried to make use of some CSS-features that are not supported in the browser you are using.
External stylesheets shall also be served from the www-server with a MIME-type of 'text/css' in its 'Content Type:' HTTP header.
You may need to negotiate with your server admin to add this MIME type to your server if you are not able to configure the server yourself.
What can be done with style sheets that can not be accomplished with regular HTML?
Many of the recent extensions to HTML have been tentative and somewhat crude attempts to control document layout. Style sheets go several steps beyond, and introduces complex border, margin and spacing control to most HTML elements. It also extends the capabilities introduced by most of the existing HTML browser extensions. Background colors or images can now be assigned to ANY HTML element instead of just the BODY element and borders can now be applied to any element instead of just to tables. For more information on the possible properties in CSS, see the Index DOT Css Property Index.
How do I make my div 100% height?
You need to know what the 100% is of, so the parent div must have a height set. One problem that people often come up against is making the main page fill the screen if there's little content. You can do that like this :
CSS
body, html {
height:100%;
}
body {
margin:0;
padding:0;
}
#wrap {
position:relative;
min-height:100%;
}
* html #wrap {
height:100%;
}
Here, the #wrap div goes around your whole page - it's like a sub-body.
You need to use 'min-height' rather than 'height' for Firefox because otherwise it will set it to 100% of the viewport and no more. Internet Explorer, being well... crap, treats 'height' as it should be treating 'min-height' which it doesn't recognise. (You can target IE by preceding your code with ' * html ').
To make floated divs within this #wrap div 100% of the #wrap div... well that's more difficult. I think the best way is to use the 'faux columns' technique which basically means that you put the background in your body rather than your columns. If the body has columns and your floats don't then it looks like your floated content is in a column that stretches to the bottom of the page. I've used this technique in my layout demos.
The problem is often not that the columns aren't 100% height, but that they're not equal lengths. Columns usually don't start from the top of the page and end at the bottom - there's often a header and a footer or sometimes, more interesting designs don't have a recognisable columnar layout, but do require div boxes to be equal heights. This can be done with the aid of a couple of images and some css or with some javascript.
What is property?
Property is a stylistic parameter (attribute) that can be influenced through CSS, e.g. FONT or WIDTH. There must always be a corresponing value or values set to each property, e.g. font: bold or font: bold san-serif.
How do I write my style sheet so that it gracefully cascades with user's personal sheet ?
You can help with this by setting properties in recommended places. Style rules that apply to the whole document should be set in the BODY element -- and only there. In this way, the user can easily modify document-wide style settings.
What are pseudo-elements?
Pseudo-elements are fictional elements that do not exist in HTML. They address the element's sub-part (non-existent in HTML) and not the element itself. In CSS1 there are two pseudo-elements: 'first-line pseudo-element' and 'first-letter pseudo-element'. They can be attached to block-level elements (e.g. paragraphs or headings) to allow typographical styling of their sub-parts. Pseudo-element is created by a colon followed by pseudo-element's name, e.g:
P:first-line
H1:first-letter
and can be combined with normal classes; e.g:
P.initial:first-line
First-line pseudo-element allows sub-parting the element's first line and attaching specific style exclusively to this sub-part; e.g.:
P.initial:first-line {text-transform: uppercase}
<P class=initial>The first line of this paragraph will be displayed in uppercase letters</P>
First-letter pseudo-element allows sub-parting the element's first letter and attaching specific style exclusively to this sub-part; e.g.:
P.initial:first-letter { font-size: 200%; color: red}
<P class=initial>The first letter of this paragraph will be displayed in red and twice as large as the remaining letters</P>
As a developer who works with CSS every day, I find one complication that continues to bother me in my daily work. Support for CSS has always been good on the horizontal scope, but vertical positioning has always been quite complicated. Alone the procedure to affix a footer to the bottom of a screen in dependance of the amount of content is unnecessarily difficult. The old table method provided much easier methods for this. What are your thoughts on this and do you see improvement following in future CSS revisions?
Indeed, the CSS formatting model allows more control horizontally than vertically. This is due to (typically) having a known width, but an unknown height. As such, the height is harder to deal with. However, CSS2 fixed positioning allows you to place content relative to the viewport (which is CSS-speak for window) instead of the document. For example, by setting position: fixed; bottom: 0 on an element, it will stick to the bottom. This works in Opera, Safari and Mozilla-based browsers. IE6 doesn't support it, however. It remains to be seen if IE7 will support it.
How can I make a page look the same in e.g. NS and MSIE ?
The simple answer is, you can't, and you shouldn't waste your time trying to make it exactly the same. Web browsers are allowed, per definition, to interpret a page as they like, subject to the general rules set down in the HTML and CSS specifications. As a web author you can not have a prior knowledge of the exact situation and/or medium that will be used to render your page, and it's almost always rather counterproductive to try to control that process. There is no necessity for a well-written page to look the same in different browsers. You may want to strive to ensure that it looks good in more than one browser, even if the actual display (in the case of graphical browsers) comes out a bit different. "Looking good" can be achieved by adopting sensible design and guidelines, such as not fixing the size or face of your fonts, not fixing the width of tables, etc… Don't fight the medium; most web users only use one browser and will never know, or bother to find out, that your page looks different, or even "better", in any other browser.
Is there anything that CAN'T be replaced by Style Sheets?
Quite a bit actually. Style sheets only specify information that controls display and rendering information. Virtual style elements that convey the NATURE of the content can not be replaced by style sheets, and hyperlinking and multimedia object insertion is not a part of style sheet functionality at all (although controlling how those objects appear IS part of style sheets functionality.) The CSS1 specification has gone out of its way to absorb ALL of the HTML functionality used in controlling display and layout characteristics. For more information on the possible properties in CSS, see the Index DOT Css Property Index.
Rule of Thumb: if an HTML element or attribute gives cues as to how its contents should be displayed, then some or all of its functionality has been absorbed by style sheets.
Can I include comments in my Style Sheet?
Yes. Comments can be written anywhere where whitespace is allowed and are treated as white space themselves. Anything written between /* and */ is treated as a comment (white space). NOTE: Comments cannot be nested.
What is the difference between ID and CLASS?
ID identifies and sets style to one and only one occurrence of an element while class can be attached to any number of elements. By singling out one occurrence of an element the unique value can be declared to said element.
CSS
#eva1 {background: red; color: white}
.eva2 {background: red; color: white}
HTML - ID
<P ID=eva1>Paragraph 1 - ONLY THIS occurrence of the element P (or single occurrence of some other element) can be identified as eva1</P>
<P ID=eva1>Paragraph 2 - This occurrence of the element P CANNOT be identified as eva1</P>
HTML - CLASS
<P class=eva2>Paragraph 1 - This occurrence of the element P can be classified as eva2</P>
<P class=eva2>Paragraph 2 - And so can this, as well as occurrences of any other element, </P>
How to make text-links without underline?
a:link, a:visited {text-decoration: none}
or
<a style="text-decoration: none" HREF="...">
...will show the links without underlining. However, suppressing the underlining of links isn't a very smart idea as most people are used to having them underlined. Also, such links are not spotted unless someone coincidentally runs a mouse over them. If, for whatever reason, links without underline are required background and foreground colors can be instead declared to them so that they can be distinguished from other text, e.g.;
a:link, a:visited {text-decoration: none; background: red; color: blue}
or
<a style="text-decoration: none; background: red; color: blue" HREF="...">
Both background and foreground colors should be specified as the property that is not specified can be overridden by user's own settings.
How do you make a tool tip that appears on hover?
The most simple way is to use the 'title' attribute like this...
HTML
<span title="Example of the title attribute in use">like this</span>
CSS
a.tooltip {
position:relative;
cursor:help;
}
a.tooltip span {
display: none;
position:absolute;
top:1.5em;
left:0;
width:15em;
padding:0 2px;
}
a.tooltip:hover {
display:inline;
}
a.tooltip:hover span {
display:block;
border:1px solid gray;
background-color:white;
}
HTML
<a class="tooltip" href="#n">Karl Marx<span>-info goes here-</span></a>
Without this part... a.tooltip:hover {
display:inline;
}
..it won't work in IE.
The "#n" in the link is to prevent the page from jumping to the top if the link is clicked. The "href" part is necessary as it won't work in IE without it.
Which characters can CSS-names contain?
The CSS-names; names of selectors, classes and IDs can contain characters a-z, A-Z, digits 0-9, period, hyphen, escaped characters, Unicode characters 161-255, as well as any Unicode character as a numeric code. The names cannot start with a dash or a digit. (Note: in HTML the value of the CLASS attribute can contain more characters).
What browsers support style sheets? To what extent?
Microsoft's Internet Explorer version 3.0 Beta 2 and above supports CSS, as does Netscape Communicator 4.0 Beta 2 and above and Opera 3.5 and above. Take note that the early implementations in these browsers did not support ALL of the properties and syntax described in the full CSS1 specification and beyond. Later versions have been getting much closer to full CSS1 compliance, but then comes the next hurdle - CSS2...it was such a big leap over CSS1 that it has taken the browsers years to come close to supporting a majority of CSS2's features. Mozilla and Opera's current versions both offer excellent CSS standards compliance. The Macintosh version of Internet Explorer is said to be very impressive in its CSS capabilities as well, but PC IE lags behind these implementations. Quite a few other implementations of CSS now exist in browsers that are not as widely-used (such as Amaya, Arena and Emacs-W3), but coverage of features in these documents currently only covers Internet Explorer, NCSA Mosaic, Netscape and Opera browsers.
What is cascading order?
Cascading order is a sorting system consisting of rules by which declarations are sorted out so that there are not conflicts as to which declaration is to influence the presentation. The sorting begins with rule no 1. If a match is found the search is over. If there is no match under rule no 1 the search continues under rule no 2 and so on.
1. Find all declarations that apply to a specific selector/property and Declare the specified style if the selector matches the element if there isn't any Let the element inherit its parent property if there isn't any Use initial value
2. Sort by weight (! important) Increased weight take precedence over normal weight
3. Sort by origin Rules with normal weight declared in author's style sheet will override rules with normal weight declared in user's personal style sheets Rules with increased weight declared in user's personal style sheet will override rules with normal weight declared in author's style sheet Rules with increased weight declared in author's style sheet will override rules with increased weight declared in user's personal style sheets Author's and user's rules will override UA's default style sheet.
4. Sort by selector's specificity More specific selector will override less specific one: ID-selector (most specific), followed by Classified contextual selectors (TABLE P EM.fot) Class selectors (EM.fot) Contextual selectors - the "lower down" the more weight, (TABLE P EM), (TABLE P EM STRONG) - STRONG has more weight than EM.
5. Sort by order specified If two rules have the same weight, the latter specified overrides ones specified earlier. Style sheets are sorted out as follows: The STYLE attribute (inline style) overrides all other styles The Style element (embedded style) overrides linked and imported sheets The LINK element (external style) overrides imported style The @import statement - imported style sheets also cascade with each other in the same order as they are imported
Why shouldn't I use fixed sized fonts ?
Only in very rare situations we will find users that have a "calibrated" rendering device that shows fixed font sizes correct. This tells us that we can never know the real size of a font when it's rendered on the user end. Other people may find your choice of font size uncomfortable. A surprisingly large number of people have vision problems and require larger text than the average. Other people have good eyesight and prefer the advantage of more text on the screen that a smaller font size allows. What is comfortable to you on your system may be uncomfortable to someone else. Browsers have a default size for fonts. If a user finds this inappropriate, they can change it to something they prefer. You can never assume that your choice is better for them. So, leave the font size alone for the majority of your text. If you wish to change it in specific places (say smaller text for a copyright notice at the bottom of page), use relative units so that the size will stay in relationship to what the user may have selected already. Remember, if people find your text uncomfortable, they will not bother struggling with your web site. Very few (if any) web sites are important enough to the average user to justify fighting with the author's idea of what is best.
How do you make a whole div into a link?
You can't put 'a' tags around a div, but you can do this with javascript :
HTML
<div onclick="javascript:location='http://bonrouge.com'" id="mydiv">
... stuff goes here ...
</div>
If you want to use an empty div with a background image as a link instead of putting your image into the html, you can do something like this:
CSS
#empty {
background-image:url(wine.jpg);
width:50px;
height:50px;
margin:auto;
}
#empty a {
display:block;
height:50px;
}
* html #empty a {
display:inline-block;
}
HTML
<div id="empty"><a href="#n"></a></div>
How do I have links of different colors on the same page?
Recommending people to use classes in their 'a' tags like this :
CSS
a.red {
color:red;
}
a.blue {
color:blue;
}
HTML
<a href="#" class="red">A red link</a>
<a href="#" class="blue">A blue link</a>
This is a valid way to do it, but usually, this isn't what a page looks like - two links next to each other with different colours - it's usually something like a menu with one kind of link and main body text or another menu with different links. In this (normal) situation, To go higher up the cascade to style the links. Something like this :
CSS
a {
color:red;
}
#menu a {
color:blue;
}
HTML
<ul id="menu">
<li><a href="#">A red link</a></li>
<li><a href="#">A red link</a></li>
</ul>
<div id="content">
<p>There's <a href="#">a blue link</a> here.</p>
</div>
What is shorthand property?
Shorthand property is a property made up of individual properties that have a common "addressee". For example properties: font-weight, font-style, font-variant, font-size, font-family, refer to the font. To reduce the size of style sheets and also save some keystrokes as well as bandwidth they can all be specified as one shorthand property font, e.g.:
H1 {font-weight: bold;
font-style: italic;
font-variant: small-caps;
font-size: 160%;
font-family: serif}
can be all shorthanded to a space separated list:
H1 {font: bold italic small-caps 160% serif}
Note: To make things even simpler the line-height property can be specified together with the font-size property:
H1 {font: bold italic small-caps 160%/170% serif}
How to use CSS building a standards based HTML template?
It should:
1. Contain: header, navigation, content, footer
2. Use well-structured HTML
3. Be error-free and encourage good coding
Let’s start with number one there:
HTML document split up in four parts all with different meaning, use the
-tag. Div is short for “division” and isn’t header, navigation and so on ...
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Your own page title</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="header">
<h1>The name of this page</h1>
</div>
<div id="navigation">
<h2>Navigation</h2>
<ul>
<li><a href="first.html">First</a></li>
<li><a href="second.html">Second</a></li>
<li><a href="third.html">Third</a></li>
</ul>
</div>
<div id="content">
<h2>Content</h2>
<p>Some sample content, add your own here</p>
</div>
<div id="footer">
<p>This page is written by [Your name] and builds
n a <a href="http://friendlybit.com">
Friendlybit template</a>.</p>
</div>
</body>
</html>
body {
background-color: Green;
}
div {
border: 3px solid Black;
padding: 7px;
width: 600px;
}
h1, h2, h3, h4, h5, h6 {
margin: 0;
}
#navigation {
float: left;
width: 150px;
}
#content {
float: left;
width: 430px;
}
#footer {
clear: both;
}
What is value?
Value is a 'physical' characteristic of the property. Property declares what should be formatted, e.g. FONT while value suggests how the property should be formatted, e.g. 12pt. By setting the value 12pt to the property FONT it is suggested that the formatted text be displayed in a 12 point font. There must always be a corresponding property to each value or set of values.
H1 {font: bold 180%}
In the example above the H1 selector is declared the FONT property which in its turn is declared the values BOLD and 180%.
The values suggesting alternatives are specified in a comma separated list, e.g.
H1 {font-family: font1, font2}
What is initial value?
Initial value is a default value of the property, that is the value given to the root element of the document tree. All properties have an initial value. If no specific value is set and/or if a property is not inherited the initial value is used. For example the background property is not inherited, however, the background of the parent element shines through because the initial value of background property is transparent.
<P style="background: red">Hello <strong>World </strong> </P>
Content of the element P will also have red background
How frustrating is it to write a specification knowing that you're at the browser vendors' mercy?
That's part of the game. I don't think any specification has a birthright to be fully supported by all browsers. There should be healthy competition between different specifications. I believe simple, author-friendly specifications will prevail in this environment.
Microformats are another way of developing new formats. Instead of having to convince browser vendors to support your favorite specification, microformats add semantics to HTML through the CLASS attribute. And style it with CSS.
How far can CSS be taken beyond the web page--that is, have generalized or non-web specific features for such things as page formatting or type setting?
Yes, it's possible to take CSS further in several directions. W3C just published a new Working Draft which describes features for printing, e.g., footnotes, cross-references, and even generated indexes.
Another great opportunity for CSS is Web Applications. Just like documents, applications need to be styled and CSS is an intrinsic component of AJAX. The "AJAX" name sounds great.
How To Style Table Cells?
Margin, Border and Padding are difficult to apply to inline elements. Officially, the <TD> tag is a block level element because it can contain other block level elements (see Basics - Elements).
If you need to set special margins, borders, or padding inside a table cell, then use this markup:
<td>
yourtext </div></td>
to apply the CSS rules to the div inside the cell. </p>
How To Style Forms?
Forms and form elements like SELECT, INPUT etc. can be styled with CSS - partially.
Checkboxes and Radiobuttons do not yet accept styles, and Netscape 4.xx has certain issues, but here is a tutorial that explains the application of CSS Styles on Form Elements.
How do I get my footer to sit at the bottom...?
Need a div which makes space at the bottom of the main page (inside the #wrap div). Then, the footer (being inside #wrap) can be placed in that space by using absolute positioning. Like this :
CSS body, html {
height:100%;
}
body {
margin:0;
padding:0;
}
#wrap {
position:relative;
width:780px;
margin:auto; min-height:100%;
}
* html #wrap {
height:100%;
}
#clearfooter {
height:50px;
overflow:hidden;
}
#footer {
position:absolute;
bottom:0;
width:100%;
height:50px;
}
HTML
<div id="wrap">
...content goes here...
<div id="clearfooter"></div>
<div id="footer">Footer</div>
</div>
Can I attach more than one declaration to a selector?
Yes. If more than one declaration is attached to a selector they must appear in a semi colon separated list, e.g.;
Selector {declaration1; declaration2}
P {background: white; color: black}
Border around a table?
Try the following:
.tblboda {
border-width: 1px;
border-style: solid;
border-color: #CCCCCC;
}
/*color, thickness and style can be altered*/
You put this style declaration either in
an external stylesheet, or you can stuff it in
the <head></head> section, like:
<style type="text/css">
(here you can place your styles)
</style>
and apply it to the table as follows:
<div class="tblboda">
<table yaddayadda>
<tr>
<td>Content text and more content</td>
</tr>
</table>
</div>
That should give you a grey thin border around this table.
If you want the border to 'shrink wrap' around the table, then you have to use the <span> tag instead the
tag. But that is not quite proper CSS or HTML, because a is for inline elements. A table is not an inline element, therefore the correct tag is a <div>. If you play around with it a bit then you have a good chance to achieve what you want and still have correct HTML/CSS.
The other way would be that you apply the class .tblboda directly to the table (for IE and other contemporary browsers), like <table ... class="tableboda"> and you define another class for each stylesheet: .tblboda2
In the NN4.xx stylesheet, you use the same properties as above, and in the IE and other contemporary browsers you carefully set all those properties to default, like {border-style: none;}
Then you wrap the table in the <div> with the class .tblboda2 (NN4.xx does that) (IE a.o.c.b. don't do anything, because the border-style is set to "none" = no border at all).
This way you have a table that is wrapped in a nice little border: .tblboda2 for NN4.xx, .tblboda for IE and other modern browsers.
How do you target a certain browser?
IE can be targetted by preceding your properties with '* html'. For example...
#nav {
position:fixed;
}
* html #nav { /* this will target IE */
position:absolute;
}
Another way to target IE is with conditional comments. Put this (below) in the head - just before the closing tag - and put anything you want to be directed only at IE in another stylesheet.
<!--[if IE]>
<link href="ieonly.css" rel="stylesheet" type="text/css">
<![endif]-->
If you need to target IE5x...
#wrap {
width:760px; /* for IE5x */
w\idth:780px; /* for all other major browsers */
}
How does inheritance work?
HTML documents are structured hierarchically. There is an ancestor, the top level element, the HTML element, from which all other elements (children) are descended. As in any other family also children of the HTML family can inherit their parents, e.g. color or size.
By letting the children inherit their parents a default style can be created for top level elements and their children. (Note: not all properties can be inherited). The inheritance starts at the oldest ancestor and is passed on to its children and then their children and the children's children and so on.
Inherited style can be overridden by declaring specific style to child element. For example if the EM element is not to inherit its parent P then own style must be declared to it. For example:
BODY {font-size: 10pt}
All text will be displayed in a 10 point font
BODY {font-size: 10pt}
H1 {font-size: 14pt} or H1 {font-size: 180%}
All text except for the level 1 headings will be displayed in a 10 point font. H1 will be displayed in a 14 point font (or in a font that is 80% larger than the one set to BODY). If the element H1 contains other elements, e.g. EM then the EM element will also be displayed in a 14 point font (or 180%) it will inherit the property of the parent H1. If the EM element is to be displayed in some other font then own font properties must be declared to it, e.g.:
BODY {font-size: 10pt}
H1 {font-size: 14pt} or H1 {font-size: 180%}
EM {font-size: 15pt} or EM {font-size: 110%}
The EM element will be displayed in a 15 point font or will be 10% larger than H1. NOTE: EM is, in this example, inside H1 therefore will inherit H1's properties and not Body's.
The above declaration will display all EM elements in 15 point font or font that is 10% larger than font declared to the parent element. If this specific font is to apply to EM elements but only if they are inside H1 and not every occurrence of EM then EM must take a form of a contextual selector.
H1 EM {font-size: 15pt} or H1 EM {font-size: 110%}
In the example above EM is a contextual selector. It will be displayed in specified font only if it will be found in the context of H1.
Not all properties are inherited. One such property is background. However, since it's initial value is transparent the background of the parent element will shine through by default unless it is explicitly set.
What is the percentage value in 'font-size' relative to?
It is relative to the parent element's font-size. For example, if the style sheet says:
H1 {font-size: 20pt;}
SUP {font-size: 80%;}
...then a <SUP> inside an <H1> will have a font-size of 80% times 20pt, or 16pt.
What is wrong with font-family: "Verdana, Arial, Helvetica"?
The quotes. This is actually a list with a single item containing the well-known 'Verdana, Arial, Helvetica' font family. It is probably intended to be a list of three items.
Unlike in most other CSS1 properties, values for the font-family are separated by a comma to indicate that they are alternatives. Font names containing whitespace should be quoted. If quoting is omitted, any whitespace characters before and after the font name are ignored and any sequence of whitespace characters inside the font name is converted to a single space.
So to ask for two fonts foo and bar the syntax is:
font-family: foo, bar
To ask for the two fonts Revival 555 and Iodine you can do this:
font-family: "Revival 555", Iodine
You could also do this:
font-family: Revival 555, Iodine
which is equivalent. Notice that this is not three fonts; you can tell because after the "l" you didn't hit a comma, (more list items to come) a semicolon (end of that property, another property coming up) or a curly brace (end of that rule). This is also equivalent:
font-family: Revival 555, Iodine
^^^^^^ whole bunch of spaces converts to one space
But this next one is asking for a different font with two spaces in the name
font-family: "Revival 555", Iodine
^^two spaces, which are not converted
In general it is more tolerant of user typing to leave out the quotes. Sometimes you need them, for example there is a real font sold by Fontworks and designed in 1995 by Stephan Müller called Friday, Saturday, Sunday. Yes, two commas in the actual font name. CSS1 can handle this:
font-family: "Friday, Saturday, Sunday", cursive
Because it can handle this, the example in the title is syntactically correct. But what the author or tool wrote was almost certainly not what the document author intended.
How do I centre my page?
This is very easy. If we take the code in the last question and change it to this :
CSS
body, html {
height:100%;
}
body {
margin:0;
padding:0;
}
#wrap {
position:relative;
width:780px;
margin:auto; min-height:100%;
}
* html #wrap {
height:100%;
}
you get a page that fits an 800x600 resolution screen without a horizontal scrollbar, which will be centered at higher resolutions.
Must I quote property values?
Generally no. However, values containing white spaces, e.g. font-family names should be quoted as whitespaces surrounding the font name are ignored and whitespaces inside the font name are converted to a single space, thus font names made up of more than one word (e.g.) 'Times New Roman' are interpreted as three different names: Times, New and Roman.
Do any WYSIWYG editors support the creation of Style Sheets? Any text-based HTML editors?
As support for CSS in browsers has matured in the last year, both WYSIWYG and Text-based HTML editors have appeared that allow the creation or the assistance of creating Cascading Style Sheet syntax. There are now at least two dozen editors supporting CSS syntax in some form. The W3C maintains an up-to-date list of these WYSIWYG and text-based editors.
Which style specification method should be used? Why?
The answer to this one is tricky. The short answer is: "it depends." The long answer is, however, another story.
If you are planning on using more than one style specification method in your document, you must also worry about Cascading Order of Style methods (see question 11.) If you are going to use only one method, then some guidelines about the nature of each method need to be kept in mind. The answer to this question is also very much related to the advantages and disadvantages to using each of them (next question.)
Method 1: External Style Sheets (The LINK [-->Index DOT Html] element)
This method should be used if you want to apply the same style to multiple documents. Each document can reference the stand-alone style sheet and use the styles contained within. Using this method, the appearance of many documents can be controlled using a single or small number of style sheets. This can save a LOT of time for an author.
Method 2: Embedded Style Sheets (The Style [-->Index DOT Html] element)
The syntax used with Method 2 is the same as that for Method 1. This method is a happy medium between External Style Sheets and Inline Styles (see below.). It should be used in place of Method 1 if you only want to specify styles for a single document. This method should also be used when you want to specify a style for multiple tag types at once or the list of style definitions is of larger size. Method 3: Inline Styles (STYLE attribute to HTML elements)
If you only have to apply style to one or a few elements in a single document, your best bet will often be an Inline Style. This method attaches a style definition within the HTML element it is modifying.
Justified Text?
You redefine the <p> tag like:
p {text-align: justify;}
and that renders all <p>s with justified text.
Another possibility is to define a class, like:
.just {text-align: justify;}
and then you style the paragraphs in question like:
<td class="just">text </td>
Note that NN 4.xx has problems with the inheritance of styles; that some NN4.xx browsers have a funny way to interpret "justify"; and that you have to have at least one blank space between the last character and the </p> tag, because otherwise NN 4.xx likes to justify even a three word half-sentence... also, some browsers do a pretty ugly job of rendering justified text, adding spaces between words, instead of spaces between letters, as with word-processing programs.
Why can @import be at the top only?
A style sheet that is imported into another one has a lower ranking in the cascading order: the importing style sheet overrides the imported one. Programmers may recognize this as the same model as in Java, Modula, Object-Pascal, Oberon and other modular programming languages.
However, there is a competing model, well-known to C programmers, where the imported material is not lower in rank, but is expanded in-place and becomes an integral part of the importing document.
By allowing @import only at the top of the style sheet, people that think in terms of the second model (although in principle incorrect) will still get the expected results: as long as the @import is before any other overriding rules, the two models are equivalent.
Btw. In all the modular languages import statements are only allowed at the top. In C, the #include can be put elsewhere, but in practice everybody always puts it at the top. So there may not be that much need to allow @import elsewhere in the style sheet either.
Colored Horizontal Rule?
You can apply styles to Horizontal Rules <HR> in IE without problems, but NN4.xx can only render the silvery HR. But there is a way around it:
.rule {border-top-width: 1px;
border-top-style: solid;
border-color: #FF0000;
margin: 0px 2%;}
that, applied to a div, should give you a red HR in NN4.xx and IE, with a 2% gap on the left and right side.
CSSharky Logo
On this page is an Example of a coloured 'Horizontal Rule'.
Update:
Thanks to Matt Del Vecchio here is an improved format for the Horizontal Rule:
hr { height:0px;
border:0px;
border-top:1px solid #ff1493; }
....this works in both IE and Netscape. It tells the browser to not render the hr rule itself, and then sets a 1px border, which looks just how most folks want to render the hr rule. It uses the <hr> element and that is better than writing your own class as all devices will know what to do with an <hr> tag.
Do URL's have quotes or not?
Double or single quotes in URLs are optional. The tree following examples are equally valid:
BODY {background: url(pics/wave.png) blue}
BODY {background: url("pics/wave.png") blue}
BODY {background: url('pics/wave.png') blue}
To what are partial URLs relative?
Partial URLs are relative to the source of the style sheet. The style sheet source can either be linked or embedded. To which source partial URLs are relative to depends on their occurrence.
If a partial URL occurs in a linked style sheet then it is relative to the linked style sheet. The URL of the linked style sheet is the URL of the directory where the sheet is kept.
If a partial URL occurs in an embedded style sheet then it is relative to the embedded style sheet. The URL of the embedded style sheet is the URL of the HTML document in which the sheet is embedded.
Note that Navigator 4.x treats partial URLs as being relative to the HTML document, regardless of the place where the partial URL occurs. This is a serious bug which forces most authors to use absolute URLs in their CSS.
What's the difference between 'class' and 'id'?
As a person, you may have an ID card - a passport, a driving license or whatever - which identifies you as a unique individual. It's the same with CSS. If you want to apply style to one element use 'id' (e.g. <div id="myid">). In the stylesheet, you identify an 'id' with a '#' ie. '#myid'...
As a person, if you are in a class, you are one of many. It's the same with CSS. If you want to apply the same style to more than one element, use 'class' (e.g. <div class="myclass">). In the stylesheet, you identify a 'class' with a '.' ie. '.myclass'...
If id's are more restrictive than classes, then why not just litter your page with classes? Well, I think the main thing is that it's simply wrong. You don't put headings in 'p' tags - you use 'h1', 'h2', etc. You don't (or shouldn't) make a list by writing asterisks or the little divider bar ( | ) - you use list tags ('ol'/'ul' + 'li') . You don't say that your footer is part of a class of elements called 'footer' - that's just stupid - you can't have more than one footer - it can't be a class. Of course, practically, the effect is about the same - the rules are applied - but that's not the point - it's semantically wrong to do it that way... However, if you try to give more than one element the same id, you will have problems - so don't do it.
An element may have an id and a class, but that's usually not necessary. You can also give an element two classes if you need to - like this : class="class1 class2". It can be very useful. Needless to say, you can't give an element two id's.
Another difference is to do with power. You can give an element an id and a class, but if any of the properties of the two conflict, the id style will win. Ids are more powerful than classes.
One more useful thing about id's is that they can be used as a link reference. Many people still think that you need named anchors to make links within a page, but that's simply not true - in fact, the name attribute is deprecated in XHTML except for in forms. One example of using id's as link references is this page. There are no named anchors on this page - the questions at the top of the page link to the id's of the divs that the answers are in.
I made a 10px-high div, but IE makes it 20px high...
Yeah ,This problem sometimes comes up when you make a div just to contain the bottom border of a box, or something like that. In this situation, there's no text in the div, but IE won't let the height of the div be smaller than the line-height (which usually depends on the font-size). The answer is to set the font-size to zero.
CSS
#thediv {
font-size:0;
}
How do I place two paragraphs next to each other?
There are several ways to accomplish this effect, although each has its own benefits and drawbacks. We start with the simplest method of positioning two paragraphs next to each other.
<DIV style="float: left; width: 50%">Paragraph 1</DIV>
<DIV style="float: left; width: 50%">Paragraph 2</DIV>
Trickier is this example, which relies on positioning but does not suffer the vertical-overlap problems which plague many other positioning solutions. The problem is that it relies on an incorrect positioning implementation, and will break down dramatically in conformant browsers.
<P>
<SPAN STYLE="position: relative; left: 50%; width: 50%">
<SPAN STYLE="position: absolute; left: -100%; width: 100%">
Paragraph 1</SPAN>
Paragraph 2</SPAN>
</P>
If floating is not sufficient to your purposes, or you cannot accept display variances in older browsers, then it may be best to fall back to table-based solutions.
Can you use someone else's Style Sheet without permission?
This is a somewhat fuzzy issue. As with HTML tags, style sheet information is given using a special language syntax. Use of the language is not copyrighted, and the syntax itself does not convey any content - only rendering information.
It is not a great idea to reference an external style sheet on someone else's server. Doing this is like referencing an in-line image from someone else's server in your HTML document. This can end up overloading a server if too many pages all over the net reference the same item. It can't hurt to contact the author of a style sheet, if known, to discuss using the style sheet, but this may not be possible. In any case, a local copy should be created and used instead of referencing a remote copy.
If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!
0 comments:
Have any question? Feel Free To Post Below: