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
    • 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 Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `前端技能樹` # 玩轉CSS樣式,進入美麗新世界 在上一篇學到了 CSS 的基本語法、使用選擇器以及檔案的套用方法。接著就讓我們來學習如何運用樣式屬性!首先來簡單了解一下 CSS 是如何去進行網頁的排版和調整元素的樣式。 ## Box Model **在 CSS 裡面,所有 HTML 元素都會由一個盒子構成**,這個盒子就稱為 Box Model,針對這個盒子去做調整,就能慢慢建構出網頁想要的排版。 可以用屬性 ```display``` 來設定元素的 Box Model 類型,但通常會預設成兩種: ```css display: inline/block; ``` - **行內元素 (inline類型)** 元素不會換行,並且盒子的大小由元素的內容撐開,因此不能設定盒子的寬度和高度。常見的元素有:```<a>```、```<img>```、```<span>```...。 - **區塊元素 (block類型)** 元素會自動換行,在不設定盒子大小的情況下和包裹它的容器一樣寬,也可以另外設定盒子的寬度和高度。常見的元素有:```<h1>```、```<p>```、```<div>```...。 ![](https://i.imgur.com/e4DSki2.jpg) >除了 inline / block,display 還有更多設定值可以改變元素的佈局 ### 設定盒子的樣式 ![](https://i.imgur.com/3EIhui2.jpg) >盒子分成四個區塊:**content(藍區) / padding / border / margin** 試著為四個區塊新增樣式屬性: - 設定**內容content** 的寬 / 高 / 背景顏色 — **```width```** / **```height```** / **```background-color```** ```css= width: 150px; height: 80px; background-color: yellow; ``` - 設定**內邊距padding** — **```padding```** ```css= padding: 10px; /* 等同於: padding-top: 10px; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; */ ``` - 設定**邊框border** — **```border```**: ```css= /* border: 粗細 顏色 樣式 */ border: 5px blue solid; ``` - 設定**與其他元素的邊距margin** — **```margin```** ```css= margin: 20px; /* 等同於: margin-top: 20px; margin-bottom: 20px; margin-left: 20px; margin-right: 20px; */ ``` ![](https://i.imgur.com/2lm5Ob3.gif) >網頁就是由這樣一個一個的盒子堆砌而成的喔! ### 盒子寬高的計算方式 盒子預設採用的元素寬高的計算方式還有不一樣?一般情況下,**當設定 ```width``` 和 ```height``` ,其實並不是完全代表看到的寬高度,因為 ```padding``` 和 ```boder``` 一樣會讓盒子看起來變大**,只是設定盒子Content的大小,而這個計算方式就是 **Content-box**。 ![](https://i.imgur.com/Ce8GA7B.png) >Content-box 的清況下,設定 ```width: 200px;```、```hegiht: 100px;``` 但我們通常會想要直接通過 ```width``` / ```height``` 就可以直接設定外觀的大小,所以就有了 **Border-box,它的寬高設定就會等於 Content + Padding + Border**。 ![](https://i.imgur.com/kpclGdt.png) >Border-box 的情況下,設定 ```width: 200px;```、```hegiht: 100px;``` 可以透過 ```box-sizing``` 這個屬性來設定元素要採用的計算方式: ```css= box-sizing: content-box | border-box; ``` ## 常用樣式 知道網頁元素是如何進行排列了,但我們還沒了解 CSS 的真正的能耐,接下來就直接動手做,認識一些常用的樣式屬性。 首先在 HTML 裡新增一個元素: ```html <div id="test">HELLO<div> ``` ![](https://i.imgur.com/KZVQXtf.jpg) ### 移動位置 - **```position```**:設定元素的定位方式 - **```top``` / ```bottom``` / ```left``` / ```right```**:設定文字顏色 ```css= position: relative; //相對定位:相對調整本身的位置 top: 100px; //向下相對移動 100 pixel ``` ![](https://i.imgur.com/MvIZpZm.jpg) ### 改變顏色 - **```color```**:設定文字顏色 - **```background-color```**:設定背景顏色 ```css= color: #FAC9B8; background-color: #C5DECD; ``` ![](https://i.imgur.com/R13VrPp.jpg) ### 設定文字 - **```font-family```**:設定字型(找字體可以到[Google Fonts](https://fonts.google.com/?selection.family=Acme)) - **```font-size```**:設定字體大小 - **```font-weight```**:設定字體粗細 - **```text-align```**:設定文字對齊方式 ```css= font-family: "Noto Sans CJK TC"; font-size: 28px; font-weight: bold; //粗體字 text-align: center; //文字置中對齊 ``` ![](https://i.imgur.com/H85Juda.jpg) 怎麼樣,是不是跟原本不一樣了呢!這就是 CSS 的魔力,當你熟悉更多樣式屬性,你就能更靈活的美化你的網頁。 ## 小結 今天講到 CSS 的本體 Box Model、還有一些常用的樣式。如果你還想學習更多的屬性,你可以參考 [CSS Reference](https://cssreference.io/) 或是 [w3schools-css](https://www.w3schools.com/css/) 網站,上面有詳盡的樣式表還有畫面範例供你學習。明天的文章會進一步提到如何用 Flexbox 來實現多樣化的網頁佈局,那就下章再會囉! 👉 如果文章中有錯誤的地方,要麻煩各位大大不吝賜教;喜歡的話,也要記得幫我按讚訂閱喔❤️ ### 參考資料 - [MDN Web Docs - CSS](https://developer.mozilla.org/zh-TW/docs/Learn/CSS)

    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