日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

css2

發布時間:2024/3/12 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 css2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CSS屬性相關

寬和高

width屬性可以為元素設置寬度。

height屬性可以為元素設置高度。

塊級標簽才能設置寬度,內聯標簽的寬度由內容來決定。

字體屬性

文字字體

font-family可以把多個字體名稱作為一個“回退”系統來保存。如果瀏覽器不支持第一個字體,則會嘗試下一個。瀏覽器會使用它可識別的第一個值

body {font-family: "Microsoft Yahei", "微軟雅黑", "Arial", sans-serif }

字體大小

p {font-size: 14px; }

如果設置成inherit表示繼承父元素的字體大小值

字重(粗細)?

font-weight用來設置字體的字重(粗細)。

值描述
normal默認值,標準粗細
bold粗體
bolder更粗
lighter更細
100~900設置具體粗細,400等同于normal,而700等同于bold
inherit繼承父元素字體的粗細值

文本顏色

?

顏色屬性被用來設置文字的顏色。

顏色是通過CSS最經常的指定:

  • 十六進制值 - 如:?FF0000
  • 一個RGB值 - 如:?RGB(255,0,0)
  • 顏色的名稱 - 如: ?red

還有rgba(255,0,0,0.3),第四個值為alpha, 指定了色彩的透明度/不透明度,它的范圍為0.0到1.0之間。

文字屬性

文字對齊

text-align 屬性規定元素中的文本的水平對齊方式。

?

值描述
left左邊對齊 默認值
right右對齊
center居中對齊
justify兩端對齊

文字裝飾

text-decoration 屬性用來給文字添加特殊效果。

?

值描述
none默認。定義標準的文本。
underline定義文本下的一條線。
overline定義文本上的一條線。
line-through定義穿過文本下的一條線。
inherit繼承父元素的text-decoration屬性的值。

常用的為去掉a標簽默認的自劃線:

a {text-decoration: none; }

首行縮進

將段落的第一行縮進 32像素:

p {text-indent: 32px; }

背景屬性

/*背景顏色*/
background-color: red;
/*背景圖片*/
background-image: url('1.jpg');
/*
背景重復
repeat(默認):背景圖片平鋪排滿整個網頁
repeat-x:背景圖片只在水平方向上平鋪
repeat-y:背景圖片只在垂直方向上平鋪
no-repeat:背景圖片不平鋪
*/
background-repeat: no-repeat;
/*背景位置*/
background-position: left top;
/*background-position: 200px 200px;*/

?

支持簡寫:

background:#336699 url('1.png') no-repeat left top;

使用背景圖片的一個常見案例就是很多網站會把很多小圖標放在一張圖片上,然后根據位置去顯示圖片。減少頻繁的圖片請求。https://www.w3school.com.cn/css/css_background.asp

一個有趣的例子:

?

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>滾動背景圖示例</title><style>* {margin: 0;}.box {width: 100%;height: 500px;background: url("http://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D450%2C600/sign=e9952f4a6f09c93d07a706f3aa0dd4ea/4a36acaf2edda3cc5c5bdd6409e93901213f9232.jpg") center center;background-attachment: fixed;}.d1 {height: 500px;background-color: tomato;}.d2 {height: 500px;background-color: steelblue;}.d3 {height: 500px;background-color: mediumorchid;}</style> </head> <body><div class="d1"></div><div class="box"></div><div class="d2"></div><div class="d3"></div> </body> </html>圖片不動

邊框

邊框屬性?

  • border-width
  • border-style
  • border-color #i1 {border-width: 2px;border-style: solid;border-color: red; }

    通常使用簡寫方式:

    #i1 { border: 2px solid red; }

邊框樣式

?

值描述
none無邊框。
dotted點狀虛線邊框。
dashed矩形虛線邊框。
solid實線邊框。

除了可以統一設置邊框外還可以單獨為某一個邊框設置樣式,如下所示:

#i1 {
border-top-style:dotted;
border-top-color: red;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:none;
}

border-radius? ? ? % 50一般

用這個屬性能實現圓角邊框的效果。

將border-radius設置為長或高的一半即可得到一個圓形。

display屬性

用于控制HTML元素的顯示效果。

?

意義
display:"none"HTML文檔中元素存在,但是在瀏覽器中不顯示。一般用于配合JavaScript代碼使用。
display:"block"默認占滿整個頁面寬度,如果設置了指定寬度,則會用margin填充剩下的部分。
display:"inline"按行內元素顯示,此時再設置元素的width、height、margin-top、margin-bottom和float屬性都不會有什么影響。
display:"inline-block"使元素同時具有行內元素和塊級元素的特點。

display:"none"與visibility:hidden的區別:

visibility:hidden: 可以隱藏某個元素,但隱藏的元素仍需占用與未隱藏之前一樣的空間。也就是說,該元素雖然被隱藏了,但仍然會影響布局。

display:none: 可以隱藏某個元素,且隱藏的元素不會占用任何空間。也就是說,該元素不但被隱藏了,而且該元素原本占用的空間也會從頁面布局中消失。

CSS盒子模型

  • margin: ? ? ? ? ? ?用于控制元素與元素之間的距離;margin的最基本用途就是控制元素周圍空間的間隔,從視覺角度上達到相互隔開的目的。
  • padding: ? ? ? ? ? 用于控制內容與邊框之間的距離; ??
  • Border(邊框): ? ? 圍繞在內邊距和內容外的邊框。
  • Content(內容): ? 盒子的內容,顯示文本和圖像。

?

?

?

margin外邊距

.margin-test {margin-top:5px;margin-right:10px;margin-bottom:15px;margin-left:20px; }

推薦使用簡寫:

.margin-test {margin: 5px 10px 15px 20px; }

順序:上右下左

常見居中:

.mycenter {margin: 0 auto; }

padding內填充

.padding-test {padding-top: 5px;padding-right: 10px;padding-bottom: 15px;padding-left: 20px; }


推薦使用簡寫:

.padding-test {padding: 5px 10px 15px 20px; }


順序:上右下左

補充padding的常用簡寫方式:

  • 提供一個,用于四邊;
  • 提供兩個,第一個用于上-下,第二個用于左-右;
  • 如果提供三個,第一個用于上,第二個用于左-右,第三個用于下;
  • 提供四個參數值,將按上-右-下-左的順序作用于四邊

float

在 CSS 中,任何元素都可以浮動。

浮動元素會生成一個塊級框,而不論它本身是何種元素。

關于浮動的兩個特點:

  • 浮動的框可以向左或向右移動,直到它的外邊緣碰到包含框或另一個浮動框的邊框為止。
  • 由于浮動框不在文檔的普通流中,所以文檔的普通流中的塊框表現得就像浮動框不存在一樣。

三種取值

left:向左浮動

right:向右浮動

none:默認值,不浮動

范例? ?https://www.w3school.com.cn/css/css_positioning_floating.asp

清除浮動

清除浮動的副作用(父標簽塌陷問題)

主要有三種方式:

  • 固定高度
  • 偽元素清除法
  • overflow:hidden

偽元素清除法(使用較多):

.clearfix:after {content: "";display: block;clear: both; }

?overflow溢出屬性?

值描述
visible默認值。內容不會被修剪,會呈現在元素框之外。
hidden內容會被修剪,并且其余內容是不可見的。
scroll內容會被修剪,但是瀏覽器會顯示滾動條以便查看其余的內容。
auto如果內容被修剪,則瀏覽器會顯示滾動條以便查看其余的內容。
inherit規定應該從父元素繼承 overflow 屬性的值。

?

  • overflow(水平和垂直均設置)
  • overflow-x(設置水平方向)
  • overflow-y(設置垂直方向)

?

?圓形頭像示例

<!DOCTYPE HTML> <html> <head><meta charset="UTF-8"><meta http-equiv="x-ua-compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>圓形的頭像示例</title><style>* {margin: 0;padding: 0;background-color: #eeeeee;}.header-img {width: 150px;height: 150px;border: 3px solid white;border-radius: 50%;overflow: hidden;}.header-img>img {width: 100%;}</style> </head> <body><div class="header-img"><img src="https://pic.cnblogs.com/avatar/1342004/20180304191536.png" alt=""> </div></body> </html>圓形頭像示例

?

定位(position)

static

static 默認值,無定位,不能當作絕對定位的參照物,并且設置標簽對象的left、top等值是不起作用的的。

relative(相對定位)

相對定位是相對于該元素在文檔流中的原始位置,即以自己原始位置為參照物。有趣的是,即使設定了元素的相對定位以及偏移值,元素還占有著原來的位置,即占據文檔流空間。對象遵循正常文檔流,但將依據top,right,bottom,left等屬性在正常文檔流中偏移位置。而其層疊通過z-index屬性定義。

注意:position:relative的一個主要用法:方便絕對定位元素找到參照物。

absolute(絕對定位)

定義:設置為絕對定位的元素框從文檔流完全刪除,并相對于最近的已定位祖先元素定位,如果元素沒有已定位的祖先元素,那么它的位置相對于最初的包含塊(即body元素)。元素原先在正常文檔流中所占的空間會關閉,就好像該元素原來不存在一樣。元素定位后生成一個塊級框,而不論原來它在正常流中生成何種類型的框。

重點:如果父級設置了position屬性,例如position:relative;,那么子元素就會以父級的左上角為原始點進行定位。這樣能很好的解決自適應網站的標簽偏離問題,即父級為自適應的,那我子元素就設置position:absolute;父元素設置position:relative;,然后Top、Right、Bottom、Left用百分比寬度表示。

另外,對象脫離正常文檔流,使用top,right,bottom,left等屬性進行絕對定位。而其層疊通過z-index屬性定義。

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>絕對定位</title><style>.c1 {height: 100px;width: 100px;background-color: red;float: left;}.c2 {height: 50px;width: 50px;background-color: #ff6700;float: right;margin-right: 400px;position: relative;}.c3 {height: 200px;width: 200px;background-color: green;position: absolute;top: 50px;}</style> </head> <body> <div class="c1"></div> <div class="c2"><div class="c3"></div> </div></body> </html>絕對定位

fixed(固定)

fixed:對象脫離正常文檔流,使用top,right,bottom,left等屬性以窗口為參考點進行定位,當出現滾動條時,對象不會隨著滾動。而其層疊通過z-index屬性 定義。 注意點: 一個元素若設置了 position:absolute | fixed; 則該元素就不能設置float。這 是一個常識性的知識點,因為這是兩個不同的流,一個是浮動流,另一個是“定位流”。但是 relative 卻可以。因為它原本所占的空間仍然占據文檔流。

在理論上,被設置為fixed的元素會被定位于瀏覽器窗口的一個指定坐標,不論窗口是否滾動,它都會固定在這個位置。

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta http-equiv="x-ua-compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>返回頂部示例</title><style>* {margin: 0;}.d1 {height: 1000px;background-color: #eeee;}.scrollTop {background-color: darkgrey;padding: 10px;text-align: center;position: fixed;right: 10px;bottom: 20px;}</style> </head> <body> <div class="d1">111</div> <div class="scrollTop">返回頂部</div> </body> </html>返回頂部按鈕樣式示例

是否脫離文檔流

?

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.c1 {height: 50px;width: 100px;background-color: dodgerblue;}.c2 {height: 100px;width: 50px;background-color: orange;position: relative;left: 100px;}</style> </head> <body> <div class="c1"></div> <div class="c2"></div> <div style="height: 100px;width: 200px;background-color: black"></div> </body> </html>相對定位 相對定位 <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>.c1 {height: 50px;width: 100px;background-color: red;position: relative;}.c2 {height: 50px;width: 200px;background-color: green;position: absolute;left: 50px;}</style> </head> <body> <div class="c1">購物車<div class="c2">空空如也~</div><div style="height: 50px;width: 100px;background-color: deeppink"></div> </div></body> </html>絕對定位 絕對定位 <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div class="c1" style="height: 50px;width: 500px;background-color: black"></div> <div class="c2" style="height: 50px;width: 100px;background-color: deeppink;position: fixed;right: 10px;bottom: 20px"></div> <div class="c3" style="height: 10px;width: 100px;background-color: green"></div></body> </html>固定定位 固定定位

上述例子可知:

脫離文檔流:

  絕對定位

  固定定位

不脫離文檔流:

  相對定位

z-index

#i2 {z-index: 999; }

?

設置對象的層疊順序。

  • z-index 值表示誰壓著誰,數值大的壓蓋住數值小的,
  • 只有定位了的元素,才能有z-index,也就是說,不管相對定位,絕對定位,固定定位,都可以使用z-index,而浮動元素不能使用z-index
  • z-index值沒有單位,就是一個正整數,默認的z-index值為0如果大家都沒有z-index值,或者z-index值一樣,那么誰寫在HTML后面,誰在上面壓著別人,定位了元素,永遠壓住沒有定位的元素。
  • 從父現象:父親慫了,兒子再牛逼也沒用
  • <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta http-equiv="x-ua-compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>自定義模態框</title><style>.cover {background-color: rgba(0,0,0,0.65);position: fixed;top: 0;right: 0;bottom: 0;left: 0;z-index: 998;}.modal {background-color: white;position: fixed;width: 600px;height: 400px;left: 50%;top: 50%;margin: -200px 0 0 -300px;z-index: 1000;}</style> </head> <body><div class="cover"></div> <div class="modal"></div> </body> </html>自定義模態框示例

    opacity

    用來定義透明效果。取值范圍是0~1,0是完全透明,1是完全不透明。

    ?

    頂部導航菜單

    ?

    <!DOCTYPE HTML> <html> <head><meta charset="UTF-8"><meta http-equiv="x-ua-compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>li標簽的float示例</title><style>/*清除瀏覽器默認外邊距和內填充*/* {margin: 0;padding: 0;}a {text-decoration: none; /*去除a標簽默認的下劃線*/}.nav {background-color: black;height: 40px;width: 100%;position: fixed;top: 0;}ul {list-style-type: none; /*刪除列表默認的圓點樣式*/margin: 0; /*刪除列表默認的外邊距*/padding: 0; /*刪除列表默認的內填充*/}/*li元素向左浮動*/li {float: left;}li > a {display: block; /*讓鏈接顯示為塊級標簽*/padding: 0 15px; /*設置左右各15像素的填充*/color: #b0b0b0; /*設置字體顏色*/line-height: 40px; /*設置行高*/}/*鼠標移上去顏色變白*/li > a:hover {color: #fff;}/*清除浮動 解決父級塌陷問題*/.clearfix:after {content: "";display: block;clear: both;}</style> </head> <body> <!-- 頂部導航欄 開始 --> <div class="nav"><ul class="clearfix"><li><a href="">玉米商城</a></li><li><a href="">MIUI</a></li><li><a href="">ioT</a></li><li><a href="">云服務</a></li><li><a href="">水滴</a></li><li><a href="">金融</a></li><li><a href="">優品</a></li></ul> </div> <!-- 頂部導航欄 結束 --> </body> </html> View Code

    ?

    轉載于:https://www.cnblogs.com/xuzhaolong/p/11488144.html

    總結

    以上是生活随笔為你收集整理的css2的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。