当前位置:网站首页>前端工程师需要懂的前端面试题(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
边栏推荐
- 数据产品不就是报表吗?大错特错!这分类里有大学问
- 容联完成1.25亿美元F轮融资
- vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
- 数字城市响应相关国家政策大力发展数字孪生平台的建设
- Didi elasticsearch cluster cross version upgrade and platform reconfiguration
- 关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
- 加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?
- After reading this article, I understand a lot of webpack scaffolding
- Thoughts on interview of Ali CCO project team
- Polkadot series (2) -- detailed explanation of mixed consensus
猜你喜欢

容联完成1.25亿美元F轮融资

axios学习笔记(二):轻松弄懂XHR的使用及如何封装简易axios

Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】

CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识

阿里云Q2营收破纪录背后,云的打开方式正在重塑

带你学习ES5中新增的方法

I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think

中小微企业选择共享办公室怎么样?

EOS创始人BM: UE,UBI,URI有什么区别?

What is the side effect free method? How to name it? - Mario
随机推荐
JVM memory area and garbage collection
This article will introduce you to jest unit test
DevOps是什么
How to get started with new HTML5 (2)
Wiremock: a powerful tool for API testing
PN8162 20W PD快充芯片,PD快充充电器方案
Character string and memory operation function in C language
数字城市响应相关国家政策大力发展数字孪生平台的建设
從小公司進入大廠,我都做對了哪些事?
6.6.1 localeresolver internationalization parser (1) (in-depth analysis of SSM and project practice)
I've been rejected by the product manager. Why don't you know
大数据应用的重要性体现在方方面面
It's so embarrassing, fans broke ten thousand, used for a year!
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
Python crawler actual combat details: crawling home of pictures
Skywalking series blog 1 - install stand-alone skywalking
Classical dynamic programming: complete knapsack problem
Tool class under JUC package, its name is locksupport! Did you make it?
The practice of the architecture of Internet public opinion system
前端都应懂的入门基础-github基础