gpt4 book ai didi

html - css 多元素高度 100%

转载 作者:行者123 更新时间:2023-11-28 03:35:31 25 4
gpt4 key购买 nike

肯定有十几个标题相似的帖子,但我发现没有一个帖子能够有效地完成我认为允许多个元素具有 100% 高度的简单事情。采取以下代码:

<html>
<head>
<style>
html, body, [role="main"] {height:100%; width:6in; overflow:hidden;}
[role="banner"] {position:fixed; top:0; left:0;}
.height {height:100%; width:100%}
</style>
</head>
<body>
<header role="banner">
<nav>
<a href="#1">Section one</a>
<a href="#2">Section two</a>
<a href="#3">Section three</a>
</nav>
</header>

<div role="main">

<section id="1" class="height">
</section>

<section id="2" class="height">
<header>
<h1>section title</h1>
</header>
<button>Navigate Articles</button>

<article class="height">
<h1>title</h1>
<p>paragraph</p>
</article>

<article class="height">
<h1>title</h1>
<p>paragraph</p>
</article>

</section>

<section id="3" class="height">
</section>

</div>

</body>
</html>

我希望无需滚动即可导航。单击将您带到某个部分的链接,该部分将填充 100% 的屏幕高度。当我使用上面的代码时,我没有得到我想要的效果。我在一个点上使用类“高度”的元素上的固定位置接近。它适用于第一部分,但下部分和第二部分中的文章会重叠。

最佳答案

仅使用 CSS 实现您所请求的功能是不切实际的。由于您指的是显示和隐藏内容,因此您可能需要实现少量 javascript 来绑定(bind)点击操作以在点击导航链接时隐藏/显示功能。

我将以下内容应用于您的代码:

jQuery:

//Hide all .height sections at first.
$('section.height').hide();

//Show them, when their respective link is clicked.
$('nav a').click(function() {
var $this = $(this);
section = $this.attr('href');

$(section).siblings('.height').hide();
$(section).show();
});

并更新了您的 CSS;

html, body, [role="main"] { 
height:100%;
overflow:hidden;
}
body {
position: relative; /*so .height is relative to body when absolutely positioned*/
}

[role="banner"] {
background: yellow;
position:fixed;
top:0;
left:0;
z-index: 999;
}

.height {
background: red;
height:100%;
width:100%;
position: absolute;
}
h1 {
margin-top: 30px; /* to prevent menu overlap. */
}

您可以在此处查看结果:http://jsfiddle.net/mUEYM/2/

基本前提是将 .height 元素设置为 position: absolute;。这将允许它们扩展到浏览器窗口的确切高度/宽度,前提是 htmlbody 也有 100% 的宽度和高度。

我将 z-index 值应用于导航,以确保它在显示时位于 .height 部分之上。

关于html - css 多元素高度 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636939/

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