Home  >  Article  >  Web Front-end  >  10 little-known CSS techniques_CSS/HTML

10 little-known CSS techniques_CSS/HTML

WBOY
WBOYOriginal
2016-05-16 12:11:371106browse

本翻译并未得到作者或网站授权。一切权利都归原作者及原网站所有。

如果你得到原作者或原发表网站的授权,可以自由使用本翻译。

1.CSS字体属性简写规则

一般用CSS设定字体属性是这样做的:

font-weight:bold;

font-style:italic;

font-varient:small-caps;

font-size:1em;

line-height:1.5em;

font-family:verdana,sans-serif;

但也可以把它们全部写到一行上去:

font: bold italic small-caps 1em/1.5em verdana,sans-serif;

真不错!只有一点要提醒的:这种简写方法只有在同时指定font-size和font-family属性时才起作用。而且,如果你没有设定font-weight, font-style, 以及 font-varient ,他们会使用缺省值,这点要记上。

2. 同时使用两个类

一般只能给一个元素设定一个类(Class),但这并不意味着不能用两个。事实上,你可以这样:

...

同时给P元素两个类,中间用空格格开,这样所有text和side两个类的属性都会加到P元素上来。如果它们两个类中的属性有冲突的话,后设置的起作用,即在CSS文件中放在后面的类的属性起作用。

补充:对于一个ID,不能这样写

...

也不能这样写

3. CSS border的缺省值

通常可以设定边界的颜色,宽度和风格,如:

border: 3px solid #000

这位把边界显示成3像素宽,黑色,实线。但实际上这里只需要指定风格即可。

如果只指定了风格,其他属性就会使用缺省值。一般地,Border的宽度缺省是medium,一般等于3到4个像素;缺省的颜色是其中文字的颜色。如果这个值正好合适的话,就不用设那么多了。

4. CSS用于文档打印

许多网站上都有一个针对打印的版本,但实际上这并不需要,因为可以用CSS来设定打印风格。

也就是说,可以为页面指定两个CSS文件,一个用于屏幕显示,一个用于打印:

第1行就是显示,第2行是打印,注意其中的media属性。

但 应该在打印CSS中写什么东西呢?你可以按设计普通CSS的方法来设定它。设计的同时就可以把这个CSS设成显示CSS来检查它的效果。也许你会使用 display: none 这个命令来关掉一些装饰图片,再关掉一些导航按钮。要想了解更多,可以看“打印差异”这一篇。

5. 图片替换技巧

一般都建议用标准的HTML来显示文字,而不要使用图片,这样不但快,也更具可读性。但如果你想用一些特殊字体时,就只能用图片了。

比如你想整个卖东西的图标,你就用了这个图片:

Buy widgets

这当然可以,但对搜索引擎来说,和正常文字相比,它们对alt里面的替换文字几乎没有兴趣这是因为许多设计者在这里放许多关键词来骗搜索引擎。所以方法应该是这样的:

Buy widgets

但这样就没有特殊字体了。要想达到同样效果,可以这样设计CSS:

h1 { background: url(widget-image.gif) no-repeat; height: image height text-indent: -2000px }

Be careful to replace the image height with the height of the real image. Here, the image will be displayed as the background, and because the indentation of -2000 pixels is set, the real text will appear 2000 points on the left side of the screen and will be invisible. But for people who turn off the picture, they may not be able to see it at all, so be careful.

6. Another adjustment technique for CSS box model

The adjustment of this Box model is mainly for IE browsers before IE6. They count the border width and whitespace into the element width. For example:

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

Call it like this:

...

The full width of the box should now be 150 points, which is correct on all browsers except IE before IE6. But on browsers like IE5, its full width is still 100 points. This difference can be handled using the Box adjustment method invented by previous people.

But the same purpose can be achieved using CSS to make them display consistent.

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

Call like this:

...

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

7. Align block elements in the center

If you want to make a fixed-width web page and want the web page to be horizontally centered, it usually looks like this:

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

You would use

to surround all elements. This is simple, but not good enough, and versions prior to IE6 will not display this effect. Change the CSS as follows:

body { text-align: center } #content { text-align: left; width: 700px; margin: 0 auto }

This will center the content of the web page, so I added

to Content.

text-align: left .

8. Use CSS to handle vertical alignment

Vertical alignment can be easily achieved using tables. Just set the table unit vertical-align: middle. But this is useless with CSS. If you want to set a navigation bar to be 2em high and want the navigation text to be vertically centered, setting this attribute is useless.

What is the CSS method? By the way, set the line-height of these words to 2em: line-height: 2em and that's it.

9. CSS positioning within the container

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

#container { position: relative }

In this way, all elements in the container will be relatively positioned. You can use it like this:

If you want to locate 30 points from the left and 5 points from the top, you can do this:

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

Of course, you can also do this:

margin: 5px 0 0 30px

Note that the order of the 4 numbers is: up, right, down, left. Of course, sometimes positioning rather than margins is better.

10. Background color straight to the bottom of the screen

Control in the vertical direction is beyond the capabilities of CSS. If you want the navigation bar to go straight to the bottom of the page like the content bar, using a table is very convenient, but if you only use CSS like this:

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

A shorter navigation bar will not go straight to the bottom, it will end when the content ends halfway. What to do?

Unfortunately, the only way to cheat is to add a background image to the shorter column, with the same width as the column width, and make it the same color as the set background color.

body { background: url(blue-image.gif) 0 0 repeat-y }

You cannot use em as the unit at this time, because then the trick will be revealed once the reader changes the font size, and you can only use px.

Original source:
http://blog.csdn.net/zhoujian2003/archive/2006/04/28/696012.aspx

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn