css中 border, margin, padding,Background,Font,属性简写速记说明

 css  css中 border, margin, padding,Background,Font,属性简写速记说明已关闭评论
9月 262016
 

以下文字来自msn官网,内容简明扼要,分享下:

Definition

Shorthand properties are CSS properties that let you set the values of several other CSS properties simultaneously. Using a shorthand property, a Web developer can write more concise and often more readable style sheets, saving time and energy.

The CSS specification defines shorthand properties to group the definition of common properties acting on the same theme. E. g. the CSSbackground property is a shorthand property that’s able to define the value of background-colorbackground-imagebackground-repeat, and background-position. Similarly, the most common font-related properties can be defined using the shorthand font, and the different margins around a box can be defined using the margin shorthand.

Tricky edge cases

Even if they are very convenient to use, there are a few edge cases to keep in mind when using them:

  1. A value which is not specified is set to its initial value. That sounds anecdotal, but it really means that it overrides previously set values. Therefore:

    background-color: red; background: url(images/bg.gif) no-repeat top right;

    will not set the color of the background to red but to background-color‘s default, transparent, as the second rule has precedence.

  2. Only the individual properties values can inherit. As missing values are replaced by their initial value, it is impossible to allow inheritance of individual properties by omitting them. The keyword inherit can be applied to a property, but only as a whole, not as a keyword for one value or another. That means that the only way to make some specific value to be inherited is to use the longhand property with the keywordinherit.
  3. Shorthand properties try not to force a specific order for the values of the properties they replace. This works well when these properties use values of different types, as the order has no importance, but this does not work as easily when several properties can have identical values. Handling of these cases are grouped in several categories:

    1. Shorthands handling properties related to edges of a box, like border-stylemargin or padding, always use a consistent 1-to-4-value syntax representing those edges:

      border1.png The 1-value syntax: border-width: 1em — The unique value represents all edges
      border2.png The 2-value syntax: border-width: 1em 2em — The first value represents the vertical, that is top and bottom, edges, the second the horizontal ones, that is the left and right ones.
      border3.png The 3-value syntax: border-width: 1em 2em 3em — The first value represents the top edge, the second, the horizontal, that is left and right, ones, and the third value the bottom edge
      border4.png

      The 4-value syntax: border-width: 1em 2em 3em 4em — The four values represent the top, right, bottom and left edges respectively, always in that order, that is clock-wise starting at the top (The initial letter of Top-Right-Bottom-Left matches the order of the consonant of the word trouble: TRBL)

    2. Similarly, shorthands handling properties related to corners of a box, like border-radius, always use a consistent 1-to-4-value syntax representing those corners:

      corner1.png The 1-value syntax: border-radius: 1em — The unique value represents all corners
      corner2.png The 2-value syntax: border-radius: 1em 2em — The first value represents the top left and bottom right corner, the second the top right and bottom left ones.
      corner3.png The 3-value syntax: border-radius: 1em 2em 3em — The first value represents the top left corner, the second the top right and bottom left ones, and the third value the bottom right corner
      corner4.png

      The 4-value syntax: border-radius: 1em 2em 3em 4em — The four values represent the top left, top right, bottom right and bottom left corners respectively, always in that order, that is clock-wise starting at the top left.

Background properties

A background with the following properties:

background-color: #000; background-image: url(images/bg.gif); background-repeat: no-repeat; background-position: top right;

Can be shortened to just one declaration:

background: #000 url(images/bg.gif) no-repeat top right;

(The shorthand form is actually the equivalent of the longhand properties above plus background-attachment: scroll and, in CSS3, some additional properties.)

Font properties

The following declarations:

font-style: italic; font-weight: bold; font-size: .8em; line-height: 1.2; font-family: Arial, sans-serif;

can be shortened to the following:

font: italic bold .8em/1.2 Arial, sans-serif;

This shorthand declaration is actually equivalent to the longhand declarations above plus font-variant: normal and font-size-adjust: none (CSS2.0 / CSS3), font-stretch: normal (CSS3).

Border properties

With borders, the width, color, and style can be simplified into one declaration. For example,

border-width: 1px; border-style: solid; border-color: #000;

Can be written as

border: 1px solid #000;

Margin and Padding properties

Shorthand versions of margin and padding values work the same way. The following CSS declarations:

margin-top: 10px; margin-right: 5px; margin-bottom: 10px; margin-left: 5px;

are the same as the following declaration (note that the values are in clockwise order from top: top, right, bottom, then left (TRBL, the consonants in “trouble”))

margin: 10px 5px 10px 5px;

用 CSS 实现三角形与平行四边形

 css  用 CSS 实现三角形与平行四边形已关闭评论
12月 242015
 

最近项目中看到个使用css实现三角形的功能,收集下相关资料:

1. 转自:http://jerryzou.com/posts/use-css-to-paint-triangle-and-parallelogram/

2. 更牛B的网站在这里: https://css-tricks.com/examples/ShapesOfCSS/ 供参考

最近在逛某个技术网站的时候,感觉文章关键词上的样式好酷炫啊。于来张截图:

分页样式的截图

你在首页的底部也可以看到这样一个分页栏;是不是看上去还不错?下面就来看看这是如何实现的吧~

第一种方法:利用border

第一种方法是借助border属性 hack 出三角形,然后通过一个矩形拼接两个三角形最终制造出一个平行四边形。为什么使用border可以产生三角形呢?先来看看一张图片:

CSS triangle

看了图中的三个小图形的变化过程,你应该已经清楚了一半。其实 hack 出三角形只需要两个条件,第一,元素本身的长宽为0;其次,将不需要的部分通过 border-color 来设置隐藏。通过类似的方法,你还可以创造出梯形,上图中的三个图形的代码如下。(另附 CodePen 示例

#first { width: 20px; height: 20px; border-width: 10px; border-style: solid; border-color: red green blue brown; } #second { width: 0; height: 0; border-width: 10px; border-style: solid; border-color: red green blue brown; } #third { width: 0; height: 0; border-width: 10px; border-style: solid; border-color: red transparent transparent transparent; }

接下来就要考虑如何拼接出一个平行四边形了。在border法中,它由三部分组成,分别是左三角形、矩形、右三角形。如果每次绘制平行四边形都要创建三个元素显然过于麻烦了,所以在这里:before和:after伪元素是个不错的选择。下面我们实现一下这样的效果:

three parallelogram

为了将三角形与矩形无缝拼接到一起,多处属性要保持一致,所以使用类似 Less, Sass, Stylus 等 CSS 预处理器来写这段代码会更容易维护,下面给出 Scss 版本的代码。(另附 CodePen 链接

//三角形的宽高 $height: 24px; $width: 12px; //对平行四边形三部分的颜色进行赋值 @mixin parallelogram-color($color) { background: $color; &:before { border-color: transparent $color $color transparent; } &:after { border-color: $color transparent transparent $color; } } //单个三角形的样式 @mixin triangle() { content: ''; display: block; width: 0; height: 0; position: absolute; border-style: solid; border-width: $height/2 $width/2; top: 0; } //平行四边形的样式 .para { display: inline-block; position: relative; padding: 0 10px; height: $height; line-height: $height; margin-left: $width; color: #fff; &:after { @include triangle(); right: -$width; } &:before { @include triangle(); left: -$width; } @include parallelogram-color(red); }

需要注意的是,如果通过 $height、$width 设置的三角形斜率太小或太大都有可能造成渲染出锯齿,所以使用起来要多多测试一下不同的宽高所得到的视觉效果如何。

第二种方法:利用transform

使用transform来实现平行四边形的方法是我在逛去啊的时候看到的,效果大概是这个样子:

去啊中的平行四边形

看到之后觉得好神奇啊,原来还可以只有平行四边形的外轮廓。(因为方法一只能创造填充效果的平行四边形)实现起来非常简单,主要是借助了transform: skew(…),下面就来看看源码吧。

<style> .city { display: inline-block; padding: 5px 20px; border: 1px solid #44a5fc; color: #333; transform: skew(-20deg); } </style> <div class="city">上海</div>

于是我们得到了这样的效果:

skew shanghai

看到图片的你一定是这样想的:

坑爹呢这是

别着急嘛,我们的确是把整个 div 进行了歪曲,导致中间的文字也是倾斜的,而这显然不是我们所要的效果。所以我们需要加一个内层元素,并对内层元素做一次逆向的歪曲,从而得到我们想要的效果:

normal shanghai

实现代码如下,另附 CodePen 示例

<style> .city { display: inline-block; padding: 5px 20px; border: 1px solid #44a5fc; color: #333; transform: skew(-20deg); } .city div { transform: skew(20deg); } </style> <div class="city"> <div>上海</div> </div>

总结

第一种方法使用 border 属性 hack 出三角形,并通过对三个元素进行拼接最终实现了平行四边形;而第二种方法则通过 transform: skew来得到平行四边形。总体来说,第二种方法相对于第一种代码量小得多,而且也很好理解。唯一的不足是无法构造像本站的分页中所使用的梯形。希望本文对各位有所帮助。