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世界》這本書,強烈推薦前端er仔細閱讀下,里面詳細說明了許多不怎么被注意的CSS特性,對前端進階很有幫助。
本文簡要列舉書中前四章比較實用的知識點,書中干貨很多,值得一讀。
利用元素的包裹性,元素的尺寸由內部元素決定,且永遠小于容器的寬度。
具有包裹性的元素:inline-block、浮動元素、絕對定位元素。
<style>
.content{
display: inline-block;
text-align: left;
}
.box{
text-align: center;
}
</style>
<div class="box">
<span class="content"></span>
</div>
min-width/min-height初始值是auto,max-height/max-width初始值是none。
設置min-height漸變效果,需要指定min-height的值。
<style>
.box{
min-height: 20px;
width: 200px;
background-color: coral;
transition: min-height .5s;;
}
.box:hover{
min-height: 300px;
}
</style>
<template>
<div class="box"></div>
</template>
超越important,超越最大。簡而言之,min-width/min-height,max-width/max-height會覆蓋width/height。當min-xxx與max-xxx設置相沖突時,實際結果以min-xxx為準。
如下代碼,div沒有設置寬高,span為空白標簽,但是div的高度卻為18px,這個高度是由字體大小和行高決定的,要想去除這個影響,需要將font-size設置為0。
<style>
div {
background-color: #cd0000;
}
span {
display: inline-block;
}
</style>
<template>
<div><span></span></div>
</template>
指定圖片寬高,使圖片自適應寬高,常用的兩種方式,第一種是background,第二種是object-fit。常用屬性如下:
background-size | object-fit | CSS屬性 | 說明 |
cover | cover | 覆蓋 | 會存在圖片展示不全 |
contain | contain | 包含 | 等比縮放,空白自動填充 |
-- | fill(默認) | 填充 | 不符合尺寸,橫向、縱向壓縮 |
CSS content屬性結合before/after偽類,能實現很多意想不到的效果。
<style>
dot {
display: inline-block;
height: 1em;
line-height: 1;
text-align: left;
vertical-align: -.25em;
overflow: hidden;
}
dot::before {
display: block;
content: '...\A..\A.';
white-space: pre-wrap;
animation: dot 3s infinite step-start both;
}
@keyframes dot {
33% { transform: translateY(-2em); }
66% { transform: translateY(-1em); }
}
</style>
<template>
<div>
正在加載中<dot>...</dot>
</div>
</template>
<style>
.edit{
width: 200px;
height: 50px;
background-color: azure;
}
.edit:empty::before{
content: attr(data-placeholder);
}
</style>
<template>
<div class="edit" contenteditable="true" data-placeholder="this is a placeholder"></div>
</template>
background-clip可以設置background作用范圍,結合padding,可以做出一些好玩的東西。
<style>
.icon-dot {
display: inline-block;
width: 100px; height: 100px;
padding: 10px;
border: 10px solid;
border-radius: 50%;
background-color: currentColor;
background-clip: content-box;
}
</style>
<template>
<span class="icon-dot"></span>
</template>
<style>
.column-box {
overflow: hidden;
}
.column-left,
.column-right {
margin-bottom: -9999px;
padding-bottom: 9999px;
float: left;
}
.column-left{
width: 100px;
background-color: #ccc;
}
.column-right{
width: 100px;
background-color: aquamarine;
}
</style>
<template>
<div class="column-box">
<div class="column-left">
123
</div>
<div class="column-right">
456
</div>
</div>
</template>
<style>
.left{
width: 200px;
height: 100%;
float: left;
}
.right{
margin-left: 200px;
}
</style>
<template>
<body>
<div class='left'></div>
<div class='right'></div>
</body>
</template>
顏色屬性被用來設置文字的顏色。
顏色是通過CSS最經常的指定:
一個網頁的文本顏色是指在主體內的選擇:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
body {
color: blue;
}
h1 {
color: #00ff00;
}
h2 {
color: rgb(255, 0, 0);
}
</style>
</head>
<body>
<h2>hello world</h2>
<h1>welcome to CaoZhou</h1>
</body>
</html>
注:對于W3C標準的CSS:如果你定義了顏色屬性,你還必須定義背景色屬性。
文本排列屬性是用來設置文本的水平對齊方式。
文本可居中或對齊到左或右,兩端對齊。
當text-align設置為"justify",每一行被展開為寬度相等,左,右外邊距是對齊(如雜志和報紙)。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
h1 {
text-align: center;
}
p.date {
text-align: right;
}
p.main {
text-align: justify;
}
</style>
</head>
<body>
<p class="date">2015 年 3 月 14 號</p>
<p class="main"> 從前有個書生,和未婚妻約好在某年某月某日結婚。到那一天,未婚妻卻嫁給了別人。書生受此打擊, 一病不起。 這時,路過一游方僧人,從懷里摸出一面鏡子叫書生看。書生看到茫茫大海,一名遇害的女子一絲不掛地躺在海灘上。路過一人, 看一眼,搖搖頭,走了。又路過一人,將衣服脫下,給女尸蓋上,走了。再路過一人,過去,挖個坑,小心翼翼把尸體掩埋了。 僧人解釋道, 那具海灘上的女尸,就是你未婚妻的前世。你是第二個路過的人,曾給過他一件衣服。她今生和你相戀,只為還你一個情。但是她最終要報答一生一世的人,是最后那個把她掩埋的人,那人就是他現在的丈夫。書生大悟,病愈。
</p>
<p><b>注意:</b> 重置瀏覽器窗口大小查看 "justify" 是如何工作的。</p>
</body>
</html>
text-decoration 屬性用來設置或刪除文本的裝飾。
從設計的角度看 text-decoration屬性主要是用來刪除鏈接的下劃線:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.none {}
.del {
text-decoration: none;
}
</style>
</head>
<body>
<p>原來的樣子</p>
<a href="#" class="none">wwwwwwwwwwwwwwwwww</a>
<p>去掉下劃線</p>
<a href="#" class="del">wwwwwwwwwwwwwwwwwwwww</a>
</body>
</html>
也可以這樣裝飾文字:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>
</html>
注:不建議強調指出不是鏈接的文本,因為這常常混淆用戶。
text-transform文本轉換屬性是用來指定在一個文本中的大寫和小寫字母。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
</style>
</head>
<body>
<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>
</body>
</html>
text-indent文本縮進屬性是用來指定文本的第一行的縮進。
p {text-indent:50px;}
增加或減少字符之間的空間。
<style>
h1 {
letter-spacing:2px;
}
h2 {
letter-spacing:-3px;
}
</style>
指定在一個段落中行之間的空間。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
p.small {
line-height: 70%;
}
p.big {
line-height: 200%;
}
</style>
</head>
<body>
<p>
This is a paragraph with a standard line-height.<br> This is a paragraph with a standard line-height.<br> The default line height in most browsers is about 110% to 120%.<br>
</p>
<p class="small">
This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br>
</p>
<p class="big">
This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br>
</p>
</body>
</html>
增加一個段落中的單詞之間的空白空間。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style type="text/css">
p {
word-spacing: 30px;
}
</style>
</head>
<body>
<p>
This is some text. This is some text.
</p>
</body>
</html>
設置文本的垂直對齊圖像。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
img{
width: 200px;
height: 100px;
}
img.top {
vertical-align: text-top;
}
img.bottom {
vertical-align: text-bottom;
}
</style>
</head>
<body>
<p>An <img src="img/logo.png" /> image with a default alignment.</p>
<p>An <img class="top" src="img/logo.png" /> image with a text-top alignment.</p>
<p>An <img class="bottom" src="img/logo.png" /> image with a text-bottom alignment.</p>
</body>
</html>
設置文本陰影。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
h1{
text-shadow: 2px 2px #FF0000;
}
</style>
</head>
<body>
<h1>Text-shadow effect</h1>
</body>
</html>
本文主要介紹了CSS文本樣式實際應用中應該如何去操作,通過講解文本中對應的屬性去改變文本的表現形式。使用豐富的效果圖的展示,能夠更直觀的看到運行的效果,能夠更好的理解。使用Html語言,代碼結構更佳的清晰,能夠幫助你更好的學習。
eta標簽介紹
meta標簽是HTML語言head區域的一個輔助性標簽,常用于定義頁面的說明,關鍵字,最后修改的日期和其他的元數據。這些元數據將服務于瀏覽器,搜索引擎和其他網絡服務。
meta標簽的組成
meta標簽共有兩個屬性,分別是http-equiv屬性和name屬性。
name屬性
name屬性主要是用于描述網頁,比如網頁的關鍵詞,敘述等。與之對應的屬性值為content,content中的內容是對name填入類型的具體描述,便于搜索引擎抓取。
meta標簽中name屬性語法格式是:
<meta name="參數" content="具體的描述">
其中name屬性共有以下幾種參數。(A-C為常用屬性)
(1) keywords(關鍵字)
說明:用于告訴搜索引擎,你網頁的關鍵字。舉例:
<meta name="keywords" content="PHP中文網">
(2)description(網站內容的描述)
說明:用于告訴搜索引擎,你網站的主要內容。舉例:
<meta name="description" content="php中文網提供大量免費、原創、高清的php視頻教程">
(3)viewport(移動端的窗口)
說明:這個概念較為復雜,具體的會在下篇博文中講述。這個屬性常用于設計移動端網頁。在用bootstrap,AmazeUI等框架時候都有用過viewport。
<meta name="viewport" content="width=device-width, initial-scale=1">
(4) robots(定義搜索引擎爬蟲的索引方式)
說明:robots用來告訴爬蟲哪些頁面需要索引,哪些頁面不需要索引。content的參數有all,none,index,noindex,follow,nofollow。默認是all。
<meta name="robots" content="none">
具體參數如下:
1、none : 搜索引擎將忽略此網頁,等價于noindex,nofollow。
2、noindex : 搜索引擎不索引此網頁。
3、nofollow: 搜索引擎不繼續通過此網頁的鏈接索引搜索其它的網頁。
4、all : 搜索引擎將索引此網頁與繼續通過此網頁的鏈接索引,等價于index,follow。
5、index : 搜索引擎索引此網頁。
6、follow : 搜索引擎繼續通過此網頁的鏈接索引搜索其它的網頁。
(5)author(作者)
說明:用于標注網頁作者舉例:
<meta name="author" content="PHP中文網">
(6) generator(網頁制作軟件)
說明:用于標明網頁是什么軟件做的舉例: (不知道能不能這樣寫):
<meta name="generator" content="Sublime Text3">
(7)copyright(版權)
說明:用于標注版權信息舉例:
<meta name="copyright" content="PHP中文網"> //代表該網站為PHP中文網個人版權所有。
(8)revisit-after(搜索引擎爬蟲重訪時間)
說明:如果頁面不是經常更新,為了減輕搜索引擎爬蟲對服務器帶來的壓力,可以設置一個爬蟲的重訪時間。如果重訪時間過短,爬蟲將按它們定義的默認時間來訪問。舉例:
<meta name="revisit-after" content="7 days" >
(9)renderer(雙核瀏覽器渲染方式)
說明:renderer是為雙核瀏覽器準備的,用于指定雙核瀏覽器默認以何種方式渲染頁面。比如說360瀏覽器。舉例:
<meta name="renderer" content="webkit"> //默認webkit內核
<meta name="renderer" content="ie-comp"> //默認IE兼容模式
<meta name="renderer" content="ie-stand"> //默認IE標準模式
http-equiv屬性
http-equiv顧名思義,相當于HTTP的作用。
meta標簽中http-equiv屬性語法格式是:
<meta http-equiv="參數" content="具體的描述">
其中http-equiv屬性主要有以下幾種參數:
(1) content-Type(設定網頁字符集)(推薦使用HTML5的方式)
說明:用于設定網頁字符集,便于瀏覽器解析與渲染頁面舉例:
<meta http-equiv="content-Type" content="text/html;charset=utf-8"> //舊的HTML,不推薦
<meta charset="utf-8"> //HTML5設定網頁字符集的方式,推薦使用UTF-8
(2)X-UA-Compatible(瀏覽器采取何種版本渲染當前頁面)
說明:用于告知瀏覽器以何種版本來渲染頁面。(一般都設置為最新模式,在各大框架中這個設置也很常見。)
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> //指定IE和Chrome使用最新版本渲染當前頁面
(3) cache-control(指定請求和響應遵循的緩存機制)
說明:指導瀏覽器如何緩存某個響應以及緩存多長時間
<meta http-equiv="cache-control" content="no-cache">
共有以下幾種用法:
no-cache: 先發送請求,與服務器確認該資源是否被更改,如果未被更改,則使用緩存。
no-store: 不允許緩存,每次都要去服務器上,下載完整的響應。(安全措施)
public : 緩存所有響應,但并非必須。因為max-age也可以做到相同效果
private : 只為單個用戶緩存,因此不允許任何中繼進行緩存。(比如說CDN就不允許緩存private的響應)
maxage : 表示當前請求開始,該響應在多久內能被緩存和重用,而不去服務器重新請求。例如:max-age=60表示響應可以再緩存和重用 60 秒。
禁止百度自動轉碼
說明:用于禁止當前頁面在移動端瀏覽時,被百度自動轉碼。雖然百度的本意是好的,但是轉碼效果很多時候卻不盡人意。所以可以在head中加入例子中的那句話,就可以避免百度自動轉碼了。
<meta http-equiv="Cache-Control" content="no-siteapp" />
(4)expires(網頁到期時間)
說明:用于設定網頁的到期時間,過期后網頁必須到服務器上重新傳輸。
<meta http-equiv="expires" content="Sunday 26 October 2016 01:00 GMT" />
(5) refresh(自動刷新并指向某頁面)
說明:網頁將在設定的時間內,自動刷新并調向設定的網址。
<meta http-equiv="refresh" content="2;URL=http://www.xxx.com/"> //意思是2秒后跳轉到PHP中文網
(6) Set-Cookie(cookie設定)
說明:如果網頁過期。那么這個網頁存在本地的cookies也會被自動刪除。
<meta http-equiv="Set-Cookie" content="name, date"> //格式
<meta http-equiv="Set-Cookie" content="User=Lxxyx; path=/; expires=Sunday, 10-Jan-16 10:00:00 GMT"> //具體范例
總結:meta標簽的自定義屬性實在太多了。所以只總結了一些常用的,希望對大家有所幫助。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。