关于菜单的CSS定位

时间:2017-12-31 00:04:50

标签: html css css-position

如果你看下面提供的代码,有一些定位问题。主要是我需要这样做,幻灯片菜单从打开按钮下打开,不会将图像推开。此外,我需要打开按钮彼此相邻,但更多的是分散。我的定位不是很好,任何其他提示都会非常感激,谢谢!

html,
body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;
    overflow-x: hidden;
}

#Bar {
    position: relative;
    top: 0;
    left: 0;
    width: 100%;
    height: 64px;
    background-color: #592881
}

#logo {
    position: absolute;
    top: 30px;
    text-align: center;
    left: 0;
    right: 0;
    z-index: 1;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 100px;
    left: 0;
    background: rgba(89, 40, 129, .7);
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
    text-align: center;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: black;
    display: block;
    transition: 0.3s;
    position: static;
}

.sidenav a:hover {
    color: grey;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

@media screen and (max-height: 450px) {
    .sidenav {
        padding-top: 15px;
    }
    .sidenav a {
        font-size: 18px;
    }
}

.fade {
    -webkit-animation-name: fade;
    -webkit-animation-duration: 3s;
    animation-name: fade;
    animation-duration: 3s;
}

@-webkit-keyframes fade {
    from {
        opacity: .4
    }
    to {
        opacity: 1
    }
}

@keyframes fade {
    from {
        opacity: .4
    }
    to {
        opacity: 1
    }
}

.container {
    position: relative;
    text-align: center;
}

.slides {
    position: relative;
    height: 100%;
    vertical-align: middle;
    margin-left: auto;
    margin-right: auto;
}

.mySlides {
    display: block;
    left: 0;
    right: 0;
    max-width: 100%;
    padding-top: 140px;
    max-height: auto;
    vertical-align: middle;
    position: relative;
    margin-left: auto;
    margin-right: auto;
}

.prev,
.next {
    cursor: pointer;
    position: relative;
    width: auto;
    padding: 16px;
    margin-top: -22px;
    color: white;
    font-weight: bold;
    font-size: 18px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;
}

.next {
    float: right;
    border-radius: 3px 0 0 3px;
}

.prev {
    left: 0px;
    border-radius: 3px 0 0 3px;
}


.prev:hover,
.next:hover {
    background-color: rgba(0, 0, 0, 0.8);
}

.footer {
    position: relative;
    background-color: #F1F1F1;
    color: #A8A8A8;
    font-size: 18px;
    padding-left: 400px;
}

.overlay {
    float:left;
    height: 70%;
    width: 0;
    position: relative;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0, 0, 0);
    background-color: rgba(0, 0, 0, 0.9);
    overflow-x: hidden;
    transition: .5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
    padding-bottom: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover,
.overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 300px) {
    .overlay a {
        font-size: 20px
    }
    .overlay .closebtn {
        font-size: 40px;
        top: 15px;
        right: 35px;
    }
}
<!DOCTYPE html>
<html>

<head>
    <title>Masters of Engineering</title>
    <link rel="stylesheet" type="text/css" href="C:\Users\Zach\Desktop\Coding\Robotics Site\css\Main.css">
    <link rel="icon" href="img/Corner Logo.ico">
    <meta charset="utf-8">
</head>

<body>
    <div id="Bar"></div>
    <script>
    function openNav() {
        document.getElementById("myNav").style.width = "100%";
    }

    function closeNav() {
        document.getElementById("myNav").style.width = "0%";
    }
    </script>
    <span style="font-size:30px;cursor:pointer; display: inline-block;" onmouseenter="openNav()" onmouseleave="closeNav()">&#9776;open</span>
    <div id="myNav" class="overlay" onmouseleave="closeNav()" onmouseenter="openNav()">
        <div class="overlay-content">
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Clients</a>
            <a href="#">Contact</a>
        </div>
    </div>
    <span style="font-size:30px;cursor:pointer; display: inline-block;" onmouseenter="openNav()" onmouseleave="closeNav()">&#9776;open</span>
    <div id="myNav" class="overlay" onmouseleave="closeNav()" onmouseenter="openNav()">
        <div class="overlay-content">
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Clients</a>
            <a href="#">Contact</a>
        </div>
    </div>
    <div class='slideshow'>
        <div class="slides">
            <img class="mySlides fade" src="https://wallpaperbrowse.com/media/images/3848765-wallpaper-images-download.jpg" height="auto" width="auto">
            <img class="mySlides fade" src="https://avante.biz/wp-content/uploads/Wallpaper-Images/Wallpaper-Images-005.jpg" height=auto width=auto>
            <img class="mySlides fade" src='https://svs.gsfc.nasa.gov/vis/a010000/a012000/a012005/Swift_Tidal_Disruption_2_Still.jpg' height="auto" width="auto">
        </div>
        <div class='buttons'>
            <a class="prev" onclick="plusDivs(-1); 
            clearInterval(myTimer); 
            myTimer = setInterval(carousel, 3500);">&#10094;
            </a>
            <a class="next" onclick="plusDivs(1); 
            clearInterval(myTimer);
            myTimer = setInterval(carousel, 3500);">&#10095;
            </a>
        </div>
    </div>
    <script>
    var myIndex = 0;
    var myTimer = setInterval(carousel, 3500);
    carousel();

    function carousel() {
        var i;
        var x = document.getElementsByClassName("mySlides");
        for (i = 0; i < x.length; i++) {
            x[i].style.display = "none";
        }

        myIndex++;
        if (myIndex > x.length) { myIndex = 1 }
        x[myIndex - 1].style.display = "block ";
    }
    myTimer; //change image every 3.5 seconds
    </script>
    <script>
    var slideIndex = 1;
    showDivs(slideIndex);

    function plusDivs(n) {
        showDivs(slideIndex += n);
    }

    function showDivs(n) {
        var i;
        var x = document.getElementsByClassName("mySlides");
        if (n > x.length) { slideIndex = 1 }
        if (n < 1) { slideIndex = x.length }
        for (i = 0; i < x.length; i++) {
            x[i].style.display = "none";
        }
        x[slideIndex - 1].style.display = "block ";
    }
    </script>
    </div>
    <div class='footer'>
        <img src='http://www.mastersny.org/uploaded/template/default/images/common/bottom-logo.png'>
        <p>49 Clinton Avenue</p>
        <p>Dobbs Ferry, NY 10522</p>
        <p>(914) 479-6400</p>
    </div>
</body>

2 个答案:

答案 0 :(得分:0)

我创建了一个div容器,并将第二个跨度移动到绝对位置。添加了一些边距,在底部删除了一个额外的结束div,你需要在图像上添加一个alt。希望它有所帮助。

这是你的CSS。

<style type="text/css">
html, body {
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
overflow-x: hidden;
}

#Bar {
position: relative;
top: 0;
left: 0;
width: 100%;
height: 64px;
background-color: #592881
}

#logo {
position: absolute;
top: 30px;
text-align: center;
left: 0;
right: 0;
z-index: 1;
}

.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 100px;
left: 0;
background: rgba(89, 40, 129, .7);
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
text-align: center;
}

.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: black;
display: block;
transition: 0.3s;
position: static;
}

.sidenav a:hover {color: gray;}

.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {
font-size: 18px;
}}

.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 3s;
animation-name: fade;
animation-duration: 3s;
}

@-webkit-keyframes fade {
0% {-webkit-opacity: .4;
opacity: .4;
}
100% {-webkit-opacity: 1;
opacity: 1;
}}

@keyframes fade {
0%  {
-webkit-opacity: .4;
opacity: .4;
}
100%  {
-webkit-opacity: 1;
opacity: 1;
}}

.container {
position: relative;
text-align: center;
}

.slides {
position: relative;
height: 100%;
vertical-align: middle;
margin-left: auto;
margin-right: auto;
}

.mySlides {
display: block;
left: 0;
right: 0;
max-width: 100%;    
padding-top: 140px;
max-height: auto;
vertical-align: middle;
position: relative;
margin-left: auto;
margin-right: auto;
}

.prev, .next {
cursor: pointer;
position: relative;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}

.next {
float: right;
border-radius: 3px 0 0 3px;
}
.prev {
left: 0px;
border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8);}
.footer {
position: relative;
background-color: #F1F1F1;
color: #A8A8A8;
font-size: 18px;
padding-left: 400px;
}

.overlay {
float:left;
height: 70%;
width: 0;
position: relative;
z-index: 1;
top: 0;
left: 0;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.9);
overflow-x: hidden;
transition: .5s;
}
.overlay-content {
position: relative;
top: 25%;
width: 100%;
text-align: center;
margin-top: 30px;
padding-bottom: 30px;
}
.overlay a {
padding: 8px;
text-decoration: none;
font-size: 36px;
color: #818181;
display: block;
transition: 0.3s;
}
.overlay a:hover, .overlay a:focus {color: #f1f1f1;}
.overlay .closebtn {
position: absolute;
top: 20px;
right: 45px;
font-size: 60px;
}
@media screen and (max-height: 300px) {
.overlay a {font-size: 20px}
.overlay .closebtn {
font-size: 40px;
top: 15px;
right: 35px;
}}
.buttonContainer{
height:3em;
width:100%;
background-color:#ffffff;
}
</style>

这是HTML。

<!DOCTYPE html>
<html>
<head>
<title>Masters of Engineering</title>
<link rel="stylesheet" type="text/css" href="../../Zach/Desktop/Coding/Robotics%20Site/css/Main.css">
<link rel="icon" href="img/Corner Logo.ico">
<meta charset="utf-8">

<style>append your style here....</style>
<script>
function openNav() {   document.getElementById("myNav").style.width = "100%"; }
function closeNav() {document.getElementById("myNav").style.width = "0%";}
</script>
</head>
<body>
<div id="Bar"></div>
<div class="buttonContainer">  
<span style="font-size:30px;cursor:pointer; display: inline-block; position:absolute;" onmouseover="openNav();" onmouseout="closeNav();">&#9776;open</span>
<span style="margin-left:10%;font-size:30px;cursor:pointer; position:relative; display: inline-block;" onmouseover="openNav();" onmouseout="closeNav();">&#9776;open</span>
</div>
<div id="myNav" class="overlay" onmouseover="openNav();" onmouseout="closeNav();">
<div class="overlay-content">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a></div></div>
<div id="myNav" class="overlay" onmouseover="openNav();" onmouseout="closeNav();">
<div class="overlay-content">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a></div></div>
<div class='slideshow'>
<div class="slides">
<img class="mySlides fade" src="https://wallpaperbrowse.com/media/images/3848765-wallpaper-images-download.jpg" height="auto" width="auto">
<img class="mySlides fade" src="https://avante.biz/wp-content/uploads/Wallpaper-Images/Wallpaper-Images-005.jpg" height=auto width=auto>
<img class="mySlides fade" src='https://svs.gsfc.nasa.gov/vis/a010000/a012000/a012005/Swift_Tidal_Disruption_2_Still.jpg' height="auto" width="auto">
</div>
<div class='buttons'>
<a class="prev" onclick="plusDivs(-1); 
clearInterval(myTimer); 
myTimer = setInterval(carousel, 3500);">&#10094;
</a>
<a class="next" onclick="plusDivs(1); 
clearInterval(myTimer);
myTimer = setInterval(carousel, 3500);">&#10095;
</a>
</div>
</div>


<script>
var myIndex = 0;
var myTimer = setInterval(carousel, 3500);
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) { myIndex = 1 }
x[myIndex - 1].style.display = "block ";
}
myTimer; //change image every 3.5 seconds
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) { showDivs(slideIndex += n);}

function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
if (n > x.length) { slideIndex = 1 }
if (n < 1) { slideIndex = x.length }
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex - 1].style.display = "block ";
}
</script>
<div class='footer'>
<img src='http://www.mastersny.org/uploaded/template/default/images/common/bottom-logo.png'>
<p>49 Clinton Avenue</p>
<p>Dobbs Ferry, NY 10522</p>
<p>(914) 479-6400</p>
</div>
</body>

答案 1 :(得分:0)

您只需为幻灯片菜单设置position:absolute,为第一个菜单按钮设置margin-right:)

"localhost"
html,
body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;
    overflow-x: hidden;
}

#Bar {
    position: relative;
    top: 0;
    left: 0;
    width: 100%;
    height: 64px;
    background-color: #592881
}

#logo {
    position: absolute;
    top: 30px;
    text-align: center;
    left: 0;
    right: 0;
    z-index: 1;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 100px;
    left: 0;
    background: rgba(89, 40, 129, .7);
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
    text-align: center;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: black;
    display: block;
    transition: 0.3s;
    position: static;
}

.sidenav a:hover {
    color: grey;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

@media screen and (max-height: 450px) {
    .sidenav {
        padding-top: 15px;
    }
    .sidenav a {
        font-size: 18px;
    }
}

.fade {
    -webkit-animation-name: fade;
    -webkit-animation-duration: 3s;
    animation-name: fade;
    animation-duration: 3s;
}

@-webkit-keyframes fade {
    from {
        opacity: .4
    }
    to {
        opacity: 1
    }
}

@keyframes fade {
    from {
        opacity: .4
    }
    to {
        opacity: 1
    }
}

.container {
    position: relative;
    text-align: center;
}

.slides {
    position: relative;
    height: 100%;
    vertical-align: middle;
    margin-left: auto;
    margin-right: auto;
}

.mySlides {
    display: block;
    left: 0;
    right: 0;
    max-width: 100%;
    padding-top: 140px;
    max-height: auto;
    vertical-align: middle;
    position: relative;
    margin-left: auto;
    margin-right: auto;
}

.prev,
.next {
    cursor: pointer;
    position: relative;
    width: auto;
    padding: 16px;
    margin-top: -22px;
    color: white;
    font-weight: bold;
    font-size: 18px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;
}

.next {
    float: right;
    border-radius: 3px 0 0 3px;
}

.prev {
    left: 0px;
    border-radius: 3px 0 0 3px;
}


.prev:hover,
.next:hover {
    background-color: rgba(0, 0, 0, 0.8);
}

.footer {
    position: relative;
    background-color: #F1F1F1;
    color: #A8A8A8;
    font-size: 18px;
    padding-left: 400px;
}

.overlay {
position: absolute;
top: 115px;
    float:left;
    height: 70%;
    width: 0;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0, 0, 0);
    background-color: rgba(0, 0, 0, 0.9);
    overflow-x: hidden;
    transition: .5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
    padding-bottom: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover,
.overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 300px) {
    .overlay a {
        font-size: 20px
    }
    .overlay .closebtn {
        font-size: 40px;
        top: 15px;
        right: 35px;
    }
}