2017-07-25 41 views
0

我想使用Jekyll設置一個github頁面站點。我的做法是通過相對URL將_site的內容上傳到回購和參考資產的根目錄。GitHub頁面 - 在CSS中的相對URL的問題

下面是相關的代碼的某些代碼段被調用的文件夾:

CSS:

@font-face { 
    font-family: supermario; 
    src: url('/dpd/font/fontfamily.ttf'); 
} 

.scenery { 
    position: absolute; 
    background:url('/img/image1.gif'); 
} 

.image2 { 
    position: absolute; 
    bottom: 0; 
    width: 100%; 
    height: 40px; 
    background-image:url('/img/image2.png'); 
} 

.icon2 { 
    background-image:url('/img/icon2.png'); 
    width: 30px; 
    height: 30px; 
    position: absolute; 
    bottom: $floorheight; 
    background-position-x: 350px; 
    z-index: 5; 
    transform: scaleX(-1); 
} 

HTML:

<link rel="stylesheet" href="build/css/styles.css"> 
<script src="dpd/jquery-3.2.1.min.js"></script> 

<script src="build/js/app.js"></script> 

我的HTML元素加載正確的URL結構。這是因爲如下:

myusername.github.io/repository-name/build/js/app.js 
myusername.github.io/repository-name/build/css/styles.css 

我的CSS URL不正確加載...他們看起來像:

myusername.github.io/img/icon1.png 
myusername.github.io/img/icon2.png 

等等,這是產生404。

編輯:我有幫助經營傑基爾一個gulptask - 它看起來像這樣:

//server + file watching 
gulp.task('serve', function() { 

    browserSync.init({ 
     server: "_site" 
    }); 

    gulp.watch("dev/scss/*.scss", ['styles', 'jekyll']); 
    gulp.watch('dev/js/*.js', ['scripts', 'jekyll']); 
    gulp.watch('_includes/*.html', ['jekyll']).on('change', browserSync.reload); 
    gulp.watch('_site').on('change', browserSync.reload); 

}); 


gulp.task('jekyll', function(gulpCallBack){ 
    var spawn = require('child_process').spawn; 
    // After build: cleanup HTML 
    var jekyll = spawn('jekyll', ['build'], {stdio: 'inherit'}); 

    jekyll.on('exit', function(code) { 
     gulpCallBack(code === 0 ? null : 'ERROR: Jekyll process exited with code: '+code); 
    }); 
}); 

我想,沒有補救解決的幾件事情:

  1. 更改爲相對路徑../
  2. 刪除斜槓所以它看起來像:background-image:url('/img/icon.png');
  3. 移動img文件夾的根目錄該項目的工程

是否有一個額外的步驟,我失蹤?有沒有人有任何建議如何更好地做到這一點?

+0

是在生成目錄中的img目錄? 在這種情況下,像這樣的相對網址應該是最好的選擇: background-image:url('../ img/image2.png'); –

+0

你可能與#2最接近。您實際上在CSS中使用絕對URL(當它們以斜槓開頭時)。它看起來像其他答案在這裏清理它。 –

+0

在CSS中使用相對URL時,URL是相對於CSS文件本身的。 (請參閱:https://stackoverflow.com/questions/940451/)。在你的例子中,如果你只刪除了開始的斜槓,那麼'img/icon.png'會引用'repo-name/build/css/img/icon。png',例如,假設你的CSS位於'repo-name/build/css/styles.css'。 –

回答

0

考慮你的URL喜歡myusername.github.io/repository-name/..,你需要修復使用absolute_url並添加base_url_config.yml相對路徑:

<link rel="stylesheet" href="{{'/build/css/styles.css'|absolute_url}}"> 
<script src="{{'/dpd/jquery-3.2.1.min.js'|absolute_url}}"></script> 

<script src="{{ '/build/js/app.js'|absolute_url}}"></script> 

然後在_config.yml

baseurl: '/repository-name' 

要解決在CSS圖片路徑你可以使用類似/repository-name/css/...的URL或手動使用absolute_url「手動」調整它們,使Jekyll處理文件時增加了三個-在這樣的開始,在CSS文件:

--- 
--- 
.image2 { 
    background-image:url({{'/img/image2.png'|absolute_url}}); 
} 
+0

感謝您的回覆 - unfortunatley這不能解決我的github頁面網站,它也打破了我的本地構建。當我運行'jekyll serve'時,它根本沒有加載我的字體或圖像,並且正在運行gulp(其中我將jekyll集成到我的gulp任務中)返回'錯誤:\「background:\」後出現無效CSS:expected \ 「{\」,是\「url({{'/ img/sce ... \」\ 43行的...'我想嘗試避免顯式定義我的URL中的repo名稱,因爲我沒有 – kawnah

+0

你沒有提到這個問題中的問題,更新了包含構建中涉及的所有信息的問題。 – marcanuy

+0

好的,我編輯了問題 – kawnah