CSS高度なレイアウトテクニック

3226 ワード

:emptyで空の要素を区別
互換性:IE 8はサポートされていません
Demo
次のリストがあります.
a
b

  

我们希望可以对空元素和非空元素区别处理,那么有两种方案。

用 :empty 选择空元素:

.item:empty {
  display: none;
}

  
または、:not(:empty)を使用して空でない要素を選択します.
.item:not(:empty) {
  border: 1px solid #ccc;
  /* ... */
}

   :*-Of-Typeで要素を選択
互換性:IE 8はサポートされていません
例を挙げて説明する.
最初のp段落を太くします.
p:first-of-type {
  font-weight: bold;
}

  
最後のimgに枠線を付けます.
img:last-of-type {
  border: 10px solid #ccc;
}

  
接続されていないblockquoteにスタイルを追加します.
blockquote:only-of-type {
  border-left: 5px solid #ccc;
  padding-left: 2em;
}

  
奇数列のp段落を先に赤にします.
p:nth-of-type(even) {
  color: red;
}

  
さらに、:nth-of-typeには、他のタイプのパラメータがあります.
/*     */
:nth-of-type(even)

/* only     */
:nth-of-type(3)

/*      */
:nth-of-type(3n)

/*       ,  3, 7, 11, ... */
:nth-of-type(4n+3)

   calcでフローレイアウト
互換性:IE 8はサポートされていません
Demo
左中右のフローレイアウト:
nav {
  position: fixed;
  left: 0;
  top: 0;
  width: 5rem;
  height: 100%;
}

aside {
  position: fixed;
  right: 0;
  top: 0;
  width: 20rem;
  height: 100%;
}

main {
  margin-left: 5rem;
  width: calc(100% - 25rem);
}

   vwvhでフルスクリーンスクロール効果
互換性:IE 8はサポートされていません
Demo vwおよびvhはviewportに対して存在するため、コンテンツおよびレイアウトの変化に伴って変化しない.
section {
  width: 100vw;
  height: 100vh;
  
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

section:nth-of-type(1) {
  background-image: url('https://unsplash.it/1024/683?image=1068');
}
section:nth-of-type(2) {
  background-image: url('https://unsplash.it/1024/683?image=1073');
}
section:nth-of-type(3) {
  background-image: url('https://unsplash.it/1024/683?image=1047');
}
section:nth-of-type(4) {
  background-image: url('https://unsplash.it/1024/683?image=1032');
}

body {
  margin: 0;
}
p {
  color: #fff;
  font-size: 100px;
  font-family: monospace;
}

   unsetでCSS Resetを作る
互換性:IEはサポートされていません
Demo
body {
  color: red;
}
button {
  color: white;
  border: 1px solid #ccc;
}

/*    section   button   color    */
section button {
  color: unset;
}

   columnを応答式の列レイアウトとする
互換性:IE 9はサポートされていません
Demo
nav {
  column-count: 4;
  column-width: 150px;
  column-gap: 3rem;
  column-rule: 1px dashed #ccc;
  column-fill: auto;
}

h2 {
  column-span: all;
}

作者:sorrycc原文アドレス:https://github.com/sorrycc/blog/issues/14
転載先:https://www.cnblogs.com/ZachChan/p/6182833.html