2017-12-18 271 views
1

enter image description here我想创建一个网页,有一个背景图片,然后就在我的导航栏下方有一种封面图像,坐在它下面。如何在网页上的背景图像顶部放置图像

我已经设法得到一个背景图片,但接下来的图片只是坐在页面的底部。

我试过看过几篇文章和关于如何做到这一点的教程,但也一直在发生。

代码片段演示:

* { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
header { 
 
    height: 100 vh; 
 
    background-size: cover; 
 
    background-position: center; 
 
} 
 

 
#wrapper { 
 
    width: auto; 
 
    margin: 0 auto; 
 
} 
 

 
#row, #logo { 
 
    border: 1px; 
 
    float: left; 
 
    min-height: 150px; 
 
    color: white; 
 
} 
 

 
#row { 
 
    float: left; 
 
    width: 70%; 
 
} 
 

 
.main-nav { 
 
    float: center; 
 
    list-style: none; 
 
    margin-top: 100px; 
 
    margin-left: 350px; 
 
} 
 

 
.main-nav li { 
 
    display: inline-block; 
 
} 
 

 
.main-nav li a { 
 
    color: white; 
 
    text-decoration: none; 
 
    padding: 5px 10px; 
 
    font-family: "Roboto", sans-serif; 
 
    font-size: 15px; 
 
} 
 

 
.main-nav li.active a { 
 
    border: 1px solid white; 
 
} 
 

 
.main-nav li a: hover { 
 
    border: 1px solid white; 
 
} 
 

 
#logo img { 
 
    width: 150px; 
 
    height: 145px; 
 
    float: left; 
 
    padding-left: 10%; 
 
} 
 

 
body { 
 
    /*background-image: linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)),url(hockey.jpg);*/ 
 
    background-size: cover; 
 
    font-family: sans-serif; 
 
} 
 

 
hr { 
 
    width: 100%; 
 
    height: 1px; 
 
    background: #fff; 
 
} 
 

 
.title { 
 
    position: relative; 
 
    width: 1200px; 
 
    margin-left: 0px; 
 
    margin-top: 0px; 
 
} 
 

 
.instagram img { 
 
    width: 20px; 
 
    float: left; 
 
    margin-top: 10px; 
 
    padding-left: 960px; 
 
} 
 

 
.twitter img { 
 
    width: 65px; 
 
    float: left; 
 
    margin-top: 10px; 
 
    padding-left: 30px; 
 
} 
 

 
body, html { 
 
    height: 100%; 
 
} 
 

 
.bg { 
 
    /* The image used */ 
 
    background-image: url(https://goravens.ca/wp-content/themes/goravens/images/events/womens-hockey-3.jpg); 
 

 
    /* Full height */ 
 
    height: 100%; 
 

 
    /* Center and scale the image nicely */ 
 
    background-position: center; 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
} 
 

 
/* 
 
h1 { 
 
    color:white; 
 
    font-size: 70px; 
 
    text-align: center; 
 
    margin-top: 275px; 
 
    padding-left:150px; 
 
} 
 
*/
<header> 
 
    <div id="wrapper"> 
 
    <div id="logo"> 
 
     <img src="logo.png"> 
 
    </div> 
 
    <div id="row"> 
 
     <ul class="main-nav"> 
 
     <li class="active"><a href="/Users/jenny/Desktop/SCHOOL/COMPUTER SCIENCE/HOME.html">HOME</a></li> 
 
     <li><a href="/Users/jenny/Desktop/SCHOOL/COMPUTER SCIENCE/PHOTOS.html">PHOTOS</a></li> 
 
     <li><a href="/Users/jenny/Desktop/SCHOOL/COMPUTER SCIENCE/PLAYERS.html">PLAYERS</a></li> 
 
     <li><a href="/Users/jenny/Desktop/SCHOOL/COMPUTER SCIENCE/NEWS.html">NEWS</a></li> 
 
     <li><a href="/Users/jenny/Desktop/SCHOOL/COMPUTER SCIENCE/SCHEDULE.html">SCHEDULE</a></li> 
 
     </ul> 
 
     <br> 
 
     <br> 
 
    </div> 
 
    <div class="block_1"></div> 
 
    <hr> 
 
    <ul class="instagram"> 
 
     <a href="https://www.instagram.com/embrunpanthers/"><img src="instagram.png"></a> 
 
    </ul> 
 
    <ul class="twitter"> 
 
     <a href="https://twitter.com/NCJHLEmbrun?lang=en"><img src="twitter.png"></a> 
 
    </ul> 
 
    </div> 
 
</header> 
 
<img src='https://cdn.hockeycanada.ca/hockey-canada/Team-Canada/Men/World-Cup/2016/sep_29_wch_team.jpg'>

+0

改变了,但同样的问题仍然发生 –

+0

为什么在'css'语法中在短划线之间使用'space'?像'min - height'?你也有许多错误的'html'代码。 – Pedram

+0

CSS中没有'-'选择器,但它的目的似乎是将'.main - nav'写为'.main-nav'(加入选择器并删除空格)。考虑通过以下方式改进此问题:** 1。**纠正此性质的语法错误,** 2。**将'css'样式和'html'标记组合成一个嵌入式代码片段(图中的<<>'图标文本编辑器与其他格式化选项,如** bold **和* italics *),** 3。**整理语法以提高可读性(这可以在上述代码片段中轻松完成) – UncaughtTypeError

回答

0

我想你应该摆脱花车。他们只是让页面的推理更加困难,至少对我来说。 我使用了CSS的flexbox功能来定位标题中的元素。你可以阅读更多关于此这里:https://css-tricks.com/snippets/css/a-guide-to-flexbox/enter link description here

我仍然不知道,如果你想要的头在顶部,然后消失,当你向下滚动或者说,如果你想让它浮在内容和留即使向下滚动,也会显示在顶部。因此,我在CSS中留下了一条评论 - 取消注释以查看其他选项。

HTML:

<body> 
    <div class="bg"> 
    <header> 
     <div class="logoContainer"> 
     <img src="http://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg"> 
     </div> 
     <ul class="main-nav"> 
     <li class="active"><a href="#">HOME</a></li> 
     <li><a href="#">PHOTOS</a></li> 
     <li><a href="#">PLAYERS</a></li> 
     <li><a href="#">NEWS</a></li> 
     <li><a href="#">SCHEDULE</a></li> 
     </ul> 
    </header> 
    <div class="socialLinksContainer"> 
     <div class="instagram"> 
     <a href="#"><img src="https://static.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg"></a> 
     </div> 
     <div class="twitter"> 
     <a href="#"><img src="https://www.petmd.com/sites/default/files/petmd-cat-happy-13.jpg"></a> 
     </div> 
    </div> 
    Main content goes here 
    </div> 
</body> 

CSS:

header { 
    height: 10vh; 
    background-color: rgba(255, 255, 255, 0.4); 
    display: flex; 
    flex-direction: row; 
    justify-content: space-between; 
    width: 100%; 
    //uncomment this for the header to float over the content 
    //position: fixed; 
    //top: 10px; 
} 

.logoContainer { 
    height: 10vh; 
} 

.logoContainer > img { 
    max-width: 100%; 
    max-height: 100%; 
} 

.main-nav { 
    list-style: none; 
    margin-top: 100 px; 
    margin-left: 350 px; 
} 

.main-nav li { 
    display: inline-block; 
} 

.main-nav li a { 
    color: white; 
    text-decoration: none; 
    padding: 5 px 10 px; 
    font-family: "Roboto", sans - serif; 
    font-size: 15 px; 
} 

.main-nav li.active a { 
    border: 1 px solid white; 
} 

.main-nav li a: hover { 
    border: 1 px solid white; 
} 

# logo img { 
    width: 150 px; 
    height: 145 px; 
    padding-left: 10 %; 
} 

body { 
    font-family: sans-serif; 
    color: red; 
} 

hr { 
    width: 100 %; 
    height: 1 px; 
    background: #fff 
} 

.socialLinksContainer { 
    position: fixed; 
    right: 5px; 
    bottom: 5px; 
    display: flex; 
    flex-direction: row; 
} 

.title { 
    position: relative; 
    width: 1200 px; 
    margin-left: 0 px; 
    margin-top: 0 px; 
} 

.instagram > a > img { 
    max-width: 30px; 
    max-height: 30px; 
    margin-top: 10 px; 
    padding-left: 960 px; 
} 

.twitter img { 
    max-width: 30px; 
    max-height: 30px; 
    margin-top: 10 px; 
    padding-left: 30 px; 
} 

body, 
html { 
    height: 100 %; 
} 

body { 
    background-image: url('https://cdn.hockeycanada.ca/hockey-canada/Team-Canada/Men/World-Cup/2016/sep_29_wch_team.jpg'); 
    background-repeat: no-repeat; 
    background-size: cover; 
} 

.imageUnderTheNavbar { 
    max-height: 50px; 
} 

请看看这个小提琴:https://jsfiddle.net/5ufpomy4/4/

这东西,你脑子里想的?

+0

背景图像是我想要的,但是我试图让另一个图像出现在页面主体内的导航栏下面,如果这样做有道理的话。 –

+0

我通过在标题下添加图像更新了提琴。如果这不是你想要的,那么我不知道。也许你想让导航栏漂浮在页面的内容上?请再次解释或画一个你想要达到的简单图片。 –

+0

是的,只是在中心位置,并且与导航栏的长度相同,就像封面图片一样。并且让导航链接向右浮动,这样徽标就可以在左侧独立显示,如果这样更有意义的话? –