Jin
    • 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
    • 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
    • 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 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
  • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `iron man` # Day21 Box-model ## Box model box-model是CSS的基礎概念。我們在HTML檔中寫入很多元素,在瀏覽器渲染時,我們可以在畫面上看到一個個區域,個別裝著不同的內容,這個矩形區域是每個元素所產生的element box(元素盒),而box是由下列部分組成的: ![](https://i.imgur.com/1IMjdOj.png) - `content area`(內容區域) 元素的內容會放在這個區域,像是文字、圖片、子元素。content area的範圍通常是元素的寬高值,或是由內容來撐開的範圍。 - 內容撐開的範圍(邊框內的區域就是content area) ```css span { border: 1px solid #000; } ``` ![](https://i.imgur.com/svgq7jV.png) - 寬高值撐開的範圍 ```css div { width: 300px; height: 100px; border: 1px solid #000; } ``` ![](https://i.imgur.com/ONroid9.png) >inline元素content area的無法用設定寬與高,只能用內容來撐出範圍 ; block元素的content area除了內容會撐開範圍,也可以用寬高來設定範圍。 - `padding`(內距) padding通常用來撐出元素內部空間,可以透過屬性聲明內距,如果padding的數值是0的話,那padding box的大小就是會content area的大小。 padding又可以拆解成padding-top、padding-right、padding-bottom、padding-left。 ```css span { border: 1px solid #000; padding: 30px; } ``` 用開發者介面來看,就可以看到綠色區域是padding的範圍。 ![](https://i.imgur.com/6USsGio.png) - `border`(邊框) 可以透過border屬性設定樣式,如果border的數值是0,那border box的大小就是會padding box的大小。 border也可以拆解成上下左右來聲明樣式。 可以看[CSS基本樣式-border&outline](https://ithelp.ithome.com.tw/articles/10224613)了解樣式聲明。 - `margin`(邊界範圍) margin通常用來當元素與元素之間的距離,所以也常稱它為外邊距。它也可以拆解成上下左右來聲明外距。 >先前我們提過html就是由block元素跟inline元素來結構內容,block元素會佔滿父元素的寬度,垂直排列 ; inline元素會不佔滿寬度水平排列,直到寬度撐不下去才換到下一行。 - block元素的margin: ```css div { width: 300px; height: 50px; padding: 30px; border: 1px solid #000; margin: 30px; } ``` 橘色的區域是margin,上下左各是30px,可是怪怪的,為什麼右邊不是30px?這是因為`<div>`是block element,所以在box生成的當下,它就會佔滿父元素的寬度,而block元素的的水平margin則是扣掉content area、padding、border寬度後,剩下的寬度都會被margin撐滿。 ![](https://i.imgur.com/ZLQ5yg7.png) - inline元素的margin: ```css span { border: 1px solid #000; margin: 30px; } ``` 橘色的區域是margin,雖然我們對元素四周都設了`margin:30px`,但垂直方向的margin值對inline是無效的,水平方向有margin值,並且不會佔滿整個寬度。 ![](https://i.imgur.com/Bu1ehLc.png) - `outline`(輪廓)輪廓不佔空間,所以也常常不被算在box組成之一。 上一篇有說到outline的樣式跟border很像,可是它不佔空間,不管它的outline-width有多大,都不會被計算在元素所佔的空間裡。 outline無法拆解成上下左右聲明樣式。 可以看[CSS基本樣式-border&outline](https://ithelp.ithome.com.tw/articles/10224613)了解樣式聲明。 - 關於outline不佔空間這件事,我們可以來驗證看看: - 我們把兩個box的寬設40%,padding-left設5%,margin-left設5%。 ```css div { width: 40%; padding-left: 5%; margin-left: 5%; } ``` 計算兩個box所佔的空間 `40%*2+5%*2+5%*2=100%。兩個box所佔的空間剛好是100%,剛好佔滿父元素的寬度。 ![](https://i.imgur.com/TaaOHWI.png) ```css div { width: 40%; padding-left: 5%; margin-left: 5%; border: 10px solid #000; } ``` 如果同樣條件,再加個`border:10px`進去。就會超過100%,第二個元素被擠到下面去。 ```css div { width: 40%; padding-left: 5%; margin-left: 5%; outline: 10px solid #000; } ``` ![](https://i.imgur.com/SPxts6b.png) 但把border換成outline,它的10px不會佔到空間,所以兩個box的寬度仍然是100%,不會有元素被擠到下面。 ```css div { width: 40%; padding-left: 5%; margin-left: 5%; border: 10px solid #000; } ``` ![](https://i.imgur.com/GsGC8gI.png) CSS的box model筆記告一段落,這個概念在切版上很常遇到。接下來會在講跟box model也有關的box-sizing跟margin collapse,那就明天見~ *有任何錯誤或描述不夠精準之處,歡迎指教,非常感謝。

    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