标头和div出现在同一行

时间:2019-01-12 11:45:34

标签: html css

所以我的标题设置了背景图像,我想在其下创建一个div,以便可以在其中写文本,但是当我创建新的div时,我在其中写入的文本会显示在已经存在的顶部创建的标题。我认为这是由于我设置了背景图片。这是我自己完成的第一个项目,因此,如果你们能帮助我,我将不胜感激。

这些是HTML和CSS文件:

body {
	margin: 0;
	padding: 0;
	background-color:#f4f4f4;
	float: left;
}

header{
	overflow: hidden;
	margin: auto;
}

nav {
	width: 100%;
	height: 50px;
	background-color: rgba(192,192,192,0.3);
	margin: auto;
}


.wtf {
	padding-right: 50px;
	position: relative;
	bottom: 3px;
}

#logo {
	float: left;
	color: white;
	font-family: 'Concert One', cursive;
	padding-left: 20px;
}


p.highlight {
	margin: 0;
	position: relative;
	bottom: 7px;
}


ul {
	float:right;
}

li {
	display: inline;
	padding: 0 10px 0 10px;
}

a {
	text-decoration: none;
	color: white;
	font-size: 21px;
	margin: 0 0 3px 0;
	font-family: 'Montserrat', sans-serif;
}

li a:hover {
  background-color: #ddd;
  color: black;
}

.image {
	background-image: url('nebula-stars-universe-galaxy-space-4k-kx-1920x1080.jpg');
}

.main {
	width: 100%;
	height: 400px;
	color: white;
	text-align: justify;
	position: absolute;
	font-family: 'Montserrat', sans-serif;
	background-image: url('nebula-stars-universe-galaxy-space-4k-kx-1920x1080.jpg')
}

.main p,h1,h3 {
	text-align: center;
	position: relative;
}

.main h1 {
	top: 100px;
	font-size: 50px;
}

.main h3 {
	top: 120px;
}

.main p {
	top: 90px;
	font-size: 20px;
}

.development {
	position: relative;
}

.development p {
	color: red;
}
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Упражнение</title>
	<link rel="stylesheet" 
	<link rel="stylesheet" type="text/css" href="style.css">
	<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css?family=Concert+One" rel="stylesheet">
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/solid.css" integrity="sha384-+0VIRx+yz1WBc


</head>
<body>

<header>
	<div class="image">
     <nav>
     	<div id="logo">
     	<h1><p class="highlight">ATANAS DEVELOPING</p></h1>
    	</div>
    	
    	<div class="wtf">
    	<ul> 
    		<li class="current">
    			<a href="#">Home</a>
    		</li>
    		<li>
    			<a href="#">Services</a>
    		</li>
    		<li>
    			<a href="#">Clients</a>
    		</li>
    		<li>
				<a href="#">Team</a>
    		</li>
    		<li>
    			<a href="#">Contact</a>
    		</li>
    	</ul>
    	</div>
    </nav>
	
	<div class="main">
		<p>Can you build the website of my dreams?</p>
		<h1>YUP, WE CAN DO THAT.</h1>
		<h3>Learn More</h3>
	</div>
	</div>
</header>
	
	<br>
	<div class="development">
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aliquam lacus quis ex malesuada fermentum. Sed fringilla porttitor massa sit amet sollicitudin.</p>
	</div>


</body>
</html>

3 个答案:

答案 0 :(得分:0)

您绝对定位了.main

.main {
    position: absolute;
}

使用这种样式,您可以从常规文档流中删除.main,并将其放在左上角。其他元素保持规则流动并在其上方。摆脱这种定位,您就可以开始了。

body {
	margin: 0;
	padding: 0;
	background-color:#f4f4f4;
	float: left;
}

header{
	overflow: hidden;
	margin: auto;
}

nav {
	width: 100%;
	height: 50px;
	background-color: rgba(192,192,192,0.3);
	margin: auto;
}


.wtf {
	padding-right: 50px;
	position: relative;
	bottom: 3px;
}

#logo {
	float: left;
	color: white;
	font-family: 'Concert One', cursive;
	padding-left: 20px;
}


p.highlight {
	margin: 0;
	position: relative;
	bottom: 7px;
}


ul {
	float:right;
}

li {
	display: inline;
	padding: 0 10px 0 10px;
}

a {
	text-decoration: none;
	color: white;
	font-size: 21px;
	margin: 0 0 3px 0;
	font-family: 'Montserrat', sans-serif;
}

li a:hover {
  background-color: #ddd;
  color: black;
}

.image {
	background-image: url('https://picsum.photos/200/300?image=15');
}

.main {
	width: 100%;
	height: 400px;
	color: white;
	text-align: justify;
	/* position: absolute; <-- do not position this absolutely */
	font-family: 'Montserrat', sans-serif;
	background-image: url('https://picsum.photos/1920/1080?image=10')
}

.main p,h1,h3 {
	text-align: center;
	position: relative;
}

.main h1 {
	top: 100px;
	font-size: 50px;
}

.main h3 {
	top: 120px;
}

.main p {
	top: 90px;
	font-size: 20px;
}

.development {
	position: relative;
}

.development p {
	color: red;
}
	<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css?family=Concert+One" rel="stylesheet">
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/solid.css" >


	<div class="image">
     <nav>
     	<div id="logo">
     	<h1><p class="highlight">ATANAS DEVELOPING</p></h1>
    	</div>
    	
    	<div class="wtf">
    	<ul> 
    		<li class="current">
    			<a href="#">Home</a>
    		</li>
    		<li>
    			<a href="#">Services</a>
    		</li>
    		<li>
    			<a href="#">Clients</a>
    		</li>
    		<li>
				<a href="#">Team</a>
    		</li>
    		<li>
    			<a href="#">Contact</a>
    		</li>
    	</ul>
    	</div>
    </nav>
	
	<div class="main">
		<p>Can you build the website of my dreams?</p>
		<h1>YUP, WE CAN DO THAT.</h1>
		<h3>Learn More</h3>
	</div>
  
	</div>

	
	<div class="development">
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aliquam lacus quis ex malesuada fermentum. Sed fringilla porttitor massa sit amet sollicitudin.</p>
	</div>

答案 1 :(得分:0)

您的.main css类具有绝对位置。禁用即可解决问题

答案 2 :(得分:0)

从CSS中删除一些position属性。

.development {
    // remove
    //position: relative;
}

.main {
    width: 100%;
    height: 400px;
    color: white;
    text-align: justify;
   // remove 
   // position: absolute;
    font-family: 'Montserrat', sans-serif;
    background-image: url(nebula-stars-universe-galaxy-space-4k-kx-1920x1080.jpg);
}

可以切换到上方,但是您的body会向左浮动,因此也应将其删除。

body {
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
   // remove
   // float: left;
}