font-size: 1em;
line-height: 1.5em;
font-weight: bold;
font-style: italic;
font-variant: small-caps;
font-family: verdana,serif;
font: 1em/1.5em bold
italic small-caps verdana,serif
font-size
and the font-family
. Also, if you don't specify
the font-weight
, font-style
, or font-varient
then these values will
automatically default to a value of normal
,
so do bear this in mind too.
2. Two classes together
<p
class="text side">...</p>
text
and side
. If any rules overlap between
the two classes then the class which is below the other in the CSS
document will take precedence.
3. CSS border default value
When writing a border rule you'll usually specify the colour, width and style (in any order). For example,
border:
3px solid #000
will give you a black solid border, 3px thick.
However the only required value here is the border style.If you were to write just
border: solid
then the defaults for that border will be used. But what defaults? Well, the
default width for a border is medium (equivalent to about 3 to 4px) and the
default colour is that of the text colour within that border. If either of
these are what you want for the border then you can leave them out of the CSS
rule!
4. !important ignored by IE
Normally in CSS whichever rule is specified last takes precedence. However if you use
!important
after a command
then this CSS command will take
precedence regardless of what appears after it. This is true for all
browsers except IE. An example of this would be:margin-top: 3.5em
!important; margin-top: 2em
(Many of you may also be aware of the CSS child selector, the contents of which IE ignores.)
5. Image replacement technique
It's always advisable to use regular HTML markup to display text, as opposed to an image. Doing so allows for a faster download speed and has accessibility benefits. However, if you've absolutely got your heart set on using a certain font and your site visitors are unlikely to have that font on their computers, then really you've got no choice but to use an image.
Say for example, you wanted the top heading of each page to be ‘Buy widgets’, as you're a widget seller and you'd like to be found for this phrase in the search engines. You're pretty set on it being an obscure font so you need to use an image:
<h1><img
src="widget-image.gif" alt="Buy widgets"
/></h1>
<h1><span>Buy
widgets</span></h1>
h1
{
background: url(widget-image.gif) no-repeat;
}
h1 span
{
position: absolute;
left:-2000px;
}
6. CSS box model hack alternative
The box model hack is used to fix a rendering problem in pre-IE 6 browsers, where by the border and padding are included in the width of an element, as opposed to added on. For example, when specifying the dimensions of a container you might use the following CSS rule:
#box
{
width: 100px;
border: 5px;
padding: 20px;
}
<div
id="box">...</div>
A simple alternative is to use this CSS:
#box
{
width: 150px;
}
#box div
{
border: 5px;
padding: 20px;
}
<div
id="box"><strong><div></strong>...<strong></div></strong></div>
Perfect! Now the box width will always be 150px, regardless of the browser!
7. Centre aligning a block element
Say you wanted to have a fixed width layout website, and the content floated in the middle of the screen. You can use the following CSS command:
#content
{
width: 700px;
margin: 0 auto;
}
<div
id="content">
around every item in the
body of the HTML document and it'll be given an automatic
margin on both its left and right, ensuring that it's always placed in the
centre of the screen. Simple... well not quite - we've still got the pre-IE
6 versions to worry about, as these browsers won't centre align the element
with this CSS command. You'll have to change the CSS
rules:body
{
<strong>text-align: center</strong>;
}
#content
{
<strong>text-align: left</strong>;
width: 700px;
margin: 0 auto;
}
text-align: left
into the content div.8. Vertically aligning with CSS
Vertically aligning with tables was a doddle. To make cell content line up in the middle of a cell you would use
vertical-align: middle
. This doesn't
really work with a CSS layout.
Say you have a navigation menu item whose height is assigned 2em and you insert
this vertical align command into the CSS rule. It basically
won't make a difference and the text will be pushed to the top of the box.Hmmm... not the desired effect. The solution? Specify the line height to be the same as the height of the box itself in the CSS. In this instance, the box is 2em high, so we would insert
line-height: 2em
into the CSS
rule and the text now floats in the middle of the box - perfect!9. CSS positioning within a container
One of the best things about CSS is that you can position an object absolutely anywhere you want in the document. It's also possible (and often desirable) to position objects within a container. It's simple to do too. Simply assign the following CSS rule to the container:
#container
{
position: relative;
}
<div
id="container"><div
id="navigation">...</div></div>
To position the navigation exactly 30px from the left and 5px from the top of the container box, you could use these CSS commands:
#navigation
{
position: absolute;
left: 30px;
top: 5px;
}
margin: 5px 0 0 30px
, but there are some
cases where it's preferable to use positioning.10. Background colour running to the screen bottom
One of the disadvantages of CSS is its inability to be controlled vertically, causing one particular problem which a table layout doesn't suffer from. Say you have a column running down the left side of the page, which contains site navigation. The page has a white background, but you want this left column to have a blue background. Simple, you assign it the appropriate CSS rule:
#navigation
{
background: blue;
width: 150px;
}
Unfortunately the only solution to this is to cheat, and assign the body a background image of exactly the same colour and width as the left column. You would use this CSS command:
body
{
background: url(blue-image.gif) 0 0 repeat-y;
}
At the time of writing though, this is the only solution to this particular problem so the left column will have to be expressed in px if you want it to have a different background colour to the rest of the page.
0 comments:
Post a Comment