gpt4 book ai didi

html - 为什么静态定位父级的绝对定位子级占据视口(viewport)的整个宽度?

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:14 25 4
gpt4 key购买 nike

我一直在学习从 this w3schools example 制作基本的下拉菜单.据说我们可以通过将 min-width 设置为 100% 来改变下拉内容的宽度。它工作得很好。但是,如果我将 position: relative 更改为 position: static,则下拉内容最终会占用窗口的 100% 宽度。 AFAIK width: 100% 应该将宽度设置为父容器的 100%。将父容器的位置更改为静态不会改变它的宽度,因此子容器不应分布在父容器中。以下是我正在尝试的代码:

<!DOCTYPE html>
<html>
<head>
<style>
.dropdown {
position: static;
display: inline-block;
border: 2px solid red;
}

.dropdown-content {
display: none;
position: absolute;
background-color: grey;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
}

.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>

<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>

<div class="dropdown">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>

</body>
</html>

  • So, why is setting position-static for .dropdown class(or parent element in general) causing the .dropdown-content class elements(or child elements in general) to have full screen width rather than having same width as that of the parent container?

附录: 静态位置父项的绝对定位子项似乎占据了视口(viewport)的整个宽度。例如。以下代码:

<DOCTYPE html>
<html>
<head>
<style>
div#one{border: 2px dotted red; position: static; width:100px;}

div#two{border: 2px solid blue; width:100%; position: absolute;}
</style>
</head>
<body>
<div id="one">

Text of statically positioned box
<div id="two"> Text of absolutely positioned box.</div>

</div>

</body>
</html>

蓝色框分布在整个视口(viewport)中,位于具有红色边框的父容器之外。为什么会这样?

最佳答案

Static 是页面上任何元素的默认行为。因此,当您将 .dropdown-content 的位置设置为 absolute 时,它会根据最近的相对定位的父级设置其宽度,这在您的case 是视口(viewport),因为没有其他元素相对定位。

但是您将 .dropdown 定位为相对移动,将 .dropdown-content 定位为绝对移动,.dropdown-content(这是.dropdown) 的子级从最近的relatively positioned 父级 (.dropdown) 获取它的宽度,这就是为什么 .dropdown-content 在它的父级(.dropdown)静态定位时占据全宽。

.dropdown {
position: static;
display: inline-block;
border: 2px solid red;
}
.dropdownRelative {
position: relative;
display: inline-block;
border: 2px solid red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: grey;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
z-index: 2;
}
.parent{
position: relative;
margin-top:20px;
width: 400px;
border: 2px solid red;
}
.dropdown:hover .dropdown-content,.dropdownRelative:hover .dropdown-content,.parent:hover .dropdown-content {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<style>

</style>
</head>
<body>

<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>

<div class="dropdown">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>I am statically positioned and parent as static.</p>
</div>
</div>
<div class="dropdownRelative">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>I am absolutely positioned and my parent is relatively positioned</p>
</div>
</div>
<div class="parent">
<span>Hover over me</span>
<div class="dropdown-content">My parent is relatively positioned!</div>
</div>
</body>
</html>

关于html - 为什么静态定位父级的绝对定位子级占据视口(viewport)的整个宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com