当前位置: 首页 > news >正文

css常见问题处理

文章目录

    • 1:禁止文字被复制粘贴
      • 1.1 Css 处理
      • 1.2 Js 处理
    • 2:元素垂直水平居中
      • 2.1:方案一
      • 2.2 方案二
      • 2.3 方案三
      • 2.4 方案四
      • 2.5 方案五

1:禁止文字被复制粘贴

1.1 Css 处理

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

1.2 Js 处理

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

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>

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>

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>

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;
}

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>

只在父元素上添加属性

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;
}
http://www.lryc.cn/news/193374.html

相关文章:

  • 蓝桥杯(迷宫,C++)
  • Python爬虫selenium安装谷歌驱动解决办法
  • 生信教程:使用拓扑加权探索基因组进化(3)
  • React js原生 详解 HTML 拖放 API(鼠标拖放功能)
  • LiveMedia视频中间件如何与第三方系统实现事件录像关联
  • 机器学习-有监督算法-决策树和支持向量机
  • luffy项目之后台项目搭建、目录调整、封装日志、全局异常、Response、数据库连接
  • C++标准模板(STL)- 类型支持 (数值极限,min_exponent10,max_exponent,max_exponent10)
  • linux 服务器类型Apache配置https访问
  • langchain 加载各种格式文件读取方法
  • 飞花令游戏(Python)
  • 解决“413 Request Entity Too Large”错误 代表请求包太大,服务器拒绝响应
  • MoeCTF2023web
  • C语言编写简易图书管理系统
  • C++入门 第一篇(C++关键字, 命名空间,C++输入输出)
  • python股票波动性分析
  • 53 打家劫舍
  • CentOS 7 基于C 连接ZooKeeper 客户端
  • 2023-2024-1 for循环-1(15-38)
  • 初级问题 程序中的变量是指什么?中级问题 把若干个数据沿直线排列起来的数据结构叫作什么?高级问题 栈和队列的区别是什么?
  • clickhouse数据库简介,列式存储
  • flask 发送ajax
  • Android Gradle 命令打包AAR
  • 如何导出带有材质的GLB模型?
  • C/C++面试常见知识点
  • 详细介绍数据结构-堆
  • 001flutter基础学习
  • leetCode 1143.最长公共子序列 动态规划 + 图解
  • 解密人工智能:KNN | K-均值 | 降维算法 | 梯度Boosting算法 | AdaBoosting算法
  • Python深度学习实践