Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of medium and small skill codes in CSS Application

Shulou Source: shulou.com Published: 2022-06-01 07:27:34 11月14日 Update

小编给大家分享一下CSS应用中小技巧代码的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

一.清除浮动

方法1:#test{clear:both;}

#test为浮动元素的下一个兄弟元素

方法2:#test{display:block;zoom:1;overflow:hidden;}

#test为浮动元素的父元素。zoom:1也可以替换为固定的width或height

方法3:#test{zoom:1;}

#test:after{display:block;clear:both;visibility:hidden;height:0;content:'';}

#test为浮动元素的父元素

二.定义鼠标指针的光标形状为手型并兼容所有浏览器

方法:#test{cursor:pointer;}

若将cursor设置为hand,将只有IE和Opera支持,且hand为非标准属性值

三.让已知高度的容器在页面中水平垂直居中

方法:#test{position:absolute;top:50%;left:50%;width:200px;height:200px;margin:-100px 0 0 -100px;}

Know More:已知高度的容器如何在页面中水平垂直居中

四.让未知尺寸的图片在已知宽高的容器内水平垂直居中

方法:

#test{display:table-cell;*display:block;*position:relative;width:200px;height:200px;text-align:center;vertical-align:middle;}

#test p{*position:absolute;*top:50%;*left:50%;margin:0;}

#test p img{*position:relative;*top:-50%;*left:-50%;vertical-align:middle;}

#test是img的祖父节点,p是img的父节点。Know More:未知尺寸的图片如何水平垂直居中

五.设置span的宽度和高度(即如何设置内联元素的宽高)

方法:span{display:block;width:200px;height:100px;}

要使内联元素可以设置宽高,只需将其定义为块级或者内联块级元素即可。所以方法非常多样,既可以设置display属性,也可以设置float属性,或者position属性等等。

六.给一个元素定义多个不同的css规则

方法:

.a{color:#f00;}

.b{background:#eee;}

.c{background:#ccc;}

测试1

测试2

多个规则之间使用空格分开,并且只有class能同时使用多个规则,id不可以

以上是"CSS应用中小技巧代码的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Tags: Elements methods attributes floats multiple containers articles rules height small and medium code techniques examples analysis applications content only pictures dimensions horizontal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei MySQL Xiaomi Shulou Technology