Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[CS Foundation] Web - 3 - css layout

[CS Foundation] Web - 3 - css layout

x-village

August 14, 2018
Tweet

More Decks by x-village

Other Decks in Programming

Transcript

  1. CSS LAYOUT & HOW TO USE THEM By POCHUN LU

    FROM THE WORLD OF X-VILLAGE Sli.do:
  2. 1. Web Layout… Web Layout! Not Web Layout Web layout

    is like Tetris, we pile elements one by one.
  3. 1. Ideal web Layout… But in “CSS”, we do not

    have error messages. So… budy { funt-size: 15qx; buger-radius: 5px } “You didn’t see anything. “ Everything is default!
  4. 1. Real World Web Layout… Image: 1, 2 It’s a

    little bit hard to trace the bug…
  5. 1. About Web Layout… If you want to move something

    to some place, you need to know its attributes. Display Position Margin Padding Center Box
  6. 2. CSS For Layout ✻✻ Reset: The first step. width

    & height: auto & % Display: “Where the magic happens” Position: How to put the element?? Flex & Grid: OMG It’s so useful!!! or bug!
  7. 2.1. Reset CSS Browsers are enthusiastic, they will put some

    different default CSS style. More You’re welcome~ Like this and this one
  8. 2.1. Reset CSS So we need to reset our CSS

    to keep UI consistency among browsers. We can also use complex reset code: Link Or you can try normalize CSS.
  9. 2.2.3 height: auto & 100% height: auto; (black border) height:

    100%; height: 100% depend upon the height of its parent (parent -> gray border) height: auto; (default) depend upon the height of its children (children -> green border)
  10. 2. CSS For Layout Reset: The first step. width &

    height: auto & % Display: Where the magic happens Position: How to put the element?? Flex & Grid: OMG It’s so useful!!! or bug! v v
  11. 2.3 display - basic value block inline inline-block No wrap

    Must wrap No wrap & we can set width/height inline inline inline inline inline inline inline inllne long long inline-block display: inline- block; \n Expecto.. Partronum!!! \n \n \n \n display: inline- block; Inline- block display: block; block; block black block
  12. 2.3.1 display: block; Default: <div>, <p>, <h1> ~ <h6>, We

    can set width/height, padding, margin, but the next element will start on a new line.
  13. Note: center block - {margin: auto;} We can use "margin:

    auto;” to horizontally center that block element.
  14. 2.3.2 display: inline; Default: <span>, <a>, <img/> “An inline element

    does not start on a new line and only takes up as much width as necessary.” - w3school
  15. Note: inline {text-align: center;} “text-align” set alignment for its child

    elements (or text) (child elements should be {display: inline or inline-block)}.
  16. display: Basic behavior margin padding center block; wrap V V

    margin: auto; inline; No wrap No top & bottom overlay text-align: center; inline-block; No wrap V V text-align: center; %JTQMBZ
  17. Tips 1.2: Debug SOP - Delete Or just remove each

    element until finding the strange one. Press delete(keyboard) Press delete(keyboard)
  18. Brainstorming: Remove the gap - 2 What happened here? (gaps)

    Because those dinosaurs are too brutal!
  19. Note: inline-block __space__ Issue In 2018, we use “display: flex;”

    You may see lots of website use “float: left;” & “clear: both;” to fix it. But that’s a little annoying. Image:
  20. Warn: Do not use table layout, No!!! You may see

    old website(10 years) use <table>,<tr>, <td> as web layout method. But it is not responsive and it can not survive in 2018. Only use <table > in displaying “table” . Image (mostly)
  21. 2. CSS For Layout Reset: The first step. width &

    height: auto & % Display: Where the magic happens Position: How to put the element?? Flex & Grid: OMG It’s so useful!!! or bug! v v v
  22. 2.4 position static relative absolute fixed Default { left: 20px;

    top: 20px; } Scroll to bottom { left: 20px; top: 20px; } { left: 20px; top: 20px; } footer Always here! We can not set left/top
  23. 2.4.2 position: absolute; - (origin) 50px 50px 40px 40px The

    origin point will be locate at upper left corner of its first parent element which is absolute or relative. (e.g.p-rela) <- The origin of cage is here Origin is here ->
  24. 2.4.2 position: absolute - (center) We can use {left:0; right:0;

    margin: auto} to horizontally center the element.
  25. 2. CSS For Layout Reset: The first step. width &

    height: auto & % Display: Where the magic happens Position: How to put the element?? Flex & Grid: OMG It’s so useful!!! or bug! v v v v
  26. Note: CSS - Prefix -webkit- (Chrome, Safari, newer versions of

    Opera, almost all iOS browsers (including Firefox for iOS); basically, any WebKit based browser) -moz- (Firefox) Ref
  27. 2.5.1 Flexbox - center Center a div vertically & horizontally

    Flexbox absolute position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; display: flex; align-items: center; justify-content: center; Old but not obsolete
  28. 2.5.1 Flexbox - future learning • Flexbox Cheatsheet • How

    Flexbox works — explained with big, colorful, animated gifs • A Complete Guide to Flexbox • Just search ‘flexbox’ on Medium!!
  29. 2.5.2 Grid (not bootstrap grid) CSS Grid Layout Image credit:

    Responsive Grid Layout: The past, present and future
  30. 2.5.2 Grid 與 CSS Grid 的第⼀一次接觸 Learn CSS Grid in

    5 Minutes Or Search CSS grid on Medium!!
  31. 2. CSS For Layout Reset: The first step. width &

    height: auto & % Display: Where the magic happens Position: How to put the element?? Flex & Grid: OMG It’s so useful!!! or bug! v v v v v
  32. Exercise 2. Image resize & compress tools We must compress

    & resize our images.(≤1366px) Tiny jpg Resizeimage.net index.html: 10KB An image: 2MB
  33. 3. RWD & Mobile Devices: Lots of devices Attributes: viewport,

    media, vw/vh Event: No hover but swipe Components: Different thinking
  34. 3.1. Devices - (screen width) > 768px > 375px <

    768px > 320px < 375px Width Usually:
  35. 3.2.4 Attributes - vw, vh, vmax, vmin 400px X 300px

    800px X 600px 80px 80px 160px 160px Easily to build full bleed layout!
  36. 3. RWD & Mobile Devices: Lots of devices Attributes: viewport,

    media, vw/vh Event: No hover but swipe Components: Different thinking v v
  37. 3.4. Components - sample UI Hamburger menu, sidebar, scrollable nav

    Image: why-and-how-to-avoid-hamburger-menus/, when-card-ui-design-doesnt-work Tab menu, fab, card list…
  38. 3. RWD & Mobile Devices: Lots of devices Attributes: viewport,

    media, vw/vh Event: No hover but swipe Components: Different thinking v v v v
  39. Exercise 5 - Hamburger menu 分解動作 1. 設定 media query

    當螢幕寬度少於一定值時, 1. 就把選單改成垂直的 2. 再把選單藏起來 2. 加一個☰按鈕,設定螢幕寬度過寬就不顯示 3. 設定點擊那個按鈕之後,把選單顯示出來 (增減class)
  40. Appendix - CSS • Pseudo Elements (::before, ::after) • Web

    Animation Infographics • Maintainable CSS