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; //文本不能選擇
//基礎用法 <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:不繼承,不級聯
查看詳情javascript
//因爲設備的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; } }
//對每一個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; }
<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%);
<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; }
<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; }