(转) 50个CSS技巧

这里我工做中收集了10个很不错的CSS技巧,你能够用在你的项目上。它能够帮你很好地整理你的元素并让他们看起来蛮酷的。下面开始咱们的内容,但愿你会喜欢它。下面是我收集的CSS技巧,但愿能帮助到你,感受收藏吧。css

WEB前端开发工程师必备实用的50个CSS技巧

1. 黑白图像html

这段代码会让你的彩色照片显示为黑白照片,是否是很酷?前端

img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%);}

2. 使用 :not() 在菜单上应用/取消应用边框web

先给每个菜单项添加边框chrome

/* add border */.nav li { border-right: 1px solid #666;}

……而后再除去最后一个元素……浏览器

// remove border /.nav li:last-child { border-right: none;}

……能够直接使用 :not() 伪类来应用元素:服务器

.nav li:not(:last-child) { border-right: 1px solid #666;}

这样代码就干净,易读,易于理解了。ssh

固然,若是你的新元素有兄弟元素的话,也可使用通用的兄弟选择符(~):ide

..nav li:first-child ~ li { border-left: 1px solid #666;}

3. 页面顶部阴影svg

下面这个简单的 CSS3 代码片断能够给网页加上漂亮的顶部阴影效果:

body:before { content: ""; position: fixed; top: -10px; left: 0; width: 100%; height: 10px; -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8); -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8); box-shadow: 0px 0px 10px rgba(0,0,0,.8); z-index: 100;}

4. 给 body 添加行高

你不须要分别添加 line-height 到每一个p,h标记等。只要添加到 body 便可:

body { line-height: 1;}

这样文本元素就能够很容易地从 body 继承。

5. 全部一切都垂直居中

要将全部元素垂直居中,太简单了:

html, body { height: 100%; margin: 0;}body { -webkit-align-items: center; -ms-flex-align: center; align-items: center; display: -webkit-flex; display: flex;}

看,是否是很简单。

注意:在IE11中要当心flexbox。

6. 逗号分隔的列表

让HTML列表项看上去像一个真正的,用逗号分隔的列表:

ul > li:not(:last-child)::after { content: ",";}

对最后一个列表项使用 :not() 伪类。

7. 使用负的 nth-child 选择项目

在CSS中使用负的 nth-child 选择项目1到项目n。

li { display: none;}/* select items 1 through 3 and display them */li:nth-child(-n+3) { display: block;}

8. 对图标使用 SVG

咱们没有理由不对图标使用SVG:

.logo { background: url("logo.svg");}

SVG对全部的分辨率类型都具备良好的扩展性,并支持全部浏览器都回归到IE9。这样能够避开.png、.jpg或.gif文件了。

9. 优化显示文本

有时,字体并不能在全部设备上都达到最佳的显示,因此可让设备浏览器来帮助你:

html { -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;}

注:请负责任地使用 optimizeLegibility。此外,IE /Edge没有 text-rendering 支持。

10. 对纯 CSS 滑块使用 max-height

使用 max-height 和溢出隐藏来实现只有CSS的滑块:

.slider ul { max-height: 0; overlow: hidden;}.slider:hover ul { max-height: 1000px; transition: .3s ease;}

11. 继承 box-sizing

让 box-sizing 继承 html:

html { box-sizing: border-box;}*, *:before, *:after { box-sizing: inherit;}

这样在插件或杠杆其余行为的其余组件中就能更容易地改变 box-sizing 了。

12. 表格单元格等宽

表格工做起来很麻烦,因此务必尽可能使用 table-layout: fixed 来保持单元格的等宽:

.calendar { table-layout: fixed;}

13. 用 Flexbox 摆脱外边距的各类 hack

当须要用到列分隔符时,经过flexbox的 space-between 属性,你就能够摆脱nth-,first-,和 last-child 的hack了:

.list { display: flex; justify-content: space-between;}.list .person { flex-basis: 23%;}

如今,列表分隔符就会在均匀间隔的位置出现。

14. 使用属性选择器用于空连接

当a元素没有文本值,但 href 属性有连接的时候显示连接:

a[href^="http"]:empty::before { content: attr(href);}

至关方便。

15. 检测鼠标双击

HTML:

<div class="test3"> <span><input type="text" value=" " readonly="true" /> <a href="http://renpingjun.com">Double click me</a></span></div>

CSS:

.test3 span { position: relative;}.test3 span a { position: relative; z-index: 2;}.test3 span a:hover, .test3 span a:active { z-index: 4;}.test3 span input { background: transparent; border: 0; cursor: pointer; position: absolute; top: -1px; left: 0; width: 101%; /* Hacky */ height: 301%; /* Hacky */ z-index: 3;}.test3 span input:focus { background: transparent; border: 0; z-index: 1;}

16. CSS 写出三角形

/* create an arrow that points up */div.arrow-up { width:0px; height:0px; border-left:5px solid transparent; /* left arrow slant */ border-right:5px solid transparent; /* right arrow slant */ border-bottom:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px;}/* create an arrow that points down */div.arrow-down { width:0px; height:0px; border-left:5px solid transparent; border-right:5px solid transparent; border-top:5px solid #2f2f2f; font-size:0px; line-height:0px;}/* create an arrow that points left */div.arrow-left { width:0px; height:0px; border-bottom:5px solid transparent; /* left arrow slant */ border-top:5px solid transparent; /* right arrow slant */ border-right:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px;}/* create an arrow that points right */div.arrow-right { width:0px; height:0px; border-bottom:5px solid transparent; /* left arrow slant */ border-top:5px solid transparent; /* right arrow slant */ border-left:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px;}

17. CSS3 calc() 的使用

calc() 用法相似于函数,可以给元素设置动态的值:

/* basic calc */.simpleBlock { width: calc(100% - 100px);}/* calc in calc */.complexBlock { width: calc(100% - 50% / 3); padding: 5px calc(3% - 2px); margin-left: calc(10% + 10px);}

18. 文本渐变

文本渐变效果很流行,使用 CSS3 可以很简单就实现:

h2[data-text] { position: relative;}h2[data-text]::after { content: attr(data-text); z-index: 10; color: #e3e3e3; position: absolute; top: 0; left: 0; -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));}

19. 禁用鼠标事件

CSS3 新增的 pointer-events 让你可以禁用元素的鼠标事件,例如,一个链接若是设置了下面的样式就没法点击了。

.disabled { pointer-events: none; }

20. 模糊文本

简单但很漂亮的文本模糊效果,简单又好看!

.blur { color: transparent; text-shadow: 0 0 5px rgba(0,0,0,0.5);}

21.简单的方法调整图片大小

.content img {

height:auto;

width:500px;

}

22.IE HTML Hack

div#content {width: 580px}

* html body div#content {width: 600px}

23.CSS阴影

.shadow {

-moz-box-shadow: 3px 3px 5px 6px #ccc;

-webkit-box-shadow: 3px 3px 5px 6px #ccc;

box-shadow: 3px 3px 5px 6px #ccc;

}

24.CSS首字放大

p:first-letter {

display: block;

float: left;

margin: 5px 5px 0 0;

color: red;

font-size: 1.4em;

background: #ddd;

font-family: Helvetica;

}

25.用CSS翻转图像

#content img {

-moz-transform: scaleX(-1);

-o-transform: scaleX(-1);

-webkit-transform: scaleX(-1);

transform: scaleX(-1);

filter: FlipH;

-ms-filter: "FlipH";

}

26.移除被点连接的点框

a {outline: none}

或者

a {outline: 0}

27.在CSS中使用特殊字体

你可使用CSS来加载特殊字体,你要作的就是把这个TTF格式的字体上传到服务器上,而后使用字体规则在CSS上导入它。

28.元素透明

.element {

filter:alpha(opacity=50);

-moz-opacity:0.5;

-khtml-opacity: 0.5;

opacity: 0.5;

}

29.使用CSS显示连接以后的URL

a:after{content:" (" attr(href) ") ";}

这会在连接锚点后显示URL。你也能够用字体或其余样式定义它。

30.为手持设备定制特殊样式

<link type="text/css" rel="stylesheet" href="handheldstyle.css" media="handheld">

31.文字的水平居中

text-align:center;

32.link状态的设置顺序

a:link

a:visited

a:hover

a:active

33.用图片充当列表标志

ul {list-style: none}

ul li {

background-image: url("path-to-your-image");

background-repeat: none;

background-position: 0 0.5em;

}

34.禁止自动换行

h1 { white-space:nowrap; }

35.得到焦点的表单元素 

input:focus { border: 2px solid green; }

36.user-select 禁止用户选中文本

div {

user-select: none; /* Standard syntax */

}

37.清除手机tap事件后element 时候出现的一个高亮

* {

-webkit-tap-highlight-color: rgba(0,0,0,0);

}

38.加强用户体验,使用伪元素实现增大点击热区

.btn::befoer{

content:"";

position:absolute;

top:-10px;

right:-10px;

bottom:-10px;

left:-10px;

}

39.伪元素实现换行,替代换行标签

inline-element ::after{

content:"A";

white-space: pre;

}

40.will-change提升页面滚动、动画等渲染性能

/* 关键字值 */

will-change: auto;

will-change: scroll-position;

will-change: contents;

will-change: transform; /* <custom-ident>示例 */

will-change: opacity; /* <custom-ident>示例 */

will-change: left, top; /* 两个<animateable-feature>示例 */

will-change的使用也要谨慎,遵循最小化影响原则,不要这样直接写在默认状态中,由于will-change会一直挂着:

.will-change {

will-change: transform;

transition: transform 0.3s;

}

.will-change:hover {

transform: scale(1.5);

}

可让父元素hover的时候,声明will-change,这样,移出的时候就会自动remove,触发的范围基本上是有效元素范围。

.will-change-parent:hover .will-change {

will-change: transform;

}

.will-change {

transition: transform 0.3s;

}

.will-change:hover {

transform: scale(1.5);

}

41.box-sizing 让元素的宽度、高度包含border和padding

{

box-sizing: border-box;

}

42.calc() function, 计算属性值

div {

width: calc(100% - 100px);

}

例子就是让宽度为100%减去100px的值

43.css实现不换行、自动换行、强制换行

//不换行

white-space:nowrap;

//自动换行

word-wrap: break-word;

word-break: normal;

//强制换行

word-break:break-all;

44.perspective 透视

这个属性的存在决定你看到的元素是2d仍是3d。通常设置在包裹元素的父类上。

.div-box {

perspective: 400px;

}

45.设置图像透明度的两种方式

  • opcity:0.6;

  • background:rgba(0,0,0,.6);

46.position定位属性

position属性指定一个元素(静态的、相对的、绝对或固定)的定位方法的类型。

position的属性值:

absolute:生成绝对定位的元素;

fixed:生成绝对定位的元素,相对于浏览器窗口进行定位;

relative:生成相对定位的元素,相对于其正常位置经行定位。

z-index:指定一个元素的堆叠顺序。

47.cursor属性

cursor属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状。

CSS提供的cursor值:

pointer :小手指;

help:箭头加问号;

wait:转圈圈;

move:移动光标;

crosshair:十字光标。

经过pointer属性咱们能够伪造超连接:

<span >pointer</span>

48.隐藏没有静音、自动播放的影片

video[autoplay]:not([muted]) {

display: none;

}

49.Font-Size 基准

/* 假设浏览器的默认的大小是 16px , 首先将其设置为10px (font-size:10/16) */

body {font-size:10/16;}

/* 而后就能够用em作统一字体单位了 2.4em = 24px */

h1 {font-size: 2.4 em}

50.透明容器

.element {

filter:alpha(opacity=50); /* for ie */

-moz-opacity:0.5; /* for ff */

-khtml-opacity: 0.5; /* for webkit as chrome */

opacity: 0.5; /* for opera */

}

相关文章
相关标签/搜索