溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

css的三種定位方式是什么

發布時間:2021-11-02 13:01:39 來源:億速云 閱讀:201 作者:柒染 欄目:web開發

本篇文章為大家展示了css的三種定位方式是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

css的三種定位方式是:1、相對定位,元素的位置相對于它的原始位置計算而來,語法“position:relative;”;2、固定定位,語法“position:fixed;”;3、絕對定位,語法“position:absolute;”。

本教程操作環境:windows7系統、CSS3&&HTML5版、Dell G3電腦。

相對定位

該元素的位置是相對于它的原始位置計算而來的。

position:relative;

他是默認參照父級的原始點為原始點,配合top、right、bottom、left進行定位。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>相對定位</title>
<style type="text/css">
.king{
margin-top: 30px;
margin-left: 30px;
border: 1px solid silver;
background-color: skyblue;
width: 40%;
}
.king div{
width: 100px;
height: 60px;
margin: 10px;
background-color: snow;
color: black;
border: 1px solid black;
}
.three{
position: relative;
top: 20px;
left: 50px;
}
</style>
<body>
<div class="king">
<div class="one">one</div>
<div class="two">two</div>
<div class="three">three</div>
<div class="four">four</div>
</div>
</body>
</html>

固定定位

被固定的元素不會隨著滾動條的拖動而改變位置。

position:fixed;

在默認情況下,固定定位元素的位置是相對瀏覽器而言,結合top、bottom、left和right這4個屬性一起使用。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>固定定位</title>
<style type="text/css">
.first{
width: 50px;
height: 160px;
border: 1px solid gray;
background-color: #b7f1b7;
}
.second{
position: fixed;
top: 50px;
left: 160px;
width: 150px;
height: 100px;
border: 1px solid silver;
background-color:#b7f1b7;
}
</style>
<body>
<div class="first">div元素</div>
<div class="second">固定定位的div元素</div>
</body>
</html>

絕對定位

position:absolute;

默認情況下,絕對定位的位置是相對于瀏覽器而言,配合top、right、bottom、left進行定位。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>絕對定位</title>
<style type="text/css">
.king{
padding: 15px;
border: 1px solid silver;
background-color: skyblue;
width: 30%;
}
.king div{
padding: 10px;
}
.one{
background-color: chartreuse;
}
.two{
background-color: cyan;
position: absolute;
top: 20px;
right: 40px;
}
.three{
background-color: darkred;
}
.four{
background-color: dimgrey;
}
</style>
<body>
<div class="king">
<div class="one">one</div>
<div class="two">two</div>
<div class="three">three</div>
<div class="four">four</div>
</div>
</body>
</html>

z-index

z-index 屬性設置元素的堆疊順序。擁有更高堆疊順序的元素總是會處于堆疊順序較低的元素的前面。

  • 元素可擁有負的 z-index 屬性值。

  • Z-index 僅能在定位元素上奏效(例如 position:absolute;)

屬性值: auto:默認,堆疊順序與父元素相等。 number:設置元素的堆疊順序。 inherit:規定應該從父元素繼承 z-index 屬性的值。

例: 設置圖像的 z-index:

img{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}

上述內容就是css的三種定位方式是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

css
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女