首页 > 其他分享 >css29 CSS Layout - The z-index Property

css29 CSS Layout - The z-index Property

时间:2024-06-01 11:47:25浏览次数:12  
标签:box index elements top element css29 position Property

https://www.w3schools.com/css/css_z-index.asp

 

CSS Layout - The z-index Property

   

The z-index property specifies the stack order of an element.


The z-index Property

When elements are positioned, they can overlap other elements.

The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).

An element can have a positive or negative stack order:

 

 

Example

img {
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: -1;
}
<!DOCTYPE html>
<html>
<head>
<style>
img {
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: -1;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<img src="img_tree.png">
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>

</body>
</html>

 

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display: flex elements).

 

Another z-index Example

Example

Here we see that an element with greater stack order is always above an element with a lower stack order:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
}

.black-box {
  position: relative;
  z-index: 1;
  border: 2px solid black;
  height: 100px;
  margin: 30px;
}

.gray-box {
  position: absolute;
  z-index: 3; /* gray box will be above both green and black box */
  background: lightgray;
  height: 60px;  
  width: 70%;
  left: 50px;
  top: 50px;
}

.green-box {
  position: absolute;
  z-index: 2; /* green box will be above black box */
  background: lightgreen;
  width: 35%;
  left: 270px;
  top: -15px;
  height: 100px;
}
</style>
</head>
<body>

<h1>Z-index Example</h1>

<p>An element with greater stack order is always above an element with a lower stack order.</p>

<div class="container">
  <div class="black-box">Black box (z-index: 1)</div>
  <div class="gray-box">Gray box (z-index: 3)</div>
  <div class="green-box">Green box (z-index: 2)</div>
</div>

</body>
</html>

 



Without z-index

If two positioned elements overlap each other without a z-index specified, the element defined last in the HTML code will be shown on top.

Example

Same example as above, but here with no z-index specified:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
}

.black-box {
  position: relative;
  border: 2px solid black;
  height: 100px;
  margin: 30px;
}

.gray-box {
  position: absolute;
  background: lightgray;
  height: 60px;  
  width: 70%;
  left: 50px;
  top: 50px;
}

.green-box {
  position: absolute;
  background: lightgreen;
  width: 35%;
  left: 270px;
  top: -15px;
  height: 100px;
}
</style>
</head>
<body>

<h1>Overlapping elements</h1>

<p>If two positioned elements overlap each other without a z-index specified,
the element defined last in the HTML code will be shown on top:</p>

<div class="container">
  <div class="black-box">Black box</div>
  <div class="gray-box">Gray box</div>
  <div class="green-box">Green box</div>
</div>

</body>
</html>

 


 

CSS Property

PropertyDescription
z-index Sets the stack order of an element

标签:box,index,elements,top,element,css29,position,Property
From: https://www.cnblogs.com/emanlee/p/18225756

相关文章

  • css28 CSS Layout - The position Property
    https://www.w3schools.com/css/css_positioning.aspCSSLayout-ThepositionPropertyThepositionpropertyspecifiesthetypeofpositioningmethodusedforanelement(static,relative,fixed,absoluteorsticky).ThepositionPropertyThepositionpr......
  • css26 CSS Layout - The display Property
    https://www.w3schools.com/css/css_display_visibility.asp  CSSLayout-ThedisplayPropertyThedisplaypropertyisthemostimportantCSSpropertyforcontrollinglayout.ThedisplayPropertyThedisplaypropertyisusedtospecifyhowanelementi......
  • mysql中key 、primary key 、unique key 与index区别
    索引被用来快速找出在一个列上用一特定值的行。没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它找出相关的行。表越大,花费时间越多。如果表对于查询的列有一个索引,MySQL能快速到达一个位置去搜寻到数据文件的中间,没有必要考虑所有数据。如果一个表有1000行,这比......
  • 使用 LlamaIndex + Eleasticsearch ,进行 RAG 检索增强生成
    节前,我们星球组织了一场算法岗技术&面试讨论会,邀请了一些互联网大厂朋友、参加社招和校招面试的同学.针对算法岗技术趋势、大模型落地项目经验分享、新手如何入门算法岗、该如何准备、面试常考点分享等热门话题进行了深入的讨论。合集:《大模型面试宝典》(2024版)正式发......
  • height_scale = scales[2] IndexError: index 2 is out of bounds for axis 0 with si
    1.yolov5网络层优化在yolov5训练之前最好是改一下网络层,要不会报这个错。Traceback(mostrecentcalllast): File"convertCaffe.py",line159,in<module>   convertToCaffe(graph,prototxt_path,caffemodel_path,exis_focus=True,focus_concat_name="Concat_40",......
  • error Conflict: Multiple assets emit different content to the same filename ind
    ERRORFailedtocompilewith1error20:32:04errorConflict:Multipleassetsemitdifferentcontenttothesamefilenameindex.htmlERRORinConflict:Multipleassetsemitdif......
  • Qt 动画类(QPropertyAnimation)
    前言QPropertyAnimation是QT中的一个动画类,用于对目标对象的属性进行动画效果展示。该类继承自QAbstractAnimation类,使用起来非常方便和灵活。一、QPropertyAnimation类介绍QPropertyAnimation可以对任何QObject的子类的属性进行动画的展示,只要该属性是可写的,即存在set方......
  • 自定义CSS属性(@property)解决自定义CSS变量无法实现过渡效果的问题
    且看下面的代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>demot</t......
  • AI菜鸟向前飞 — LangChain系列之十二 - RAG(下篇):Index和Retriever
    AI菜鸟向前飞—LangChain系列之十-RAG(上篇)AI菜鸟向前飞—LangChain系列之十一-RAG(中篇)先分享个问题的解法#在使用Chroma实例化过程中,可能会出现如下报错AttributeError:typeobject'hnswlib.Index'hasnoattribute'file_handle_count'当使用代码遇到如上问......
  • lua 元表__index 元方法应用1
    1--普通表2localtab={1,2,3}34--元表5localmeta={6insert=function(t,v)7--监测表增加成员8print("增加一个值",v)9table.insert(t,v)10end,11remove=function(t,i)12--监测表删除成......