当前位置:网站首页>前端工程师需要懂的前端面试题(c s s方面)总结(二)
前端工程师需要懂的前端面试题(c s s方面)总结(二)
2020-11-06 20:48:00 【叫我詹躲躲】
实现元素水平垂直居中的几种方法:
<div id="wrap">
<div class="box"></div>
</div>
1. 定位方法实现水平垂直居中
<style> *{
margin: 0;
padding: 0;
}
#wrap {
width: 500px;
height: 500px;
background: grey;
position: relative;
}
#wrap .box {
width: 200px;
height: 200px;
background: pink;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
position: absolute;
}
</style>
定位和transform方法实现元素水平垂直居中
<style> *{
margin: 0;
padding: 0;
}
#wrap {
width: 500px;
height: 500px;
background: grey;
position: relative;
}
#wrap .box {
width: 200px;
height: 200px;
background: pink;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
3. 最新版本的flex实现元素的水平垂直居中
<style> *{
margin: 0;
padding: 0;
}
#wrap {
width: 500px;
height: 500px;
background: grey;
display: flex;
justify-content: center;
align-items: center;
}
#wrap .box {
width: 200px;
height: 200px;
background: pink;
}
</style>
4. 使用老版本flex实现元素水平垂直居中
<style> *{
margin: 0;
padding: 0;
}
#wrap {
width: 500px;
height: 500px;
background: grey;
display: -webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
}
#wrap .box {
width: 200px;
height: 200px;
background: pink;
}
</style>
用纯css创建一个三角形
主要是把高度和宽度设置成为0,用边框来实现三角形效果
html代码:
<div id="box">
</div>
css代码:
<style> *{
margin: 0;
padding: 0;
}
#box {
width: 0px;
height: 0px;
border: 100px solid ;
border-top-color: red;
border-right-color: blue;
border-left-color: yellowgreen;
border-bottom-color: deeppink;
}
</style>

由上图效果可以根据自己需要三角形类型改成相应边框的颜色,不需要的边框设置成transparent
如例子:左边和上边的边框设置成红色
#box {
width: 0px;
height: 0px;
border: 100px solid ;
border-top-color: red;
border-right-color: transparent;
border-left-color: red;
border-bottom-color:transparent ;
}

如何实现移动端rem适配
html根元素的字体大小设置屏幕区域的宽
<div id="box">
</div>
<style> *{
margin: 0;
padding: 0;
}
#box {
width: 1rem;
height: 1rem;
background: red;
}
</style>
<script type="text/javascript"> window.onload = function () {
// 获取屏幕区域宽度
var width=document.documentElement.clientWidth
// 获取html
var htmlNode = document.querySelector('html')
// 设置字体大小
htmlNode.style.fontSize= width + 'px'
}
</script>
版权声明
本文为[叫我詹躲躲]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3995971/blog/4558927
边栏推荐
- Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】
- Using consult to realize service discovery: instance ID customization
- Nodejs crawler captures ancient books and records, a total of 16000 pages, experience summary and project sharing
- 2019年的一个小目标,成为csdn的博客专家,纪念一下
- 一篇文章带你了解CSS 分页实例
- xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
- 速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)
- 5.4 static resource mapping
- 100元扫货阿里云是怎样的体验?
- Listening to silent words: hand in hand teaching you sign language recognition with modelarts
猜你喜欢
“颜值经济”的野望:华熙生物净利率六连降,收购案遭上交所问询
前端都应懂的入门基础-github基础
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
小程序入门到精通(二):了解小程序开发4个重要文件
Vue 3 responsive Foundation
Summary of common string algorithms
hadoop 命令总结
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
多机器人行情共享解决方案
I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think
随机推荐
ipfs正舵者Filecoin落地正当时 FIL币价格破千来了
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
Summary of common algorithms of binary tree
TRON智能钱包PHP开发包【零TRX归集】
6.5 request to view name translator (in-depth analysis of SSM and project practice)
How to become a data scientist? - kdnuggets
[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor
5.4 static resource mapping
How to select the evaluation index of classification model
50 + open source projects are officially assembled, and millions of developers are voting
Not long after graduation, he earned 20000 yuan from private work!
人工智能学什么课程?它将替代人类工作?
Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)
PHP应用对接Justswap专用开发包【JustSwap.PHP】
The choice of enterprise database is usually decided by the system architect - the newstack
多机器人行情共享解决方案
Installing the consult cluster
Listening to silent words: hand in hand teaching you sign language recognition with modelarts
Architecture article collection
速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)