設計網頁中,錨點滾動是不可或缺的,現在分享給大家,好好看看。
為了好復制,插入整個demo源碼
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width, initial-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">
<title>js錨點滾動效果</title>
<style>
ul>li{float: left;margin: 10px ;}
.clearfix:after{display:block;clear:both;content:"";visibility:hidden;height:0}
.clearfix{zoom:1}
.step{width:80%;height:1300px;border:10pxsolidred;margin:0auto;}
</style>
</head>
<body>
<ulclass="clearfix">
<li><ahref="#step1">#step1</a></li>
<li><ahref="#step2">#step2</a></li>
<li><ahref="#step3">#step3</a></li>
</ul>
<div>
<divid="step1"class="step">step1</div>
<divid="step2"class="step">step2</div>
<divid="step3"class="step">step3</div>
</div>
</body>
<scriptsrc="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<script>
//頁面錨點滾動js
$('a[href*=#],area[href*=#]').click(function {
if (location.pathname.replace(/^//, '') ==this.pathname.replace(/^//, '') &&location.hostname ==this.hostname) {
var$target=$(this.hash);
$target=$target.length &&$target||$('[name='+this.hash.slice(1) +']');
if ($target.length) {
vartargetOffset=$target.offset.top;
$('html,body').animate({
scrollTop: targetOffset
},
1000);
returnfalse;
}
}
});
//頁面錨點滾動js結束
</script>
</html>
ps:希望喜歡我的朋友點贊,關注,轉發一下。
網站開發,程序設計,UI等相關問題,編程技巧以及其他你想向我問的問題,來者不拒。
顏色屬性被用來設置文字的顏色。
顏色是通過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語言,代碼結構更佳的清晰,能夠幫助你更好的學習。
這里是云端源想IT,幫你輕松學IT”
嗨~ 今天的你過得還好嗎?
睡眠等同于希望
每次醒來都是一個新的開始
一個新的希望
- 2024.03.22 -
在Web開發的世界中,CSS(層疊樣式表)是構建視覺吸引力和定義網頁布局的不可或缺的工具。
掌握如何恰當地引入CSS樣式以及理解它們的優先級規則,對于前端開發者來說至關重要。今天,我們就來深入探討CSS的四種引入方式,以及選擇器的優先級之謎,了解常用的CSS樣式及使用方法!
CSS(層疊樣式表)為網頁提供了豐富的樣式定義,允許開發者通過多種方式將樣式應用到HTML文檔中。以下是四種主要的CSS引入方式:
1.1 行內樣式
這是最直接也最簡單的方法,通過在HTML元素的style屬性中直接編寫CSS規則。
示例:
<p style="color: red; font-size: 20px;">這是一段紅色的文字。</p>
這種方式的優點是簡單快捷,但缺點是它使得HTML代碼與樣式混合,不夠純凈,且不利于樣式的復用和維護。
1.2 內嵌樣式
在一個HTML文檔中使用<style>標簽將CSS規則嵌入到HTML的head部分。這種方式適用于定義特定于某一頁面的樣式。
示例:
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
1.3 外部樣式
這是最常用的方法,它通過<link>標簽將外部的CSS文件鏈接到HTML文檔中。這種方法的優勢在于可以在多個頁面間共享同一個樣式文件,有助于保持代碼的整潔和一致性。
示例:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
其中,mystyle.css的內容可能如下:
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
1.4 導入樣式
使用@import語句在CSS文件中導入另一個CSS文件。盡管這種方法可以分離樣式表,但它通常不被推薦使用,因為其加載時序可能會影響頁面渲染效率。
示例:
@import url('https://fonts.googleapis.com/css?family=Roboto');
body {
font-family: 'Roboto', sans-serif;
}
1.5 樣式單優先級
作用域范圍:外部樣式表>內部樣式表>行內樣式表
優先級:
2.1 字體樣式
normal - 文字正常顯示
italic - 文本以斜體顯示
oblique - 文本為“傾斜”(傾斜與斜體非常相似,但支持較少)
font-weight 屬性指定字體的粗細
示例:
<!DOCTYPE html>
<html>
<head>
<style>
.sp1{
color: darkorange;
font-size: 20px;
font-weight: bolder; /* bolder 字體是否加粗*/
font-style: italic; /* italic 字體是否傾斜*/
font-family: "宋體"; /* 設置字體樣式*/
}
.sp2{
/* 簡寫 */
/* 順序不能能變:style-weigth-size-family */
font:italic bolder 15px 宋體 ;
color:rgb(28, 235, 97);
}
</style>
<body>
<span>
編程學習,從云端源想開始!
</span><br>
<span>
讓知識觸手可及
</span>
<p>讓知識觸手可及</p>
</body>
</html>
2.2 文本樣式
color: 字體顏色
text-align: center; - - 文本對齊方式
text-decoration:none; - - 文本的線
text-shadow: pink 5px 5px 2px; - - 文本的陰影 【陰影顏色-水平方向的偏移量-垂直方向的偏移量-模糊距離】
line-height: - - 行高 (文本在標簽內所占的高度)
width:
height:
border: 1px #ffffff solid; - - 盒子邊框【邊框粗細-顏色-邊框線樣式】
示例:
<style>
.p{
color: rgb(0, 255, 21); /* 字體顏色 */
text-align: center; /* 文本對齊方式 */
text-decoration:none; /* 文本的線 */
text-shadow: pink 5px 5px 2px; /* 文本的陰影 【陰影顏色-水平方向的偏移量-垂直方向的偏移量-模糊距離】*/
line-height: 400px; /* 行高 (文本在標簽內所占的高度)*/
width: 400px;
height: 300px;
border: 1px rgb(76, 14, 223) solid; /* 盒子邊框【邊框粗細-顏色-邊框線樣式】 */
}
</style>
</head>
<body>
<p>歡迎來到云端源想!</p>
<a href="https://www.baidu.com"></a>
</body>
2.3 背景樣式
width: 500px;
height: 1200px;
background-color: pink; - - 背景顏色
background-image: url(…/img/background.jpg); - - 背景圖片
background-repeat: no-repeat; - - 背景圖片是否平鋪
background-position: left top; - - 指定背景圖片的位置
background-attachment: fixed; - - 背景圖片是否隨著標簽滾動 【fixed-固定 scroll-滾動】
示例:
<style>
.d{
width: 500px;
height: 1200px;
background-color: pink; /* 背景顏色 */
background-image: url(../img/background.jpg); /* 背景圖片 */
background-repeat: no-repeat; /* 背景圖片是否平鋪 */
background-position: left top; /* 指定背景圖片的位置 */
background-attachment: fixed; /* 背景圖片是否隨著標簽滾動 【fixed-固定 scroll-滾動】 */
}
</style>
</head>
<body>
<div>
</div>
2.4 列表樣式
<!DOCTYPE html>
<html>
<head>
<style>
li{
background-color: lemonchiffon;
/*列表樣式:常用取值:none-無樣式 square-正方形 circle-空心圓 decimal-數字*/
list-style-type: circle;
/*列表樣式為自定義圖片*/
list-style-image: url(../img/2.jpg);
/*列表樣式的放置位置*/
list-style-position: inside;
/*列表樣式縮寫*/
list-style: square url(../img/2.jpg) inside;
/*常用的列表樣式*/
list-style: none;
}
</style>
</head>
<body>
<ul>
<li>列表項1</li>
<li>列表項2</li>
<li>列表項3</li>
</ul>
</body>
</html>
2.5 邊框樣式
<!DOCTYPE html>
<html>
<head>
<style>
.border{
/*邊框寬度*/
border-width: 2px;
/*邊框顏色*/
border-color: red;
/*邊框樣式:solid 實線 dotted 點線 dashed 虛線*/
border-style: dashed;
/*邊框樣式縮寫:樣式 顏色 寬度*/
border:solid green 5px;
/*邊框可以為4個方向分別設置*/
border-top: dashed black 4px;
border-right: dashed #FF00FF 4px;
border-bottom: dotted darkblue 4px;
border-left: solid fuchsia 5px;
/*沒有邊框*/
border: none;
/*常用的細邊框樣式*/
border: solid 1px #ccc;
}
</style>
</head>
<body>
<div class="border">這是一個帶有邊框的元素</div>
</body>
</html>
2.6 盒子模型
所有的html元素可以看做是盒子,在css中,"box model"是用來設計和布局時使用。
CSS盒子模型本質是一個盒子,封裝周圍的html元素,它包括:邊框、邊距、填充、實際內容。
盒子模型允許我們在其他元素和周圍元素邊框之間的空間放置元素。
想要快速入門前端開發嗎?推薦一個前端開發基礎課程,這個老師講的特別好,零基礎學習無壓力,知識點結合代碼,邊學邊練,可以免費試看試學,還有各種輔助工具和資料,非常適合新手!點這里前往學習哦!云端源想
示例:
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* border:邊框,分4個方向,同理margin、padding也分為四個方向
* margin:元素與元素之間對的距離
* padding:內容與邊框之間的距離
* 設置的時候順序:上 右 下 左
*/
.div{
border: solid red 10px;
/*四個方向上的元素與元素之間的距離都是50px*/
margin: 50px;
/*兩個值的時候:第一個參數表示上下距離都是50px,第二個參數表示左右距離都是100px*/
margin: 50px 100px;
padding: 50px;
/*
一個元素真正的寬度=width+左右padding值+左右的border值
一個元素的真正高度=height+上下的padding值+上下的border值
* */
}
</style>
</head>
<body>
<div>111111111112222222222223333333333333333</div>
</body>
1)盒子的寬高
元素的實際寬度和高度:
2)設置寬度=元素實際寬度,box-sizing屬性。
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* box-sizing:確認元素的大小
content-box: 實際寬度=width+左右的psdding值+上下的border值
實際高度=height+上下的padding值+上下的border值
border-box:實際寬度=width;實際高度=height
padding和border不會影響元素的實際寬高
* */
.box{
width: 100px;
height: 200px;
border: 5px solid;
padding: 5px;
box-sizing: content-box;
}
</style>
</head>
<body>
<div>你好中國</div>
</body>
CSS的世界博大精深,以上只是冰山一角,希望通過這些基礎的常用樣式可以幫助你快速進入CSS世界的大門。
掌握CSS的引入方式和選擇器優先級是構建高效、可維護網站的關鍵。通過這些知識,你可以更好地管理和優化你的樣式代碼,創造出既美觀又專業的網頁設計。現在,準備好邁入CSS的世界,開啟你的創意之旅吧!
我們下期再見!
END
文案編輯|云端學長
文案配圖|云端學長
內容由:云端源想分享
*請認真填寫需求信息,我們會在24小時內與您取得聯系。