gpt4 book ai didi

css - 如何将 html 元素定位在固定的 div 下

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

我试图在 position: fixed 下放置一些 html 元素(特别是 h1 和 p) div 而不必使用 <br>元素,因为如果调整顶部 div 的大小(高度),那么它将与 <h1> 重叠和 <p>元素。由于这类问题通常需要代码,这里是:

index.html 文件:

<!DOCTYPE html>
<html>
<head>

<title>Home | lobuo</title>

<link type="text/css" rel="stylesheet" href="stylesheet-main.css">

</head>
<body>

<div id="menuBack">

<ul id="menuBar">

<li class="menuItem"><a href="index.html">Home</a></li>
<li class="menuItem" class="subMenuHolder">

<a>Projects ▾</a>

<ul class="subMenu">

<li><a href="pages/minecraft.html">Minecraft projects</a></li>
<li><a href="pages/mods.html">Minecraft mods/plugins</a></li>
<li><a href="pages/webapps.html">Web apps</a></li>

</ul>

</li>

</ul>

<a href="https://github.com/lobuo">
<img class="socialIcon" src="img/Octocat.png" />
</a>
<a href="https://www.youtube.com/LobuoDev">
<img class="socialIcon" src="img/YouTube-icon-full_color.png" />
</a>

</div>

<br><br><br><br>

<div>
<h1>Welcome to my website!</h1>
<p>This is my website. I know how to code a little HTML and JavaScript. I do not have many projects here yet, but there will be some soon. The website is currently under construction, so don't be too dissapointed if a link doesnt work, or a page doesnt exist.</p>
<p>If you find something wrong with the website, you can report it as a bug <a>here.</a></p>
</div>
</body>
</html>

这是 stylesheet-main.css 文件:

body {
margin: 0px;
}

#menuBack {
height: auto;
width: 100%;
background-color: rgba(9, 52, 100, 0.92);
position: fixed;
}

.menuItem {
color: #e5822e;
display: inline-block;
font-size: 25px;
font-family: verdana, sans-serif;
padding-left: 15px;
padding-right: 15px;
}

.menuItem:hover {
color: #ab6122;
background-color: rgba(48, 95, 147, 0.92);
}

.socialIcon {
height: 30px;
display: inline-block;
vertical-align: middle;
float: right;
padding: 20px;
}

a {
text-decoration: none;
color: #f44d4d;
}

h1 {
font-family: sans-serif;
text-align: center;
}

p {
font-family: "andale mono", "courier new", courier, serif;
padding: 10px;
}

/* MenuBar dropdown menu came from here: http://www.onextrapixel.com/2011/06/03/how-to-create-a-horizontal-dropdown-menu-with-html-css-and-jquery/ */

#menuBar {
float: left;
}

#menuBar > li {
float: left;
}

#menuBar li a {
display: block;
height: 2em;
line-height: 2em;
padding: 0 1.5em;
text-decoration: none;
}

#menuBar ul {
position: absolute;
list-style: none;
left: 7.1em;
display: none;
}

#menuBar ul li a {
width: auto;
background-color: rgba(9, 52, 100, 0.92);
}

#menuBar ul li a:hover {
background-color: rgba(48, 95, 147, 0.92);
}

#menuBar li:hover ul {
display: block;
}

提前致谢:D

最佳答案

设置需要下推的divmargin-top

因此,将需要下推的所有内容包装在一个带有类名的 div 中。喜欢:

<div class="container">
//everything that needs to be pushed down goes here
</div>

然后在你的 css 中,你可以通过设置它的 margin-top 来将整个容器向下推

.container {
margin-top: 100px;
}

关于css - 如何将 html 元素定位在固定的 div 下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22741451/

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