当前位置:网站首页>前端工程师需要懂的前端面试题(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
边栏推荐
- xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
- Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
- Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)
- How long does it take you to work out an object-oriented programming interview question from Ali school?
- 快快使用ModelArts,零基础小白也能玩转AI!
- Python crawler actual combat details: crawling home of pictures
- 每个前端工程师都应该懂的前端性能优化总结:
- 业内首发车道级导航背后——详解高精定位技术演进与场景应用
- A debate on whether flv should support hevc
- Not long after graduation, he earned 20000 yuan from private work!
猜你喜欢

比特币一度突破14000美元,即将面临美国大选考验

一篇文章带你了解CSS3圆角知识

中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律

Python Jieba segmentation (stuttering segmentation), extracting words, loading words, modifying word frequency, defining thesaurus

Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)

数字城市响应相关国家政策大力发展数字孪生平台的建设

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

做外包真的很难,身为外包的我也无奈叹息。

Mongodb (from 0 to 1), 11 days mongodb primary to intermediate advanced secret

至联云解析:IPFS/Filecoin挖矿为什么这么难?
随机推荐
htmlcss
High availability cluster deployment of jumpserver: (6) deployment of SSH agent module Koko and implementation of system service management
“颜值经济”的野望:华熙生物净利率六连降,收购案遭上交所问询
每个前端工程师都应该懂的前端性能优化总结:
PHP应用对接Justswap专用开发包【JustSwap.PHP】
Vuejs development specification
6.5 request to view name translator (in-depth analysis of SSM and project practice)
Linked blocking Queue Analysis of blocking queue
Calculation script for time series data
axios学习笔记(二):轻松弄懂XHR的使用及如何封装简易axios
6.1.2 handlermapping mapping processor (2) (in-depth analysis of SSM and project practice)
PN8162 20W PD快充芯片,PD快充充电器方案
A debate on whether flv should support hevc
Deep understanding of common methods of JS array
采购供应商系统是什么?采购供应商管理平台解决方案
How to get started with new HTML5 (2)
Python Jieba segmentation (stuttering segmentation), extracting words, loading words, modifying word frequency, defining thesaurus
H5 makes its own video player (JS Part 2)
Keyboard entry lottery random draw
全球疫情加速互联网企业转型,区块链会是解药吗?