Peiyun Lee
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `前端技能樹` # 用HTML、CSS、JS打造個人網站 (3) > 在上一篇:[用HTML、CSS、JS打造個人網站 (2)](),完成了網頁的所有內容,接下來的工作就是讓網頁能夠適應各種螢幕大小,讓使用者在每個裝置都能有最基本的使用體驗。 前面的文章[跨平台生存之道 — RWD響應式網頁設計](/anm_nbj7S_KJGaBZsBKefA),有提到可以設置斷點(Break point),讓網頁在縮放的時候可以根據斷點去做響應的變化。在這次練習的設計稿,定義出了網頁在三種不同的尺寸下的排版,你可以在 assets/design 資料夾裡面找到,分別是:**desktop.png、tablet.png、mobile.png**。 **設置的斷點會是 1024px、768px,螢幕寬度來到 1024px 以上就會顯示 Desktop 版本、1024px~768px 顯示 Tablet 版本、768px 以下顯示 Mobile 版本**,接下來就會帶大家分別調整三個區段的排版。 ## 螢幕寬度>=1024px ![](https://i.imgur.com/JkCUSB1.png) 雖然我們說版面要適應螢幕的大小來縮放,但當你在很大的螢幕上瀏覽網頁,需要從螢幕最左邊看到最右邊其實是很累人的一件事。**所以在大尺寸下,通常不會讓網頁的內容滿版顯示**,會去設定內容文字最大的縮放寬度 ```max-width``` ,只有讓背景適應網頁做滿版顯示。 ```css= ... .wrapper-content { ... width: 100%; max-width: 1024px; margin: 0 auto; } ... header,.section-primary,footer { background-color: #fff5da; } ``` 這時候開啟瀏覽器的開發者工具,調整尺寸到 1024px 以上,就可以看到設置的結果囉。 **Before** ![](https://i.imgur.com/fDQFWGd.png) **After** ![](https://i.imgur.com/54W4lTu.png) <!-- <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/uDsqXcR.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/fLRrMnA.png" width="100%"/> </div> </center> --> ## 螢幕寬度1023px~768px ![](https://i.imgur.com/Mx79SYa.png) 我們會將 Desktop 設為預設的樣式,接著是 Tablet 的版本。使用 Media Query 偵測螢幕寬度,在 1024px 以下、768px (含)以上時,就會採用裡面設定的樣式。 ### Section Primary 在 Pimary 區塊,除了調整左右兩欄的寬度佔比,還要讓右側的圖片貼緊區塊的底部。 ```css= @media screen and (max-width: 1023px) and (min-width: 768px) { .primary-content { padding-bottom: 0; } .wrapper-primary-text { width: 50%; padding-bottom: 80px; } .primary__main-img { width: 50%; align-self: flex-end; } } ``` **Before** ![](https://i.imgur.com/Nhpj8CF.png) **After** ![](https://i.imgur.com/aMINDCu.png) <!-- <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/F9VnVFf.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/VdWssn1.png" width="100%"/> </div> </center> --> ### Selected works 原本是讓作品並排顯示,但在 Tablet 版本要讓作品垂直排列,所以設定 ```wrapper-selected-works``` 的 ```flex-direction``` 值為 ```column``` ;另外再將 ```wrapper-work``` 設定 ```display``` 值為 ```flex```,就會自動讓內部的圖片跟文字區根據水平的方向去排列。 ```css= @media screen and (max-width: 1023px) and (min-width: 768px) { .wrapper-selected-works { width: 600px; flex-direction: column; justify-content: flex-start; } .wrapper-work { display: flex; margin-bottom: 50px; } .wrapper-work-text { display: flex; flex-direction: column; align-items: flex-start; margin-left: 30px; } .work__title { margin-top: 0; } } ``` **Before** ![](https://i.imgur.com/6S5hLKa.png) **After** ![](https://i.imgur.com/bpqQuFk.png) <!-- <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/t1tkNrl.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/XoWHWwp.png" width="100%"/> </div> </center> --> ### About 在 About 這個區塊,依照之前百分比的設定,左右兩欄會自動去縮放。但縮放到某個尺寸的時候,你會發現兩欄的大小好像不太夠,圖片跟文字會擠在一起。所以就將最外層的容器寬度再調大一點,並設定它最大的尺寸: ```css= @media screen and (max-width: 1023px) and (min-width: 768px) { .wrapper-about { max-width: 820px; width: 90%; } } ``` <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/tFQeqeo.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/UilqG4z.png" width="100%"/> </div> </center> ### Footer Footer 這個區塊也很簡單,因為寬度會不夠把 Logo 跟連結選單並排顯示,只要修改排列的方式變成垂直排列就行了。 ```css= @media screen and (max-width: 1023px) and (min-width: 768px) { .footer-content { flex-direction: column; justify-content: flex-start; width: fit-content; } footer nav { margin-top: 30px; } } ``` <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/cEovrDo.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/DNXiCtp.png" width="100%"/> </div> </center> ## 螢幕寬度<768px ![](https://i.imgur.com/MiNmgsk.png) 最後一個版本 Mobile,終於會來到我們的重頭戲 — 漢堡選單。**當你在手機上瀏覽網頁的時候,你應該有注意到大部分的導覽列,都會被收進一個像漢堡的按鈕裡,按下按鈕就可以開啟導覽選單**。那是因為在手機上因為螢幕太小,很難去放置前面做的那種展開的導覽列,所以會選擇把選單收合起來,是在網站上非常常見的一個做法。 ### Header — 漢堡選單 首先先設定選單尚未展開的樣式,在 Header 區塊裡加上按鈕的 HTML,展開後的選單元件不會另外新增,會直接使用 Header 內部的元素,並調整它的樣式。 ```html= <header> <div class="header-content wrapper-content"> <h1 class="logo">PEI-YUN</h1> <div class="wrapper-hamburger"> <div class="cross-button-top cross-button"></div> <div class="cross-button-bottom cross-button"></div> </div> <nav class="header-nav"> ... </nav> </div> </header> ``` 除了設定按鈕的樣式,在還沒展開選單的狀態下,要讓導覽列先消失不見,而且不會影響到其他元素,想要做到這樣的效果可以用 ```display: none```。 ```css= /* header */ @media screen and (max-width: 767px) { .wrapper-hamburger { height: 15px; display: flex; flex-direction: column; justify-content: space-between; cursor: pointer; } .cross-button { width: 30px; height: 3px; background-color: #333; transition: all 0.2s; } .header-nav { display: none; } } /* section-primary */ @media screen and (max-width: 767px) { .primary-content { ... padding-top: 70px; } } ``` ![](https://i.imgur.com/kmSjA2R.png) >沒有展開選單時的 Header 要如何做到**按下按鈕之後展開選單**?很簡單,在前面的章節有提到可以利用 Javascript 來操作 DOM 元素。當按下按鈕展開選單,就在指定的元素加上展開後的樣式;相反的要收合選單,移除那些樣式就能回復到展開之前。 **所以只要加上 ```header-content_active```,並用 CSS 的後代選擇器來設定底下元素在展開後的樣式**。這樣一來只要透過 Javascript 新增移除 ```header-content_active``` 這個 ClassName,就能實現選單開闔的效果。 ```html= <header> <div class="header-content_active header-content wrapper-content"> ... </div> </header> ``` ```css= @media screen and (max-width: 767px) { .header-content_active { background-color: #333; height: 100vh; display: flex; flex-wrap: wrap; justify-content: space-between; align-content: flex-start; align-items: center; padding-bottom: 20px; } .header-content_active .header-nav { width: 100%; display: flex; flex-direction: column; align-items: center; margin-top: 20vh; } .header-content_active .logo { color: white; } .header-content_active .link-text { color: white; font-size: 36px; margin-bottom: 50px; } .header-content_active .wrapper-hamburger { height: 30px; justify-content: center; } .header-content_active .cross-button { background-color: white; } .header-content_active .cross-button-top { transform: rotate(45deg) translateY(2px); } .header-content_active .cross-button-bottom { transform: rotate(-45deg) translateY(-2px); } } ``` ![](https://i.imgur.com/J7HFjq0.png) >選單展開後的 Header 設定完選單展開前後的樣式後,最後一步就是要加上 Javascript 來操作元素,點擊按鈕打開選單,要對 ```header-content``` 新增 ClassName ```header-content_active```;點擊按鈕關閉選單,再移除 ```header-content_active```,透過這樣的機制讓底下元素變化對應的樣式。 ```html= <header> <div id="header-content" class="header-content wrapper-content"> ... <div id="hamburger" class="wrapper-hamburger"> ... </div> ... </div> </header> ... <!-- 記得將js檔放在body最後引入 --> <script src="js/index.js"></script> </body> </html> ``` index.js ```javascript= let target = document.getElementById("header-content"); let button = document.getElementById("hamburger"); button.addEventListener("click", function (event) { target.classList.toggle("header-content_active"); }); ``` 利用 CSS 屬性 ```transition```,加上一點轉場效果: ```css= @media screen and (max-width: 767px) { .header-content { transition: background-color 0.2s; } } ``` ![](https://i.imgur.com/O5QXzQs.gif) ### Section Primary 做完最難的漢堡選單,剩下的區塊基本上就跟調整 Tablet 的時候一樣。在 Primary 區塊,改變左右欄的排列方式變成上下欄: ```css= @media screen and (max-width: 767px) { .primary-content { flex-direction: column; padding-bottom: 0; } .wrapper-primary-text { width: 90%; } .primary__title { font-size: 48px; } .primary__social { display: none; } .primary__main-img { width: 80%; align-self: center; } } ``` <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/3F6jUpy.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/pzBD0cx.png" width="100%"/> </div> </center> ### Selected works Selected works 區塊在 768px 以下,會從 Desktop 版本的並排模式,轉換成作品垂直排列,所以一樣設定 ```flex-direction``` 值為 ```column```,讓作品內容可以完整顯示在網頁上。 ```css= @media screen and (max-width: 767px) { .secondary__title { font-size: 28px; } .wrapper-selected-works { width: 300px; flex-direction: column; } .work__title { font-size: 20px; } .wrapper-work { margin-bottom: 50px; } } ``` <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/RGcVE1M.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/mTIYLHy.png" width="100%"/> </div> </center> ### About About 區塊也是一樣。基本上你可以發現,在 Mobile 版本,**就是把原本寬度不夠不好並排顯示的內容,轉換一下排列的方式變成垂直排列,再調整一下寬度比例,就可以讓內容最佳的呈現在網頁上**。 ```css= @media screen and (max-width: 767px) { .wrapper-about { width: 95%; flex-direction: column; align-items: center; justify-content: flex-start; } .wrapper-about-text { width: 80%; margin-top: 50px; } } ``` <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/xrM3hjN.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/yM4JIQD.png" width="100%"/> </div> </center> ### Footer 終於來到最後一個區塊 Footer,除了左右兩欄要調整成垂直排列,選單內部的每個小區塊也要垂直顯示。 ```css= @media screen and (max-width: 767px) { .footer-content { width: 100%; flex-direction: column; justify-content: flex-start; } footer nav { width: 100%; flex-direction: column; margin-top: 30px; } .footer-nav-list { margin-top: 30px; } } ``` ![](https://i.imgur.com/D8FpxTd.png) ![](https://i.imgur.com/RfXYB5B.png) <center class="half" style="display:flex; justify-content: space-between;"> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">Before</div> <img src="https://i.imgur.com/ujqZWe7.png" width="100%"/> </div> <div style="width: 48%"> <div style="font-weight: bold;margin-bottom: 5px;">After</div> <img src="https://i.imgur.com/GrkfgwX.png" width="100%"/> </div> </center> --- ## 小結 >[你做完的網頁應該會長成這樣](https://peiyunlee.github.io/ithome-21ironman-personalwebsite/) 終於帶大家完成我們的個人網頁,在過程中也使用到前面一路學習到的HTML、CSS、RWD、JS,這些都是構成一個完整的網頁不可獲缺的技能。當然網頁版面五花八門,如何成為一等一的網頁刻版大師?**最好的學習方法就是:「動手做!」,建議大家可以去找各式各樣的網站,動手做出一模一樣的版型,你可以在過程中學習到更多技能的知識**,未來碰到什麼樣式的版面也難不倒你喔! 如果文章中有錯誤的地方,要麻煩各位大大不吝賜教;喜歡的話,也要記得幫我按讚訂閱喔❤️

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully