float-css与div图像

时间:2017-09-13 05:09:36

标签: html css html5 css-float clearfix

当屏幕到达40rem时,我的徽标和导航分别向左和向右浮动。但是它与包含背景图像的下一个div重叠。我尝试设置边距,但只是移动了徽标/导航和页面上较低的图像。

@import url('https://fonts.googleapis.com/css?family=Raleway:300,400,700,900');
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Raleway', sans-serif;
  text-align: center;
}

img {
  max-width: 100%;
  height: auto;
  width: auto;
}

.container {
  width: 95%;
  max-width: 70em;
  margin: 0 auto;
}

.clearfix::after,
section::after {
  content: '';
  display: block;
  clear: both;
}


/* typography
    ================= */

.unstyled-list {
  margin: 0;
  padding: 0;
  list-style-type: none;
}


/* header
    ================= */

header {
  position: relative;
  left: 0;
  right: 0;
  margin: 1em;
}

.logo {
  width: 200px;
  height: 40px;
}

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

nav li {
  display: inline-block;
  margin: 1em;
  padding: 0.125em;
}

nav a {
  font-weight: 900;
  text-decoration: none;
  text-transform: uppercase;
  font-size: .8rem;
  padding: .75em;
  color: #DDD;
}


/* home-main
    ================= */

.home-main {
  background-image: url('../manjitcss/img/manjit-main.png');
  background-size: cover;
  background-position: center;
  padding: 10em 0;
}

@media(min-width:40rem) {
  .logo {
    float: left;
    margin-top: .5em;
  }
  nav {
    float: right;
  }
}

@media(min-width:60rem) {
  .logo {
    width: 250px;
    height: 50px;
  }
  nav {
    margin-top: .5em;
  }
  nav a {
    font-size: 1.04em;
  }
}
<header>
  <img src="../manjitcss/img/logo.jpg" alt="Foo logo" class="logo">

  <nav>
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About Me</a></li>
      <li><a href="">Contact</a></li>
    </ul>
  </nav>
</header>

<section class="home-main">
  <div class="container">
  </div>
</section>

3 个答案:

答案 0 :(得分:2)

您的css中有.clearfix,但您尚未应用于<{1}} ,您的内容正在浮动。

将课程添加到header - 请参阅下面的演示:

&#13;
&#13;
header
&#13;
@import url('https://fonts.googleapis.com/css?family=Raleway:300,400,700,900');
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Raleway', sans-serif;
  text-align: center;
}

img {
  max-width: 100%;
  height: auto;
  width: auto;
}

.container {
  width: 95%;
  max-width: 70em;
  margin: 0 auto;
}

.clearfix::after,
section::after {
  content: '';
  display: block;
  clear: both;
}


/* typography
================= */

.unstyled-list {
  margin: 0;
  padding: 0;
  list-style-type: none;
}


/* header
================= */

header {
  position: relative;
  left: 0;
  right: 0;
  margin: 1em;
}

.logo {
  width: 200px;
  height: 40px;
}

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

nav li {
  display: inline-block;
  margin: 1em;
  padding: 0.125em;
}

nav a {
  font-weight: 900;
  text-decoration: none;
  text-transform: uppercase;
  font-size: .8rem;
  padding: .75em;
  color: #DDD;
}


/* home-main
================= */

.home-main {
  background-image: url('../manjitcss/img/manjit-main.png');
  background-size: cover;
  background-position: center;
  padding: 10em 0;
}

@media(min-width:40rem) {
  .logo {
    float: left;
    margin-top: .5em;
  }
  nav {
    float: right;
  }
}

@media(min-width:60rem) {
  .logo {
    width: 250px;
    height: 50px;
  }
  nav {
    margin-top: .5em;
  }
  nav a {
    font-size: 1.04em;
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

当您使用float尝试在父级上使用overflow属性时。当浮动离开它的父母。或者您也可以使用clearfix

答案 2 :(得分:0)

这是浮动的问题。您可以使用clearfix或将float应用于.home-main,如下所示

@media(min-width:40rem) {
      .logo {
        float: left;
        margin-top: .5em;
      }
      nav {
        float: right;
      }
     .home-main{
        width: 100%;
        float: left;
      }
    }