Good 10 CSS tricks you don't necessarily know _ experience exchange

Source: Internet
Author: User
This translation is not authorized by the author or the website. All rights are vested in the original author and the original website.
If you are authorized by the original author or the original published website, you are free to use this translation.

1.CSS font attribute shorthand rules

The general use of CSS to set font properties is to do this:

Font-weight:bold;
Font-style:italic;
Font-varient:small-caps;
Font-size:1em;
Line-height:1.5em;
Font-family:verdana,sans-serif;

But you can also write them all in one line:

Font:bold Italic small-caps 1em/1.5em Verdana,sans-serif;

That's great! There is only one point to be reminded: this shorthand method only works if you specify both the Font-size and Font-family properties. Also, if you do not set Font-weight, Font-style, and font-varient, they will use the default values, which should be remembered.

2. Use two classes at a time

Typically you can only set a class for one element, but that doesn't mean you can't use two of them. In fact, you can do this:

...



The P element is also given two classes, the middle is opened with an empty space, so that all text and side two class attributes will be added to the P element. If there are conflicting attributes in their two classes, the latter setting works, that is, the properties of the class that are placed in the CSS file are in effect.

Add: For an ID, this cannot be written

...

I can't write that.

3. Default values for CSS border

You can usually set the color, width and style of the boundary, such as:
BORDER:3PX Solid #000
This shows the border as 3 pixels wide, black, solid line. But in fact, you just need to specify the style.

If only the style is specified, the default values are used by the other properties. Generally, the border width defaults to medium, typically 3 to 4 pixels, and the default color is the color of the text in it. If the value is just right, you don't have to set that much.

4. CSS for document printing

Many web sites have a print-ready version, but this is not really necessary because you can use CSS to set the print style.

That is, you can specify two CSS files for the page, one for the screen display, and one for printing:



Line 1th is the display, line 2nd is print, note the media properties.

But what should I write in the printed CSS? You can set it up by designing a common CSS method. At the same time, the CSS can be set to display CSS to check its effect. Perhaps you will use the Display:none command to turn off some decorative pictures, and then turn off some of the navigation buttons. To learn more, you can look at the "Print differences" article.

5. Photo Replacement Tips

It is generally recommended that you display text in standard HTML instead of using a picture, which is faster and more readable. But if you want to use some special fonts, you can only use pictures.

For example, you want the entire sale of the icon, you use this picture:


Of course, but for search engines, compared to normal text, they have little interest in alt text, because many designers here put a lot of keywords to cheat search engines. So the method should be this:

Buy Widgets


But then there is no special font. To achieve the same effect, you can design css like this:
H1 {Background:url (widget-image.gif) no-repeat; height:image height text-indent: -2000px}

Be careful to change the image height to the true picture. Here, the picture will be shown as the background, and the real text because of the set-2000 pixels of this indentation, they will appear on the left side of the screen 2000 points, it is not visible. But this is for the person who closes the picture, may not see all, this should pay attention to.

6. Another adjustment technique for CSS box models

The box model is mostly tuned for IE before IE6, which calculates the width of the boundary and the space between the elements. Like what:

#box {width:100px; border:5px; padding:20px}

Call it this way:
...
At this point the full width of the box should be 150 points, which is correct on all browsers except for the IE browser before IE6. But on a browser like IE5, its full width is still 100 points. This difference can be handled using the box adjustment method previously invented by people.

But using CSS can also achieve the same goal, so that they show the same effect.

#box {width:150px} #box div {border:5px; padding:20px}

This is called:
...
In this way, no matter what browser, the width is 150 points.

7. Center-aligned block elements

If you want to make a fixed-width page and want the page to be centered horizontally, this is usually the case:

#content {width:700px; margin:0 auto}

You will be used to surround all the elements. This is simple, but not good enough, IE6 the previous version will not show this effect. Change the CSS as follows:

Body {Text-align:center} #content {text-align:left; width:700px; margin:0 Auto}

This will center the contents of the page, so in the content is added
Text-align:left.

8. Use CSS to handle vertical alignment

The Vertical alignment table can be easily implemented, and the table cell Vertical-align:middle can be set. But it doesn't work for CSS. Setting this property is useless if you want to set a navigation bar that is 2em high and you want the navigation text to be centered vertically.

What is a CSS method? By the 2EM:LINE-HEIGHT:2EM, you can set the line height of these words to be the same.

9. CSS positioning within the container

One of the benefits of CSS is that you can position an element arbitrarily, within a container. For example, for this container:

#container {position:relative}

This allows all elements in the container to be positioned relative to each other so that they can be used:
...
If you want to position to the left 30 points, 5 points above, you can:

#navigation {position:absolute; left:30px; top:5px}

Of course, you can also:
margin:5px 0 0 30px
Note that the order of 4 numbers is: top, right, bottom, left. Of course, it is sometimes better to locate the method rather than the margin.

10. Background color through to the bottom of the screen

In the vertical direction is the control is not the CSS. If you want the navigation bar and the content bar to pass directly to the bottom of the page, it is convenient to use the table, but if you only use CSS like this:

#navigation {background:blue; width:150px}

A shorter navigation bar won't pass through to the bottom, and it will end when the content ends. What should we do?

Unfortunately, you can only use deception, give the shorter column a background, width and column width, and let its color and set the background color.

Body {background:url (blue-image.gif) 0 0 Repeat-y}

Do not use EM at this time, because then, once the reader changes the font size, the trick will be revealed, only use PX.

The author of this article is: Trenton Moss.
Posted website is: http://www.webcredible.co.uk/.

This translation is not authorized by the author or the website. All rights are vested in the original author and the original website.
If you are authorized by the original author or the original published website, you are free to use this translation.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.