名人說:莫道桑榆晚,為霞尚滿天。——劉禹錫(劉夢得,詩豪)
創作者:Code_流蘇(CSDN)(一個喜歡古詩詞和編程的Coder😊)目錄
- 二、CSS
- 1、CSS-初始入門
- ①快速了解
- ②CSS應用方式
- ③選擇器
- ④樣式
- 2、CSS-常見樣式和案例
- ①浮動
- ②內邊距
- ③外邊距
- 3、CSS-樣式和小米商城
- ①CSS案例
- ②小結
- 4、CSS-案例:小米商城新品部分
- ①案例:推薦區域
- ②案例實現
- 5、CSS-邊框和背景色
- ①:hover(偽類)
- ②:after(偽元素)
- ③position屬性
- ④relative和absolute定位
- ⑤border屬性
- ⑥background-color屬性
二、CSS
1、CSS-初始入門
HTML vs CSS
1?? 超文本標記語言HTML
,使用多種“標簽”來描述網頁的結構和內容。(網頁擴展名為.html)
2?? 層疊樣式表CSS
,從審美角度來描述網頁的樣式。(文件擴展名為.css)
CSS,專門用來”美化“標簽,HTML如果是"身體",CSS就是在身體外的各種裝飾,可以美化HTML寫出來的"身體"。
- 基礎CSS,寫簡單頁面 & 看懂 & 修改;
- 模塊,調整和修改。
①快速了解
CSS是一種用于描述HTML或XML(包括各種XML語言,如SVG或XHTML)文檔的樣式的語言。CSS描述了這些文檔在屏幕、紙張、語音閱讀器等媒介上的呈現方式。
1.基礎概念
- 選擇器(Selectors):用于選擇你想要樣式化的HTML元素。
- 屬性(Properties):定義了如何樣式化元素。
- 值(Values):屬性的具體設置。
- 聲明(Declarations):由屬性和值組成,如
color: red;
。 - 聲明塊(Declaration Blocks):用大括號
{}
包圍的一系列聲明。 - 規則集(Rule Sets):由選擇器和聲明塊組成。
2.基本語法
selector {property: value;
}
示例
p {color: red;font-size: 14px;
}
這個示例會將所有 <p>
(段落)元素的文字顏色設置為紅色,并且字體大小為14像素。
3.常用屬性
color
: 文本顏色background-color
: 背景色font-size
: 字體大小border
: 邊框margin
: 外邊距padding
: 內邊距width
和height
: 寬度和高度
<img src="..." style="height:100px"/><div style="color:red;">中國聯通</div>
4.響應式設計
- 媒體查詢(Media Queries):允許你根據不同的屏幕尺寸或設備特性應用不同的樣式。
示例
@media screen and (max-width: 600px) {body {background-color: lightblue;}
}
②CSS應用方式
1.在標簽上應用
直接在標簽里進行添加。
<img src="..." style="height:100px"/><div style="color:red;">中國聯通</div>
2.在head標簽中寫style標簽( * )
在頭部head標簽中添加style標簽,在style標簽中來進行添加CSS樣式。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{color:red;}</style>
</head>
<body><h1 class='c1'>用戶登錄</h1><h1 class='c1'>用戶登錄</h1><h1 class='c1'>用戶登錄</h1><h1 class='c1'>用戶登錄</h1><form method="post" action="/login">用戶名:<input type="text" name="username">密碼:<input type="text" name="password"><input type="submit" value="提交"></form>
</body>
</html>
3.寫到文件中( * )
寫到文件里,直接使用文件里的樣式。
.c1{height:100px;
}.c2{color:red;
}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="common.css" />
</head>
<body><h1 class='c1'>用戶登錄</h1><h1 class='c1'>用戶登錄</h1><h1 class='c1'>用戶登錄</h1><h1 class='c1'>用戶登錄</h1>
</body>
</html>
案例:flask中的應用(登錄 注冊)
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/static/commons.css">
</head>
<body><h1 class="xx">用戶注冊</h1><form action="/register" method="post"><div>用戶名: <input type="text" name="user"></div><div>密碼: <input type="password" name="pwd"></div><div><input type="submit" value="注冊"></div></form>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/static/commons.css">
</head>
<body><h1 class="xx">用戶注冊</h1><form action="/register" method="post"><div>用戶名: <input type="text" name="user"></div><div>密碼: <input type="password" name="pwd"></div><div><input type="submit" value="注冊"></div></form>
</body>
</html>
.xx{color: green;
}
問題:用Flask框架開發有些不便之處:
- 每次都需要重啟
- 規定有些文件必須要放在特定的文件夾
- 新創建一個頁面
- 函數
- HTML文件
有沒有一種方式,可以快速編寫前端代碼及查看響應效果,最后將頁面集成到Flask中?
Pycharm為我們提供了一種非常便捷開發前端頁面的工具,鼠標滑到界面右側即可顯示,打開此處頁面可以實時地看到代碼更新所帶來的頁面變化。
③選擇器
在CSS中,選擇器用于定位一個或多個元素,以便應用樣式。
1.ID選擇器
通過HTML元素的id屬性選擇特定元素。ID是唯一的,在一個HTML頁面中,每個ID只能出現一次。ID選擇器在CSS中以#字符開始,后跟ID的名稱。例如,#navbar選擇具有id="navbar"的元素。
#c1{}<div id='c1'></div>
2.類選擇器(使用較多)
通過HTML元素的class屬性選擇一個或多個元素。同一類可以應用于頁面上的多個元素,一個元素也可以有多個類。類選擇器在CSS中以.字符開始,后跟類的名稱。例如,.button選擇所有具有class="button"的元素。
.c1{}<div class='c1'></div>
3.標簽選擇器(使用較多)
又稱為元素選擇器,通過元素名稱選擇所有特定類型的元素。例如,div選擇頁面上的所有div元素。
div{}<div>xxx
</div>
4.屬性選擇器
根據元素的屬性及屬性值來選擇元素。它們用方括號[]表示,內部可以指定屬性名,也可以指定屬性名和值。例如,[type=“text”]選擇所有type屬性值為text的元素。
input[type='text']{border:1px solid red;
}.v1[xx="456"]{color: gold;
}
<input type="text">
<input type="password"><div class="v1" xx="123">s
</div><div class="v1" xx="456">f
</div><div class="v1" xx="999">a
</div>
5.后代選擇器(使用較多)
用于選擇一個元素的后代元素,即位于指定元素內部的元素,不論是直接還是間接后代。后代選擇器通過空格分隔父元素和子元素的選擇器來表示。例如,div p選擇所有位于div元素內部的p元素。
.yy li{color: pink;}.yy > a{color: dodgerblue;}
<div class="yy"><a>百度</a><div><a>谷歌</a></div><ul><li>美國</li><li>日本</li><li>韓國</li></ul></div>
關于選擇器:
日常使用多少:
多:類選擇器、標簽選擇器、后代選擇器
少:屬性選擇器、ID選擇器
關于多個樣式和覆蓋的問題:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{color: red !important;border: 1px solid red;}.c2{font-size: 28px;color: green;}</style>
</head>
<body><div class="c1 c2">中國聯通</div>
</body>
</html>
④樣式
1.高度和寬度
.c1{/*height高度 width寬度*/height:300px;width:500px;
}
注意:
- 寬度,支持百分比。
- 行內標簽:默認無效
- 塊級標簽:默認有效(霸道,右側區域空白,也不給你占用)
2.塊級和行內標簽
- 塊級
- 行內
- CSS樣式:標簽->
display:inline-block
示例1:行內+塊級特性
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{display: inline-block;height: 100px;width: 300px;border: 1px solid red;}</style>
</head>
<body><span class="c1">中國</span><span class="c1">聯通</span><span class="c1">聯通</span><span class="c1">聯通</span>
</body>
</html>
示例2:塊級和行內標簽的設置
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style></style>
</head>
<body><div style="display: inline;">中國</div><span style="display: block;">聯通</span>
</body>
</html>
注意:塊級+跨級和行內
3.字體設置
- 顏色 color
- 大小 font-size
- 加粗 font-weight
- 字體格式 font-family
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{color: #66CDAA;font-size: 50px;font-weight: 500;font-family:"Microsoft Yahei";}</style>
</head>
<body><div class="c1">中國聯通</div><div>中國移動</div>
</body>
</html>
4.文字對齊方式
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{height: 59px;width: 300px;border: 1px solid red;text-align: center; /*水平方向居中*/line-height: 59px; /*垂直方向居中*/}</style>
</head>
<body><div class="c1">張三</div>
</body>
</html>
2、CSS-常見樣式和案例
①浮動
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div><span>左邊</span><span style="float: right">右邊</span></div>
</body>
</html>
div默認塊級標簽(比較霸道),如果浮動起來,就不一樣了。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.item{float: left;width: 280px;height: 170px;border: 1px solid red;}</style>
</head>
<body><div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div></div>
</body>
</html>
可是如果你讓標簽浮動起來之后,就會脫離文檔流,那該怎么解決呢?
可以通過添加這個語句<div style="clear: both"></div>
解決。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.item{float: left;width: 280px;height: 170px;border: 1px solid red;}</style>
</head>
<body><div style="background-color: dodgerblue"><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div><div style="clear: both"></div></div><div>你好</div></body>
</html>
為什么添加<div style="clear: both"></div>
能夠解決呢?
在HTML中,<div style="clear: both"></div>
用于控制布局,特別是在使用浮動(float)元素時。浮動元素會脫離文檔流的正常排列,可能導致布局混亂。clear
屬性用來清除元素的左側、右側或兩側的浮動,確保不會有浮動元素影響到它的布局。
具體來說,clear: both;
表示清除前面元素的左右浮動,使得接下來的內容顯示在浮動元素的下方,而不是旁邊或者覆蓋浮動元素。這個技術常用于確保父容器能夠包含其內部浮動的子元素,防止布局錯亂。
例如,如果你有幾個浮動的 <div>
元素用于創建并排的布局,之后你希望接下來的內容不受這些浮動元素的影響,顯示在它們下面,那么可以在浮動元素之后使用 <div style="clear: both"></div>
來實現這個布局需求。這樣,它會創建一個不可見的分界線,確保浮動不會影響到后續的內容布局。
②內邊距
內部距離,自己內部的距離
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.outer{border: 1px solid red;height: 200px;width: 200px;padding-top: 20px;padding-left: 20px;padding-right: 20px;padding-bottom: 20px;}</style>
</head>
<body><div class="outer"><div style="background-color: gold">聽媽媽的話</div><div>小朋友你是否有很多問號?</div></div>
</body>
</html>
③外邊距
外部距離,自己與別人之間的距離
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div style="height: 200px;background-color: dodgerblue"></div><div style="background-color: red;height: 100px; margin-top: 20px"></div>
</body>
</html>
綜合案例:小米商城
案例1 小米商城頂部
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>body{margin: 0;}.header{background: #333;}.container{width: 1226px;margin: 0 auto;}.header .menu{float: left;color: white;height: 38px;line-height: 38px;}.header .account{float: right;color: white;height: 38px;line-height: 38px;}.header a{color: #b0b0b0;line-height: 40px;display: inline-block;font-size: 12px;margin-right: 10px;}</style>
</head>
<body><div class="header"><div class="container"><div class="menu"><a>小米官網</a><a>小米商城</a><a>小米澎湃OS</a><a>云服務</a><a>小愛開放平臺</a><a>下載app</a></div><div class="account"><a>登錄</a><a>注冊</a><a>消息通知</a></div><div style="clear:both"></div></div></div>
</body>
</html>
小結
- body標簽,默認有一個邊距,會造成頁面四周有白色間隙,如何去除?
body{margin:0;
}
-
內容居中
-
文本居中
<div style="width: 200px; background-color:pink; text-align: center">張三</div>
-
區域居中,自己要有寬度 +
margin-left:auto; margin-right:auto;
.container{width: 980px;margin: 0 auto; }<div class="container">asddqsadas </div>
-
-
父親沒有高度或沒有寬度,被孩子支撐起來。
-
如果存在浮動,要加上
<div style="clear:both"></div>
-
如果想用別人實現的樣式
-
關于布局,不知道如何下手的話,學會分析頁面的布局
3、CSS-樣式和小米商城
- 案例應用(利用之前所學知識)
- CSS知識點
- 模版 + CSS + 構建頁面
①CSS案例
1.內容回顧
-
HTML標簽
固定格式,記住標簽長什么樣,例如: h / div / span / a / img / ul / li / table / input / form
-
CSS樣式
引用CSS:標簽、頭部、文件
.xx{... }<div class='xx xx'></div>
高度height / 寬度width /塊級 or 行內 or 塊級行內 / 浮動float / 字體font / 文字對齊方式text-align / 內邊距 / 外邊距關于邊距:- body- 區域居中
-
頁面布局
根據你看到的頁面把他們劃分成很多小的區域,再去填充樣式。
2.案例:二級菜單
a.分析布局
b.搭建骨架
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>二級菜單</title><style>.sub-header{height: 100px;background-color: #b0b0b0;}.container{width: 1128px;margin: 0 auto;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;}.sub-header .menu-list{float: left;}.sub-header .search{float: right;}</style>
</head>
<body><div class="sub-header"><div class="container"><div class="ht logo">1</div><div class="ht menu-list">2</div><div class="ht search">3</div><div style="clear: both"></div></div></div><div><div class="logo"></div></div>
</body>
</html>
c.完善logo區域
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>二級菜單</title><style>.sub-header{height: 100px;background-color: #b0b0b0;}.container{width: 1128px;margin: 0 auto;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;border: 1px solid red;line-height: 100px;}.sub-header .logo a{margin-top: 22px;display: inline-block}.sub-header .logo a img{height: 56px;width: 56px;}.sub-header .menu-list{float: left;}.sub-header .search{float: right;}</style>
</head>
<body><div class="sub-header"><div class="container"><div class="ht logo"><!--a,行內標簽,默認設置高度、邊距無效 解決:轉換成塊級 OR 行內&塊級--><a href="https://www.mi.com/shop" style="margin-top: 22px;display: inline-block"><img src="images/logo-mi2.png" style="height: 56px; width:56px;"alt=""></a></div><div class="ht menu-list">2</div><div class="ht search">3</div><div style="clear: both"></div></div></div><div><div class="logo"></div></div>
</body>
</html>
d.布置菜單區域
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>二級菜單</title><style>.sub-header{height: 100px;}.container{width: 1128px;margin: 0 auto;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;line-height: 100px;}.sub-header .logo a{margin-top: 22px;display: inline-block}.sub-header .logo a img{height: 56px;width: 56px;}.sub-header .menu-list{float: left;line-height: 100px;}.sub-header .menu-list a{display: inline-block;padding: 0 10px;color: #333;font-size: 16px;text-decoration: none;}.sub-header .menu-list a:hover{color: #ff6700;}.sub-header .search{float: right;}</style>
</head>
<body><div class="sub-header"><div class="container"><div class="ht logo"><!--a,行內標簽,默認設置高度、邊距無效 解決:轉換成塊級 OR 行內&塊級--><a href="https://www.mi.com/shop" style="margin-top: 22px;display: inline-block"><img src="images/logo-mi2.png" style="height: 56px; width:56px;"alt=""></a></div><div class="ht menu-list"><a href="https://www.mi.com/shop">Xiaomi手機</a><a href="https://www.mi.com/shop">Redmi手機</a><a href="https://www.mi.com/shop">電視</a><a href="https://www.mi.com/shop">電筆記本</a><a href="https://www.mi.com/shop">平板</a><a href="https://www.mi.com/shop">家電</a><a href="https://www.mi.com/shop">路由器</a></div><div class="ht search">3</div><div style="clear: both"></div></div></div><div><div class="logo"></div></div>
</body>
</html>
3.綜合案例:頂部菜單+二級菜單
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>小米頂部</title><style>body{margin: 0;}.header{background: #333;}.container{width: 1226px;margin: 0 auto;}.header .menu{float: left;color: white;height: 38px;line-height: 38px;}.header .account{float: right;color: white;height: 38px;line-height: 38px;}.header a{color: #b0b0b0;line-height: 40px;display: inline-block;font-size: 12px;margin-right: 10px;text-decoration: none;}.header a:hover{color: white;}.sub-header{height: 100px;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;line-height: 100px;}.sub-header .logo a{margin-top: 22px;display: inline-block;}.sub-header .logo a img{height: 56px;width: 56px;}.sub-header .menu-list{float: left;line-height: 100px;}.sub-header .menu-list a{display: inline-block;padding: 0 10px;color: #333;font-size: 16px;text-decoration: none;}.sub-header .menu-list a:hover{color: #ff6700;}.sub-header .search{float: right;padding-top: 35px; /* Adjust padding to align search box as needed */}.search input[type="text"]{right: 51px;z-index: 1;width: 223px;height: 48px;padding: 0 10px;font-size: 14px;line-height: 48px;vertical-align: middle;}.search input[type="submit"]{right: 0;z-index: 2;width: 52px;height: 50px;font-size: 24px;line-height: 24px;background: #fff;color: #616161;}.search-btn {background: url('/images/search.png') no-repeat center center; /* 搜索圖標的路徑 */border: 1px solid;cursor: pointer; /* 鼠標懸停時顯示手形圖標 */width: 52px;height: 50px;background-size: contain; /* 使背景圖像適應按鈕大小 */vertical-align: middle;}/*可能需要的額外樣式調整*/.search-btn:focus {outline: none; /* 移除聚焦時的輪廓線 */}</style>
</head>
<body><div class="header"><div class="container"><div class="menu"><a href="https://www.mi.com/shop">小米官網</a><a href="https://www.mi.com/shop">小米商城</a><a href="https://www.mi.com/shop">小米澎湃OS</a><a href="https://www.mi.com/shop">云服務</a><a href="https://www.mi.com/shop">小愛開放平臺</a><a href="https://www.mi.com/shop">下載app</a></div><div class="account"><a>登錄</a><a>注冊</a><a>消息通知</a></div><div style="clear:both"></div></div></div><div class="sub-header"><div class="container"><div class="ht logo"><a href="https://www.mi.com/shop" style="margin-top: 22px;display: inline-block"><img src="images/logo-mi2.png" style="height: 56px; width:56px;"alt=""></a></div><div class="ht menu-list"><a href="https://www.mi.com/shop">Xiaomi手機</a><a href="https://www.mi.com/shop">Redmi手機</a><a href="https://www.mi.com/shop">電視</a><a href="https://www.mi.com/shop">電筆記本</a><a href="https://www.mi.com/shop">平板</a><a href="https://www.mi.com/shop">家電</a><a href="https://www.mi.com/shop">路由器</a></div><div class="ht search"><form action="/search" method="get"><input type="text" name="q" placeholder="搜索產品" required><input type="image" src="/images/search.png" alt="搜索" class="search-btn iconfont"></form></div><div style="clear: both"></div></div></div>
</body>
</html>
②小結
-
a標簽是行內標簽,行內標簽的高度、內外邊距,默認無效。
-
垂直方向居中
- 文本
line-height
- 圖片 邊距
- 文本
-
a標簽默認有下劃線
-
鼠標放上去之后hover
.c1:hover{... } a:hover{}
4、CSS-案例:小米商城新品部分
①案例:推薦區域
1.劃分區域
2.搭建骨架
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>小米頂部</title><style>body{margin: 0;}.header{background: #333;}.container{width: 1226px;margin: 0 auto;}.header .menu{float: left;color: white;height: 38px;line-height: 38px;}.header .account{float: right;color: white;height: 38px;line-height: 38px;}.header a{color: #b0b0b0;line-height: 40px;display: inline-block;font-size: 12px;margin-right: 10px;text-decoration: none;}.header a:hover{color: white;}.sub-header{height: 100px;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;line-height: 100px;}.sub-header .logo a{margin-top: 22px;display: inline-block;}.sub-header .logo a img{height: 56px;width: 56px;}.sub-header .menu-list{float: left;line-height: 100px;}.sub-header .menu-list a{display: inline-block;padding: 0 10px;color: #333;font-size: 16px;text-decoration: none;}.sub-header .menu-list a:hover{color: #ff6700;}.sub-header .search{float: right;padding-top: 35px; /* Adjust padding to align search box as needed */}.search input[type="text"]{right: 51px;z-index: 1;width: 223px;height: 48px;padding: 0 10px;font-size: 14px;line-height: 48px;vertical-align: middle;}.search input[type="submit"]{right: 0;z-index: 2;width: 52px;height: 50px;font-size: 24px;line-height: 24px;background: #fff;color: #616161;}.search-btn {background: url('/images/search.png') no-repeat center center; /* 搜索圖標的路徑 */border: 1px solid;cursor: pointer; /* 鼠標懸停時顯示手形圖標 */width: 52px;height: 50px;background-size: contain; /* 使背景圖像適應按鈕大小 */vertical-align: middle;}/*可能需要的額外樣式調整*/.search-btn:focus {outline: none; /* 移除聚焦時的輪廓線 */}.slider img{width: 1226px;height: 460px;text-align: right;}</style>
</head>
<body><div class="header"><div class="container"><div class="menu"><a href="https://www.mi.com/shop">小米官網</a><a href="https://www.mi.com/shop">小米商城</a><a href="https://www.mi.com/shop">小米澎湃OS</a><a href="https://www.mi.com/shop">云服務</a><a href="https://www.mi.com/shop">小愛開放平臺</a><a href="https://www.mi.com/shop">下載app</a></div><div class="account"><a>登錄</a><a>注冊</a><a>消息通知</a></div><div style="clear:both"></div></div></div><div class="sub-header"><div class="container"><div class="ht logo"><a href="https://www.mi.com/shop" style="margin-top: 22px;display: inline-block"><img src="images/logo-mi2.png" style="height: 56px; width:56px;"alt=""></a></div><div class="ht menu-list"><a href="https://www.mi.com/shop">Xiaomi手機</a><a href="https://www.mi.com/shop">Redmi手機</a><a href="https://www.mi.com/shop">電視</a><a href="https://www.mi.com/shop">電筆記本</a><a href="https://www.mi.com/shop">平板</a><a href="https://www.mi.com/shop">家電</a><a href="https://www.mi.com/shop">路由器</a></div><div class="ht search"><form action="/search" method="get"><input type="text" name="q" placeholder="搜索產品" required><input type="image" src="/images/search.png" alt="搜索" class="search-btn iconfont"></form></div><div style="clear: both"></div></div></div><div class="slider"><div class="container"><div class="sd-img"><img src="images/b1.jpg" alt=""></div></div></div><div class="news"><div class="container"><div class="channel"></div><div class="list-item"></div><div class="list-item"></div><div class="list-item"></div></div></div></body>
</html>
②案例實現
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>小米頂部</title><style>body{margin: 0;}.header{background: #333;}.container{width: 1226px;margin: 0 auto;}.left{float: left;}.header .menu{float: left;color: white;height: 38px;line-height: 38px;}.header .account{float: right;color: white;height: 38px;line-height: 38px;}.header a{color: #b0b0b0;line-height: 40px;display: inline-block;font-size: 12px;margin-right: 10px;text-decoration: none;}.header a:hover{color: white;}.sub-header{height: 100px;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;line-height: 100px;}.sub-header .logo a{margin-top: 22px;display: inline-block;}.sub-header .logo a img{height: 56px;width: 56px;}.sub-header .menu-list{float: left;line-height: 100px;}.sub-header .menu-list a{display: inline-block;padding: 0 10px;color: #333;font-size: 16px;text-decoration: none;}.sub-header .menu-list a:hover{color: #ff6700;}.sub-header .search{float: right;padding-top: 35px; /* Adjust padding to align search box as needed */}.search input[type="text"]{right: 51px;z-index: 1;width: 223px;height: 48px;padding: 0 10px;font-size: 14px;line-height: 48px;vertical-align: middle;}.search input[type="submit"]{right: 0;z-index: 2;width: 52px;height: 50px;font-size: 24px;line-height: 24px;background: #fff;color: #616161;}.search-btn {background: url('/images/search.png') no-repeat center center; /* 搜索圖標的路徑 */border: 1px solid;cursor: pointer; /* 鼠標懸停時顯示手形圖標 */width: 52px;height: 50px;background-size: contain; /* 使背景圖像適應按鈕大小 */vertical-align: middle;}/*可能需要的額外樣式調整*/.search-btn:focus {outline: none; /* 移除聚焦時的輪廓線 */}.slider img{width: 1226px;height: 460px;text-align: right;}.news{margin-top: 14px;}.news .channel{width: 228px;height: 164px;background-color:#5f5750;padding: 3px;}.news .channel .item img{display: block;width: 24px;height: 24px;margin: 0 auto 4px;}.news .channel .item{height: 82px;width: 76px;float: left;text-align: center;}.news .channel .item a{display: inline-block;font-size: 12px;padding-top: 18px;color: #ffffff;text-decoration: none;opacity: 0.7;}.news .channel .item a:hover{opacity: 1;}.news .list-item img{width: 316px;height: 170px;display: block;margin: 0 auto 4px;}</style>
</head>
<body><div class="header"><div class="container"><div class="menu"><a href="https://www.mi.com/shop">小米官網</a><a href="https://www.mi.com/shop">小米商城</a><a href="https://www.mi.com/shop">小米澎湃OS</a><a href="https://www.mi.com/shop">云服務</a><a href="https://www.mi.com/shop">小愛開放平臺</a><a href="https://www.mi.com/shop">下載app</a></div><div class="account"><a>登錄</a><a>注冊</a><a>消息通知</a></div><div style="clear:both"></div></div></div><div class="sub-header"><div class="container"><div class="ht logo"><a href="https://www.mi.com/shop" style="margin-top: 22px;display: inline-block"><img src="images/logo-mi2.png" style="height: 56px; width:56px;"alt=""></a></div><div class="ht menu-list"><a href="https://www.mi.com/shop">Xiaomi手機</a><a href="https://www.mi.com/shop">Redmi手機</a><a href="https://www.mi.com/shop">電視</a><a href="https://www.mi.com/shop">電筆記本</a><a href="https://www.mi.com/shop">平板</a><a href="https://www.mi.com/shop">家電</a><a href="https://www.mi.com/shop">路由器</a></div><div class="ht search"><form action="/search" method="get"><input type="text" name="q" placeholder="搜索產品" required><input type="image" src="/images/search.png" alt="搜索" class="search-btn iconfont"></form></div><div style="clear: both"></div></div></div><div class="slider"><div class="container"><div class="sd-img"><img src="images/b1.jpg" alt=""></div></div></div><div class="news"><div class="container"><div class="channel left"><div class="item"><a href="https://www.mi.com/shop"><img src="images/c1.png" alt=""><span>保障服務</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c2.png" alt=""><span>企業團購</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c3.png" alt=""><span>F碼通道</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c4.png" alt=""><span>米粉卡</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c5.png" alt=""><span>以舊換新</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c6.png" alt=""><span>話費充值</span></a></div><div class="clear:both"></div></div><div class="list-item left" style="margin-left: 14px"><img src="/images/w1.png" alt=""></div><div class="list-item left" style="margin-left: 15px"><img src="/images/w2.jpg" alt=""></div><div class="list-item left" style="margin-left: 15px"><img src="/images/w3.png" alt=""></div></div></div></body>
</html>
-
設置透明度
通過opacity來設置opacity:0.5 /* 透明度為0.5 透明度范圍:0~1 */
5、CSS-邊框和背景色
①:hover(偽類)
用于定義鼠標懸停在元素上時的樣式。例如,a:hover { color: red; }會使鏈接在鼠標懸停時變為紅色。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{color: red;font-size: 18px;}.c1:hover{color: green;font-size: 50px;}.c2{height: 300px;width: 500px;border: 3px solid red;}.c2:hover{border: 3px solid green;}.download{display: none;}.app:hover .download{display: block;}.app:hover .title{color: yellowgreen;}</style>
</head>
<body><div class="c1">聯通</div><div class="c2">廣西</div><div class="app"><div class="title">下載App</div><div class="download"><img src="images/QRcode.png" alt=""></div></div>
</body>
</html>
②:after(偽元素)
用于在元素的內容之后插入額外的內容。它通常與content屬性一起使用。例如,p:after { content: “Read more”; }會在所有p元素的內容后添加"Read more"文本。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1:after{content:"大帥哥";}</style>
</head>
<body><div class="c1">張三</div><div class="c1">李四</div>
</body>
</html>
很重要的應用,可以清除浮動,以免脫離文檔流。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.clearfix:after{content: "";display: block;clear: both;}.item{float: left;}</style>
</head>
<body><div class="clearfix"><div class="item">1</div><div class="item">2</div><div class="item">3</div></div>
</body>
</html>
③position屬性
用于設置元素的定位方式。它的值可以是static、relative、absolute、fixed或sticky,分別對應不同的定位行為。常用的三個方式如下:
- fixed
- relative
- absolute
a.fixed
固定在窗口的某個位置
案例1:返回頂部
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>小米頂部</title><style>body{margin: 0;}.header{background: #333;}.container{width: 1226px;margin: 0 auto;}.left{float: left;}.header .menu{float: left;color: white;height: 38px;line-height: 38px;}.header .account{float: right;color: white;height: 38px;line-height: 38px;}.header a{color: #b0b0b0;line-height: 40px;display: inline-block;font-size: 12px;margin-right: 10px;text-decoration: none;}.header a:hover{color: white;}.sub-header{height: 100px;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;line-height: 100px;}.sub-header .logo a{margin-top: 22px;display: inline-block;}.sub-header .logo a img{height: 56px;width: 56px;}.sub-header .menu-list{float: left;line-height: 100px;}.sub-header .menu-list a{display: inline-block;padding: 0 10px;color: #333;font-size: 16px;text-decoration: none;}.sub-header .menu-list a:hover{color: #ff6700;}.sub-header .search{float: right;padding-top: 35px; /* Adjust padding to align search box as needed */}.search input[type="text"]{right: 51px;z-index: 1;width: 223px;height: 48px;padding: 0 10px;font-size: 14px;line-height: 48px;vertical-align: middle;}.search input[type="submit"]{right: 0;z-index: 2;width: 52px;height: 50px;font-size: 24px;line-height: 24px;background: #fff;color: #616161;}.search-btn {background: url('/images/search.png') no-repeat center center; /* 搜索圖標的路徑 */border: 1px solid;cursor: pointer; /* 鼠標懸停時顯示手形圖標 */width: 52px;height: 50px;background-size: contain; /* 使背景圖像適應按鈕大小 */vertical-align: middle;}/*可能需要的額外樣式調整*/.search-btn:focus {outline: none; /* 移除聚焦時的輪廓線 */}.slider img{width: 1226px;height: 460px;text-align: right;}.news{margin-top: 14px;}.news .channel{width: 228px;height: 164px;background-color:#5f5750;padding: 3px;}.news .channel .item img{display: block;width: 24px;height: 24px;margin: 0 auto 4px;}.news .channel .item{height: 82px;width: 76px;float: left;text-align: center;}.news .channel .item a{display: inline-block;font-size: 12px;padding-top: 18px;color: #ffffff;text-decoration: none;opacity: 0.7;}.news .channel .item a:hover{opacity: 1;}.news .list-item img{width: 316px;height: 170px;display: block;margin: 0 auto 4px;}.back{position: fixed;width: 60px;height: 60px;border: 1px solid red;right: 10px;bottom: 50px;}</style>
</head>
<body><div class="header"><div class="container"><div class="menu"><a href="https://www.mi.com/shop">小米官網</a><a href="https://www.mi.com/shop">小米商城</a><a href="https://www.mi.com/shop">小米澎湃OS</a><a href="https://www.mi.com/shop">云服務</a><a href="https://www.mi.com/shop">小愛開放平臺</a><a href="https://www.mi.com/shop">下載app</a></div><div class="account"><a>登錄</a><a>注冊</a><a>消息通知</a></div><div style="clear:both"></div></div></div><div class="sub-header"><div class="container"><div class="ht logo"><a href="https://www.mi.com/shop" style="margin-top: 22px;display: inline-block"><img src="images/logo-mi2.png" style="height: 56px; width:56px;"alt=""></a></div><div class="ht menu-list"><a href="https://www.mi.com/shop">Xiaomi手機</a><a href="https://www.mi.com/shop">Redmi手機</a><a href="https://www.mi.com/shop">電視</a><a href="https://www.mi.com/shop">電筆記本</a><a href="https://www.mi.com/shop">平板</a><a href="https://www.mi.com/shop">家電</a><a href="https://www.mi.com/shop">路由器</a></div><div class="ht search"><form action="/search" method="get"><input type="text" name="q" placeholder="搜索產品" required><input type="image" src="/images/search.png" alt="搜索" class="search-btn iconfont"></form></div><div style="clear: both"></div></div></div><div class="slider"><div class="container"><div class="sd-img"><img src="images/b1.jpg" alt=""></div></div></div><div class="news"><div class="container"><div class="channel left"><div class="item"><a href="https://www.mi.com/shop"><img src="images/c1.png" alt=""><span>保障服務</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c2.png" alt=""><span>企業團購</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c3.png" alt=""><span>F碼通道</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c4.png" alt=""><span>米粉卡</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c5.png" alt=""><span>以舊換新</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c6.png" alt=""><span>話費充值</span></a></div><div class="clear:both"></div></div><div class="list-item left" style="margin-left: 14px"><img src="/images/w1.png" alt=""></div><div class="list-item left" style="margin-left: 15px"><img src="/images/w2.jpg" alt=""></div><div class="list-item left" style="margin-left: 15px"><img src="/images/w3.png" alt=""></div></div></div><div style="height: 1000px;"></div><div class="back"></div>
</body>
</html>
案例2:對話框
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>body{margin: 0;}.dialog{position: fixed;height: 300px;width: 500px;background-color: white;/*left: 50%;*//*margin-left: -250px;*/left: 0;right: 0;margin: 0 auto;top: 200px;z-index: 1000;}.mask{background-color: black;position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;opacity: 0.7;z-index: 999;}</style>
</head>
<body><div class="mask"></div><div style="height: 1000px;">qwewqeqw</div><div class="dialog"></div>
</body>
</html>
④relative和absolute定位
-
relative定位:是position屬性的一個值,表示元素將相對于其正常位置進行定位。也就是說,即使你對元素應用了定位,它仍然占據原來的空間,但可以通過top、right、bottom、left屬性移動位置。
-
absolute定位:也是position屬性的一個值,表示元素相對于最近的已定位的祖先元素(不是static定位的元素)進行定位。如果沒有這樣的元素,則相對于文檔體()元素定位。絕對定位的元素不占據文檔流中的空間。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{height: 300px;width: 500px;border: 1px solid red;margin: 100px;position: relative;}.c1 .c2{height: 59px;width: 59px;background-color: #00FF7F;position: absolute;right: 20px;bottom: 10px;}</style>
</head>
<body><div class="c1"><div class="c2"></div></div>
</body>
</html>
案例1:小米商城下載app
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>小米頂部</title><style>body{margin: 0;}.header{background: #333;}.container{width: 1226px;margin: 0 auto;}.left{float: left;}.header .menu{float: left;color: white;height: 38px;line-height: 38px;}.header .account{float: right;color: white;height: 38px;line-height: 38px;}.header a{color: #b0b0b0;line-height: 40px;display: inline-block;font-size: 12px;margin-right: 10px;text-decoration: none;}.header a:hover{color: white;}.sub-header{height: 100px;}.sub-header .ht{height: 100px;}.sub-header .logo{width: 234px;float: left;line-height: 100px;}.sub-header .logo a{margin-top: 22px;display: inline-block;}.sub-header .logo a img{height: 56px;width: 56px;}.sub-header .menu-list{float: left;line-height: 100px;}.sub-header .menu-list a{display: inline-block;padding: 0 10px;color: #333;font-size: 16px;text-decoration: none;}.sub-header .menu-list a:hover{color: #ff6700;}.sub-header .search{float: right;padding-top: 35px; /* Adjust padding to align search box as needed */}.search input[type="text"]{right: 51px;z-index: 1;width: 223px;height: 48px;padding: 0 10px;font-size: 14px;line-height: 48px;vertical-align: middle;}.search input[type="submit"]{right: 0;z-index: 2;width: 52px;height: 50px;font-size: 24px;line-height: 24px;background: #fff;color: #616161;}.search-btn {background: url('/images/search.png') no-repeat center center; /* 搜索圖標的路徑 */border: 1px solid;cursor: pointer; /* 鼠標懸停時顯示手形圖標 */width: 52px;height: 50px;background-size: contain; /* 使背景圖像適應按鈕大小 */vertical-align: middle;}/*可能需要的額外樣式調整*/.search-btn:focus {outline: none; /* 移除聚焦時的輪廓線 */}.slider img{width: 1226px;height: 460px;text-align: right;}.news{margin-top: 14px;}.news .channel{width: 228px;height: 164px;background-color:#5f5750;padding: 3px;}.news .channel .item img{display: block;width: 24px;height: 24px;margin: 0 auto 4px;}.news .channel .item{height: 82px;width: 76px;float: left;text-align: center;}.news .channel .item a{display: inline-block;font-size: 12px;padding-top: 18px;color: #ffffff;text-decoration: none;opacity: 0.7;}.news .channel .item a:hover{opacity: 1;}.news .list-item img{width: 316px;height: 170px;display: block;margin: 0 auto 4px;}.app .download{position: absolute;height: 100px;width: 100px;display: none;}.app .download img{height: 100px;width: 100px;}.app:hover .download{display: block;}</style>
</head>
<body><div class="header"><div class="container"><div class="menu"><a href="https://www.mi.com/shop">小米官網</a><a href="https://www.mi.com/shop">小米商城</a><a href="https://www.mi.com/shop">小米澎湃OS</a><a href="https://www.mi.com/shop">云服務</a><a href="https://www.mi.com/shop">小愛開放平臺</a><a href="https://www.mi.com/shop" class="app">下載app<div class="download"><img src="images/QRcode.png" alt=""></div></a></div><div class="account"><a>登錄</a><a>注冊</a><a>消息通知</a></div><div style="clear:both"></div></div></div><div class="sub-header"><div class="container"><div class="ht logo"><a href="https://www.mi.com/shop" style="margin-top: 22px;display: inline-block"><img src="images/logo-mi2.png" style="height: 56px; width:56px;"alt=""></a></div><div class="ht menu-list"><a href="https://www.mi.com/shop">Xiaomi手機</a><a href="https://www.mi.com/shop">Redmi手機</a><a href="https://www.mi.com/shop">電視</a><a href="https://www.mi.com/shop">電筆記本</a><a href="https://www.mi.com/shop">平板</a><a href="https://www.mi.com/shop">家電</a><a href="https://www.mi.com/shop">路由器</a></div><div class="ht search"><form action="/search" method="get"><input type="text" name="q" placeholder="搜索產品" required><input type="image" src="/images/search.png" alt="搜索" class="search-btn iconfont"></form></div><div style="clear: both"></div></div></div><div class="slider"><div class="container"><div class="sd-img"><img src="images/b1.jpg" alt=""></div></div></div><div class="news"><div class="container"><div class="channel left"><div class="item"><a href="https://www.mi.com/shop"><img src="images/c1.png" alt=""><span>保障服務</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c2.png" alt=""><span>企業團購</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c3.png" alt=""><span>F碼通道</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c4.png" alt=""><span>米粉卡</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c5.png" alt=""><span>以舊換新</span></a></div><div class="item"><a href="https://www.mi.com/shop"><img src="images/c6.png" alt=""><span>話費充值</span></a></div><div class="clear:both"></div></div><div class="list-item left" style="margin-left: 14px"><img src="/images/w1.png" alt=""></div><div class="list-item left" style="margin-left: 15px"><img src="/images/w2.jpg" alt=""></div><div class="list-item left" style="margin-left: 15px"><img src="/images/w3.png" alt=""></div></div></div></body>
</html>
⑤border屬性
用于設置元素邊框的樣式、寬度和顏色。例如,border: 1px solid black;會給元素添加一條1像素寬、實線、黑色的邊框。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{height: 50px;width: 500px;/*1、邊框粗細3px 2、實線 solid/虛線 dotted 3、邊框顏色red*//*border: 3px solid red;*//*border-left: 3px solid transparent;*/margin: 100px;background-color: #5f5750;border-left: 2px solid transparent;/*position: relative;*/}.c1:hover{border-left: 2px solid red;}</style>
</head>
<body><div class="c1">菜單</div>
</body>
</html>
⑥background-color屬性
用于設置元素的背景顏色。例如,background-color: blue;會將元素的背景顏色設置為藍色。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.c1{height: 50px;width: 500px;margin: 100px;background-color: #66CDAA;}</style>
</head>
<body><div class="c1">菜單</div>
</body>
</html>
注意:以上不是所有的CSS樣式,上述僅為開發中常用的核心知識點,通過此篇內容可以幫助你大致了解頁面的樣式和標簽:
后續會了解一些模版,內容包括:
- 模版的基本使用邏輯
- 模版 + 自己CSS知識點(開發頁面)
很感謝你能看到這里,如有相關疑問,還請下方評論留言。
筆記記錄來源:B站 python的web開發全家桶(django+前端+數據庫)
Code_流蘇(CSDN)(一個喜歡古詩詞和編程的Coder😊)
如果對大家有幫助的話,希望大家能多多點贊+關注!這樣我的動力會更足!