evantianx / Bloooooooog

Place to record what I thought and learned
0 stars 0 forks source link

818 CSS 属性 #29

Open evantianx opened 7 years ago

evantianx commented 7 years ago

目录: Custom Properties

参考文章:

It’s Time To Start Using CSS Custom Properties Making Custom Properties (CSS Variables) More Dynamic

evantianx commented 7 years ago

Custom Properties(自定义属性)

基本用法:

/*-----声明方法举例-----*/
:root {
  /* 在:root中声明的变量全局皆可访问 */
  --main-color: #4d4e53;
  --main-bg: rgb(255, 255, 255);
  --logo-border-color: rebeccapurple;

  --header-height: 68px;
  --content-padding: 10px 20px;

  --base-line-height: 1.428571429;
  --transition-duration: .35s;
  --external-link: "external link";`
  --margin-top: calc(2vh + 20px);

  /* 可以用在JS中 */
  --foo: if(x > 5) this.width = 10;
}

/*-----使用方法-----*/
.box {
  --box-color:#4d4e53;
  --box-padding: 0 10px;

  padding: var(--box-padding);
}

.box div {
  color: var(--box-color);
}

CSS 自定义属性和all属性

当我们需要初始化某个组件的样式时,我们可以利用all属性:

.my-initial-component {
  all: initial;
}

但是很可惜,目前为止all属性的重置范围不包括自定义属性,目前解决方案正在商议中。

CSS 自定义属性使用场景

CSS 自定义属性的浏览器支持

目前各大浏览器都提供了很好的支持(除 IE 外 🙄 )

若需要兼容旧式浏览器,在 CSS 中:

@supports ((--a: 0)) {
   /* supported */
}

@supports (not(--a: 0)) {
  /* not supported */
}

在 JS 中:

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

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

这种兼容方式还需要浏览器支持@supports,略为不妥。

CSS3 自定义属性用来修改属性集中的单个属性

:root {
  --box-shadow-offset-x: 10px;
  --box-shadow-offset-y: 2px;
  --box-shadow-blur: 5px;
  --box-shadow-spread: 0;
  --box-shadow-color: #333;
}

.element {
  box-shadow:
    var(--box-shadow-offset-x)
    var(--box-shadow-offset-y)
    var(--box-shadow-blur)
    var(--box-shadow-spread)
    var(--box-shadow-color);
}

并不存在box-shadow-color这个属性,但我们利用 CSS3 自定义属性还是可以单独地去修改它。

Now that CSS Custom Properties are a Thing, All Value Parts Can Be Changed Individually

Using CSS variables correctly

合理使用自定义属性,亮点在于通过媒体查询来设置自定义属性值,这样可以使代码更为简洁

Everything you need to know about CSS Variables

evantianx commented 7 years ago

CSS3 / CSS4 中的各种伪类或伪元素

:focus-within

The :focus-within pseudo class

:focus-within不同于:focus,前者在本身获取到焦点或子元素获取到焦点的时候激活,后者只有本身获取到焦点的时候才激活。

目前各大浏览器支持度还挺低

the-focus-within-pseudo

evantianx commented 7 years ago

CSS2 中属性

参考文章:

outline-offset

outline-offset

设置outlineborder之间的距离,值可以设置为负数, 0, 正数,不能为百分数

不同于borderoutline不占据空间,即不会破坏布局。

outlineoutline-widthoutline-styleoutline-width的缩写,不包括outline-offset!

关于 float

How browsers position floats

evantianx commented 7 years ago

参考文章: Fun with Viewport Units

Viewport Units

分别有 vw, vh, vmin, vmax 四个视口单位。

响应式字体

根据设备尺寸调整显示字体的大小


h1 {
font-size: calc(1.2em + 3vw);
}

@media (min-width: 50em) { h1 { font-size: 50px; } }

evantianx commented 6 years ago

filter: drop-shadow()

box-shadow 不同的是,阴影是按照元素形状生成的,而非 box 盒子阴影。

breaking-css-box-shadow-vs-drop-shadow