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設計模式,擺脫義大利麵代碼 當別人說你的程式是義大利麵代碼 (Spaghetti code),聽起來很好吃,應該是稱讚的意思吧!千萬不要誤會,那是在說你的程式碼雜亂無章、難以理解。 在前面的章節我們學到可以隨意命名 HTML 元素的 ID 和 Class 名稱,**意味著沒有結構化的命名模式下,可能會因為重複命名,需要去解決 CSS 衝突的問題,就一直往下新增 CSS 代碼**,導致程式亂亂的。所以本篇的教學重點就是:建立易讀易寫的CSS代碼規則 — OOCSS / BEM。 ## OOCSS OOCSS (Object Oriented CSS),**主張用物件的形式管理 CSS 的分類規則,專注在讓組件的重複使用性提升**,並且提出下面兩個主要的原則: ### 結構與樣式的分離 將重複的樣式抽離,獨立成一個組件重複使用。舉例來說如下面的程式碼,紅色的 box 和紅色的 button,那我們就可以把紅色的樣式抽離出來: CSS ```css= .box { width: 200px; height: 50px; } .button { width: 100px; height: 50px; padding: 10px; } .red-skin { background-color: red; } ``` HTML ```html= <div class="red-skin box"></div> <button class="red-skin button"></div> ``` ### 容器和內容的分離 將容器內部通用的結構分離。舉例來說如下面的程式碼,Header 雖然在 Section 跟 Footer 區塊都會出現,外觀一樣但安裝的位置不一樣,那我們就可以將 Header 像零件一樣抽離,再另外設定各自安裝的位置: CSS ```css= .global-header { font-size: 28px; width:500px; height:10px } .section-header { padding: 10px; } .footer-header { padding: 5px; } ``` HTML ```html= <section> <div class="section-header global-header">...</div> </section> <footer> <div class="footer-header global-header">...</div> </footer> ``` ## BEM BEM (Block, Element, Modifier),**主張劃分網頁區塊,以區塊 (Block)、元素 (Element) 和修飾子 (Modifier) 命名**,用組件的模式去進行介面開發。 ### 區塊 (Block) 獨立的頁面元件 ```html= <header class="header"> <div class="logo">...</div> <ul class="menu"> <li>...</li> <li>...</li> </ul> </header> ``` ### 元素 (Element) 存在於 Block 中不可單獨存在的小元件,使用雙底線 ```__``` 做分隔。 ```html= <header class="header"> <div class="logo"> <!-- logo中的元素 --> <img class="logo__img" /> <h1 class="logo__title" /> </div> <ul class="menu"> <!-- menu中的item元素 --> <li class="menu__item">...</li> <li class="menu__item">...</li> </ul> </header> ``` ### 修飾子 (Modifier) 描述 Block 或 Element 的外觀,不能單獨使用,用單底線 ```_``` 做分隔。 ```html= <header class="header"> <div class="logo">...</div> <ul class="menu"> <li class="menu__item">...</li> <!-- menu中的item--active元素 --> <li class="menu__item_active">...</li> </ul> </header> ``` ## 小結 **OOCSS 強調的能提升組件的重複使用性、BEM 能讓命名結構更清楚**,最主要的是能**活用兩者的核心規則**,建立易讀易寫的CSS,方便之後維護與管理。這個章節雖然只介紹了這兩種,但其實還有 **SMACSS** 的方式來管理 CSS,建議你都可以了解一下。那今天的介紹就到這,接下來我們會提到最後一個網頁核心技術 — Javascript,那我們就下章再見囉! 👉 如果文章中有錯誤的地方,要麻煩各位大大不吝賜教;喜歡的話,也要記得幫我按讚訂閱喔❤️ ### 參考資料 - [Scalable CSS - 介紹OOCSS/SMACSS/BEM](http://sj82516-blog.logdown.com/posts/1077348/finish-css-intro-oocss-smacss-bem) - [How to Organize Your CSS with a Modular Architecture(OOCSS, BEM, SMACSS)](https://snipcart.com/blog/organize-css-modular-architecture) - [BEM Document](https://en.bem.info/methodology/quick-start/) <!-- - [SMACSS](http://smacss.com/) -->

    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