?
Hello!小伙伴!
首先非常感謝您閱讀海轟的文章,倘若文中有錯誤的地方,歡迎您指出~
哈哈 自我介紹一下
昵稱:海轟
標簽:程序猿一只|C++選手|學生
簡介:因C語言結識編程,隨后轉入計算機專業,有幸拿過國獎、省獎等,已保研。目前正在學習C++/Linux(真的真的太難了~)
學習經驗:扎實基礎 + 多做筆記 + 多敲代碼 + 多思考 + 學好英語!
上面效果可以概括為:
根據效果圖可以得出實現的一些思路:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<button class="btn"><span>Haihong Pro</span></button>
</body>
</html>
CSS
html,body{
margin: 0;
height: 100%;
}
body{
display: flex;
justify-content: center;
align-items: center;
}
.btn{
width: 390px;
height: 120px;
background: radial-gradient(circle,rgba(247,150,192,1) 0%,rgba(118,174,241,1) 100%);
border: none;
color: #fff;
font-family: 'Lato',sans-serif;
font-weight: 500;
font-size: 48px;
border-radius: 10px;
cursor: pointer;
position: relative;
line-height: 120px;
}
.btn:hover{
background: transparent;
color: #71aef1;
}
.btn span{
width: 100%;
height: 100%;
position: relative;
display: block;
}
.btn::before,
.btn::after{
content: '';
position: absolute;
width: 1px;
height: 0px;
box-shadow: -1px -1px 20px 0px rgba(255,255,255,1),
-4px -4px 5px 0px rgba(255,255,255,1),
10px 10px 20px 0px rgba(0, 0, 0 ,.4),
6px 6px 5px 0px rgba(0, 0, 0 ,.3);
padding: 0;
transition: all 0.8s ease;
}
.btn::before{
top: 0;
right: 0;
}
.btn::after{
bottom:0;
left: 0;
}
.btn:hover::before,
.btn:hover::after{
height: 100%;
}
.btn span::before,
.btn span::after{
content: '';
position: absolute;
width: 0px;
height: 1px;
box-shadow: -1px -1px 20px 0px rgba(255,255,255,1),
-4px -4px 5px 0px rgba(255,255,255,1),
10px 10px 20px 0px rgba(0, 0, 0 ,.4),
6px 6px 5px 0px rgba(0, 0, 0 ,.3);
padding: 0;
transition: all 0.8s ease;
}
.btn span::before{
top:0;
left: 0;
}
.btn span::after{
bottom: 0;
right: 0;
}
.btn span:hover::before,
.btn span:hover::after{
width: 100%;
}
1.怎么實現下圖中的效果呢?
其實這個利用boder-shadow就可以實現了,對每一條直線都添加一下陰影效果就可以了
box-shadow:
-1px -1px 20px 0px rgba(255, 255, 255, 1),
-4px -4px 5px 0px rgba(255, 255, 255, 1),
10px 10px 20px 0px rgba(0, 0, 0, .4),
6px 6px 5px 0px rgba(0, 0, 0, .3);
2.偽類元素的位置
與上一篇文章的區別在于,這里button的兩個偽類::before和::after的位置有所變化,分別位于左下和右上
初始width都為1px,height為0
注:這里為了演示,將width/heigth都設置為了10px,背景色為紅色,便于觀察
然后鼠標停留時,利用過渡transition,將height設置為100%,就可以實現左右兩條線的效果了
上下兩條直線就是利用span的兩個偽元素實現的,原理也是一樣的,這里就不再贅述了。
1.忘了將span的position設置為relative
2.沒有記得將display設置為block
因為span不是塊級元素,這里需要的是塊級元素,如果沒有聲明為塊級元素,就會出現下面的結果:
注:這里使用了紅色背景,以便觀察
希望對您有所幫助
如有錯誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
如果您覺得寫得可以的話
請點個贊吧
謝謝支持 ??
這里是云端源想IT,幫你輕松學IT”
嗨~ 今天的你過得還好嗎?
幸福從來不曾遠離我們
只是有時候它會用試探的方式
看我們還在不在意
- 2024.04.01 -
在CSS的世界里,有些選擇器并不像它們的名字那樣直接。今天,我們要探索的是兩種特殊的選擇器:偽類選擇器和偽元素選擇器。它們雖然名字相似,但功能和用途卻大有不同。
下面就讓我們一起來了解一下它們是如何在我們的頁面布局中扮演著不可或缺的角色的吧。
1、什么是偽類選擇器
偽類選擇器,顧名思義,是一種特殊的選擇器,它用來選擇DOM元素在特定狀態下的樣式。這些特定狀態并不是由文檔結構決定的,而是由用戶行為(如點擊、懸停)或元素的狀態(如被訪問、被禁用)來定義的。
例如,我們可以用偽類選擇器來改變鏈接在不同狀態下的顏色,從而給用戶以視覺反饋。
2、偽類選擇器的語法
selector:pseudo-class {
property: value;
}
a:link {
color: #FF0000;
}
input:focus {
background-color: yellow;
}
注意:偽類名稱對大小寫不敏感;
3、常用的偽類選擇器
下面分別介紹一下比較常用幾類偽類選擇器:
3.1 動態偽類選擇器
這類選擇器主要用于描述用戶與元素的交互狀態。例如:
1):hover:當鼠標懸停在元素上時的樣式。
代碼示例:將鏈接的文本顏色改為紅色
a:hover {
color: red;
}
2):active:當元素被用戶激活(如點擊)時的樣式。
代碼示例:將按鈕的背景色改為藍色
button:active {
background-color: blue;
}
3):focus:當元素獲得焦點(如輸入框被點擊)時的樣式。
代碼示例:將輸入框的邊框顏色改為綠色
input:focus {
border-color: green;
}
4):visited:用于設置已訪問鏈接的樣式,通常與:link一起使用來區分未訪問和已訪問的鏈接。
代碼示例:將已訪問鏈接的顏色改為紫色
a:visited {
color: purple;
}
3.2 UI元素狀態偽類選擇器
這類選擇器用于描述元素在用戶界面中的狀態。例如:
1):enabled和:disabled:用于表單元素,表示元素是否可用。
示例:將禁用的輸入框的邊框顏色改為灰色
input:disabled {
border-color: gray;
}
2):checked:用于單選框或復選框,表示元素是否被選中。
示例:將選中的單選框的背景色改為黃色
input[type="radio"]:checked {
background-color: yellow;
}
3.3 結構偽類選擇器
這類選擇器用于根據元素在文檔結構中的位置來選擇元素。例如:
1):first-child:選取父元素的第一個子元素。
示例:將列表中的第一個項目的背景色改為綠色:
li:first-child {
background-color: green;
}
2):last-child:選取父元素的最后一個子元素。
示例:將列表中的最后一個項目的背景色改為紅色:
li:last-child {
background-color: red;
}
3):nth-child(n):選取父元素中第n個子元素。
示例:將列表中的奇數位置的項目的背景色改為藍色:
li:nth-child(odd) {
background-color: blue;
}
3.4 否定偽類選擇器
這類選擇器用于排除符合特定條件的元素。例如:
:not(selector):選取不符合括號內選擇器的所有元素。
示例:將不是段落的元素的背景色改為灰色:
*:not(p) {
background-color: gray;
}
4、常見應用
// 示例:a 標簽的四種狀態,分別對應 4 種偽類;
/* 未訪問的鏈接 */
a:link {
color: blue;
}
/* 已訪問的鏈接 */
a:visited {
color: red;
}
/* 鼠標懸停鏈接 */
a:hover {
color: orange;
}
/* 已選擇的鏈接(鼠標點擊但不放開時) */
a:active {
color: #0000FF;
}
注意:
1、什么是偽元素選擇器
與偽類選擇器不同,偽元素選擇器是用來選擇DOM元素的特定部分,而不是整個元素。它們通常用于處理那些不是由HTML標簽直接表示的內容,比如首行文字、首字母或者生成的內容(如內容前面的編號)。
偽元素選擇器允許我們對頁面上的某些部分進行精確的樣式控制,而這些部分在HTML結構中并不存在。
2、偽元素選擇器語法
selector::pseudo-element {
property: value;
}
p::first-line {
color: #ff0000;
}
h1::before {
content: '?';
}
3、常用偽元素選擇器
偽元素選擇器并不是針對真正的元素使用的選擇器,而是針對CSS中已經定義好的偽元素使用的選擇器,CSS中有如下四種常用偽元素選擇器:first-line、 first-letter、 before、after。
3.1 ::first-line
::first-line表示第一行(第一行內容根據屏幕大小來決定顯示多少字),例如: p::first-line{}。
代碼示例:
<style>
p::first-line{
color: blue;
}
</style>
3.2 ::first-letter
::first-letter表示第一個字母,例如:p::first-letter{}。
代碼示例:
<style>
p::first-letter{
font-size: 30px;
color: blueviolet;
}
</style>
::before表示元素的開始,::after表示元素的最后,before和after必須結合content屬性來使用。
代碼示例:
<style>
p::after{
content: "hahaha";
color: red;
}
p::before{
content: "hehehe";
color: coral;
}
</style>
注意:
想要快速入門前端開發嗎?推薦一個前端開發基礎課程,這個老師講的特別好,零基礎學習無壓力,知識點結合代碼,邊學邊練,可以免費試看試學,還有各種輔助工具和資料,非常適合新手!點這里前往學習哦!云端源想
CSS中的偽類選擇器和偽元素選擇器都是用來選取DOM中特定元素的選擇器。具體區別如下:
總的來說,偽類選擇器關注的是元素在特定狀態下的樣式變化,而偽元素選擇器則是通過創建新的元素來實現特定的樣式效果。兩者都是CSS中非常強大的工具,可以幫助開發者實現復雜的頁面布局和動態效果。
偽類選擇器和偽元素選擇器雖然不是真正的元素,但它們在CSS中扮演著極其重要的角色。了解并熟練運用它們,可以讓你的網頁更加生動、互動性更強,同時也能更好地控制頁面的布局和內容的表現。
我們下期再見!
END
文案編輯|云端學長
文案配圖|云端學長
內容由:云端源想分享
adio、checkbox和switch應該是一個比較常用的html標簽,尤其是在中后臺ERP系統里面更為常見。不過瀏覽器自帶的樣式不怎么好看,而且不同瀏覽器效果也不一樣。出于美化和統一視覺效果的需求,自定義樣式就被提出來了。
純css實現的主要手段是利用label標簽的模擬功能。label的for屬性可以關聯一個具體的input元素,即使這個input本身不可被用戶可見,有個與它對應的label后,用戶可以直接通過和label標簽交互來替代原生的input——而這給我們的樣式模擬留下了空間。簡而言之就是:
隱藏原生input,樣式定義的過程留給label (那為什么不直接改變checkbox的樣式?因為checkbox作為瀏覽器默認組件,樣式更改上并沒有label那么方便,很多屬性對checkbox都是不起作用的,比如background,而label在樣式上基本和div一樣'任人宰割')
而在選擇事件上,由于css的“相鄰選擇符(E+F)”的存在,讓我們可以直接利用html的默認checkbox,免去了js模擬選擇的麻煩。
DEMO的部分CSS3屬性只寫了webkit前綴,所以建議用webkit內核的瀏覽器查看本頁案例,當然只要你給樣式補上對應的瀏覽器前綴,就可以實現更多樣式匹配
HTML代碼:
<!-- input的id必須有,這個是label進行元素匹配所必需的 --> <!-- 可以看到每個input的id和label的“for”屬性對應同一字符串 --> <input type="checkbox" id="checkbox01" /> <label for="checkbox01"></label> <input type="checkbox" id="checkbox02" /> <label for="checkbox02"></label> <input type="checkbox" id="checkbox03" /> <label for="checkbox03"></label> <input type="checkbox" id="checkbox04" /> <label for="checkbox04"></label>
HTML構建完成,接下來是對應的css:
/* 隱藏所有checkbox */ input[type='checkbox'] { display: none; } /* 對label進行模擬.背景圖片隨便拼湊的,不要吐槽品味*/ /* transition效果是做個背景切換效果,這里單純演示而已,實際上這個過渡不加更自然*/ label { display: inline-block; width: 60px; height: 60px; position: relative; background: url(//www.chitanda.me/images/blank.png); background-position: 0 0px; -webkit-transition: background 0.5s linear; } /* 利用相鄰選擇符和checkbox`:checked`的狀態偽類來模擬默認選中效果(就是點擊后那個勾號的效果) */ /*如果這段代碼注釋,點擊后將沒有任何反饋給用戶*/ /*因為label本身是沒有點擊后被選中的狀態的,checkbox被隱藏后,這個狀態只能手動模擬*/ input[type='checkbox']:checked+label { background-position: 0 -60px; }
上面代碼看起來好像也可以了。不過仔細想想,貌似缺了點什么:選項對應的提示文字
對css不了解的新人可能這時候第一反應就是在label后面用p標簽或者span標簽來添加文字。不過這種方式都不怎么優雅。個人建議用css的::before和::after偽元素(::before和:before是一個東西。不過為了把“偽元素”和“偽類”區分出來,W3C建議的寫法是偽元素用::而偽類用:)
/* 偽元素的生效很簡單,定義`content`就好,其余的屬性和普通div一樣 */ label::after { content: attr(data-name); /*利用attr可以減少css代碼量,data-name寫在html部分的label屬性里*/ display: inline-block; position: relative; width: 120px; height: 60px; left: 100%; vertical-align: middle; margin: 10px; }
當然既然可以用::after模擬label的文字,那也就可以用::before模擬label的checkbox樣式,這里就不做解析了。
這里提一下偽類和偽元素的區分:
1)偽類:存在的意義是為了通過選擇器找到那些不存在于DOM樹中的信息以及不能被常規CSS選擇器獲取到的信息。 偽類由一個冒號:開頭,冒號后面是偽類的名稱和包含在圓括號中的可選參數。
常用的偽類:
:active 向被激活的元素添加樣式。 :focus 向擁有鍵盤輸入焦點的元素添加樣式。 :hover 當鼠標懸浮在元素上方時,向元素添加樣式。 :link 向未被訪問的鏈接添加樣式。 :visited 向已被訪問的鏈接添加樣式。 :first-child 向元素的第一個子元素添加樣式。 :checked 向選中的控件元素添加樣式
2)偽元素:偽元素在DOM樹中創建了一些抽象元素,這些抽象元素是不存在于文檔語言里的(可以理解為html源碼);
注意: css3為了區分偽類和偽元素,規定偽類前面有一個冒號,偽元素前面有兩個冒號
常用偽元素:
關于偽元素的講解,可以參考CSS偽類與偽元素總是傻傻分不清,這份總結夠面試用了
::before 為作用元素的第一個子節點插入dom中 ::after 為作用元素的最后一個子節點插入dom中
自定義radio
html代碼:
<input type="radio" id="radio"> <label for="radio"></label>
css代碼:
input{ display:none; } label { display: inline-block; width: 30px; height: 30px; border: 1px solid #333; border-radius: 50%; position: relative; } label::after { -webkit-transition: all .5s ease; -moz-transition: all .5s ease; -o-transition: all .5s ease; -ms-transition: all .5s ease; transition: all .5s ease; cursor: pointer; position: absolute; width: 16px; height: 16px; border-radius: 50%; top: 50%; left: 50%; margin-top:-8px; margin-left:-8px; z-index: 1; content: ''; border:1px solid #333; } input:checked+label::after{ background:red; }
實現效果:
點擊前和點擊后:
自定義checkbox
漂亮的checkbox長這樣的,看著就很可愛
我們可以不追求那么完美的樣式,可以實現下面簡單好看的樣式就可以
html代碼:
<input type="checkbox" id="checkbox"> <label for="checkbox"></label>
css代碼:
input{ display:none; } label { display: inline-block; width: 30px; height: 30px; border: 1px solid #333; position: relative; } label::after { -webkit-transition: opacity .5s ease; -moz-transition: opacity .5s ease; -o-transition: opacity .5s ease; -ms-transition: opacity .5s ease; transition: opacity .5s ease; cursor: pointer; position: absolute; content: ''; opacity: 0; } input:checked+label::after{ border: 2px solid #d73d32; border-top: none; border-right: none; -webkit-transform: rotate(-45deg); -ms-transform: rotate(-45deg); transform: rotate(-45deg); width:20px; height:10px; top:50%; margin-top:-8px; left:50%; margin-left:-10px; opacity: 1.0; }
實現效果:
點擊前和點擊后:
自定義switch
繼續分享一個iOS風格的switch開關按鈕,樣子也非常常見,如圖:
主要是使用了<input ?type="checkbox">來模擬實現,具體的HTML:
html 代碼:
<label><input class="mui-switch" type="checkbox"> 默認未選中</label> <label><input class="mui-switch" type="checkbox" checked> 默認選中</label> <label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默認未選中,簡單的背景過渡效果,加mui-switch-animbg類即可</label> <label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默認選中</label> <label><input class="mui-switch mui-switch-anim" type="checkbox"> 默認未選中,過渡效果,加 mui-switch-anim 類即可</label> <label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默認選中</label>
在實際的使用中后來又增加了兩個過渡效果,分別加?mui-switch-animbg和mui-switch-anim?類即可,具體效果查看下面的demo頁面。
CSS代碼(SCSS導出的,排版有些奇怪):
css 代碼:
剩下部分
這里給出具體的css,方便大家復制本地實現
<style> .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-color: #fdfdfd; box-shadow: #dfdfdf 0 0 0 0 inset; border-radius: 20px; border-top-left-radius: 20px; border-top-right-radius: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; background-clip: content-box; display: inline-block; -webkit-appearance: none; user-select: none; outline: none; } .mui-switch:before { content: ''; width: 29px; height: 29px; position: absolute; top: 0px; left: 0; border-radius: 20px; border-top-left-radius: 20px; border-top-right-radius: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; background-color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); } .mui-switch:checked { border-color: #64bd63; box-shadow: #64bd63 0 0 0 16px inset; background-color: #64bd63; } .mui-switch:checked:before { left: 21px; } .mui-switch.mui-switch-animbg { transition: background-color ease 0.4s; } .mui-switch.mui-switch-animbg:before { transition: left 0.3s; } .mui-switch.mui-switch-animbg:checked { box-shadow: #dfdfdf 0 0 0 0 inset; background-color: #64bd63; transition: border-color 0.4s, background-color ease 0.4s; } .mui-switch.mui-switch-animbg:checked:before { transition: left 0.3s; } .mui-switch.mui-switch-anim { transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s; } .mui-switch.mui-switch-anim:before { transition: left 0.3s; } .mui-switch.mui-switch-anim:checked { box-shadow: #64bd63 0 0 0 16px inset; background-color: #64bd63; transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s; } .mui-switch.mui-switch-anim:checked:before { transition: left 0.3s; } /*# sourceMappingURL=mui-switch.css.map */ </style>
如果你喜歡scss,那么代碼更加簡潔
@mixin borderRadius($radius:20px) { border-radius: $radius; border-top-left-radius: $radius; border-top-right-radius: $radius; border-bottom-left-radius: $radius; border-bottom-right-radius: $radius; } $duration: .4s; $checkedColor: #64bd63; .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-color: #fdfdfd; box-shadow: #dfdfdf 0 0 0 0 inset; @include borderRadius(); background-clip: content-box; display: inline-block; -webkit-appearance: none; user-select: none; outline: none; &:before { content: ''; width: 29px; height: 29px; position: absolute; top: 0px; left: 0; @include borderRadius(); background-color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); } &:checked { border-color: $checkedColor; box-shadow: $checkedColor 0 0 0 16px inset; background-color: $checkedColor; &:before { left: 21px; } } &.mui-switch-animbg { transition: background-color ease $duration; &:before { transition: left 0.3s; } &:checked { box-shadow: #dfdfdf 0 0 0 0 inset; background-color: $checkedColor; transition: border-color $duration, background-color ease $duration; &:before { transition: left 0.3s; } } } &.mui-switch-anim { transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration; &:before { transition: left 0.3s; } &:checked { box-shadow: $checkedColor 0 0 0 16px inset; background-color: $checkedColor; transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3; &:before { transition: left 0.3s; } } } }
鏈接文章
https://www.html.cn/archives/9274
https://segmentfault.com/a/1190000003711140
*請認真填寫需求信息,我們會在24小時內與您取得聯系。