個人覺得CSS是每個前端開發人員都必須掌握的基礎,以完成相應的互動和終端裝置的響應。
在項目開發中,有些容易被忽略的小問題帶來項目後期的膠水代碼。
本文總結一些項目開發中css的10個小技巧。
1、使用相對機關
通常我們在項目開發中,使用px作為尺寸的機關,而不是使用相對機關,如:rem、em等。在萬物互聯的時代,最好的方式是相對機關rem、vh、vw等現代 CSS 布局(如 flexbox 和 grid)方式,最大限度的支援各種終端裝置。
絕對機關
px :是一個絕對機關,主要是因為它是固定的,不會根據任何其他元素的測量而改變。
相對機關
vw(viewpoint width):相對于視口的寬度
vh(viewpoint height):相對于視口的高度
rem(font size of the root element):相對于根 ( ) 元素 (預設字型大小通常為 16px )
em(font size of the element):相對于父元素
% :相對于父元素
/* 不提倡 */
.wrap {
font-size: 14px;
margin: 10px;
line-height: 24px;
}
/* 建議 */
.wrap {
font-size: 1.2rem;
margin: 0.5rem;
line-height: 1.6em;
}
2、代碼複用
很多開發人員在談到CSS時都覺得代碼重複性很高,在項目開發中這不是一個好的做法。
好在現在有CSS預處理器(sass/scss、less、stylus、Turbine),能夠讓我們可以更好的規劃CSS代碼,提高其複用性。
當然需要提高代碼複用,還是需要一定的CSS的基礎,來設計好代碼結構,如下:
/* 不提倡 */
.container {
background-color: #efefef;
border-radius: 0.5rem;
}
.sidebar {
background-color: #efefef;
border-radius: 0.5rem;
}
/* 建議 */
.container,
.sidebar {
background-color: #efefef;
border-radius: 0.5rem;
}
3、CSS重置
每個浏覽器都有自己的預設樣式,是以,當網頁不包含CSS時,浏覽器會為文本添加一些基本的預設樣式、填充、邊距等。
可以通過使用通用選擇器 * 重置 padding、margin、box-sizing 和 font-family 來實作這一點。
像這樣:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
}
ul,
li {
list-style: none;
}
不過這些問題現在基本都被架構解決了,對于初學者建議可以模仿但不建議一開始就上架構。
4、不使用顔色名稱
不要使用red、blue等顔色名稱,相反,建議使用顔色的十六進制值。
為什麼呢?因為當使用像 red 這樣的顔色名稱時,在不同的浏覽器或者裝置中顯示會有所不同。
/* 不提倡 */
.container {
background-color: red;
}
/* 建議 */
.container {
background-color: #ff0000;
}
5、使用簡寫屬性
在CSS中,多用簡寫屬性,少用單獨屬性,具體哪些是簡寫屬性,哪些是單獨屬性,下面列舉一下常見的一些屬性,是以通常項目為原則。
簡寫屬性
background、font、 margin、padding、 border、 transition、 transform、 list-style、 border-radius
單獨屬性
rotate、scale、background-color、background-image、background-position、padding-left、padding-right、padding-top、padding-bottom、margin-left、margin-top、margin-right、margin-bottom、border-top、 border-right、 border-bottom、 border-left、 border-width、 border-color、 border-style、
/* 不提倡 */
.container {
background-image: url(bg.png);
background-repeat: no-repeat;
background-position: center;
}
/* 建議 */
.container {
background: url(bg.png) no-repeat center;
}
6、文本截取
在項目開發中,有些清單隻需要顯示一行文字,有些清單需要顯示固定函數的文字,過去通過字元截取的方式來實作,但存在截取不統一(文本内容不同英文、中文、标點符号等),再加上現在各種終端的适配,不足就被放大了。
現在最佳的方式是通過CSS來實作,在文本最後增加省略号(…)。
單行截取
元素必須是 block 或 inline-block,如果溢出被隐藏,則文本溢出不起作用,并且元素必須具有定義的寬度或最大寬度集。
p {
display: inline-block;
max-width: 300px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
多行截取
p {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3; /* 需要顯示的行數 */
overflow: hidden;
}
7、垂直居中
垂直居中是一個很常見的需求,有很多實作方式,在伸縮容器内的任何東西垂直居中:
.flex-vertically-center {
display: flex;
align-items: center;
}
inline、inline-block、table-cell 塊垂直對齊:
img {
/* 隻對block有效 */
display: inline-block;
vertical-align: middle;
}
相對容器中垂直居中的絕對元素,下面代碼是.sub-container在.container垂直居中:
.container {
position: relative;
}
.sub-container {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
8、水準居中
與垂直對齊類似,不過水準居中更容易一點。
塊居中
.block-element {
display: block;
margin: 0 auto;
}
内聯或内聯塊文本居中
.container {
text-align: center;
}
在相對容器内水準居中絕對元素:
.container {
position: relative;
}
.sub-container {
position: absolute;
top: 50%;
transform: translateX(-50%);
}
flex 容器内的任何内容水準居中:
.flex-vertically-center {
display: flex;
justify-content: center;
}
9、設定下一個或上一個兄弟元素樣式
對元素前面和後面的元素進行樣式設定,在項目開發中很有用。例如10個按鈕,目前按鈕下一個及下一個的兄弟元素設定不同的顔色。
html代碼如下:
<div>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button class="current">current</button>
<button>+ button</button>
<button>~ button</button>
<button>~ button</button>
<button>~ button</button>
<button>~ button</button>
</div>
css代碼:
.current ~ button {
background-color: #000;
color: #ffffff;
}
.current {
background-color: #ff0000;
}
.current + button {
background-color: #333;
}
效果如下:
接下來設定目前按鈕前面樣式,css代碼如下:
button {
padding: 10px 15px;
border: 1px solid #444444;
font-size: 12px;
background-color: #ff0000;
color: #000;
}
.current {
background-color: #000;
color: #fff;
}
.current ~ button {
background: initial;
}
.container {
width: 1000px;
margin: 50px auto;
text-align: center;
}
10、寬高比
如果想讓盒子容器有一定的寬高比,如視訊播放器尺寸,可以用幾種方法來實作,其中有一種方法最直覺。可以使用 calc 函數設定頂部填充 (height * width) / 100%。
如下,建立一個 720px 寬的 16 x 9 矩形:
html代碼:
<div class="container">
<div class="box"></div>
</div>
css代碼:
.container {
width: 720px;
}
.box {
padding-top: calc((9 / 16) * 100%);
background: #efefef;
}
還可以使用 after 僞元素來建立比例大小。
.box::after {
content: "";
display: block;
padding-top: calc((9 / 16) * 100%);
background: #eee;
}
上面的方案會導緻裡面所有的元素都必須向上移動或需要使用絕對定位。不過好消息是,CSS增加了aspect-ratio屬性。