position:fixed.position: relative thì nó sẽ lấy thẻ cha đó chứ không lấy của sổ trình duyệt nữa. Tuy nhiên nếu bạn khai báo một trong bốn thuộc tính sau: top - left- bottom - right thì lúc này nó sẽ không lấy thẻ cha đó nữa mà nó sẽ lấy cửa sổ trình duyệt làm chuẩn.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> body{ height: 2000px; background: blue; } .fixed{ height: 30px; line-height: 30px; background: red; color: #fff; position: fixed; width: 100%; top: 0px; } .bottom{ height: 400px; margin-top: 600px; background: yellow; } </style> </head> <body> <div class="fixed"> Chào mừng bạn đến với freetuts.net </div> <div class="bottom"> </div> </body></html> |
position:relative làm chuẩn.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> body{ height: 2000px; background: blue; } .relative{ margin-top: 200px; position: relative; height: 100px; } .fixed{ height: 30px; line-height: 30px; background: red; color: #fff; position: fixed; width: 100%; } .bottom{ height: 400px; margin-top: 600px; background: yellow; } </style> </head> <body> <div class="relative"> <div class="fixed"> Chào mừng bạn đến với freetuts.net </div> </div> <div class="bottom"> </div> </body></html> |
top:0px nên nó sẽ lấy cửa sổ trình duyệt làm chuẩn.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> body{ height: 2000px; background: blue; } .relative{ margin-top: 200px; position: relative; height: 100px; } .fixed{ height: 30px; line-height: 30px; background: red; color: #fff; position: fixed; width: 100%; top: 0px; } .bottom{ height: 400px; margin-top: 600px; background: yellow; } </style> </head> <body> <div class="relative"> <div class="fixed"> Chào mừng bạn đến với freetuts.net </div> </div> <div class="bottom"> </div> </body></html> |
position:fixed cho phần quảng cáo Google Adsense. Tại đó vì mình không có khai báo một trong bốn thuộc tính (top - left - bottom - right) nên nó Fixed theo thẻ cha. Hơi rắc rối nhưng sau này bạn sẽ kết hợp Fixed với Javascript để xây dựng các hiệu ứng trên website.Hãy để lại link bài viết gốc khi chia sẻ bài viết này, mình