每日一文

CSS一些小技巧 二

2021-03-03  本文已影响0人  永恒即是最美

用CSS计算选中标签元素

通常都是用JS来处理已经选择了的标签数量,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中创建或者重置计算器 */
        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);
        }

选择器优先级问题

<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,如果设置了内边距或者边框都会使该元素宽高大于指定的宽高值。
设置box-sizing值为border-box,则height和width属性会设置为内容、内边距以及边框的大小总和。

//设置全局盒模型
*, 
::before,
::after{
    box-sizing: box;
}

但是用第三方UI库时,会有冲突的情况。此时可以使用css继承来解决问题。

:root{
    box-sizing: border-box;
}
*, 
::before,
::after{
    box-sizing: inherit;    //盒模型通常不会被继承,但使用inherit关键字会强制继承
}
//此时选中第三方组件的顶级容器,将其恢复为content-box。这样组件的内部元素就会继承盒模型,以此区分开来
.third-component{
    box-sizing: content-box;
}

字体随页面变换大小

//0.5em保证了最小字号,1vw则确保了字体能随着视口缩放。能保证基础字号从iPhone6里的11.75px一直过渡到1200px屏幕的20px。
 :root {
    font-size: calc(0.5em + 1vw);
 }

动态placeholder效果

具体效果:输入框处于聚焦状态时,输入框的占位符内容以动画形式移动到左上角作为标题。

    <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>
上一篇 下一篇

猜你喜欢

热点阅读