css一些记录

使用Flex布局居中

.box {
    display:flex;
    align-items:center;/*垂直对齐*/
    justify-content:center;/* 水平对齐*/
}

修改input的checkbox样式

第一种

html

<div class="box">
    <input id="fruit" type="checkbox" checked />
    <label for="argee"></label>水果
</div>

sass

.box {
    position: relative;
    label{
        &::before{
            content: '\0020';
            display: inline-block;
            width: 17px;
            height: 17px;
            font-size: 15px;
            margin-right: 10px;
            position: absolute;
            background-color: #fff;
            transform: translate(-1.5em, -0.5em);
        }
    }
    #fruit + label::before{
        border-radius: 3px;
    }
    #fruit{
        display: none;
    }
    #fruit:checked + label:before{
        content: '\2713';
        font-size: 15px;
        color: #cb9f6d;
        text-align: center;
        line-height: 17px;
    }
}

第二种

html

<p class="agreement">
  <input type="checkbox" id="agree" v-model="agree">我接受
</p>

sass

.agreement{
  font-size: 0.3rem;
  text-align: center;
  margin-top: 0.7rem;
  input[type=checkbox]{
    margin-right: 0.1rem;
    outline: none;
    appearance: none;
    width: 0.22rem;
    height: 0.22rem;
    border: 1px solid #29dce3;
    border-radius: 0.05rem;
    &:checked{
      background: url('../assets/image/checkbox.png') center center no-repeat;
      background-size: 0.22rem 0.22rem;
      border-color: transparent;
    }
  }

模拟等待进度(简易)

html

<div class="clipped">
  <div class="top"></div>
</div>

css

div{
  box-sizing: border-box;
}
.clipped {
 position: relative;
 width: 200px;
 height: 200px;
 margin: 15vh auto;
 border-radius: 50%;
 border: 10px solid skyblue;
 background-color: #fff;
}
.top{
   position: absolute;
  width: 100px;
  height: 20px;
  background-color: #fff;
  top: 50%;
  left: 50%;
  transform-origin: 0 10px;
  transform: translate(0,-50%);
  animation: circle1 5s linear 0s infinite;
}

@keyframes circle1 {
  from {
    transform: translate(0,-50%) rotate(-90deg); 
  }
  to { 
    transform: translate(0,-50%) rotate(270deg);
  }
}

css变量

  1. 变量区分大小写
  2. 变量只能用于属性值,不能用于属性名
  3. 变量是字符串可以与其他字符串拼接
div{
    --bar: 'hello';
    --foo: var(--bar)' world';
}
  1. 变量值是数值,则不能与数值单位直接连用,必须使用calc()函数将其连接
div{
    --gap: 20;
    /*下面是无效的*/
    margin: var(--gap)px;
    /*生效*/
    margin: calc(var(--gap)*1px);
}
  1. 如果变量值带有单位,就不能写成字符串
/*无效*/
.foo{
    --foo: '20px';
    font-size: var(--foo);
}
/*生效*/
.foo{
    --foo: 20px;
    font-size: var(-foo);
}
:root{
    /*全局声明变量*/
    --global: #aaa;
}
body{
    /*声明变量*/
    --primary: #ddd;
}
div{
    /*局部声明*/
    --secondary: #eee;
}
div a{
    /*读取变量*/
    color: var(--primary);
    text-decoration-color: var(--secondary);
    border-color: var(--global);
    /*变量默认值,如果变量不存在,则使用后面的值*/
    /*后面的值是以 类似 模板字符串 的形式解析*/
    background: var(--color, #333);
}
/*响应式布局*/
@media screen and (max-width: 768px){
    :root{
        --global: #bbb;
    }
    body{
        --primary: #ccc;
    }
}

js中的css变量

JavaScript 也可以检测浏览器是否支持 CSS 变量

const isSupported =
  window.CSS &&
  window.CSS.supports &&
  window.CSS.supports('--a', 0);

if (isSupported) {
  /* supported */
} else {
  /* not supported */
}

js操作css变量

// 设置变量
document.body.style.setProperty('--primary', '#7F583F');

// 读取变量
document.body.style.getPropertyValue('--primary').trim();
// '#7F583F'

// 删除变量
document.body.style.removeProperty('--primary');

js和css实现通信

document.addEventListener('click',(e)=>{
    const docStyle = document.documentElement.style;
    docStyle.setProperty('--mouse-x',e.clientX);
    docStyle.setProperty('--mouse-y',e.clientY);
    docStyle.setProperty('--if',if(x>5) this.width = 10);
})
div{
    --if: if(x>4) this.width = 10;
}

CSS控制文字,超出部分显示省略号

单行

.ellipsis {
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
}

多行

  1. 移动端和WebKit浏览器
.ellipsis {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
}

-webkit-line-clamp用来限制在一个块元素显示的文本的行数。 为了实现该效果,它需要组合其他的WebKit属性。常见结合属性:

display: -webkit-box; 必须结合的属性 ,将对象作为弹性伸缩盒子模型显示 。

-webkit-box-orient 必须结合的属性 ,设置或检索伸缩盒对象的子元素的排列方式 。

  1. 不管超不超出都显示
p {
    position: relative;
    line-height: 20px;
    max-height: 40px;
    overflow: hidden;
}
p::after {
    content: "...";
    position: absolute;
    bottom: 0;
    right: 0;
    padding-left: 40px;
    background: -webkit-linear-gradient(left, transparent, #fff 55%);
    background: -o-linear-gradient(right, transparent, #fff 55%);
    background: -moz-linear-gradient(right, transparent, #fff 55%);
    background: linear-gradient(to right, transparent, #fff 55%);
}

CSS background-image: 路径中有括号时不显示

在css中设置背景图片,结果地址中有括号,导致图片请求失效

/*
* 这样会不显示
*/
div {
    background-image: url(http://xxxx-(12)-xxx.png)
}
/*
* 下面这样可以显示
*/
div {
    background-image: url('http://xxxx-(12)-xxx.png')
}

原因:括号和url的括号冲突,在地址外面加引号解决识别冲突

文字两端对齐

html

<div>两端</div>
<div>两端对齐</div>

css

div {
    margin: 10px 0;
    width: 100px;
    border: 1px solid;
    text-align: justify;
    text-align-last: justify;
}
div:after {
    content: '';
    display: inline-block;
    width: 100%;
}

一些注意的问题

尽量用padding代替margin

margin-top的塌陷

两个同属一个BFC的相邻盒模型的margin会重叠

position:fixed 降级的问题

如果父元素中有使用transform,那么当前的position:fixed;效果会降级为position:absolute;

解决方案: 当直接父元素的高度和屏幕高度相同时,fixedabsolute效果一样

用vm配合rem

scss

$vm_fontsize: 75;

@function rem($px) {
    @return ($px / $vm_fontsize) * 1rem
}

$vm_design: 750;
html {
    font-size: ($vm_fontsize / ($vm_design / 2)) * 100vw;
    @media screen and (max-width: 320px) {
        font-size: 64px;
    }
    @media screen and (min-width: 540px) {
        font-size: 108px;
    }
}
// body增加限制,防止默认100%宽度的block元素随body过大而变形
body {
    max-width: 540px;
    min-width: 320px;
}

解决1px方案

移动端要处理1px细线问题

css

.border {
    overflow: hidden;
    position: relative;
    border: none!important;
}
.border:after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    width: 200%;
    height: 200%;
    border: 1px solid red;
    transform-origin: 0 0;
    transform: scaleY(0.5);
}

选择除某个元素标签外的所有标签

<!-- 选择除p标签外的所有标签 -->
:not(p) {
    margin: 0;
    padding: 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,809评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,189评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,290评论 0 359
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,399评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,425评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,116评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,710评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,629评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,155评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,261评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,399评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,068评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,758评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,252评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,381评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,747评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,402评论 2 358

推荐阅读更多精彩内容

  • HTML 5 HTML5概述 因特网上的信息是以网页的形式展示给用户的,因此网页是网络信息传递的载体。网页文件是用...
    阿啊阿吖丁阅读 3,904评论 0 0
  • 前端开发面试题 面试题目: 根据你的等级和职位的变化,入门级到专家级,广度和深度都会有所增加。 题目类型: 理论知...
    怡宝丶阅读 2,587评论 0 7
  • 前端开发知识点 HTML&CSS对Web标准的理解、浏览器内核差异、兼容性、hack、CSS基本功:布局、盒子模型...
    Hebborn_hb阅读 845评论 0 1
  • •前端面试题汇总 一、HTML和CSS 21 你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? ...
    Simon_s阅读 2,220评论 0 8
  • 前端必读:浏览器内部工作原理[https://kb.cnblogs.com/page/129756/] 作者: T...
    我是强强阅读 1,151评论 0 2