float-css 与 div 图像重叠
float-css overlaps next to div image
当屏幕到达 40rem
时,我的徽标和导航分别向左和向右浮动。但是它与包含背景图像的下一个 div
重叠。我尝试设置页边距,但只是将 logo/nav 和图像移到页面下方。
@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>
当您使用 float
时,尝试对父级使用 overflow
属性。当 float 离开它的父级时。或者您也可以使用 clearfix
。
您的 css 中有 .clearfix
,但您还没有 将它们应用 header
谁的 你漂浮的内容。
将 class 添加到 header
- 请参阅下面的演示:
@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 class="clearfix">
<img src="http://placehold.it/50x50" 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">
container
</div>
</section>
这是浮动的问题。您可以使用 clearfix 或为 .home-main 应用浮动,如下所示
@media(min-width:40rem) {
.logo {
float: left;
margin-top: .5em;
}
nav {
float: right;
}
.home-main{
width: 100%;
float: left;
}
}
当屏幕到达 40rem
时,我的徽标和导航分别向左和向右浮动。但是它与包含背景图像的下一个 div
重叠。我尝试设置页边距,但只是将 logo/nav 和图像移到页面下方。
@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>
当您使用 float
时,尝试对父级使用 overflow
属性。当 float 离开它的父级时。或者您也可以使用 clearfix
。
您的 css 中有 .clearfix
,但您还没有 将它们应用 header
谁的 你漂浮的内容。
将 class 添加到 header
- 请参阅下面的演示:
@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 class="clearfix">
<img src="http://placehold.it/50x50" 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">
container
</div>
</section>
这是浮动的问题。您可以使用 clearfix 或为 .home-main 应用浮动,如下所示
@media(min-width:40rem) {
.logo {
float: left;
margin-top: .5em;
}
nav {
float: right;
}
.home-main{
width: 100%;
float: left;
}
}