当前位置:网站首页>前端工程师需要懂的前端面试题(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
边栏推荐
- Common algorithm interview has been out! Machine learning algorithm interview - KDnuggets
- 6.1.2 handlermapping mapping processor (2) (in-depth analysis of SSM and project practice)
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- 采购供应商系统是什么?采购供应商管理平台解决方案
- I think it is necessary to write a general idempotent component
- Skywalking series blog 5-apm-customize-enhance-plugin
- html
- ES6学习笔记(五):轻松了解ES6的内置扩展对象
- 数据产品不就是报表吗?大错特错!这分类里有大学问
- vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
猜你喜欢
至联云分享:IPFS/Filecoin值不值得投资?
中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
100元扫货阿里云是怎样的体验?
合约交易系统开发|智能合约交易平台搭建
熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
阿里云Q2营收破纪录背后,云的打开方式正在重塑
Computer TCP / IP interview 10 even asked, how many can you withstand?
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
采购供应商系统是什么?采购供应商管理平台解决方案
随机推荐
Thoughts on interview of Ali CCO project team
Grouping operation aligned with specified datum
华为云“四个可靠”的方法论
数据产品不就是报表吗?大错特错!这分类里有大学问
Vuejs development specification
基於MVC的RESTFul風格API實戰
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
(2)ASP.NET Core3.1 Ocelot路由
人工智能学什么课程?它将替代人类工作?
6.6.1 localeresolver internationalization parser (1) (in-depth analysis of SSM and project practice)
Summary of common algorithms of binary tree
htmlcss
OPTIMIZER_ Trace details
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
带你学习ES5中新增的方法
Save the file directly to Google drive and download it back ten times faster
How to select the evaluation index of classification model
6.3 handlerexceptionresolver exception handling (in-depth analysis of SSM and project practice)
From zero learning artificial intelligence, open the road of career planning!
Serilog原始碼解析——使用方法