【问题标题】:why css code not working problem in float?为什么 css 代码在 float 中不起作用?
【发布时间】:2019-10-06 12:54:20
【问题描述】:

我用 CSS 和 HTML 编写了一些代码,但 float 不起作用。我想要网站名称和徽标位于左侧,菜单或导航位于导航栏右侧。

https://codepen.io/asfafasa/pen/NWWWzgd html:

<body>
<div class="page-wrapper">
    <header>    
    <div class="banner-home">
      <h1>
            <div class="homepage"> <a href="index.html" title="CrazyPirates" target="new">
            <span class="crazy"><img src="images/Logomakr_5MZbkZ.png" alt="" width="30px" class="logo">Sitename & logo </span>
            </a>
            </div>
</h1>
    <div id="open">
            <a href="#" onClick="openMenu()">
                <i class="fas fa-ice-cream"></i>
            </a>
      </div>
        <nav>
          <div class="menu">
            <div class="button">
              <ul>
                <!--<a href="#" onClick="closeMenu()">
                  <i class="fas fa-times">
                  </i>
                </a>-->
                <li>
              <div class="homePage_re rd_button">
                  <a href="index.html" class="navigation_home">
                      Home
                    </a></div>
                  </li>
                <li>
                  <div class="crazyGallary rd_button">
                      <a href="gallary.html">
                    Gallay
                    </a></div>
                  </li>
                <li>
                  <div class="aboutUs rd_button">
                      <a href="aboutUs.html">
                          About Us
                      </a></div>
                  </li>
              </ul>
              </nav>
            </div>
      </div>
    </div>
        </div>
</header>
    <div class="space"></div>

CSS:

*{
    margin: 0;
    padding: 0;
    outline: 0;
    box-sizing: border-box;
}

div{
    display: block;
}


body{
    margin: 0;
    font-size: 14px;
}

.space{
    height: 50px;
}

.page-wrapper{
    padding: 0 50px;
}
header{
    background: darkgrey;
    height: 120px;
    color: azure;
    z-index: 1;
    margin: 0 auto;
    padding: 5px 0;
    text-align: center;
    display: block;
}

header *{
    display: inline-block;
}

.banner-home{
    align-items: center;
    overflow: auto;
}

.homepage{
    padding-left: 20px;
    float: left;
}

.menu{
    display: inline-block;
    float: right;
}


.rd_button a{
    text-decoration: none;
    display: block;
}

.rd_button{
    list-style-type: none;
    display: block;
}

.button a{
    color: #fff;
  font-weight: 300;
  letter-spacing: 2px;
  text-transform: uppercase;
  padding: 0 0 0 50px;
  font-size: 14px;
  z-index: 1;
}

.button{
    float: right;
}

.button a:hover{
  background-color: tomato;
  color: #fff;
}

.button a:hover:after{
    transform: translateY(-50%) rotate(-35deg);
}


nav{
    margin: 0 auto;
    text-align: right;
    float: right;
}

.logo{
    display: inline;
    height: 100%;
}

.banner-home h1 a{
    text-decoration: none;
}

nav ul{
    list-style: none;
    padding: 0;
    margin: 0;
}

main{
    text-align: center;
}

菜单项应位于栏的右侧,而徽标和站点名称应位于左侧。

【问题讨论】:

    标签: html css css-float web-deployment


    【解决方案1】:

    两件事:

    1. .banner-home 应该有 float:left 属性
    2. .banner-home div 未正确关闭

    更新笔:https://codepen.io/david-somekh/pen/yLLLEEK

    <body>
    <style>
    *{
        margin: 0;
        padding: 0;
        outline: 0;
        box-sizing: border-box;
    }
    
    div{
        display: block;
    }
    
    
    body{
        margin: 0;
        font-size: 14px;
    }
    
    .space{
        height: 50px;
    }
    
    .page-wrapper{
        padding: 0 50px;
    }
    header{
        background: darkgrey;
        height: 120px;
        color: azure;
        z-index: 1;
        margin: 0 auto;
        padding: 5px 0;
        text-align: center;
        display: block;
    }
    
    header *{
        display: inline-block;
    }
    
    .banner-home{
    float:left;
        align-items: center;
        overflow: auto;
    }
    
    .homepage{
        padding-left: 20px;
        float: left;
    }
    
    .menu{
        display: inline-block;
        float: right;
    }
    
    
    .rd_button a{
        text-decoration: none;
        display: block;
    }
    
    .rd_button{
        list-style-type: none;
        display: block;
    }
    
    .button a{
        color: #fff;
      font-weight: 300;
      letter-spacing: 2px;
      text-transform: uppercase;
      padding: 0 0 0 50px;
      font-size: 14px;
      z-index: 1;
    }
    
    .button{
        float: right;
    }
    
    .button a:hover{
      background-color: tomato;
      color: #fff;
    }
    
    .button a:hover:after{
        transform: translateY(-50%) rotate(-35deg);
    }
    
    
    nav{
        margin: 0 auto;
        text-align: right;
        float: right;
    }
    
    .logo{
        display: inline;
        height: 100%;
    }
    
    .banner-home h1 a{
        text-decoration: none;
    }
    
    nav ul{
        list-style: none;
        padding: 0;
        margin: 0;
    }
    
    main{
        text-align: center;
    }
    
    </style>
    <div class="page-wrapper">
        <header>    
        <div class="banner-home">
          <h1>
                <div class="homepage"> <a href="index.html" title="CrazyPirates" target="new">
                <span class="crazy"><img src="images/Logomakr_5MZbkZ.png" alt="" width="30px" class="logo">Sitename & logo </span>
                </a>
                </div></div>
    </h1>
        <div id="open">
                <a href="#" onClick="openMenu()">
                    <i class="fas fa-ice-cream"></i>
                </a>
          </div>
            <nav>
              <div class="menu">
                <div class="button">
                  <ul>
                    <!--<a href="#" onClick="closeMenu()">
                      <i class="fas fa-times">
                      </i>
                    </a>-->
                    <li>
                  <div class="homePage_re rd_button">
                      <a href="index.html" class="navigation_home">
                          Home
                        </a></div>
                      </li>
                    <li>
                      <div class="crazyGallary rd_button">
                          <a href="gallary.html">
                        Gallay
                        </a></div>
                      </li>
                    <li>
                      <div class="aboutUs rd_button">
                          <a href="aboutUs.html">
                              About Us
                          </a></div>
                      </li>
                  </ul>
                  </nav>
                </div>
          </div>
        </div>
            </div>
    </header>
    <div class="space"></div>
    

    【讨论】:

    • 如果我的回答对你有用,如果你能批准将不胜感激。
    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2011-06-27
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多