CSS: Tips and Tricks
You have already learned HTML, and you have heard that Cascading Style Sheets (or CSS) is used for styling pages, but haven’t yet learned CSS. Take a minute to learn the basics (the very basics) of CSS.
External style sheets are created in separate documents with a .css extension. An external style sheet is simply a listing of CSS rules. It cannot contain HTML tags. The
<link>tag, which goes in the head of an HTML page, is used to link to an external style sheet. There is no limit to the number of external style sheets a single HTML page can use. Also, external style sheets can be combined with embedded style sheets. Follow these steps to create an external style sheet.Aligning text in CSS can be achieved using the
text-alignproperty or thevertical-alignproperty.In this how to, you'll learn how to create a horizontal navigation menu.
A "fixed-width" layout is one in which the layout of the page is contained within a wrapper that doesn't adjust its size when the width of the browser changes. In this how to, you'll learn how to create a 2-column fixed-width layout.
When you use CSS to put a border on your
<th>and<td>tags, you will notice that you get space between them. In the old days, you would remove that space using the now-deprecatedcellspacingattribute. These days, you do it with the CSSborder-collapseproperty.You can set background images in CSS using the
background-imageand several other properties to control the behavior of the image.Text spacing and placement in CSS is controlled using the
letter-spacing,word-spacing,line-height, andtext-indentproperties. Learn how to set text spacing and placement in the following steps.Back before CSS, people used HTML tables to lay out web pages. The rule is the same now as it was then: tables should not be used to lay out pages. They should be used for tabular data, such as financial reports or a meeting agenda. To style tables with CSS, you should first be familiar with HTML table syntax.
Prior to browser support for CSS3, creating boxes with rounded corners required the creative use of background images, borders, and positioning. Thankfully, these days are gone. Today, browsers support CSS3 and its
border-radiusproperties. Theborder-radiusproperties makes rounding the corners on a box easy.In this how-to, you'll learn how to create a vertical navigation menu.
CSS3's opacity property makes it easy to change the opacity of images, text,
<div> s, and other elements. Where before we might have resorted to images with a semitransparent alpha setting, we can now change the opacity via CSS. Even better, we can change opacity more easily with:hoverto create rollover effects. Let's look at an example:CSS3 offers the ability to include multiple background images for a given element - use a comma-delimited list to specify as many as you like:
The order of the listed background images determines the position of the layer relative to the viewer, akin to the CSSbackground-image:url(img1.gif), url(img2.png), url(img3.gif)z-indexproperty: the first background image is on "top", the next below that, etc.When elements are positioned absolutely, they are removed from the normal flow. As a result, they do not affect the positioning of subsequent sibling elements. Learn how to use absolute positioning in the following steps.
Shorthand properties can take multiple values, allow you to write less code, and provide smaller files for the user to download. Find out how to use the CSS
bordershorthand property in these steps.You can use CSS pseudo-classes to create CSS buttons. Before CSS, you needed images to create links that looked like buttons.
Pages that are designed to use the full width of the browser are often described as having a "fluid" or "liquid" layout. That's because one or more of the columns must be automatically resized according to the size of the browser window. In this how to, you'll learn how to create a fluid-width layout.
The
colorandbackground-colorproperties determine how color is set in CSS.Embedded style sheets appear in the style element in the head of an HTML page. Follow these 10 steps to create a CSS embedded style sheet.
Inline styles are created by adding the
styleattribute to a tag. As with theclassandidattributes, almost all elements can take thestyleattribute. (Exceptions includehtml,head,title,meta,script, andstyle.) The value of thestyleattribute is a list of one or more property-value pairs separated by semicolons. Learn how to add inline styles to CSS in these six steps.Three properties work together to make borders:
border-style,border-color, andborder-width. In addition, each side of an element can also have a different border style. These are indicated by the following properties:- border-top
- border-right
- border-bottom
- border-left
Learn how to use these CSS properties to add a border to an element by following the steps below.
Shorthand properties can take multiple values, allow you to write less code, and provide smaller files for the user to download. Find out how to use the CSS padding property as well as the
marginproperty in these steps.In this how to, you'll learn how to create a fly-out menu with CSS.
Setting up a slide show to repeat automatically is useful in situations where the audience may view it at different times. You can set up a slide show to repeat automatically in PowerPoint in just five easy steps.
The
marginproperty is used to specify the margin around an element. It can be applied to both block and inline elements. Margins can be specified in number of units (e.g.,20px) or in percentage of the width of the containing element. Learn how to adjust margins with CSS below.The
backgroundproperty is used to set the individual properties for background-color,background-image,background-repeat,background-attachment, andbackground-position. Note that order matters (although some browsers may show some leniency). Learn how to use the CSSbackgroundshorthand property in the following steps.By using CSS to apply styles to the form, fieldset, and legend elements, you can layout a form without using tables. Here's how to do it.
In the past, we used the HTML tag to change font family, size, and other attributes. CSS makes it much easier. Learn how to modify font family and font size using CSS in the following steps.
In this how-to, you'll learn how to create a drop-down menu with CSS.
The
paddingproperty is used to specify the padding between an element's edges and its content. It can be applied to both block and inline elements. Padding can be specified in number of units (e.g.,20px) or in percentage of the width of the containing element. Learn how to apply padding with CSS below.Elements with fixed positioning stay in the same position in the browser window even when the page is scrolled. It can be used to keep an element (e.g., a navigation menu) on the page at all times. Learn how to use fixed positioning in the following steps.
The CSS3 transition property defines how an element changes from one state to another. With transition—or the specific properties listed below, for which transition serves as a shorthand—we can stretch out the change on an element's width, background color, or other property over a specified duration. You have to include appropriate vendor prefixes for transitions: -moz- for older version of Firefox, webkit for older versions of Chrome/Safari, and -o- for older versions of Opera. Internet Explorer supports CSS transitions, unprefixed, from version 10+.
Shorthand properties are CSS properties that can take multiple values, with each value relating to a different regular CSS property. Values in shorthand properties are separated by spaces, as shown below.
Changing text style in CSS is achieved using the
text-decorationandtext-transformproperties. Thetext-decorationproperty is used to add effects to text, such as underlines and line-throughs. Thetext-transformproperty is used to change the capitalization of text.A CSS sprite is an image file that contains several graphics used on a web page. By showing different parts of the sprite in different locations, it appears that there are several different images, but they are all contained in a single file, which translates to a single download. In this How-To, you'll learn how to create a grid of images by using a sprite.
Styles can be defined for different media. For example, you may want to style a page one way for viewing with a browser and a different way for viewing in print. The media type is defined in the
<link>tag for external style sheets and in the<style>tag for embedded style sheets.The
@importrule in CSS is used to import one style sheet into another.The
white-spaceproperty determines how sequences of whitespace are displayed. You can learn how to use the CSSwhite-spaceproperty in just a few steps.The
z-indexproperty specifies the stack level of an element on the page compared to other elements in its same flow. Learn how to use thez-indexproperty in the following steps.With the
box-shadowproperty, we can attach a drop shadow to an element. In this brief article, we show how.
