代码改变世界

超级实用CSS

2024-03-29 11:09  WEB前端小菜鸟  阅读(16)  评论(0)    收藏  举报

1.美化滚动条

.common_scrollbar {
  &::-webkit-scrollbar {
    /*滚动条整体样式*/
    width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
    height: 4px;
    position: absolute;
  }
  &::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 5px;
    height: 20px;
    // -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    background: transparent;
  }
  &::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    // -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0);
    border-radius: 0;
    background: rgba(235, 232, 232, 0);
  }
  &:hover {
    &::-webkit-scrollbar-thumb {
      /*滚动条里面小方块*/
      border-radius: 5px;
      height: 20px;
      // -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
      background: rgba(155, 153, 153, 0.2);
    }
  }
}

 

2.单行文字溢出时显示省略号

.one-line-ellipsis {
  /* Key Style */
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  max-width: 375px;
}

3.多行文字溢出时显示省略号

.more-line-ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;

  display: -webkit-box;
  /* set n lines, including 1 */
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

4.filter:grayscale(1)”使页面处于灰色模式[通常哪个去世了]

body{
  filter: grayscale(1);
兼容自己百度 }

  

5.“outline:none”去掉输入状态行(就是点击输入框后)

当输入框被选中时,默认会有一个蓝色的状态行,可以使用 outline: none 去掉。

 

6.动态计算宽度或者高度

calc(100vh - 50px);

 请注意,减号前后必须有空格!!! 必须有空格

calc的除法呢 --》 请注意,减号前后必须有空格!!! 必须有空格 /前后也有哈

 width: calc((100% - 20px) / 2);

 

 7.背景图的问题
语法:background: [image] [repeat] [position] / [size] 
eg:background: url('@/static/images/lianxifangshi.png') no-repeat center / 100% auto;
也可以分开自己写:.
background-image: url('@/static/images/lianxifangshi.png');
background-repeat: no-repeat;
background-position: center;【 居中背景图(最常用)
background-size: 100% auto; 【默认值是auto auto 可以省略不写,背景图片保持原始尺寸,不会被拉伸或压缩。】
=================================================================

为避免背景图显示异常,建议始终明确设置background-size:,background-size:100%宽度铺满  auto 高度自适应才不会压缩图片,一般写法就够用了,

cover图片等比缩放至完全覆盖容器,可能裁剪部分边缘。(eg:小狗的耳朵被裁了)

contain图片等比缩放至完全显示在容器内,可能留有空白区域。(eg:小狗全部显示在div,但是div会有留白部分)

100% 100% 图片被拉伸 / 压缩以填满容器,可能导致比例失真;(所以100% auto 保持等比压缩)

 

8. css文字中间加横线如下css
text-decoration:line-through

 

<div>
<sup class="in" >99+</sup>
打死了开发进口量法开始的疯狂了areyouabody
</div>

 

9.手写el-selectr如图,这个小三角

思路就是正常写下拉 v-if --循环选项--写样式--然后定位--最后写这个三角形,让后定位把横线盖着就行(z-index级别比 循环的div高就行)

10.flex布局

问题1:有个bug当flex布局是左边固定右边flxe:1;当我在最右边的div里面 动态添加表格的列的时候页面会被撑开。

 flex: 1;              /* 占满剩余空间 */
 min-width: 0;         /* 允许收缩到0宽度 */
 overflow-x: auto;     /* 超出内容显示滚动条 */

问题2:如何均分布局(div之间的间隔为20px)-关键词gap

.test{
    width: 100%;
    height: 300px;
    background-color: gainsboro;
    display: flex;
    gap: 20px;/* 替代 margin,更简洁的间距控制 */
.son{ flex: 1; height: 100%; background-color: aqua; } }

  

关键词-父元素:gap 子元素:flex;1

 问题3:这种布局怎么解决,数量不固定(最后一行从左往右排列)

.test{
    width: 100%;
    // height: 800px;
    background-color: gainsboro;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap:10px;
    box-sizing: border-box;
    padding: 10px;
    .son{
        width: calc((100% - 30px) / 4); /* 4列布局,预留间距 */
        height: 200px;
        background-color: aqua;
    }
}

  

解决办法:

1.写空的div去补位置,

2.放弃 justify-content: space-between,改用 margin-right: auto 让项目自动填充间距。

3.Grid布局替代更简单,二维布局,flex是一维布局

.test{
    width: 100%;
    // height: 800px;
    background-color: gainsboro;
    display: grid;
    grid-template-columns: repeat(4, 1fr); /* 一行4个,均分宽度 ===grid-template-columns: 1fr 1fr 1fr 1fr;*/  
    gap: 20px;
    box-sizing: border-box;
    padding: 10px;
    .son{
        height: 200px;
        background-color: aqua;
    }
}

 11.flex布局的时候被压缩了(如图中午被压缩了)

image

 

.cm_single {
        margin-bottom: 10px;
        display: flex;
        align-items: flex-start;
        .title {
          flex-shrink: 0; /* 阻止收缩 */
        }
      }

问题2:顶部视觉效果没对齐,改父元素:

align-items: baseline;搞定

image,改后

image

 

 

12.background-color 取值为 none 和 background color transparent 的区别

background-color 取值为 none 不合法的写法,CSS 中并没有 none 这个针对背景颜色的合法取值;由于 none 不是合法取值,当使用时,该样式不会生效,元素会沿用默认的背景颜色(通常是透明,即等同于transparent)或继承父元素的背景颜色。若想设置背景为透明,应使用 background-color: transparent

 

NO最后一个.初始化css(清除默认的margin等)  --参考https://blog.csdn.net/chaoPerson/article/details/130796513

html,
body {
  height: 100%;
  /* 文字风格 Sans-serif 各笔画粗细相同,Serif 笔画粗细不同,monospace 等宽体,cursive草书,fantasy梦幻 */
  font-family: 'Microsoft YaHei', sans-serif, 'Helvetica Neue', Helvetica, Arial,
    '黑体', '宋体', Arial;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
body {
  font-size: 14px;
  color: #333;
}
 
/* 重置各标签的默认样式 */
a,
body,
center,
cite,
code,
dd,
del,
div,
dl,
dt,
em,
fieldset,
figcaption,
figure,
footer,
form,
h1,
h2,
h3,
h4,
h5,
h6,
header,
hr,
html,
img,
input,
label,
legend,
li,
mark,
ol,
p,
section,
span,
textarea,
time,
td,
th,
ul {
  margin: 0;
  border: 0;
  padding: 0;
  font-style: normal;
  box-sizing: border-box;
  /*  自动换行 */
  word-wrap: break-word;
  /*  强制英文单词断行 */
  word-break: break-all;
}
 
/*  设置标签为块级分类 */
article,
aside,
details,
fieldset,
figcaption,
figure,
footer,
header,
main,
nav,
section {
  display: block;
}
 
/* 去除input标签的默认样式 */
button,
input,
textarea {
  -webkit-appearance: none;
  font-family: 'Microsoft YaHei', sans-serif, 'Helvetica Neue', Helvetica, Arial,
    '黑体', '宋体', Arial;
  border: 0;
  margin: 0;
  padding: 0;
  font-size: 1em;
  line-height: 1em;
  outline: none;
  background-color: transparent;
}
 
/*  禁止多文本框手动拖动大小 */
textarea {
  resize: none;
  -webkit-appearance: none;
}
 
/* 去掉按下的阴影盒子 */
input,
textarea,
a {
  -webkit-tap-highlight-color: transparent;
}
 
/*  清除a标签下划线 */
a,
a:visited {
  text-decoration: none;
}
a:focus,
a:active,
a:hover {
  outline: none;
}
 
/*  清除列表前面的点 */
ol,
li,
ul {
  list-style: none;
}
 
/*  清除IE下图片的边框 */
img {
  border-style: none;
  font-size: 0;
}
 
/*  解决chrome浏览器默认黄色背景问题 */
input:-webkit-autofill,
textarea:-webkit-autofill,
select:-webkit-autofill {
  -webkit-box-shadow: 0 0 0 1000px #fff inset;
}
 
/*  设置默认滚动条样式 */
::-webkit-input-placeholder {
  color: #afbdcc;
}
:-moz-placeholder {
  color: #afbdcc;
}
::-moz-placeholder {
  color: #afbdcc;
}
:-ms-input-placeholder {
  color: #afbdcc;
}
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background-color: #f5f5f5;
}
::-webkit-scrollbar-track-piece {
  background-color: #f5f5f5;
  border-radius: 6px;
}
::-webkit-scrollbar-thumb {
  background-color: #cccccc;
  border-radius: 6px;
}
::-webkit-scrollbar-corner {
  background-color: #f5f5f5;
}
::-webkit-resizer {
  background-repeat: no-repeat;
  background-position: bottom right;
}