2017-08-08 59 views
2

我试图让我的博客,但我坚持这个问题。我试图在顶部使用繁星点点的设计,然后在它下面的一个小导航栏中获得标题,但是无论何时调整页面大小,标题图像都会在页面上出现,并且下面的导航栏会与其断开连接。如何将导航栏附加到标题,以便在调整大小,连接并且不分开时?这里是我的代码Navbar下面的标题响应问题

body { 
 
    background-color: #D3D3D3; 
 
    margin: 0 0 100px; 
 
    font-family: "PT-Sans", sans-serif; 
 
} 
 
.header { 
 
    position: relative; 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; 
 
    height: 25%; 
 
    background-image: url("./img/header-img.png"); 
 
    background-repeat: no-repeat; 
 
    background-size: 100%; 
 
} 
 
.topnav { 
 
    background-color: #333; 
 
    overflow: hidden; 
 
    position: relative; 
 
    top: 0; 
 
} 
 

 
.topnav a { 
 
    float: left; 
 
    display: block; 
 
    color: #f2f2f2; 
 
    text-align: center; 
 
    padding: 14px 16px; 
 
    text-decoration: none; 
 
    font-size: 17px; 
 
} 
 

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

 
.topnav a.active { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
} 
 
.item-right { 
 
}
<html> 
 
    <title>Political Blog</title> 
 
    <link rel="stylesheet" type="text/css" href="/blog/bootstrap.css"> 
 
    <link rel="stylesheet" type="text/css" href="/blog/style.css"> 
 
    <head> 
 
    
 
    </head> 
 
    <body> 
 
    <div class="header"> 
 
    
 
    </div> 
 
    <div class="topnav"> 
 
     <a href="/">Home</a> 
 
     <a href="./latest">Latest</a> 
 
     <a href="./all">All</a> 
 
     <a href="./about">About</a> 
 
    </div> 
 
    </body> 
 
<hr>

+0

任何特别的原因,你叫引导和不使用它?这将是最快捷的方式。 试着把任何东西放在标题中,它应该改变行为,因为某些原因空元素表现奇怪。 –

+0

您已将背景图像的高度设置为25%。什么是25%?用你提供的代码看不到头。 – Gerard

回答

0

她e为工作演示:https://jsfiddle.net/ma5sn8c0/

body { 
 
    background-color: #D3D3D3; 
 
    margin: 0 0 100px; 
 
    font-family: "PT-Sans", sans-serif; 
 
} 
 
.header { 
 
    display: block; 
 
    width: 100%; 
 
    height: 200px; 
 
    background-image: url("http://via.placeholder.com/900x150/42f4aa/ffffff"); 
 
    background-repeat: no-repeat; 
 
    background-size: 100%; 
 
    background-size:cover; 
 
    background-position: center center; 
 
} 
 

 
.topnav { 
 
    background-color: #333; 
 
    overflow: hidden; 
 
    position: relative; 
 
    top: 0; 
 
} 
 

 
.topnav a { 
 
    float: left; 
 
    display: block; 
 
    color: #f2f2f2; 
 
    text-align: center; 
 
    padding: 14px 16px; 
 
    text-decoration: none; 
 
    font-size: 17px; 
 
} 
 

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

 
.topnav a.active { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
} 
 
.item-right { 
 
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 

 
<div class="header"> 
 
</div> 
 
<div class="topnav"> 
 
    <a href="/">Home</a> 
 
    <a href="./latest">Latest</a> 
 
    <a href="./all">All</a> 
 
    <a href="./about">About</a> 
 
</div>

0

在使用这个标题CSS属性和CSS和位置使用它,而不是盖的100%。

background-size:cover; 
background-position:top center; 
0

Here is the working demo in code pen:

.header { 
    position: relative; 
    width: 100%; 
    min-height: 100px; 
    background-image:url("http://lorempixel.com/1200/100/sports"); 
    background-repeat: no-repeat; 
    background-size: cover; 
    background-color:#ff0000; 
} 
0

您需要使用
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
,使内部<head>标签响应页面

body { 
 
    background-color: #D3D3D3; 
 
    margin: 0 0 100px; 
 
    font-family: "PT-Sans", sans-serif; 
 
} 
 
.header { 
 
    position: relative; 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; 
 
    height: 25%; 
 
    background-image: url("./img/header-img.png"); 
 
    background-repeat: no-repeat; 
 
    background-size: 100%; 
 
} 
 
.topnav { 
 
    background-color: #333; 
 
    overflow: hidden; 
 
    position: relative; 
 
    top: 0; 
 
} 
 

 
.topnav a { 
 
    float: left; 
 
    display: block; 
 
    color: #f2f2f2; 
 
    text-align: center; 
 
    padding: 14px 16px; 
 
    text-decoration: none; 
 
    font-size: 17px; 
 
} 
 

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

 
.topnav a.active { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
} 
 
.item-right { 
 
}
<html> 
 
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
 
<title>Political Blog</title> 
 
<link rel="stylesheet" type="text/css" href="/blog/bootstrap.css"> 
 
<link rel="stylesheet" type="text/css" href="/blog/style.css"> 
 
<head> 
 

 
</head> 
 
<body> 
 
<div class="header"> 
 

 
</div> 
 
<div class="topnav"> 
 
    <a href="/">Home</a> 
 
    <a href="./latest">Latest</a> 
 
    <a href="./all">All</a> 
 
    <a href="./about">About</a> 
 
</div> 
 
</body> 
 
</html>