2017-12-18 141 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

是的,只是在中心位置,並且與導航欄的長度相同,就像封面圖片一樣。並且讓導航鏈接向右浮動,這樣徽標就可以在左側獨立顯示,如果這樣更有意義的話? –