用CSS計算選中標簽元素
通常都是用JS來處理已經(jīng)選擇了的標簽數(shù)量呵萨,css也可以做到。但是意義不大潮峦,除非單純展示囱皿。
html:
<p>請選擇你感興趣的標簽:</p>
<input type="checkbox" id="topic1"><label for="topic1" class="topic">Vue</label></input>
<input type="checkbox" id="topic2"><label for="topic2" class="topic">react</label></input>
<input type="checkbox" id="topic3"><label for="topic3" class="topic">jquery</label></input>
<input type="checkbox" id="topic4"><label for="topic4" class="topic">css</label></input>
<p>您已選擇<span class="topic-counter"></span>個標簽。</p>
css:
/* 在body中創(chuàng)建或者重置計算器 */
body {
counter-reset: topicCounter;
}
/* 裁剪隱藏checkbox前面的小框耕渴,必須是absolut+clip,換fixed也可 */
[type="checkbox"] {
position: absolute;
clip: rect(0 0 0 0);
}
/*
選中元素其后緊跟的.topic元素,變量遞增
:checked只適用于單選按鈕和復選框
*/
:checked+.topic {
counter-increment: topicCounter;
}
/* 添加到span中 */
.topic-counter::before {
color: red;
content: counter(topicCounter);
}
選擇器優(yōu)先級問題
<div class="bar foo"></div> //與class里順序無關
div{background: orange;}
.foo{background: gray;}
.bar{background: green;} //只與css類順序有關橱脸,此時div為綠色
-------------------------------------------------
//最佳方案:1.自身增強分苇,2.借助存在的屬性選擇器。此時div為灰色
<div class="bar foo"></div>
div{background: orange;}
.foo.foo{background: gray;} //或者:.foo[class]{background: gray; }
.bar{background: green;}
全局盒模型沖突問題
box-sizing的默認值是content-box医寿,如果設置了內(nèi)邊距或者邊框都會使該元素寬高大于指定的寬高值靖秩。
設置box-sizing值為border-box,則height和width屬性會設置為內(nèi)容沟突、內(nèi)邊距以及邊框的大小總和。
//設置全局盒模型
*,
::before,
::after{
box-sizing: box;
}
但是用第三方UI庫時扩劝,會有沖突的情況求橄。此時可以使用css繼承來解決問題葡公。
:root{
box-sizing: border-box;
}
*,
::before,
::after{
box-sizing: inherit; //盒模型通常不會被繼承,但使用inherit關鍵字會強制繼承
}
//此時選中第三方組件的頂級容器催什,將其恢復為content-box。這樣組件的內(nèi)部元素就會繼承盒模型气筋,以此區(qū)分開來
.third-component{
box-sizing: content-box;
}
字體隨頁面變換大小
//0.5em保證了最小字號旋圆,1vw則確保了字體能隨著視口縮放。能保證基礎字號從iPhone6里的11.75px一直過渡到1200px屏幕的20px搀矫。
:root {
font-size: calc(0.5em + 1vw);
}
動態(tài)placeholder效果
具體效果:輸入框處于聚焦狀態(tài)時,輸入框的占位符內(nèi)容以動畫形式移動到左上角作為標題瓤球。
<div class="main">
<div class="input-box">
<input class="input-control input-outline" placeholder="賬號">
<label class="input-label">請輸入賬號</label>
</div>
</div>
<style>
* {
padding: 0;
margin: 0;
border: 0;
}
/* 基本樣式 */
.input-box {
margin: 50px 0 0 50px;
}
input {
width: 100px;
height: 30px;
border: 1px solid #aaa;
outline: none;
padding: 0 10px;
border-radius: 4px;
}
/* 默認的placeholder效果不可見 */
.input-control:placeholder-shown::placeholder {
color: transparent;
}
/* 使用.input-label元素代替瀏覽器原聲的占位符 */
.input-box {
position: relative;
}
.input-label {
position: absolute;
left: 16px;
top: 5px;
pointer-events: none;
color: #ccc;
}
/* 輸入框聚焦以及占位符不顯示的時候對<label>元素進行重定位卦羡,效果是縮小并移動到上方 */
.input-control:not(:placeholder-shown)~.input-label,
.input-control:focus~.input-label {
color: #2486ff;
transform: scale(0.75) translate(-10px, -20px);
transition-duration: 1s;
background: #fff;
}
</style>
mix-blend-mode混合元素
mix-blend-mode可以混合圖片绿饵,還可以把元素的文本和邊框與容器的背景圖片混合在一起
<div class="main">
<div class='blend'>
<h1>熊出沒</h1>
</div>
</div>
<style>
* {
padding: 0;
margin: 0;
border: 0
}
.blend {
background-image: url('https://img6.bdstatic.com/img/image/pcindex/sunjunpchuazhoutu.JPG');
background-size: cover;
background-position: center;
width: 100%;
height: 200px;
}
.blend>h1 {
mix-blend-mode: hard-light;
background-color: #c33;
color: #808080;
border: 0.1em solid #ccc;
border-width: 0.1em 0;
}
</style>