FE: CSS固定圖片顯示大小及GitHub Pages在線演示


CSS固定圖片顯示大小

分析

假設圖片區域的大小固定為250×300px,那么我們可以寫出如下的樣式

.picture-area {
    width: 250px;
    height: 300px;
    margin: 1em;
}

當然簡單如下的html是不能限制圖片大小的

<div class=“picture-area”>
    <img src=“…” alt=“…”>
</div>

換個思路,將圖片作為div的背景圖片

<div style=“background-image: url(‘…’)”></div>

此時需要將該div鋪滿picture-area,因此定義樣式

.picture {
    position: absolute;
    left:0;
    right:0;
    top:0;
    bottom:0;
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
}

於是得到限制圖片大小的div如下

<div class=“picture-area”>
    <div class=“picture” style=“background-image: url(‘…’)”></div>
</div>

由於picture使用了絕對定位,根據w3school上的解釋:“生成絕對定位的元素,相對於 static 定位以外的第一個父元素進行定位”,如果元素沒有定義position,默認position為static,因此將父元素picture-area的定位方式設為position:relative即可。

完整的CSS

 1 .picture-area {
 2 
 3     width: 250px;
 4 
 5     height: 300px;
 6 
 7     margin: 1em auto 1em auto;
 8 
 9     position: relative;
10 
11 }
12 
13 
14 
15 
16 .picture-area .picture {
17 
18     position: absolute;
19 
20     left: 0;
21 
22     top: 0;
23 
24     right: 0;
25 
26     bottom: 0;
27 
28     background-repeat: no-repeat;
29 
30     background-position: center 36%;
31 
32     background-size: cover;
33 
34 }
View Code

 

GitHub Pages

Github的每個repository有Github Pages,可以使用Github Pages做靜態頁面演示。

因此首先在Github上創建一個名為VacationSchedule的repository。

(1) clone項目到本地

git clone https://github.com/zrss/VacationSchedule.git

(2) 進入項目文件夾

cd VacationSchedule

(3) 切換到gh-pages分支,這個分支的文件才被視為Github Pages的文件

git checkout --orphan gh-pages

(4) 在項目文件夾下寫web代碼即可。目錄結構例如:

/VacationSchedule

  /bootstrap

  /css

  /images

  index.html

(5) 提交代碼

git commit -a

(6) merge到gh-pages

git push

即可通過http://zrss.github.io/VacationSchedule/查看到web頁面效果;一般來說,Github Pages可以通過http://<user_name>.github.io/<repository_name>/來訪問。

 

樣式參考:http://xiumi.us

GitHub Pages參考:http://www.ruanyifeng.com/blog/2012/08/blogging_with_jekyll.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM