cvooc

CSS常用标签

前言

此文章在于对日常使用较为频繁的 CSS 标签进行整理,希望能帮助你们.

CSS 自定义变量

:root {
    --THEME_COLOR: ##ff9800; //主题颜色
}
<style type="text/css">
    ##test {
        background-color: var(--THEME_COLOR);
    }
</style>
<div id="test" style="width: 50px;height: 20px;"></div>

禁止页面选中文字

moz-user-select: -moz-none;
-moz-user-select: none;
-o-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;

设置圆角

border-radius: 100%;

textarea 禁止拉伸

resize: none;

使 DIV 平行

display: inline;

隐藏页面滚轮

::-webkit-scrollbar {
    display: none;
}

使文字水平居中

text-align: center;

使文字垂直居中

height: 20px;
line-height: 20px;

使 DIV 垂直居中

display: flex;
align-items: center;

将 DIV 固定到页面四周

position: fixed;
top: 0;
/*或:right: 0;bottom: 0;left: 0;*/

去除 a 标签下划线

text-decoration: none;

字体加粗

font-weight: bold;

隐藏 DIV(占用空间)

visibility: hidden;
visibility: visible; /*显示*/

select 文字居中

select {
    text-align: center;
    text-align-last: center;
}

隐藏 DIV(释放空间)

display: none;
display: block; /*显示*/

DIV 添加边框阴影

box-shadow: h-shadow v-shadow blur spread color inset;
描述
h-shadow必需.水平阴影的位置.允许负值.
v-shadow必需.垂直阴影的位置.允许负值.
blur可选.模糊距离.
spread可选.阴影的尺寸.
color可选.阴影的颜色.请参阅 CSS 颜色值.
inset可选.将外部阴影 (outset) 改为内部阴影.

设置 table 边框合并为单一边框

table {
    border-collapse: collapse;
}

取消 DIV 的浮动

clear: both; /*在左右两侧不允许出现浮动元素*/

DIV 文本超出后隐藏,并显示为…

white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

响应式

.mario-col-xs-1,
.mario-col-xs-2,
.mario-col-xs-3,
.mario-col-xs-4,
.mario-col-xs-5,
.mario-col-xs-6,
.mario-col-xs-7,
.mario-col-xs-8,
.mario-col-xs-9,
.mario-col-xs-10,
.mario-col-xs-11,
.mario-col-5 {
    position: relative;
    float: left;
}
.mario-col-xs-12 {
    width: 100%;
    position: relative;
}
.mario-col-xs-11 {
    width: 91.66666667%;
}
.mario-col-xs-10 {
    width: 83.33333333%;
}
.mario-col-xs-9 {
    width: 75%;
}
.mario-col-xs-8 {
    width: 66.66666667%;
}
.mario-col-xs-7 {
    width: 58.33333333%;
}
.mario-col-xs-6 {
    width: 50%;
}
.mario-col-xs-5 {
    width: 41.66666667%;
}
.mario-col-xs-4 {
    width: 33.33333333%;
}
.mario-col-xs-3 {
    width: 25%;
}
.mario-col-xs-2 {
    width: 16.66666667%;
}
.mario-col-xs-1 {
    width: 8.33333333%;
}

选择器

选择器示例示例说明CSS
.class.intro选择所有 class=“intro"的元素1
#id#firstname选择所有 id=“firstname"的元素1
**选择所有元素2
elementp选择所有

元素

1
element,elementdiv,p选择所有
元素和

元素

1
element elementdiv p选择
元素内的所有

元素

1
element>elementdiv>p选择所有父级是
元素的

元素

2
element+elementdiv+p选择所有紧接着
元素之后的

元素

2
[attribute][target]选择所有带有 target 属性元素2
[attribute=value][target=-blank]选择所有使用 target="-blank"的元素2
[attribute~=value][title~=flower]选择标题属性包含单词"flower"的所有元素2
[attribute|=language][lang|=en]选择一个 lang 属性的起始值=“EN"的所有元素2
:linka:link选择所有未访问链接1
:visiteda:visited选择所有访问过的链接1
:activea:active选择活动链接1
:hovera:hover选择鼠标在链接上面时1
:focusinput:focus选择具有焦点的输入元素2
:first-letterp:first-letter选择每一个

元素的第一个字母

1
:first-linep:first-line选择每一个

元素的第一行

1
:first-childp:first-child指定只有当

元素是其父级的第一个子级的样式。

2
:beforep:before在每个

元素之前插入内容

2
:afterp:after在每个

元素之后插入内容

2
:lang(language)p:lang(it)选择一个 lang 属性的起始值=“it"的所有

元素

2
element1~element2p~ul选择 p 元素之后的每一个 ul 元素3
[attribute^=value]a[src^=“https”]选择每一个 src 属性的值以"https"开头的元素3
[attribute$=value]a[src$=".pdf”]选择每一个 src 属性的值以”.pdf"结尾的元素3
[attribute*=value]a[src*=“runoob”]选择每一个 src 属性的值包含子字符串"runoob"的元素3
:first-of-typep:first-of-type选择每个 p 元素是其父级的第一个 p 元素3
:last-of-typep:last-of-type选择每个 p 元素是其父级的最后一个 p 元素3
:only-of-typep:only-of-type选择每个 p 元素是其父级的唯一 p 元素3
:only-childp:only-child选择每个 p 元素是其父级的唯一子元素3
:nth-child(n)p:nth-child(2)选择每个 p 元素是其父级的第二个子元素3
:nth-last-child(n)p:nth-last-child(2)选择每个 p 元素的是其父级的第二个子元素,从最后一个子项计数3
:nth-of-type(n)p:nth-of-type(2)选择每个 p 元素是其父级的第二个 p 元素3
:nth-last-of-type(n)p:nth-last-of-type(2)选择每个 p 元素的是其父级的第二个 p 元素,从最后一个子项计数3
:last-childp:last-child选择每个 p 元素是其父级的最后一个子级。3
:root:root选择文档的根元素3
:emptyp:empty选择每个没有任何子级的 p 元素(包括文本节点)3
:target#news:target选择当前活动的#news 元素(包含该锚名称的点击的 URL)3
:enabledinput:enabled选择每一个已启用的输入元素3
:disabledinput:disabled选择每一个禁用的输入元素3
:checkedinput:checked选择每个选中的输入元素3
:not(selector):not(p)选择每个并非 p 元素的元素3
::selection::selection匹配元素中被用户选中或处于高亮状态的部分3
:out-of-range:out-of-range匹配值在指定区间之外的 input 元素3
:in-range:in-range匹配值在指定区间之内的 input 元素3
:read-write:read-write用于匹配可读及可写的元素3
:read-only:read-only用于匹配设置 “readonly”(只读) 属性的元素3
:optional:optional用于匹配可选的输入元素3
:required:required用于匹配设置了 “required” 属性的元素3
:valid:valid用于匹配输入值为合法的元素3
:invalid:invalid用于匹配输入值为非法的元素3