温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实用的CSS技巧有哪些

发布时间:2022-03-02 16:07:43 来源:亿速云 阅读:79 作者:iii 栏目:web开发

这篇文章主要讲解了“实用的CSS技巧有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“实用的CSS技巧有哪些”吧!

1. 黑白图像

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

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

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

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

/* 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;}

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

当然,如果你的新元素有兄弟元素的话,也可以使用通用的兄弟选择符(~):

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

3. 页面顶部阴影

下面这个简单的 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 能够很简单就实现:

h3[data-text]
 { position: relative;}h3[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.禁止自动换行

h2 { 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 style="cursor:pointer;color:blue;">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 */

h2 {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 */

}

感谢各位的阅读,以上就是“实用的CSS技巧有哪些”的内容了,经过本文的学习后,相信大家对实用的CSS技巧有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI