css常见问题处理

文章目录

1:禁止文字被复制粘贴

1.1 Css 处理

html 复制代码
<div class="text">我不可以复制信息</div>
<div>
    我可以复制
</div>
<style>
    .text {
        -webkit-user-select: none;
    }
</style>

1.2 Js 处理

javascript 复制代码
<script>
    <!-- 禁止右键 -->
    document.oncontextmenu = () => false
	<!-- 禁止文字选择 -->	
	document.onselectstart = () => false
</script>

2:元素垂直水平居中

2.1:方案一

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

html 复制代码
<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>

2.2 方案二

position: absolute; 位移属性

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

html 复制代码
<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>

2.3 方案三

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

html 复制代码
<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>

2.4 方案四

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

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

css 复制代码
.child {
    position: absolute;
    top: 50%;
    left: 50%;
    // transform: translate(-100px, -100px);  //已知元素宽高
    transform: translate(-50%, -50%);  // 元素比例
    width: 200px;
    height: 200px;
    background-color: blue;
}

2.5 方案五

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

html 复制代码
<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>

只在父元素上添加属性

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

css 复制代码
.ha {
    width: 600px;
    height: 600px;
    background-color: grey;
    display: grid;
    align-items: center;
    justify-content: center;
}
.child {
    width: 200px;
    height: 200px;
    background-color: blue;
}
相关推荐
[email protected]2 分钟前
Asp.Net Core SignalR导入数据
前端·后端·asp.net·.netcore
小满zs5 小时前
Zustand 第五章(订阅)
前端·react.js
涵信6 小时前
第一节 基础核心概念-TypeScript与JavaScript的核心区别
前端·javascript·typescript
谢尔登6 小时前
【React】常用的状态管理库比对
前端·spring·react.js
编程乐学(Arfan开发工程师)6 小时前
56、原生组件注入-原生注解与Spring方式注入
java·前端·后端·spring·tensorflow·bug·lua
小公主6 小时前
JavaScript 柯里化完全指南:闭包 + 手写 curry,一步步拆解原理
前端·javascript
姑苏洛言8 小时前
如何解决答题小程序大小超过2M的问题
前端
GISer_Jing9 小时前
JWT授权token前端存储策略
前端·javascript·面试
开开心心就好9 小时前
电脑扩展屏幕工具
java·开发语言·前端·电脑·php·excel·batch
拉不动的猪9 小时前
es6常见数组、对象中的整合与拆解
前端·javascript·面试