常用前端布局,CSS技巧介绍
常用前端布局,CSS技巧介绍
对前端常用布局的整理总结,并对其性能优劣,兼容等情况进行介绍
css常用技巧之可变大小正方形的绘制
1:若通过设置width为百分比的方式,则高度不能通过百分比来控制.
在这个地方可以使用padding来实现,首先,元素的高度=height+padding*2+border*2;所以我们可以将widht设置为0,
然后用padding来实现盒子的高度(若元素padding的值是一个百分比,则是基于其父元素的宽度来计算的)
width: 50%;
height: 0;
background: red;
padding-bottom: 50%;
2:通过在元素中添加一个正方形的图片实现,设置图片宽度100%,高度auto,元素不设置高度即可。(不推荐,特别是九宫格)
3:通过vw,vh来实现。(有一定的兼容性问题)
元素高度可变,需要元素内部元素水平垂直居中(主要是垂直)方案
1:通过display:table实现,给最外部元素display:table,同时添加一个次外层元素
设置display:table-cell,vertical-align: middle;这时第三层的元素即可垂直居中。
<div class="container">
<div class="center">
<span>
</span>
</div>
</div>
.container {
display:table;
widht:50%;
height:50vw;
}
.center {
display: table-cell;
vertical-align: middle;
}
.center > span {
display: block;
widht: 20px;
height: 20px;
background: #ff0000;
margin: 0 auto;
}
2:通过flex布局,父元素设置为justify-content:center,align-items:center;
3:通过grid布局,父元素设置为justify-content:center,align-items:center;
4:通过position + transform(margin)实现,如果使用margin则需要知道子元素的宽高且这个宽高不可变(这是由于magin如果设置为百分比是基于父元素的widht来计算的),
如果使用transform则没有这个限制(transform的值是基于元素本身的widht和height来计算的),但又一点兼容的问题(推荐使用transform的方式)
<div class="parent">
<div class="child"></div>
</div>
.parent {
width: 200px;
height: 200px;
position: relative;
background: yellow;
}
//transform: translate(-50%, -50%);
.child {
position: absolute;
width: 100px;
height: 100px;
left: 50%;
top: 50%;
margin: -50px 0 0 -50px;
background: green;
}
对于单行文本和多行文本超出部分 ...
//注意是单行文本可用
<p class="text">
这时一段单行文本
</p>
.text{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 200px;
}
//多行文本 需要注意盒子的高度要调整适当,不然会出行最后一行文字只显示一半的情况
//该方法适用于WebKit浏览器及移动端
<p class="text">
这时一段多行文本
</p>
.text{
height: 100px;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 7;
overflow: hidden;
}
自定义文本选择样式
<p class="custom-text-selection">Select some of this text.</p>
//全局设置选中文本样式
::selection {
background: aquamarine;
color: black;
}
//针对某个class设置
.custom-text-selection::selection {
background: deeppink;
color: white;
}
杂项
//文字阴影
text-shadow: 1px 1px #ad6969;
//CSS3 filter(滤镜) 属性
fliter: blur(50px); //设置高斯模糊 单位px 会导致边缘变大
filter: grayscale(100%); //灰度100%
filter: brightness(150%) //设置亮度
filter: contrast(180%); //设置对比度
...
clip: rect(0,0,0,0) //元素剪切(只在定位元素中生效)
cubic-bezier属性 三阶贝塞尔曲线 用户控制动画速度
用法:transition: transform 1s cubic-bezier();
user-select: none; //文本不能选择
css变量使用
//基础用法
<p class="var-dynamic">var-dynamic</p>
.body {
--customize-height: 100px;
}
.var-dynamic {
height: var(--customize-height);
}
//扩展高级 通过js来控制css中的变量值
<p class="var-dynamic">var-dynamic</p>
.var-dynamic {
height: var(--customize-height);
}
<script>
var varDynamic = document.querySelect(".var-dynamic");
el.style.setProperty('--customize-height', 200 + 'px')
</script>
//和less sass等预处理器的变量比较
1:预处理器的变量不是实时的,在media中试图改变变量的值是不可行的,css变量可以
2:不继承,不级联
border为1像素的边框
//由于设备的dpr不同,我们设置的1px边框在不能的设备中呈现的效果不一。
//那么通过设备dpr来设置不同的border的值不可以吗?
//我们需要知道border-widht的最低值为1px,低于1的无效
//使用 box-shadow: 0 0 0 0.5px; 来处理
//Browser Support 95.5%
.hairline-border {
box-shadow: 0 0 0 1px;
}
@media (min-resolution: 2dppx) {
.hairline-border {
box-shadow: 0 0 0 0.5px;
}
}
@media (min-resolution: 3dppx) {
.hairline-border {
box-shadow: 0 0 0 0.33333333px;
}
}
@media (min-resolution: 4dppx) {
.hairline-border {
box-shadow: 0 0 0 0.25px;
}
}
对于列表元素针对某个元素的样式做单独处理(:not 选择器)
//对每个li元素 添加右border 最后一个不加
<ul class="css-not-selector-shortcut">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
//传统解决
li {
border-right: 2px solid #d2d5e4;
}
li:last-child {
border-right: none;
}
//简洁写法
li:not(:last-child) {
border-right: 2px solid #d2d5e4;
}
//关于not的其他写法
li:not(.a) //not中放入其他选择器
ui:hover li:not(:hover) {
opacity: 0.5;
}
css三角形的绘制
<div class="triangle"></div>
//通过border的方式间接实现
.triangle {
width: 0;
height: 0;
border-top: 20px solid #333;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
//css3 clip-path裁剪属性
clip-path: polygon(50% 0px, 100% 100%, 0px 100%);
一个小小的loading动画
<div class="donut"></div>
@keyframes donut-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.donut {
display: inline-block;
border: 4px solid rgba(0, 0, 0, 0.1);
border-left-color: #7983ff;
border-radius: 50%;
width: 30px;
height: 30px;
animation: donut-spin 1.2s linear infinite;
}
css实现switch
<input type="checkbox" id="toggle" class="offscreen" />
<label for="toggle" class="switch"></label>
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
background-color: rgba(0, 0, 0, 0.25);
border-radius: 20px;
transition: all 0.3s;
}
.switch::after {
content: '';
position: absolute;
width: 18px;
height: 18px;
border-radius: 18px;
background-color: white;
top: 1px;
left: 1px;
transition: all 0.3s;
}
input[type='checkbox']:checked + .switch::after {
transform: translateX(20px);
}
input[type='checkbox']:checked + .switch {
background-color: #7983ff;
}
.offscreen {
position: absolute;
left: -9999px;
}
原文地址:https://segmentfault.com/a/1190000016960443
常用前端布局,CSS技巧介绍的更多相关文章
- 一、HTML和CSS基础--开发工具--Sublime前端开发工具技巧介绍
下载:官网下载(根据系统下载) 安装:按步骤安装即可 注意:当前稳定版本为2,但3的功能有提升:Mac和Windows下的快捷键不同 优点:启动速度快,界面简洁,可以直接打开图片. 1 菜单栏主要功能 ...
- 前端部分-CSS基础介绍
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素.也就是定义相应的标签语言来定制显示样式达到一定的显示效果. 每个CSS样式由两个组成部分:选择器和 ...
- Web设计之网页布局CSS技巧
1.两列布局 1.1.左列固定,右列自适应 #left{ width:190px; float:left; } #right{ margin-left:205px; } 1.2.右列固定, ...
- 学无止境的CSS(xHTML+CSS技巧教程资源大全)
本文里面收集一些有关CSS的技巧.教程.工具和观点等,其中一些你也许早就运用的炉火纯青,也可能有的你听都没听说过.不管是新手还是高手,大家都继续学习吧. 一,Web 标准 要玩游戏,就得先了解规则.要 ...
- android中常用的布局管理器
Android中的几种常用的布局,主要介绍内容有: View视图 RelativeLayout 相对布局管理器 LinearLayout 线性布局管理器 FrameLayout ...
- DIV+CSS常用网页布局技巧!
以下是我整理的DIV+CSS常用网页布局技巧,仅供学习与参考! 第一种布局:左边固定宽度,右边自适应宽度 HTML Markup <div id="left">Left ...
- 猪齿鱼平台常用前端css实现方案
居中 最常用的height + line-height,以及margin:0 auto的居中方式就不再阐述,以下介绍两种容错性高的实现方案. flex布局实现 猪齿鱼前端日常开发中,我们多以fle ...
- css布局左右技巧分享
无意之间发现左右侧布局很多技巧在里边,接下来分享下实例: <div style="width:40px;height:36px;float:left;overflow:hidden; ...
- 第八十四节,css布局小技巧及font-awesome图标使用
css布局小技巧及font-awesome图标使用 图片鼠标放上去遮罩效果,显示文字 当鼠标放上去时 /*最外层div*/ .a{ width: 384px; height: 240px; backg ...
随机推荐
- Rails 异常处理 && 性能
Rails 异常处理 的多种处理方法 1. routes match '*path', via: :all, to: 'controller#action' 2. application.rb 的 ...
- java中tcp小样例
服务端: ServerSocket service = new ServerSocket(7777); Socket socket = service.accept(); InputStream in ...
- POJ3185 The Water Bowls 反转(开关)
Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...
- luogu3953 逛公园
正解:SPFA+DP 将POJ3463中maxDist(Target)由minDist(Target)+1改为minDist(Target+K)即可.判断0环,需要对每个节点建立下标为maxDist- ...
- IJKPlayer问题集锦之不定时更新
1.IJKPlayer 不像系统播放器会给你旋转视频角度,所以你需要通过onInfo的what == IMediaPlayer.MEDIA_INFO_VIDEO_ROTATION_CHANGED去获取 ...
- Shell编程中Shift的用法【转】
本文转载自:http://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html Shell编程中Shift的用法 位置参数可以用shift ...
- Swift - 获取当前时间的时间戳(时间戳与时间互相转换)
(本文代码已升级至Swift3) 1,时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. 2,获取当前时间的时 ...
- C++_class_powerpoint_1.1
Types and Declarations Boolean Type bool type – boolean , logic type bool literal – true, falseint a ...
- Epos消费管理系统复制迁移SQL SERVER 2005数据库
先脱机 原来要关闭Epos消费管理系统软件才可以让对应的数据库脱机
- Juniper路由器
Juniper路由器入门之一:需要子接口的端口配置 set interfaces fe-2/0/1 vlan-tagging ――――在配置接口启用封装VLAN set in ...