CSS 常用技巧

概述

相信大家在写css属性的时候,会遇到一些问题,比如说:垂直对齐,垂直居中,背景渐变动画,表格宽度自适应,模糊文本,样式重置,清除浮动,通用媒体查询,自定义选择文本,强制出现滚动条,固定头部和页脚,自己在网上看到的一篇关于css的文章,感觉这里边一些常用的css代码片段对大家很有帮助,所以我就把这篇文章分享给大家,希望大家能够喜欢。

css代码片段

1、垂直对齐

如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,可以很优雅的解决这个困惑:

.verticalcenter{

    position: relative;

    top: 50%;

    -webkit-transform: translateY(-50%);

    -o-transform: translateY(-50%);

    transform: translateY(-50%);

}

2、伸展一个元素到窗口高度

在具体场景中,你可能想要将一个元素伸展到窗口高度,基本元素的调整只能调整容器的大小,因此要使一个元素伸展到窗口高度,

我们需要伸展顶层元素:html和body:

html, body {

    height: 100%;

}

然后将100%应用到任何元素的高

div {

    height: 100%;

}

3、基于文件格式使用不同的样式

为了更容易知道链接的目标,有时你想让一些链接看起来和其它的不同。下面的片段在文本链接前添加一个图标,对不同的资源使用不同的图标或图片:

a[href^="http://"]{

    padding-right: 20px;

    background: url(external.gif) no-repeat center right;

}

/* emails */

a[href^="mailto:"]{

    padding-right: 20px;

    background: url(email.png) no-repeat center right;

}

/* pdfs */

a[href$=".pdf"]{

    padding-right: 20px;

    background: url(pdf.png) no-repeat center right;

}

4、背景渐变动画

CSS中最具诱惑的一个功能是能添加动画效果,除了渐变,你可以给背景色、透明度、元素大小添加动画。目前,你不能为渐变添加动画,但下面的代码可能有帮助。它通过改变背景位置,让它看起来有动画效果。

button {

    background-image: linear-gradient(#5187c4, #1c2f45);

    background-size: auto 200%;

    background-position: 0 100%;

    transition: background-position 0.5s;

}

button:hover {

    background-position: 0 0;

}

5、CSS:表格列宽自适用

对于表格,当谈到调整列宽时,是比较痛苦的。然后,这里有一个可以使用的技巧:给td元素添加white-space: nowrap;能让文本正确的换行

td {

    white-space: nowrap;

}


6、只在一边或两边显示盒子阴影

如果你要一个盒阴影,试试这个技巧,能为任一边添加阴影。为了实现这个,首先定义一个有具体宽高的盒子,然后正确定位:after伪类。实现底边阴影的代码如下

.box-shadow{

    background-color:#FF8020;

    width:160px;height:90px;

    margin-top: -45px;

    margin-left: -80px;

    position: absolute;

    top:50%;

    left:50%;

   }

.box-shadow:after{

    content:"";

    width:150px;

    height:1px;

    margin-top:88px;

    margin-left: -75px;

    display: block;

    position: absolute;

    left:50%;

    z-index: -1;

    -webkit-box-shadow:0px0px8px2px#000000;

    -moz-box-shadow:0px0px8px2px#000000;

    box-shadow:0px0px8px2px#000000;

}

7、包裹长文本

如果你碰到一个比自身容器长的文本,这个技巧对你很有用。在这个示例中,默认时,不管容器的宽度,文本都将水平填充。


简单的CSS代码就能在容器中调整文本:

pre {

    white-space: pre-line;

    word-wrap: break-word;

}

效果看起来如下:


8、制造模糊文本

想要让文本模糊?可以使用color透明和text-shadow实现

.blurry-text {

    color: transparent;

    text-shadow: 0 0 5px rgba(0,0,0,0.5);

}

9、用CSS动画实现省略号动画

这个片段将帮助你制造一个ellipsis的动画,对于简单的加载状态是很有用的,而不用去使用gif图像。

.loading:after {

    overflow: hidden;

    display: inline-block;

    vertical-align: bottom;

    animation: ellipsis 2s infinite;

    content: "\2026";   /* ascii code for the ellipsis character */

}

@keyframes ellipsis {

    from {

        width: 2px;

    }

    to {

        width: 15px;

    }

}

10、样式重置

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {

    margin: 0;

    padding: 0;

    border: 0;

    font-size: 100%;

    font: inherit;

    vertical-align: baseline;

    outline: none;

    -webkit-box-sizing: border-box;

    -moz-box-sizing: border-box;

    box-sizing: border-box;

}

html {

    height: 101%;

}

body {

    font-size: 62.5%;

    line-height: 1;

    font-family: Arial, Tahoma, sans-serif;

}

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {

    display: block;

}

ol, ul {

    list-style: none;

}

blockquote, q {

    quotes: none;

}

blockquote:before, blockquote:after, q:before, q:after {

    content: '';

    content: none;

}

strong {

    font-weight: bold;

}

table {

    border-collapse: collapse;

    border-spacing: 0;

}

img {

    border: 0;

    max-width: 100%;

}

p {

    font-size: 1.2em;

    line-height: 1.0em;

    color: #333;

}


11、典型的CSS清除浮动

.clearfix:after {

    content: ".";

    display: block;

    clear: both;

    visibility: hidden;

    line-height: 0;

    height: 0;

}

.clearfix {

    display: inline-block;

}

html[xmlns] .clearfix {

    display: block;

}

* html .clearfix {

    height: 1%;

}

12、新版清除浮动(2011)

.clearfix:before, .container:after {

    content: "";

    display: table;

}

.clearfix:after {

    clear: both;

}

/* IE 6/7 */

.clearfix {

    zoom: 1;

}

13、跨浏览器的透明

.transparent {

    filter: alpha(opacity=50);     /* internet explorer */

    -khtml-opacity: 0.5;              /* khtml, old safari */

    -moz-opacity: 0.5;                 /* mozilla, netscape */

    opacity: 0.5;                          /* fx, safari, opera */

}

14、通用媒体查询

/* Smartphones (portrait and landscape) ----------- */

@mediaonly screen and (min-device-width :320px) and (max-device-width :480px) {

    /* Styles */

}

/* Smartphones (landscape) ----------- */

@mediaonly screen and (min-width :321px) {

    /* Styles */

}

/* Smartphones (portrait) ----------- */

@mediaonly screen and (max-width :320px) {

    /* Styles */

}

/* iPads (portrait and landscape) ----------- */

@mediaonly screen and (min-device-width :768px) and (max-device-width :1024px) {

    /* Styles */

}

/* iPads (landscape) ----------- */

@mediaonly screen and (min-device-width :768px) and (max-device-width :1024px) and (orientation : landscape) {

    /* Styles */

}

/* iPads (portrait) ----------- */

@mediaonly screen and (min-device-width :768px) and (max-device-width :1024px) and (orientation : portrait) {

    /* Styles */

}

/* Desktops and laptops ----------- */

@mediaonly screen and (min-width :1224px) {

    /* Styles */

}

/* Large screens ----------- */

@mediaonly screen and (min-width :1824px) {

    /* Styles */

}

/* iPhone 4 ----------- */

@mediaonly screen and (-webkit-min-device-pixel-ratio:1.5), only screen and (min-device-pixel-ratio:1.5) {

    /* Styles */

}

15、自定义文本选择

::selection {

    background: #e2eae2;

}

::-moz-selection {

    background: #e2eae2;

}

::-webkit-selection {

    background: #e2eae2;

}

16、为logo隐藏H1

h1 {

    text-indent: -9999px;

    margin: 0 auto;

    width: 320px;

    height: 85px;

    background: transparent url("images/logo.png") no-repeat scroll;

}

17、锚链接伪类

a:link {

    color: blue;

}

a:visited {

    color: purple;

}

a:hover {

    color: red;

}

a:active {

    color: yellow;

}

18、CSS3:全屏背景

html {

    background: url('images/bg.jpg') no-repeat center center fixed;

    -webkit-background-size: cover;

    -moz-background-size: cover;

    -o-background-size: cover;

    background-size: cover;

}

19、内容垂直居中

.container {

    min-height: 6.5em;

    display: table-cell;

    vertical-align: middle;


20、强制出现垂直滚动条

html { height: 101% }

21、CSS3渐变模板

#colorbox {

    background: #629721;

    background-image: -webkit-gradient(linear, left top, left bottom, from(#83b842), to(#629721));

    background-image: -webkit-linear-gradient(top, #83b842, #629721);

    background-image: -moz-linear-gradient(top, #83b842, #629721);

    background-image: -ms-linear-gradient(top, #83b842, #629721);

    background-image: -o-linear-gradient(top, #83b842, #629721);

    background-image: linear-gradient(top, #83b842, #629721);

}

22、CSS3 斑马线

tbody tr:nth-child(odd) {

    background-color: #ccc;

}

23、内部CSS3 盒阴影

#mydiv {

    -moz-box-shadow: inset 2px 0 4px #000;

    -webkit-box-shadow: inset 2px 0 4px #000;

    box-shadow: inset 2px 0 4px #000;

}


24、外部CSS3 盒阴影

#mydiv {

    -webkit-box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52);

    -moz-box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52);

    box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52);

}

25、三角形列表项目符号

ul {

    margin: 0.75em 0;

    padding: 0 1em;

    list-style: none;

}

li:before {

    content: "";

    border-color: transparent #111;

    border-style: solid;

    border-width: 0.35em 0 0.35em 0.45em;

    display: block;

    height: 0;

    width: 0;

    left: -1em;

    top: 0.9em;

    position: relative;

}

26、CSS3 鲜艳的输入

input[type=text], textarea {

    -webkit-transition: all 0.30s ease-in-out;

    -moz-transition: all 0.30s ease-in-out;

    -ms-transition: all 0.30s ease-in-out;

    -o-transition: all 0.30s ease-in-out;

    outline: none;

    padding: 3px 0px 3px 3px;

    margin: 5px 1px 3px 0px;

    border: 1px solid #ddd;

}

input[type=text]:focus, textarea:focus {

    box-shadow: 0 0 5px rgba(81, 203, 238, 1);

    padding: 3px 0px 3px 3px;

    margin: 5px 1px 3px 0px;

    border: 1px solid rgba(81, 203, 238, 1);

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容