網頁標準化:CSS代碼縮寫精簡執行個體

來源:互聯網
上載者:User

一些CSS屬性允許使用一串值代替許多屬性,值使用空格分開。
  margin,pdding和border-width允許合并margin-top-width, margin-right-width, margin-bottom-width等等,形式像這樣:property:top right bottom left;逆時針順序。

  所以下面的代碼:

p {
  border-top-width: 1px;

  border-right-width: 5px;

  border-bottom-width: 10px;

  border-left-width: 20px;}

  可以寫成:

p {
  border-width: 1px 5px 10px 20px;}

  border-width,border-color,border-style同樣可以合并到一起,例如:

p {
  border: 1px red solid;}

  (同樣可以運用到border-top,border-right等等)

  如果只使用兩個值(比如margin: 1em 10em;),第一個值包括頂部和底部,第二個值包括左右。

  字型屬性同樣可以使用font屬性合并。

p {
  font: italic bold 1em/1.5 courier;}

  (上面"/1.5"是line-height的值)

  把它們總結在一起,試下下面的代碼:

p {
  font: 1em/1.5 "Times New Roman", times, serif;

  padding: 3em 1em;

  border: 1px black solid;

  border-width: 1px 5px 5px 1px;

  border-color: red green blue yellow;

  margin: 1em 5em;}

 建議大家使用縮寫形式,促進CSS代碼的精簡、最佳化!

相關文章

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.