CSS3新特性预览:border

Posted in February 9, 2010 at 12:46

CSS3新特性介绍:border属性

Many exciting new functions and features are being thought up for CSS3. We will try and showcase some of them on this page, when they get implemented in either Firefox, Konqueror, Opera or Safari/Webkit.

Borders

  • border-color
  • border-image
  • border-radius
  • box-shadow

1. How to create colored borders with CSS3

W3C has offered some new options for borders in CSS3, of which, next to rounded borders, border-color is also very interesting. Mozila/Firefox has implemented this function, which allows you to create cool colored borders. This is an example:

Mozilla/Firefox 在火狐浏览器下,你会看到这是个渐变灰的边框.…

The CSS code for this is:

border: 8px solid #000; -moz-border-bottom-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; -moz-border-top-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; -moz-border-left-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; -moz-border-right-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc; padding: 5px 5px 5px 15px;

And ofcourse, you can also do cool color stuff:

Mozilla/Firefox 在火狐浏览器下,你会看到这是个红色渐变的边框.…

2. Border-image: using images for your border

Another exciting new border feature of CSS3 is the property border-image. With this feature you can define an image to be used instead of the normal border of an element. This feature is actually split up into a couple of properties: border-image and border-corner-image. These two values are shorthands for:

border-image: border-top-image border-right-image border-bottom-image border-left-image
border-corner-image border-top-left-image border-top-right-image border-bottom-left-image border-bottom-right-image

border-image currently works in Safari and Firefox 3.1 (Alpha). The syntax to use it is:

border-image: url(/images/border.png) 27 27 27 27 round round;

Which results in:

Open in new window

Or:

border-image: url(/images/border.png) 27 27 27 27 stretch stretch;

Which then results in:

Open in new window

3. Border-radius: CSS3圆角边框.

W3C has offered some new options for borders in CSS3, of which one is border-radius. Both Mozila/Firefox and Safari 3 have implemented this function, which allows you to create round corners on box-items. This is an example:

Mozilla/Firefox and Safari 3 浏览器可以看到这个完美的圆角边框.

The code for this example above is actually quite simple:

<div style=" background-color: #ccc; -moz-border-radius: 5px; -webkit-border-radius: 5px; border: 1px solid #000; padding: 10px;" >

These different corners can also each be handled on their own, Mozilla has other names for the feature than the spec says it should have though, as it has f.i. -moz-border-radius-topright as opposed to -webkit-border-top-right-radius:

Mozilla/Firefox and Safari 3 浏览器可以看到左上角是圆角边框.

Mozilla/Firefox and Safari 3 浏览器可以看到右上角是圆角边框.

Mozilla/Firefox and Safari 3 浏览器可以看到左下角是圆角边框.

Mozilla/Firefox and Safari 3 浏览器可以看到右下角是圆角边框.

These are handled by / should be handled by:

-moz-border-radius-topleft / -webkit-border-top-left-radius -moz-border-radius-topright / -webkit-border-top-right-radius -moz-border-radius-bottomleft / -webkit-border-bottom-left-radius -moz-border-radius-bottomright / -webkit-border-bottom-right-radius

4. Box-shadow,CSS3新特性:区块投影效果

The CSS3 backgrounds and borders module has a nice new feature called box-shadow, which is implemented in Safari 3+ and Firefox 3.1 (Alpha). The specification speaks of multiple shadows, but the author already “has his doubts” on that, and it isn’t implemented in Safari 3.

The property takes 3 lengths and a color as it’s attributes, the lengths are:

  • the horizontal offset of the shadow, positive means the shadow will be on the right of the box, a negative offset will put the shadow on the left of the box;
  • the vertical offset, a negative one means the box-shadow will be on top of the box, a positive one means the shadow will be below the box;
  • the blur radius, if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be.

The shadow should be following curved corners created with border-radius. For those of you without a supporting browser, here’s a screenshot.

Open in new window

The CSS code for this is:

box-shadow: 10px 10px 5px #888; padding: 5px 5px 5px 15px;

Open in new window

The CSS code for this is:

box-shadow: -10px -10px 0px #000; border-radius: 5px; padding: 5px 5px 5px 15px;

Add a comment

Nickname

Site URI

Email

Enable HTML Enable UBB Enable Emots Hidden Remember [Login] [Register]