(OF作品展示)
OF之前介紹了用python實現數據可視化、數據分析及一些小項目,但基本都是后端的知識。想要做一個好看的小系統,我們還要學一些前端的知識,今天OF將講解如何用pycharm(全棧開發不錯的工具)做一張好看的網頁,以后我們就可以自己開發網頁/網站放到互聯網上。
主要內容:網頁前端布局
適用人群:Python初學者,前端初學者
準備軟件:pycharm
1) 下載完成pycharm, 點擊file-New Project...
2) 按下圖步驟創建一個項目,目前我們選擇Pure python即可,以后我們可以學習用Django/flask等框架來做完整的系統
1)在創建的項目空白處鼠標右鍵-New-HTML File
2)輸入英文的網頁名字,盡量不要輸入中文/特殊字符
當雙擊打開我們創建后的HTML文件,大家會看到下面的界面
在開始開發網頁前,我們需要自己設計下想要把網頁做成什么樣,為了節省成本OF都是自己設計的頁面樣式,可以手繪,也可以在PPT上畫。
我們先學習一個比較簡單的布局如下圖,大家可以看到下圖已經畫出了我們需要添加的內容,要注意的地方是比如Taylor的圖片和文字一定要用<div class=bord>框住,否則Taylor圖片與文字將會是左右的關系,而不是上下
根據上述的css名字定義,先填充<body>內的代碼,那么我們就完成一半的工作了,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="intro">
<p class="peo">人物介紹</p>
</div>
<div id="pic1">
<div class="bord">
<img class="img" src="pic/Taylor.png"/>
<p class="word">Taylor</p>
</div>
<div class="bord">
<img class="img" src="pic/yan.png"/>
<p class="word">東</p>
</div>
<div class="bord">
<img class="img" src="pic/song.png"/>
<p class="word">喬</p>
</div>
</div>
</body>
</html>
1)鼠標移到代碼處,在右上角我們會看到一排瀏覽器,我們點擊其中一個運行
2)目前看到的網頁內容從上到下顯示
首先我們簡要了解下flex布局,大家可以看到下圖中#main的style樣式中display:flex,在body部分將3個顏色內容框在<div id="main">中,運行結果是3個顏色的內容橫向展示了,而不是上下
1)那么我們先從“人物介紹”的布局開始,“人物介紹”居中展現(用flex中justify-content:center),而且下面有一條黑線,OF準備用border樣式來實現,所以在<div id=intro>里另加了個<div class=peo>,代碼如下:
(注:style中的#main對應body中的id=main, .main對應class=main)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#intro {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
.peo {
max-width: 10rem;
border-bottom: 0.2rem solid #000000;
font-family: sans-serif;
font-size: 1.5rem;
color: #0070C0;
line-height: 3rem;
}
</style>
</head>
<body>
<div id="intro">
<p class="peo">人物介紹</p>
</div>
<div id="pic1">
<div class="bord">
<img class="img" src="pic/Taylor.png"/>
<p class="word">Taylor</p>
</div>
<div class="bord">
<img class="img" src="pic/yan.png"/>
<p class="word">東</p>
</div>
<div class="bord">
<img class="img" src="pic/song.png"/>
<p class="word">喬</p>
</div>
</div>
</body>
</html>
運行結果:
2)圖片部分是3個<div class=bord>橫向展示,所以要在框住它們的<div id=pic1>樣式中設置flex布局,在<style>里加入以下代碼:
#pic1 {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
運行結果:
3)圖片之間靠太近,圖片大小不一致,文字沒居中,我們在<style>里加入以下代碼:
.bord{
padding: 1rem 2rem;
}
.img {
border: 0.2rem solid #e3e3e3;
max-width: 15rem;
height: 22rem;
}
.word {
text-align: center;
}
運行結果:
今天我們學會了flex布局,今后所有的網頁排版都可以實現了,祝愿大家都有所收獲,完整的代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#intro {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
.peo {
max-width: 10rem;
border-bottom: 0.2rem solid #000000;
font-family: sans-serif;
font-size: 1.5rem;
color: #0070C0;
line-height: 3rem;
}
#pic1 {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
.bord{
padding: 1rem 2rem;
}
.img {
border: 0.2rem solid #e3e3e3;
max-width: 15rem;
height: 22rem;
}
.word {
text-align: center;
}
</style>
</head>
<body>
<div id="intro">
<p class="peo">人物介紹</p>
</div>
<div id="pic1">
<div class="bord">
<img class="img" src="pic/Taylor.png"/>
<p class="word">Taylor</p>
</div>
<div class="bord">
<img class="img" src="pic/yan.png"/>
<p class="word">東</p>
</div>
<div class="bord">
<img class="img" src="pic/song.png"/>
<p class="word">喬</p>
</div>
</div>
</body>
</html>
今天的知識比較基礎但非常實用,每天學會一個小技能,積少成多,以后就能成為大神。如果大家對網頁上的實現有什么不懂的,盡請留言,OF一定會一一解答的。
者:sunshine小小倩
轉發鏈接:https://juejin.im/post/599970f4518825243a78b9d5
我們剛開始學習Web前端時,就對HTML頁面的布局特別感興趣,上一講《CSS初步介紹》中,講解了CSS的入門知識,現在我們介紹一下HTML布局的知識。
HTML布局的方案有Table布局、最流行的DIV布局、以及HTML5建議的DIV布局的替代方案。
當HTML內容被瀏覽器顯示時,整個HTML頁面對使用者來說,就是一個顯示信息與進行操作的界面。我們常常見到和下面類似的界面:
在這個界面中,整個HTML網頁被分為標題區、導航區、內容去、狀態欄區,下面我們分別用Table布局、DIV布局和HTML5建議的布局方案來實現該界面。
使用Table布局方案,將整個瀏覽器的展示部分就是一個表格,然后我們設置好表格的單元格合并、大小即可。下面是使用Table布局方案的HTML頁面:
<!DOCTYPE html>
<html>
<head>
<title>layout001</title>
<meta charset="utf-8" />
<style type="text/css">
html,body, table{
width:100%;
height:100%;
}
#first_row{
height:6%;
}
#second_row{
height:91%;
}
#third_row{
height:3%;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
#header{
border:1px black solid;
color:white;
text-align:center;
background:green;
}
#navigator{
border:1px black solid;
color:white;
text-align:center;
background:blue;
}
#content{
border:1px black solid;
color:white;
text-align:center;
background:gray;
}
#footer{
border:1px black solid;
color:white;
text-align:center;
background:orange;
}
</style>
</head>
<body>
<table>
<tr id="first_row">
<td id="header" colspan="2">標題區</td>
</tr>
<tr id="second_row">
<td id="navigator" width="15%">導航區</td>
<td id="content" width="85%">內容區</td>
</tr>
<tr id="third_row">
<td id="footer" colspan="2">狀態欄區</td>
</tr>
</table>
</body>
</html>
使用瀏覽器打開這個HTML文檔,展示效果如下:
這個框架建立后,我們就可以在各個<td>內進行具體的開發了。
使用DIV布局方案,將整個瀏覽器的展示部分由各個DIV來分配。下面是使用DIV布局方案的HTML頁面:
<!DOCTYPE html>
<html>
<head>
<title>layout002</title>
<meta charset="utf-8" />
<style type="text/css">
html,body{
width:100%;
height:100%;
}
body,#header,#second_row,#navigator,#content,#footer{
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
#header{
height:6%;
width:100%;
color:white;
text-align:center;
background:green;
}
#second_row{
height:91%;
width:100%;
}
#navigator{
height:100%;
width:15%;
float:left;
color:white;
text-align:center;
background:blue;
}
#content{
height:100%;
width:85%;
float:right;
color:white;
text-align:center;
background:gray;
}
#footer{
height:3%;
width:100%;
color:white;
text-align:center;
background:orange;
}
</style>
</head>
<body>
<div id="header">
標題區
</div>
<div id="second_row">
<div id="navigator">
導航區
</div>
<div id="content">
內容區
</div>
</div>
<div id="footer">
狀態欄區
</div>
</body>
</html>
使用瀏覽器打開這個HTML文檔,展示效果如下:
這個框架建立后,我們就可以在各個<div>內進行具體的開發了。
可以發現,使用DIV元素,我們對頁面進行布局時更方便。
使用DIV布局方案,使用起來特別方便,基本上是前端開發者的首選。不過在HTML5標準來看,各個DIV的含義不明確,因此建議使用專門的元素來替代DIV。這是按照HTML5推薦的布局方案的頁面:
<!DOCTYPE html>
<html>
<head>
<title>layout003</title>
<meta charset="utf-8" />
<style type="text/css">
html,body{
width:100%;
height:100%;
}
body,header,#second_row,nav,main,footer{
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
header{
height:6%;
width:100%;
color:white;
text-align:center;
background:green;
}
#second_row{
height:91%;
width:100%;
}
nav{
height:100%;
width:15%;
float:left;
color:white;
text-align:center;
background:blue;
}
main{
height:100%;
width:85%;
float:right;
color:white;
text-align:center;
background:gray;
}
footer{
height:3%;
width:100%;
color:white;
text-align:center;
background:orange;
}
</style>
</head>
<body>
<header>
標題區
</header>
<div id="second_row">
<nav>
導航區
</nav>
<main>
內容區
</main>
</div>
<footer>
狀態欄區
</footer>
</body>
</html>
使用瀏覽器打開這個HTML文檔,展示效果和上面的一模一樣:
使用這種方案,除了使用了含義明確的<header>、<nav>、<main>、<footer>元素外,和DIV方案沒有區別。
這種做法貌似HTML更清晰,但實際上增加了元素的種類,增加了開發人員的記憶負擔,容易出錯。因此,前端程序員基本上都不喜歡這種方案。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。