วิธีทำ Css sticky footer [It's work]

อืมม์ เริ่มมั่วไปทุกทีละ jQueryTips มี Css โผล่มาด้วย 555+

หลังจากที่ผมหาวิธีตั้งนานที่จะทำให้ footer มันอยู่ติด ข้างล่าง โดยที่ ยังยิดตาม content แล้วก็ใช้ได้ ครบทุก browser ลองมาหลายๆ วิธี แม้กระทั้ง เขียน js เพิ่มคำนวน scrolling ก็ยังเคยมาแล้ว (มันกระตุก)

จนในวันนี้ได้มา วิธีที่ work ที่สุด แถมได้ทุก browser ไม่เพี้ยน ยืดขยาย ได้ตามแต่ content
(ทดสอบใน: IE6+7, FF, Win Safari, Opera)

มาลองดูกัน โคด ไม่ยากเลย

Css

  1. <style type="text/css">
  2.         html, body {
  3.                 margin: 0 auto;
  4.                 height: 100%;
  5.         }
  6.         #container {
  7.                 min-height: 100%;
  8.                 height: auto !important;
  9.                 height: 100%;
  10.                 margin: 0 auto -100px;
  11.                 background-color: #DBDBDB;
  12.         }
  13.         #footer, #push {
  14.                 height: 100px;
  15.         }
  16.         #footer {
  17.                 background-color: red;
  18.         }
  19. </style>

HTML

  1. <body>
  2.  
  3. <div id="container">
  4.         Header and content
  5.  
  6.         <div id="push"></div>
  7. </div>
  8.  
  9. <div id="footer">Footer</div>
  10. </body>

เท่่านี้เองง่ายๆ หัวใจสำคัญมันอยู่ที่ push ที่คอยดัน footer ตามขนาด margin ลองดูละกัน สำหรับผม มันยอดมาก !

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

8 Comments »

 
  • EThaiZone says:

    ใช้ได้จริงๆ ด้วย เคยนั่งทำไปทำมาเหมือนกัน แล้วก็ทำไม่ได้ = =”
    เดี๋ยวเอาไปลองแก้กับงานเก่าดู อุๆ

  • Zheiferz says:

    เป็นแนวคิดที่เรียบง่ายแต่ได้ประโยชน์มากเลยคับ อุอุ

  • mescript says:

    มี code น่าสนใสมาเผยแพร่เสมอเลยนะครับ สุดยอด

  • platoosom says:

    โอ ใช้ margin auto -100 เป็นตัวช่วย ยอดเยี่ยมครับๆ

  • Peglui says:

    เยี่ยมเลย ขอเก็บไปเป็นความรู้นะครับผม ^^

  • go2jame says:

    ติดตาม

    ทำให้สมองมีรอยหยักเพิ่มอีกแล้ว

  • nonceal says:

    สูตรนี้ ใช้ได้แค่ level แรกชั้นนอกสุดนะครับ ถ้าต้องการทำ layout ที่ต้องมี height 100% ข้างในอีกจะใช้ไม่ได้ครับ

  • css says:

    ลองแบบนี้ดูนะครับ

    br {clear:both;}

    Header and content

    Footer

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>