html - 无法理解为什么第三个div在下面推菜单栏

标签 html css css-float

我对以下代码有问题,当我只有menulogo和phonelogo div时,它可以完美地工作,但是一旦我添加了第三个div(emaillogo),菜单栏就会被向下推,高度大约变成原来的两倍。我相信这是将这3个div推向下方的方法,但是尝试了我能想到的一切(在另一个div内包含3个徽标,然后将它们浮动,从该div中删除它们并使用相对和绝对定位将其浮动,但我无法获得他们正坐在菜单中。我正在使用媒体查询来做出此网站回复,但是您不需要花点时间为我服务,我会在知道导致问题的原因后尝试尝试,但是如果您愿意的话,也请多多关照。

的HTML

<html>

<head>

<meta charset="utf-8">

<title>Primeheat | Chichester Plumbing &amp; Heating Installation, 
  Maintenance &amp; Emergency Breakdown</title>

<meta name="viewport" content="width=device-width">

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

</head>

 <body>

        <nav>

            <div class="menucontainer">

    <div class="menulogo"> <img src="images/PrimeHeatLogo.jpg" alt="PrimeHeat 
 Logo" img class="menulogo"> </div>


        <div class="phonelogo"> <p> 01903 650 224</p> </div>

            <div class="emailogo"> <p> info@primeheatltd.co.uk</p> </div>

            </div>

    <label for="drop" class="toggle">Menu</label>
    <input type="checkbox" id="drop" />
        <ul class="menu">
            <li><a href="#">Home</a></li>
            <li>
                <!-- First Tier Drop Down -->
                <label for="drop-1" class="toggle">Plumbing</label>
                <a href="#">Plumbing</a>
                <input type="checkbox" id="drop-1"/>
                <ul>
                    <li><a href="#">Installation</a></li>
                    <li><a href="#">Service</a></li>
                    <li><a href="#">Breakdown</a></li>
                </ul> 

            </li>
            <li>

            <!-- First Tier Drop Down -->
            <label for="drop-2" class="toggle">Heating</label>
            <a href="#">Heating</a>
            <input type="checkbox" id="drop-2"/>
            <ul>
                <li><a href="#">Installation</a></li>
                <li><a href="#">Service</a></li>
                <li><a href="#">Breakdown</a></li>
                <li>


           </ul>
            </li>

            <li><a href="#">Contact</a></li>
            <li><a href="#">About</a></li>
        </ul>
    </nav>


 <div id="navbar2"> 

    <h1 class="h1">Welcome to Primeheat Plumbing & Heating</h1>

 </div>

 </body>

</html>


的CSS

@charset "utf-8";
/* CSS Document */

.menucontainer {max-width:50%;
            overflow:hidden;
            margin: 0px;

}


.menulogo {  max-width: 85px;
        height: auto;
        float: left}

.phonelogo {  max-width: 150px;
        padding-left: 50px;
        height: center;
        float: left;
        font-size: 17px;
 }

.emailogo {  max-width: 200px;
        font-size: 17px;
        float: right;
 }

 body {
 background: #212121;
 font-size:22px;
 line-height: 32px;
 color: #278189;
    margin: 0;
 padding: 0;
 word-wrap:break-word !important;
 font-family: 'Open Sans', sans-serif;
 }

h1 {
font-size: 60px;
text-align: center;
color: #278189;
}   

h3 {
font-size: 30px;
line-height: 34px;
text-align: center;
color: #278189;;
}

 h3 a {
color: #278189;
 }

a {
color: #278189;
}

h1 {
margin-top: 100px;
text-align:center;
font-size:60px;
line-height: 70px;
font-family: 'Bree Serif', 'serif';
}

 #container {
margin: 0 auto;
max-width: 890px;
}

p {
text-align: center;
}

.toggle,
[id^=drop] {
display: none;
}

/* Giving a background-color to the nav container. */
nav { 
margin:0;
padding: 0;
background-color: #FFFFFF;
overflow: auto;


}

#menulogo {
display: block;
padding: 0px;
float: left;
font-size:20px;
max-width: 5%;
height: auto;

}


 /* Since we'll have the "ul li" "float:left"
 * we need to add a clear after the container. */

nav:after {
content:"";
display:table;
clear:both;
}

/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
float: right;
padding:0;
margin:0;
list-style: none;
position: relative;
}

/* Positioning the navigation items inline */
nav ul li {
margin: 0px;
display:inline-block;
float: left;
background-color: #fff;
}

 /* Styling the links */
nav a {
display:block;
padding:14px 20px;  
color:#278189;
font-size:17px;
text-decoration:none;
}


nav ul li ul li:hover { background: #3A3A3A; }

/* Background color change on Hover */
nav a:hover { 
background-color: #3A3A3A; 
}

/* Hide Dropdowns by Default
 * and giving it a position of absolute */
nav ul ul {
display: none;
position: absolute; 
/* has to be the same number as the "line-height" of "nav a" */
top: 60px; 
}

/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
}

/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}

/* Second, Third and more Tiers 
* We move the 2nd and 3rd etc tier dropdowns to the left
 * by the amount of the width of the first tier.
*/
nav ul ul ul li {
position: relative;
top:-60px;
/* has to be the same number as the "width" of "nav ul ul li" */ 
left:170px; 
}


 /* Change ' +' in order to change the Dropdown symbol */
 li > a:after { content:  ' +'; }
 li > a:only-child:after { content: ''; }


 /* Media Queries
 --------------------------------------------- */

@media all and (max-width : 768px) {

#menulogo {
    display: block;
    padding: 0;
    width: 100%;
    text-align: center;
    float: none;
}

nav {
    margin: 0;
}

/* Hide the navigation menu by default */
/* Also hide the  */
.toggle + a,
.menu {
    display: none;
}

/* Stylinf the toggle lable */
.toggle {
    display: block;
    background-color: #FFF;
    padding:14px 20px;  
    color:#278189;
    font-size:17px;
    text-decoration:none;
    border:none;
}

.toggle:hover {
    background-color: #3A3A3A;
}

/* Display Dropdown when clicked on Parent Lable */
[id^=drop]:checked + ul {
    display: block;
}

/* Change menu item's width to 100% */
nav ul li {
    display: block;
    width: 100%;
    }

nav ul ul .toggle,
nav ul ul a {
    padding: 0 40px;
}

nav ul ul ul a {
    padding: 0 80px;
}

nav a:hover,
nav ul ul ul a {
    background-color: #3A3A3A;
}

nav ul li ul li .toggle,
nav ul ul a,
 nav ul ul ul a{
    padding:14px 20px;  
    color:#278189;
    font-size:17px; 
}


nav ul li ul li .toggle,
nav ul ul a {
    background-color: #C2C2C2; 
}

/* Hide Dropdowns by Default */
nav ul ul {
    float: none;
    position:static;
    color: #278189;
    /* has to be the same number as the "line-height" of "nav a" */
}

/* Hide menus on hover */
nav ul ul li:hover > ul,
nav ul li:hover > ul {
    display: none;
}

/* Fisrt Tier Dropdown */
nav ul ul li {
    display: block;
    width: 100%;
}

nav ul ul ul li {
    position: static;
    /* has to be the same number as the "width" of "nav ul ul li" */ 

}

}

 @media all and (max-width : 330px) {

nav ul li {
    display:block;
    width: 94%;
}

}

@media all and (max-width: 768px) {

.menulogo {max-width: 100%;
            height: auto;
            padding-bottom: 8px
} 

    .phonelogo {font-size: 30px;
        max-width: 100%;
            height: auto;
        position:relative; 
        display:inline;
        float:none;
        margin:0;
        padding:0;
} 


}


谢谢

最佳答案

这是一个有点题外话,但使用bootstrap之类的框架可以为您提供实现所需内容并使用引导程序类控制媒体查询的基本条件。

我使用引导程序重新编写了您的文件,并使用了一些utility classes列来处理标题徽标,电子邮件和电话。

不需要任何CSS即可进入此阶段。然后,您可以在此之上添加设计样式。

在这里查看小提琴https://jsfiddle.net/joshmoto/u4ko7mzs/

<header>
  <div class="container pt-3 pb-3">
    <div class="row">
      <div class="col-sm-2 text-center text-sm-left">
        <a href="#" class="pr-2 pl-2">Logo</a>
      </div>
      <div class="col-sm-6 text-center">
        <a href="#">01903 650 224</a>
      </div>
      <div class="col-sm-4 text-center text-sm-right">
        <a href="#">info@primeheatltd.co.uk</a>
      </div>
    </div>
  </div>

  <nav class="navbar navbar-expand-sm navbar-light bg-light">
  <div class="container">
    <a class="navbar-brand d-block d-sm-none" href="#">Menu</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
    <div class="collapse navbar-collapse" id="navbarText">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Plumbing</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Heating</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Contact</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
        </li>
      </ul>
    </div>
      </div>
  </nav>

</header>


Bootstrap非常棒,您只需要加载2个脚本和1个CSS文件,即可编写自己的CSS以覆盖所有引导样式。主要优点是您不必对浏览器兼容性威胁太大,因为引导程序已经可以解决这个问题。

关于html - 无法理解为什么第三个div在下面推菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51548477/

相关文章:

css - IE 9 CSS Float 问题!

html - 如何根据父级按比例调整子图像的大小?

php - 如何更改php中 'echo'的位置?

html - Bootstrap 4 img-responsive 一排

html - 拆分标题 CSS

html - header : Logo to the left, 向右导航。 (垂直居中对齐)

javascript - 在鼠标悬停时找到滚动区域...?

html - 页面顶部的垂直对齐不起作用

jquery 滚动条在下拉列表中不起作用

css - 清除内联 block 而不在它们之间使用空的 div.clear?