• css常见问题处理


    1:禁止文字被复制粘贴

    1.1 Css 处理

    <div class="text">我不可以复制信息div>
    <div>
        我可以复制
    div>
    <style>
        .text {
            -webkit-user-select: none;
        }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1.2 Js 处理

    <script>
        <!-- 禁止右键 -->
        document.oncontextmenu = () => false
    	<!-- 禁止文字选择 -->	
    	document.onselectstart = () => false
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2:元素垂直水平居中

    2.1:方案一

    使用弹性布局,父元素设置display: flex另外再设置属性:水平布局justify-content: center;和垂直布局 align-items: center;

    <div class="ha"><div class="child">div>div>
    <style>
        .ha {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 200px;
            height: 200px;
            background-color: #000;
        }
        .child {
            width: 80px;
            height: 80px;
            background-color: blue;
        }  
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2.2 方案二

    position: absolute; 位移属性

    使用position: absolute; 属性各百分之五十,top: 50%;left: 50%;然后margin减去元素的一半(在已知宽高情况下);

    <div class="ha"><div class="child">div>div>
    <style>	
        .ha {
            width: 600px;
            height: 600px;
            background-color: grey;
            position: relative;
        }
        .child {
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -100px;
            margin-left: -100px;
            width: 200px;
            height: 200px;
            background-color: #000;
        }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.3 方案三

    使用calc()函数进行计算,这个方法和上面方案二类似

    <div class="ha"><div class="child">div>div>
    <style>
        .ha {
            width: 600px;
            height: 600px;
            background-color: grey;
            position: relative;
        }
        .child {
            position: absolute;
            top: calc(50% - 100px);
            left: calc(50% - 100px);
            width: 200px;
            height: 200px;
            background-color: blue;
        }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.4 方案四

    使用 平移属性 transform: translate(x轴,y轴)进行位移,各减去元素宽高的一半

    margin 和 calc() 函数是在已知元素宽高的情况下进行的处理,在不知道元素宽高的情况下,可以使用transform平移属性进行处理

    .child {
        position: absolute;
        top: 50%;
        left: 50%;
        // transform: translate(-100px, -100px);  //已知元素宽高
        transform: translate(-50%, -50%);  // 元素比例
        width: 200px;
        height: 200px;
        background-color: blue;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.5 方案五

    使用网格布局 父元素声明 display: grid; 子元素使用 align-self: center; justify-self: center;

    <div class="ha"><div class="child">div>div>
    <style>
        .ha {
            width: 600px;
            height: 600px;
            background-color: grey;
            display: grid;
        }
        .child {
           align-self: center;
           // margin: 0 auto;   // 让元素水平居中
           justify-self: center;  // 网格布局的属性
           width: 200px;
           height: 200px;
           background-color: blue;
        }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    只在父元素上添加属性

    display: grid; align-items: center; justify-content: center;

    .ha {
        width: 600px;
        height: 600px;
        background-color: grey;
        display: grid;
        align-items: center;
        justify-content: center;
    }
    .child {
        width: 200px;
        height: 200px;
        background-color: blue;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    MYSQL 主从复制与读写分离
    微信小程序 vue+uniapp书架小说阅读推荐系统
    【wp】2023第七届HECTF信息安全挑战赛 Web
    NFTScan 正式上线 TON NFTScan 浏览器!
    聚焦AI丨车企如何用AI服务争夺市场话语权
    第十课 贪心
    分库分表番外:多数据源/动态数据源实现
    【笔录】TVP技术沙龙:寻宝AI时代
    FL Studio All Plugins Edition2024中文完整版Win/Mac
    合并报表软件选哪个?这篇文章两分钟告诉你!
  • 原文地址:https://blog.csdn.net/weixin_42400404/article/details/133755569