最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用CSS定位HTML元素的實(shí)現(xiàn)方法

  發(fā)布時(shí)間:2022-07-05 16:46:11   作者:allway2   我要評(píng)論
在頁面上定位內(nèi)容時(shí),可以使用一些屬性來幫助您操縱元素的位置,本文主要介紹了使用CSS定位HTML元素的實(shí)現(xiàn)方法,主要有五種類型,感興趣的可以了解一下

在頁面上定位內(nèi)容時(shí),可以使用一些屬性來幫助您操縱元素的位置。本文將向您展示一些使用 CSSposition 屬性包含不同定位元素類型的示例。要在元素上使用定位,您必須首先聲明它的 position property,它指定用于元素的定位方法的類型。使用 position 屬性值,使用 top、bottom、left 和 right 屬性定位元素。它們的工作方式也不同,具體取決于它們的位置值。

定位值有五種類型:

  • 靜止的
  • 相對(duì)的
  • 固定的
  • 絕對(duì)

靜止的

HTML元素默認(rèn)是靜態(tài)定位的,元素按照文檔的正常流程定位;靜態(tài)定位元素不受 top、bottom、left 和 right 屬性的影響。具有位置的元素:靜態(tài);沒有以任何特殊方式定位

用于將位置設(shè)置為靜態(tài)的 CSS 是:

position: static; 

接下來是使用靜態(tài)位置值的示例:

<!DOCTYPE html>
<html>
<head>
<style>
body {
  color: WHITE;
  font:  Helvetica;
  width: 420px;
}
.square-set,
.square {
  border-radius: 15px;
}
.square-set {
  background: darkgrey;
}
.square {
  position: static;
  background: Green;
  height: 70px;
  line-height: 40px;
  text-align: center;
  width: 90px;
}
</style>
</head>
<body>
<div class="square-set">
  <figure class="square square-1">SQUARE 1</figure>
  <figure class="square square-2">SQUARE 2</figure>
  <figure class="square square-3">SQUARE 3</figure>
  <figure class="square square-4">SQUARE 4</figure>
</div>
</body>
</html>

相對(duì)的

元素根據(jù)文檔的正常流程定位,相對(duì)于其正常位置進(jìn)行定位,然后 根據(jù)頂部、右側(cè)、底部和左側(cè)的值相 對(duì)于自身進(jìn)行偏移。偏移量不影響任何其他元素的位置;因此,頁面布局中為元素指定的空間與位置是靜態(tài)的相同。設(shè)置相對(duì)定位元素的 top、right、bottom 和 left 屬性將導(dǎo)致它被調(diào)整遠(yuǎn)離其正常位置。其他內(nèi)容不會(huì)被調(diào)整以適應(yīng)元素留下的任何間隙。

用于將位置設(shè)置為相對(duì)的 CSS 是:

position: relative; 

下面的示例使用相對(duì)位置值:

<!DOCTYPE html>
<html>
<head>
<style>
body {
  color: white;
  font:   Helvetica ;
  width: 420px;
}
.square-set,
.square {
  border-radius: 15px;
}
.square-set {
  background: darkgrey;
}
.square {
  background: green;
  height: 70px;
  line-height: 40px;
  position: relative; 
  text-align: center;
  width: 80px;
}
.square-1 {
    top: 15px;
  }
.square-2 {
  left: 50px;
}
.square-3 {
  bottom: -23px;
  right: 30px;
}
</style>
</head>
<body>
<div class="square-set">
  <figure class="square square-1">SQUARE 1</figure>
  <figure class="square square-2">SQUARE 2</figure>
  <figure class="square square-3">SQUARE 3</figure>
  <figure class="square square-4">SQUARE 4</figure>
</div>
</body>
</html>

絕對(duì)

該元素將從正常的文檔流中移除,并且在頁面布局中,不會(huì)為該元素創(chuàng)建空間。元素相對(duì)于最近的定位祖先(如果有的話)定位;否則,它相對(duì)于初始包含塊放置,其最終位置由頂部、右側(cè)、底部和左側(cè)的值確定。

用于將位置設(shè)置為絕對(duì)的 CSS 是:

position: absolute; 

一個(gè)元素position: absolute; 相對(duì)于最近定位的祖先進(jìn)行定位。如果絕對(duì)定位元素沒有定位的祖先,它使用文檔正文,并與頁面滾動(dòng)一起移動(dòng)。“定位”元素的位置不是static.

下一個(gè)例子強(qiáng)調(diào)元素的絕對(duì)位置:

<!DOCTYPE html>
<html>
<head>
<style>
.square-set {
  color: WHITE;
  background: darkgrey;
  height: 200px;
  position: relative;
  border-radius: 15px;
  font:  Helvetica ;
  width: 420px;
}
.square {
  background: green;
  height: 80px;
  position: absolute;
  width: 80px;
  border-radius: 15px;
  line-height: 60px;
}
.square-1 {
  top: 10%;
  left: 6%;
}
.square-2 {
  top: 5;
  right: -20px;
}
.square-3 {
  bottom: -15px;
  right: 40px;
}
.square-4 {
  bottom: 0;
}
</style>
</head>
<body>
<div class="square-set">
  <figure class="square square-1">SQUARE 1</figure>
  <figure class="square square-2">SQUARE 2</figure>
  <figure class="square square-3">SQUARE 3</figure>
  <figure class="square square-4">SQUARE 4</figure>
</div>
</body>
</html>

固定的

從正常文檔流中刪除的元素,并且在頁面布局中,沒有為元素創(chuàng)建空間。元素相對(duì)于由視口建立的初始包含塊定位,其最終位置由值 top、right、bottom 和 left 確定。此值始終創(chuàng)建一個(gè)新的堆疊上下文。

用于將位置設(shè)置為固定的 CSS 如下所示:

position: fixed; 

元素position: fixed; 相對(duì)于視口定位,這意味著即使頁面滾動(dòng),它也始終保持在同一位置。top、right、bottom 和 left 屬性用于定位元素。

元素按照文檔的正常流程定位,然后根據(jù)top、right、bottom和left的值,相對(duì)于其最接近的升序塊級(jí)進(jìn)行偏移,包括與表格相關(guān)的元素。偏移量不會(huì)影響任何其他元素的位置。

此值始終創(chuàng)建一個(gè)新的堆疊上下文。請注意,粘性元素“粘”到其最近的具有“滾動(dòng)機(jī)制”的祖先,即使該祖先不是最近的實(shí)際滾動(dòng)祖先。

用于將位置設(shè)置為粘性的 CSS 是:

position: sticky;

元素position: sticky; 的定位基于用戶的滾動(dòng)位置,relative 并fixed 根據(jù)滾動(dòng)位置在位置之間切換。

重疊元素

網(wǎng)頁上的重疊元素對(duì)于突出、宣傳和關(guān)注我們網(wǎng)頁上的重要內(nèi)容非常有用。使元素覆蓋在您的網(wǎng)站上是非常有用且非常有價(jià)值的功能設(shè)計(jì)實(shí)踐。當(dāng)元素被定位時(shí),它們可以與其他元素重疊,所以要指定順序(什么元素應(yīng)該放在其他元素的前面或后面),我們應(yīng)該使用 z-index 屬性。具有較大堆棧順序的元素始終位于具有較低堆棧順序的元素之前。請注意,z-index 屬性僅適用于定位元素(位置:絕對(duì)、位置:相對(duì)或位置:固定)。

下一個(gè)示例顯示 z-index 屬性如何在不同的正方形上工作:

<!DOCTYPE html>
<html>
<head>
<style>
.square-set {
  color: white;
  background: purple;
  height: 170px;
  position: relative;
  border-radius: 15px;
  font:  Helvetica;
  width: 400px;
}
.square {
  background: orange;
  border: 4px solid goldenrod;
  position: absolute;
  border-radius: 15px;
  height: 80px;
  width: 80px;
}
.square-1 {
  position: relative;
  z-index: 1;
  border: dashed;
  height: 8em;
  margin-bottom: 1em;
  margin-top: 2em;
}
.square-2 {
  position: absolute;
  z-index: 2; 
  background: black;
  width: 65%;
  left: 60px;
  top: 3em;
}
.square-3 {
  position: absolute;
  z-index: 3; 
  background: lightgreen;
  width: 20%;
  left: 65%;
  top: -25px;
  height: 7em;
  opacity: 0.9;
}
 
</style>
</head>
<body>
<div class="square-set">
  <figure class="square square-1">SQUARE 1</figure>
  <figure class="square square-2">SQUARE 2</figure>
  <figure class="square square-3">SQUARE 3</figure>
</div>
</body>
</html>

在圖像上定位文本

下面的示例使用上述 CSS 定位值在圖像上覆蓋一些文本:

<!DOCTYPE html>
<html>
<head>
<style>
.module{
  background: 
    linear-gradient{
      rgba(0, 4, 5, 0.6),
      rgba(2, 0, 3, 0.6)
    ),
    url(http://www.holtz.org/Library/Images/Slideshows/Gallery/Landscapes/43098-DSC_64xx_landscape-keltern-1_wall.jpg);
  background-size: cover;
  width: 600px;
  height: 400px;
  margin: 10px 0 0 10px;
  position: relative;
  float: left;
}
.mid h3 {
  font-family: Helvetica;
  font-weight: 900;
  color: white;
  text-transform: uppercase;
  margin: 0;
  position: absolute;
  top: 30%;
  left: 50%;
  font-size: 3rem;
  transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="module mid">
  <h3>Wild nature</h3>
</div>
</body>
</html>

結(jié)論

在本文中,我們描述并給出了 CSS 定位類型的示例,并描述了如何重疊元素并在圖像上添加一些文本。

到此這篇關(guān)于使用CSS定位HTML元素的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)CSS定位HTML元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

海林市| 达尔| 库尔勒市| 靖边县| 彭泽县| 安远县| 北宁市| 清徐县| 河间市| 肃北| 盐亭县| 德化县| 清水河县| 台北市| 炎陵县| 漳平市| 寻甸| 潍坊市| 河北区| 海丰县| 志丹县| 长沙县| 文山县| 化德县| 大田县| 神木县| 紫云| 抚顺县| 天台县| 建始县| 武山县| 焦作市| 措美县| 石城县| 工布江达县| 合山市| 罗甸县| 太仆寺旗| 南江县| 中阳县| 平顶山市|