如何重構 Suka CSS ?

栏目: CSS · 发布时间: 5年前

内容简介:Suka 本身已經支援 RWD,唯在視覺要求方面,可能與原作者稍有差異,本文總結自己重構其 CSS 心得。Hexo 3.8.0我主要將 Suka CSS 做了以下重構:

Suka 本身已經支援 RWD,唯在視覺要求方面,可能與原作者稍有差異,本文總結自己重構其 CSS 心得。

Version

Hexo 3.8.0

CSS

我主要將 Suka CSS 做了以下重構:

  • CSS Module
  • Device margin
  • Device font size

CSS Module

Suka 的 CSS 主要放在 suka/src/css/style.css 下,我將之重構如下:

style.css

@import url("global.css");
@import url("menu.css");
@import url("icon.css");
@import url("header.css");
@import url("main-layout.css");
@import url("post-entry.css");
@import url("post.css");
@import url("post-content.css");
@import url("footer.css");
@import url("tags-cloud.css");
@import url("timeline.css");
@import url("archive.css");
@import url("search.css");
@import url("link.css");

原本 Suka 是將整個 blog 的 CSS 全寫在 style.css ,共 1446 行,由於行數過多,實際維護困難。

/*
 * Global
 * Menu
 * Icon
 * Header
 * Timeline
 * Main Layout
 * Post Entry
 * Post
 * Post Content: markdown css
 * Footer
 * Page: Archive
 * Page: Tagscloud
 * Page: Search
 * Page: Link
*/

其實在 style.css 最上方,原作者已經加上註解,我只是根據這些註解拆成獨立 CSS module 而已,然後全部改用 @import

Q:使用 import 會每個檔案都有 HTTP request,會增加 server loading 嗎 ?

若是一般 CSS,的確會如此沒錯。

但 Suka 使用了 Gulp 處理,最後都會壓縮成為單一 style.min.css ,因此這裡使用 @import 不會影響效能,依然只有一個 HTTP request。

Device Margin

Suka 是以 mobile first 設計,從其 CSS 也可發現其 defualt value 都是 mobile phone,其次才使用 media query 考慮 tablet 與 desktop。

Home

如何重構 Suka CSS ?

Suka 原本在 home 的上下左右 margin 保留了較多的空白。

如何重構 Suka CSS ?

我仍然維持其留白設計,但上下左右 margin 皆減少。

這這牽涉到視覺美感,每個人喜好不同,我只是以自己的喜好去調整

main-layout.css

/* iPad portrait */
@media only screen and (min-width: 721px) {
  .main-container {
    max-width: 44rem;
  }
}

/* iPhone landscape  */
@media only screen and (min-width: 800px) {
  .main-container {
    max-width: 37.5rem;
  }
}

/* desktop, iPad landscape */
@media only screen and (min-width: 961px) {
  .main-container {
    max-width: 48rem;
  }
}

/* iPad Pro  */
@media only screen and (min-width: 1335px) {
  .main-container {
    max-width: 66rem;
  }
}

調整 home 的 margin 主要在 main-layout.css

15 行

/* desktop, iPad landscape */
@media only screen and (min-width: 961px) {
  .main-container {
    max-width: 48rem;
  }
}

調整 .main-containermax-width ,即可改變左右 margin。

我們發現了 desktop 與 iPad landscape 共用相同的 media query,主要因爲 iPad landscape 為 1024 x 768 ,而 Macbook Pro 15 Retina 最低解析度為 1024 x 640 ,而 home 的 margin 在此剛好 iPad landscape 與 desktop 剛好相同,故可共用。

Post

如何重構 Suka CSS ?

Post 部分 Suka 仍保持左右有較寬的 margin,且右側支援 TOC。

如何重構 Suka CSS ?

TOC 是很好的設計,我過去很愛用,但有些限制:

  • Heading 若太長,會影響版面呈現
  • Heading 層數若過深,也會影響版面呈現

所以過去常常為了畫面美觀,最後放棄 markdown 的 heading 架構,所以這次我沒使用 TOC。

也因為不使用 TOC,因此左右 margin 稍嫌累贅,因此也調整了左右 margin。

Suka 原本 home 與 post 的 margin 也不相同,因此從 home 切到 post,會有視覺跳動的感覺,也予以修正

post.css

/* iPhne portrait */
@media screen and (min-width: 400px) {
  .post-container {
    max-width: 20rem;
  }
}

/* iPad portrait */
@media screen and (min-width: 721px) {
  .post-container {
    max-width: 37.5rem;
  }
}

/* iPhone landscape  */
@media screen and (min-width: 800px) {
  .post-container {
    max-width: 37.5rem;
  }
}

/* desktop, iPad landscape */
@media screen and (min-width: 961px) {
  .post-container {
    max-width: 48rem;
  }
}

/* iPad Pro */
@media screen and (min-width: 1281px) {
  .post-container {
    max-width: 66rem;
  }
}

調整 home 的 margin 主要在 post.css

22 行

/* desktop, iPad landscape */
@media screen and (min-width: 961px) {
  .post-container {
    max-width: 48rem;
  }
}

調整 .post-containermax-width ,即可改變左右 margin。

目前 desktop 與 iPad landscape 依然共用 media query。

Device Font Size

Suka 對於 mobile phone 的 font size 調整得很好,但 tablet 與 desktop 對我而言,font size 稍微小了些,因此稍作調整。

此次調整 RWD,遇到最大的難點在於 iPad landscape 與 desktop 的 font size 要不一樣,偏偏 iPad landscape 為 1024 x 768 ,而 desktop 為 1024 x 640 ,因此無法單純使用 min-width 分辨出 iPad landscape 或 desktop。

width 雖然相同,但 height 不同,因此直覺會想到改用 min-height ,但有趣的是:Mobile Safari 並不理會 height ,但 Safari 則會,因此使用了以下 hack:

Home

post-entry.css

/* iPad landscape */
@media only screen and (min-width: 961px) {
  .post-entry a.card-title {
    font-size: 1.1rem;
  }

  .post-entry .card-body {
    font-size: 1.0rem;
  }
}

/* desktop */
@media only screen and (min-width: 961px) and (max-height: 640px) {
  .post-entry a.card-title {
    font-size: 1rem;
  }

  .post-entry .card-body {
    padding-top: .6rem;
    font-size: .8rem;
  }

  .post-entry .post-thumbnail {
    min-height: 6.5rem;
    margin-top: 15px;
  }
}

調整 home 的 font-size 主要在 post-entry.css

第 1 行

/* iPad landscape */
@media only screen and (min-width: 961px) {
  .post-entry a.card-title {
    font-size: 1.1rem;
  }

  .post-entry .card-body {
    font-size: 1.0rem;
  }
}

既然 Mobile Safari 吃不到 height ,那 min-width: 961px 就讓給 iPad landscape。

12 行

/* desktop */
@media only screen and (min-width: 961px) and (max-height: 640px) {
  .post-entry a.card-title {
    font-size: 1rem;
  }

  .post-entry .card-body {
    padding-top: .6rem;
    font-size: .8rem;
  }

  .post-entry .post-thumbnail {
    min-height: 6.5rem;
    margin-top: 15px;
  }
}

因為 desktop 為 1024 x 640 ,所以直覺會設定 min-height: 640px ,不過實際上 Safari 所傳回的 height 會扣掉一些值,反而要設定成 max-height 才抓得到。

如此就能 iPad landscape 與 desktop 分別使用不同 media query。

Post

post-content.css

/* iPad portrait */
@media only screen and (min-width: 721px) {
  #post-content {
    font-size: 1.1rem;
  }
}

/* iPhone landscape */
@media only screen and (min-width: 800px) {
  #post-content {
    font-size: 0.95rem;
  }
}

/* iPad landscape */
@media only screen and (min-width: 961px) {
  #post-content {
    font-size: 1.1rem;
  }
}

/* desktop */
@media only screen and (min-width: 961px) and (max-height:640px) {
  #post-content {
    font-size: .85rem;
  }
}

調整 post 的 font-size 主要在 post-content.css

15 行

/* iPad landscape */
@media only screen and (min-width: 961px) {
  #post-content {
    font-size: 1.1rem;
  }
}

/* desktop */
@media only screen and (min-width: 961px) and (max-height:640px) {
  #post-content {
    font-size: .85rem;
  }
}

依然使用相同技巧分辨 iPad landscape 與 desktop。

Archive

調整 archive 的 font-size 主要在 archive.css

Search

調整 archive 的 font-size 主要在 search.css

Summary

一共調整了以下 CSS:

main-layout.css
post.css
post-entry.css
post-content.css
archive.css
search.css

Conclusion

  • CSS 也要使用 module,將來才容易維護
  • RWD 的 media query 難在 break point 該如何下,尤其 iPad landscape 與 desktop 的 width 相同,透過 max-height 技巧,可暫時讓 iPad landscape 與 desktop 使用不同 media query

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

调试九法

调试九法

David J.Agans / 赵俐 / 人民邮电出版社 / 2010-12-7 / 35.00元

硬件缺陷和软件错误是“技术侦探”的劲敌,它们负隅顽抗,见缝插针。本书提出的九条简单实用的规则,适用于任何软件应用程序和硬件系统,可以帮助软硬件调试工程师检测任何bug,不管它们有多么狡猾和隐秘。 作者使用真实示例展示了如何应用简单有效的通用策略来排查各种各样的问题,例如芯片过热、由蛋酒引起的电路短路、触摸屏失真,等等。本书给出了真正能够隔离关键因素、运行测试序列和查找失败原因的技术。 ......一起来看看 《调试九法》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具