Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
天小編為大家介紹五種css樣式布局以及內服源代碼作為介紹,采用的方式是行內級樣式(就是將css樣式代碼與html寫在一起)
已知布局元素的高度,寫出三欄布局,要求左欄、右欄寬度各為300px,中間自適應。
一、浮動布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>浮動布局</title>
<style type="text/css">
.wrap1 div{
min-height: 200px;
}
.wrap1 .left{
float: left;
width: 300px;
background: red;
}
.wrap1 .right{
float: right;
width: 300px;
background: blue;
}
.wrap1 .center{
background: pink;
}
</style>
</head>
<body>
<div class="wrap1">
<div class="left"></div>
<div class="right"></div>
<div class="center">
浮動布局
</div>
</div>
</body>
</html>
浮動布局的兼容性比較好,但是浮動帶來的影響比較多,頁面寬度不夠的時候會影響布局。
二、絕對定位布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>絕對定位布局</title>
<style type="text/css">
.wrap2 div{
position: absolute;
min-height: 200px;
}
.wrap2 .left{
left: 0;
width: 300px;
background: red;
}
.wrap2 .right{
right: 0;
width: 300px;
background: blue;
}
.wrap2 .center{
left: 300px;
right: 300px;
background: pink;
}
</style>
</head>
<body>
<div class="wrap2 wrap">
<div class="left"></div>
<div class="center">
絕對定位布局
</div>
<div class="right"></div>
</div>
</body>
</html>
絕對定位布局快捷,但是有效性比較差,因為脫離了文檔流。
三、flex布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>flex布局</title>
<style type="text/css">
.wrap3{
display: flex;
min-height: 200px;
}
.wrap3 .left{
flex-basis: 300px;
background: red;
}
.wrap3 .right{
flex-basis: 300px;
background: blue;
}
.wrap3 .center{
flex: 1;
background: pink;
}
</style>
</head>
<body>
<div class="wrap3 wrap">
<div class="left"></div>
<div class="center">
flex布局
</div>
<div class="right"></div>
</div>
</body>
</html>
自適應好,高度能夠自動撐開
四、table-cell表格布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>table-cell表格布局</title>
<style type="text/css">
.wrap4{
display: table;
width: 100%;
height: 200px;
}
.wrap4>div{
display: table-cell;
}
.wrap4 .left{
width: 300px;
background: red;
}
.wrap4 .right{
width: 300px;
background: blue;
}
.wrap4 .center{
background: pink;
}
</style>
</head>
<body>
<div class="wrap4 wrap">
<div class="left"></div>
<div class="center">
表格布局
</div>
<div class="right"></div>
</div>
</body>
</html>
兼容性好,但是有時候不能固定高度,因為會被內容撐高。
五、網格布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>網格布局</title>
<style type="text/css">
.wrap5{
display: grid;
width: 100%;
grid-template-rows: 200px;
grid-template-columns: 300px auto 300px;
}
.wrap5 .left{
background: red;
}
.wrap5 .right{
background: blue;
}
.wrap5 .center{
background: pink;
}
</style>
</head>
<body>
<div class="wrap5 wrap">
<div class="left"></div>
<div class="center">
網格布局
</div>
<div class="right"></div>
</div>
</body>
</html>
希望大家可以一直關注我,支持我!感謝?。?!
們前端開發過程中,寫css(包括sass, less, stylus這樣的預處理器)進行設計稿的樣式還原是一項重要的工作,而其中,關于頁面布局的部分,又是書寫樣式代碼時候的重點和難點,這篇文章就盡可能的去總結常見的一些頁面布局實現方案(并不是全部,布局實現方法太多了),希望能夠對大家有所幫助。
在開始正題之前,有一點要說明:css布局中遇到的一個繞不開的問題就是瀏覽器兼容性,下面方案會遇到類似transform, flex等的兼容性問題,且由于grid布局兼容性問題,暫不涉及grid布局內容,在不同場景,大家選擇合適的布局實現方案即可。
1.1 水平居中布局
效果圖如下:
分析:display設置為inline-block的元素,具有文本元素的性質,其父元素可以通過設置文本對齊屬性text-align來控制其在行內的對齊方式,text-align: center即為水平對齊
注意:text-align屬性是具有繼承性的,會導致自己元素內部的文本也是居中顯示的,需要自身設置text-align覆蓋
<style>
.wrap {
width: 100%;
height: 200px;
background-color: aqua;
text-align: center;
}
.content {
width: 200px;
height: 200px;
background-color: blueviolet;
display: inline-block;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
分析:父元素開啟定位(relative,absolute,fixed都可以)后,子元素設置絕對定位absolute后,left設置為50%,再使用transform: translateX(-50%)將子元素往反方向移動自身寬度的50%,便完成水平居中。
注意:父級元素是否脫離文檔流,不影響子元素水平居中效果,但是transform是css3屬性,存在瀏覽器兼容問題
<style>
.wrap {
position: relative;
width: 100%;
height: 200px;
background-color: aqua;
}
.content {
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 200px;
height: 200px;
background-color: blueviolet;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
分析:這個方法只需要對子元素進行設置就可以實現水平居中,margin設置auto表示瀏覽器會自動分配,實現來外邊距等分效果。
注意:這里子元素設置display為block或者table都是可以的,如果子元素脫離文檔流(浮動,定位),會導致margin屬性的值無效。
<style>
.wrap {
width: 100%;
height: 200px;
background-color: aqua;
}
.content {
width: 200px;
height: 200px;
background-color: blueviolet;
display: table;
margin: 0 auto;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
效果圖如下:
這種方案和之前水平居中布局的方案二是同樣的原理,不在贅述
<style>
.wrap {
position: relative;
width: 200px;
height: 600px;
background-color: aqua;
}
.content {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 200px;
height: 200px;
background-color: blueviolet;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
分析:設置display: table-cell的元素具有td元素的行為,它的子元素布局方式類似文本元素,可以在父元素使用vertical-align: middle;實現子元素的垂直居中。
注意:vertical-align屬性具有繼承性,導致父元素內文本也是垂直居中顯示的。
<style>
.wrap {
display: table-cell;
vertical-align: middle;
width: 200px;
height: 600px;
background-color: aqua;
}
.content {
width: 200px;
height: 200px;
background-color: blueviolet;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
效果圖如下:
前面分別總結了一些水平居中和垂直居中的布局方式,那么進行水平垂直居中的布局,也就沒什么特別要說的了,直接上代碼:
方案一.定位 + transform
<style>
.wrap {
position: relative;
width: 1200px;
height: 800px;
background-color: aqua;
}
.content {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
background-color: blueviolet;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
方案二. 結合水平布局方案三,垂直布局方案二
<style>
.wrap {
display: table-cell;
vertical-align: middle;
width: 1200px;
height: 800px;
background-color: aqua;
}
.content {
margin: 0 auto;
width: 200px;
height: 200px;
background-color: blueviolet;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
分析:使用flex,水平垂直居中會變得非常容易,默認情況下,align-items: center垂直居中(交叉軸排列方式),justify-content: center水平居中(主軸排列方式) 注意:需要考慮瀏覽器兼容性問題。
<style>
.wrap {
display: flex;
align-items: center;
justify-content: center;
width: 1200px;
height: 800px;
background-color: aqua;
}
.content {
width: 200px;
height: 200px;
background-color: blueviolet;
}
</style>
<body>
<div class="wrap">
<div class="content"></div>
</div>
</body>
2.1 兩列布局
這里的兩列布局指的是,其中一列是定寬元素,另一列元素寬度自適應。比如,我們實現左列定寬,右列自適應的布局。
效果圖如下:
分析:一個最簡單的做法,左邊元素設置浮動,定寬,右邊元素的margin-left設置為左邊元素寬度大小,可以實現效果。
注意:我們左邊元素使用了浮動,但是右邊元素沒有浮動
<style>
.l, .r {
height: 600px;
}
.l {
width: 400px;
background-color: aqua;
float: left;
}
.r {
background-color: blueviolet;
margin-left: 400px;
}
</style>
<body>
<div class="l">定寬</div>
<div class="r">自適應</div>
</body>
分析:右邊元素由于設置overflow:hidden開啟BFC,與外界隔離,所以能實現效果
注意:overflow:hidden的設置也使得右邊元素內容超出隱藏。這里如果不設置overflow:hidden,右邊元素的寬度是100%,有一部分被左邊浮動元素蓋住,不是我們要的結果,雖然看起來沒什么區別。
<style>
.l, .r {
height: 600px;
}
.l {
width: 400px;
background-color: aqua;
float: left;
}
.r {
background-color: blueviolet;
overflow: hidden;
}
</style>
<body>
<div class="l">定寬</div>
<div class="r">自適應</div>
</body>
分析:這里主要是基于表格元素,在沒有設置寬度時,會自動分配寬度來實現布局的。
注意:設置為表格后,在某些瀏覽器可能會受到表格本身特有行為的影響,比如表格邊框等等。
<style>
.w {
display: table;
table-layout: fixed;
width: 100%;
}
.l, .r {
display: table-cell;
height: 600px;
}
.l {
width: 400px;
background-color: aqua;
}
.r {
background-color: blueviolet;
}
</style>
<body>
<div class="w">
<div class="l">定寬</div>
<div class="r">自適應</div>
</div>
</body>
分析:父容器采用flex布局,左邊元素定寬之后,右邊元素,因為只有一個,所以flex屬性設置為不是0的正值(也就是設置flex-grow),都會占滿剩余空間。
注意:依然是,注意兼容性問題。
2.2 三列布局
這里的三列布局,主要分三種情況介紹,第一種是普通三列布局,還有兩種是比較有名的圣杯布局和雙飛翼布局(兩者都是實現一個兩側寬度固定,中間寬度自適應的三列布局,區別在于雙飛翼布局比起圣杯布局,中間元素會多一個子元素,而左右元素需要定位relative)
2.2.1. 普通三列布局
我們這里實現的是,左中兩列定寬,右邊一列自適應的布局,這個實際上和前面的兩列布局是類似的。
效果圖如下:<style>
.p {
display: flex;
height: 600px;
}
.l {
background-color: aqua;
width: 400px;
}
.r {
flex: 1;
background-color: blueviolet;
}
</style>
<body>
<div class="p">
<div class="l">定寬</div>
<div class="r">自適應</div>
</div>
</body>
這里的三列布局,主要分三種情況介紹,第一種是普通三列布局,還有兩種是比較有名的圣杯布局和雙飛翼布局(兩者都是實現一個兩側寬度固定,中間寬度自適應的三列布局,區別在于雙飛翼布局比起圣杯布局,中間元素會多一個子元素,而左右元素需要定位relative)
我們這里實現的是,左中兩列定寬,右邊一列自適應的布局,這個實際上和前面的兩列布局是類似的。
效果圖如下:
分析:這個方案和兩列布局方案二是相同的。
<style>
.l, .c, .r {
height: 600px;
}
.l {
width: 400px;
background-color: aqua;
float: left;
}
.c {
width: 400px;
background-color: blueviolet;
float: left;
}
.r {
background-color: brown;
overflow: hidden;
}
</style>
<body>
<div class="l">定寬</div>
<div class="c">定寬</div>
<div class="r">自適應</div>
</body>
分析:這里布局原理和兩列布局中flex布局方式是相同的。
<style>
.w {
display: flex;
height: 600px;
}
.l {
width: 400px;
background-color: aqua;
}
.c {
width: 400px;
background-color: blueviolet;
}
.r {
flex: 1;
background-color: brown;
}
</style>
<body>
<div class="w">
<div class="l">定寬</div>
<div class="c">定寬</div>
<div class="r">自適應</div>
</div>
</body>
兩側寬度固定,中間寬度自適應的三列布局(中間元素不需要嵌套子元素)
效果圖如下:
分析:這種方法就是左右兩邊浮動,給定寬度,中間元素使用margin空出左右兩邊元素的位置,實現比較簡單。
注意:這種方式,需要在書寫html結構時,將右側元素寫在中間元素的前面,因為如果右側元素在中間元素后面,由于浮動元素位置上不能高于(或平級)前面的非浮動元素,導致右側元素會下沉。但是,中間元素一般都是頁面的核心部分,放在比較后面的位置,不利于SEO。
<style>
.l, .c, .r {
height: 600px;
}
.l {
width: 400px;
background-color: aqua;
float: left;
}
.c {
background-color: blueviolet;
margin-left: 400px;
margin-right: 400px;
}
.r {
width: 400px;
background-color: brown;
float: right;
}
</style>
<body>
<div class="l">定寬</div>
<div class="r">定寬</div>
<div class="c">自適應</div>
</body>
分析:這種方法將中間元素c放置在最前面,有利于SEO。
注意:實現細節在參考下面代碼中的注釋。
<style>
.w {
/* margin-left對應左邊元素l的寬度,margin-right對應右邊元素r的寬度 */
margin-left: 400px;
margin-right: 400px;
}
.l, .c, .r {
height: 600px;
float: left;
}
.l {
width: 400px;
background-color: aqua;
position: relative;
/* 為了讓l元素從當前行移動到第一行同一位置*/
margin-left: -100%;
/* 移動到中間元素左側正確位置 */
left: -400px;
}
.c {
width: 100%;
background-color: blueviolet;
}
.r {
width: 400px;
background-color: brown;
position: relative;
/* 為了讓l元素從當前行移動到上一行*/
margin-left: -400px;
right: -400px;
}
</style>
<body>
<div class="w">
<div class="c">自適應</div>
<div class="l">定寬</div>
<div class="r">定寬</div>
</div>
</body>
兩側寬度固定,中間寬度自適應的三列布局(中間元素內部增加子元素用于放置內容)
效果圖如下:
分析:這種方法為中間元素增加子元素作為內容區域,通過子元素設置margin完成。
注意:和圣杯布局對照,有相似處,也有不同,實現的結果是一樣的。
<style>
.l, .c, .r {
height: 600px;
float: left;
}
.l {
width: 400px;
background-color: aqua;
/* 為了讓l元素從當前行移動到第一行同一位置*/
margin-left: -100%;
}
.c {
width: 100%;
background-color: blue;
}
.i {
height: 600px;
background-color: blueviolet;
margin-left: 400px;
margin-right: 400px;
}
.r {
width: 400px;
background-color: brown;
/* 為了讓r元素移動到第一行*/
margin-left: -400px;
}
</style>
<body>
<div class="c">
<div class="i">自適應</div>
</div>
<div class="l">定寬</div>
<div class="r">定寬</div>
</body>
分析:flex實現就很簡單了,可以參照普通三列布局flex實現。
注意:還是要注意瀏覽器兼容性問題。
<style>
.w {
display: flex;
height: 600px;
}
.l {
width: 400px;
background-color: aqua;
}
.c {
flex: 1;
background-color: blueviolet;
}
.r {
width: 400px;
background-color: brown;
}
</style>
<body>
<div class="w">
<div class="l">定寬</div>
<div class="c">自適應</div>
<div class="r">定寬</div>
</div>
</body>
所謂多列等分布局,就是若干列在容器中自適應等分寬度,我們以五列等分布局為例。
效果圖如下:
分析:這種方案就是每一列浮動,之后按照百分比平分寬度,實現簡單。
<style>
.col {
float: left;
width: 20%;
height: 300px;
}
.col1 {
background-color: blue;
}
.col2 {
background-color: blueviolet;
}
.col3 {
background-color: aqua;
}
.col4 {
background-color: beige;
}
.col5 {
background-color: salmon;
}
</style>
<body>
<div class="w">
<div class="col col1"></div>
<div class="col col2"></div>
<div class="col col3"></div>
<div class="col col4"></div>
<div class="col col5"></div>
</div>
</body>
分析:父容器指定display: table,設置布局行為table-layout: fixed,指定每個表格等寬。
注意:table-layout: fixed是需要設置的,默認情況下,列寬度由單元格內容設定,設置之后,列寬由表格寬度和列寬度設定。
<style>
.w {
display: table;
/* 列寬由表格寬度和列寬度設定 */
table-layout: fixed;
width: 100%;
}
.col {
display: table-cell;
height: 300px;
}
.col1 {
background-color: blue;
}
.col2 {
background-color: blueviolet;
}
.col3 {
background-color: aqua;
}
.col4 {
background-color: beige;
}
.col5 {
background-color: salmon;
}
</style>
<body>
<div class="w">
<div class="col col1"></div>
<div class="col col2"></div>
<div class="col col3"></div>
<div class="col col4"></div>
<div class="col col5"></div>
</div>
</body>
分析:使用column布局,指定內容區域需要分為5列即可。
注意:瀏覽器兼容性問題。
<style>
.w {
/* 指定列數 */
column-count: 5;
/* 指定列與列之間的間隙,默認1em */
column-gap: 0;
}
.col {
height: 300px;
}
.col1 {
background-color: blue;
}
.col2 {
background-color: blueviolet;
}
.col3 {
background-color: aqua;
}
.col4 {
background-color: beige;
}
.col5 {
background-color: salmon;
}
</style>
<body>
<div class="w">
<div class="col col1"></div>
<div class="col col2"></div>
<div class="col col3"></div>
<div class="col col4"></div>
<div class="col col5"></div>
</div>
</body>
分析:使用flex布局十分簡單,指定每一列所占空間相同即可
<style>
.w {
display: flex;
}
.col {
height: 300px;
flex: 1;
}
.col1 {
background-color: blue;
}
.col2 {
background-color: blueviolet;
}
.col3 {
background-color: aqua;
}
.col4 {
background-color: beige;
}
.col5 {
background-color: salmon;
}
</style>
<body>
<div class="w">
<div class="col col1"></div>
<div class="col col2"></div>
<div class="col col3"></div>
<div class="col col4"></div>
<div class="col col5"></div>
</div>
</body>
</html>
所謂多列等高布局,就是多類內容可能不一樣,但是保證每一列的高度是相同的,這個高度應該由內容最多的那一列決定。
效果圖如下:
分析:父元素設置display: table,子元素設置display: table-cell,這樣布局就是按照表格行為布局,表格單元格默認等高。
<style>
.w {
display: table;
}
.col {
display: table-cell;
width: 20%;
}
.col1 {
background-color: blue;
}
.col2 {
background-color: blueviolet;
}
.col3 {
background-color: aqua;
}
.col4 {
background-color: beige;
}
.col5 {
background-color: salmon;
}
</style>
<body>
<div class="w">
<div class="col col1">啊啊啊啊啊啊啊啊啊啊啊啊</div>
<div class="col col2">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
<div class="col col3">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
<div class="col col4"></div>
<div class="col col5">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
</div>
</body>
分析:默認情況下,display: flex的元素align-items屬性值為stretch,如果項目未設置高度或設為auto,將占滿整個容器的高度。
<style>
.w {
display: flex;
}
.col {
flex: 1;
}
.col1 {
background-color: blue;
}
.col2 {
background-color: blueviolet;
}
.col3 {
background-color: aqua;
}
.col4 {
background-color: beige;
}
.col5 {
background-color: salmon;
}
</style>
<body>
<div class="w">
<div class="col col1">啊啊啊啊啊啊啊啊啊啊啊啊</div>
<div class="col col2">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
<div class="col col3">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
<div class="col col4"></div>
<div class="col col5">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
</div>
</body>
所謂全屏布局,就是實現經典的頭部,內容區,底部三大區域占滿全屏的布局,這種布局很常見。
實現效果如下:
分析:這里采用的方案是,頭部底部使用fixed定位,中間使用之前講到的兩列布局技術。
注意:頭部底部可以使用header, footer標簽,內容區域結構與布局都是多種多樣的。
<style>
html, body {
margin: 0;
overflow: hidden;
}
.header {
position: fixed;
left: 0;
top: 0;
right: 0;
height: 100px;
background-color: salmon;
}
.w {
position: fixed;
left: 0;
right: 0;
top: 100px;
bottom: 100px;
overflow: auto;
background-color: palevioletred;
}
.w .l {
width: 400px;
/* height: 100%; */
position: fixed;
left: 0;
top: 100px;
bottom: 100px;
background-color: greenyellow;
}
.w .r {
position: fixed;
left: 400px;
right: 0;
top: 100px;
bottom: 100px;
background-color: blueviolet;
}
.footer {
position: fixed;
left: 0;
right: 0;
bottom: 0;
height: 100px;
background-color: goldenrod;
}
</style>
<body>
<div class="header"></div>
<div class="w">
<div class="l"></div>
<div class="r"></div>
</div>
<div class="footer"></div>
</body>
本篇文章總結了一些常見布局的實現方案,css布局的實現方案很多,需要我們平時多去積累,選擇合適的方案。
最后,希望隨著時間的推移,兼容性不再成為我們技術實現的障礙,愿世界越來越美好。
最后送福利了,自己是從事了五年的前端工程師,整理了一份最全面前端學習資料,只要私信:“前端"等3秒后即可獲取地址,
里面概括應用網站開發,css,html,JavaScript,jQuery,Ajax,node,angular等。等多個知識點高級進階干貨的相關視頻資料,等你來拿
由于漢字的特殊性,在css網頁布局中,中文排版有別于英文排版。排版是一個麻煩的問題,小編認為,作為一個優秀的網頁設計師和網頁制作人員,掌握一些簡單的中文排版技巧是不可或缺的,所以今天特意總結了幾個簡單實用的技巧,希望對大家有所幫助。
Web前端教程
一、如何設定文字字體、顏色、大小等
font-style設定斜體,比如font-style:italic
font-weight設定文字粗細,比如font-weight:bold
font-size設定文字大小,比如font-size:12px
line-height設定行距,比如line-height:150%
color設定文字顏色,注意不是font-color喔,比如color:red
font-family設定字體,比如font-family:"LucidaGrande",Verdana,Lucida,Arial,Helvetica,宋體,sans-serif
二、使用margin,text-align 控制段落
中文段落使用p標簽,左右、段前段后的空白,都可以通過margin來控制。
比如
p{
margin:18px 6px 6px 18px;/*分別是上、右、下、左,十二點開始的順時針方向*/
}
而text-align則用于文字的對齊方式。
比如
p{
text-align:center;/*居中對齊*/
}
除了居中對齊之外,對齊方式還有左對齊、右對齊和兩端對齊,對應的屬性值分別為left、right、justify。
提示:由于默認的margin值會導致頁面排版出現問題,特別是ul、ol、p、dt、dd等標簽。小編的解決之道就是把所有標簽的margin值定義為0。
三、豎排文字—使用writing-mode
writing-mode屬性有兩個值lr-tb和tb-rl,前者是默認的左-右、上-下,后者是上-下、右-左。
寫法如
p{
writing-mode:tb-rl;
}
四、使用list-style美化列表
如果我們的排版涉及到列表,不妨考慮為它添加些項目符號進行美化。
在CSS里,項目符號包括disc(實心圓點)、circle(空心圓圈)、square(實心方塊)、decimal(阿拉伯數字)、lower-roman(小寫羅馬數字)、upper-roman(大寫羅馬數字)、lower-alpha(小寫英文字母)、upper-alpha(大寫英文字母)、none(無)。
嘿嘿!我們可用的項目符號數量不少喔,但美中不足的是我們不能為它們設定大小,也不能設定垂直方向上的對齊。
如果我們想設定一個列表的項目符號為方塊,可以這樣寫:
li{
list-style:square;
}
小編在這里提醒大家一下:當一個項目列表的左外邊距設為零的時候,list-style-position:outside的項目符號不會顯示。
五、使用text-overflow 實現固定寬度漢字截斷
用后臺語言可以對從數據庫里的字段內容做截斷處理,如果想對列表應用該樣式,我們可以這樣寫:
li{
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}
六、首字下沉
如果你想制作首字下沉效果,不妨考慮偽對象:first-letter并配合font-size、float屬性。
p:first-letter{
padding:6px;
font-size:32pt;
float:left;
}
七、首行縮進—使用text-indent
text-indent可以使得容器內首行縮進一定單位。比如中文段落一般每段前空兩個漢字。
可以這么寫
p{
text-indent:2em;/*em是相對單位,2em即現在一個字大小的兩倍*/
}
注意:如果font-size是12px的話,那么text-indent:2em則代表縮進24px。
八、固定寬度漢字(詞)折行—使用word-break
在排版的時候,你是否為一個詞組,其中一個字出現在上面而另一個字折斷到下一行去而發愁呢?不用愁,這時使用word-break就可以輕松解決問題了。
九、關于漢字注音—使用ruby標簽和ruby-align屬性
最后小編向大家介紹一下ruby標簽和ruby-align屬性 。這是一個比較冷門的技巧,可能平時使用不多,但小編覺得不妨提供給大家預防不時之需。
如果我們想為漢字注音就可以這樣寫
<ruby>注音<rt style="font-size:11px;">zhuyin</rt></ruby>
然后通過ruby-align設置其對齊方式。
以上就是今日整理的“Web前端教程:簡單實用的CSS網頁布局中文排版技巧”一文,希望為正在學習Web前端的同學提供參考。你記住并理解了嗎?以后酷仔每日均會提供MySQL、Python及Web相關的教程及習題,趕快學習起來吧。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。