W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html lang="en"> <head> <title>CSS 网站布局</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * { box-sizing: border-box; } body { margin: 0; } /* 设置页眉的样式 */ .header { background-color: #f1f1f1; padding: 20px; text-align: center; } /* 设置顶部导航栏的样式 */ .topnav { overflow: hidden; background-color: #333; } /* 设置 topnav 链接的样式 */ .topnav a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; } /* 改变鼠标悬停时的颜色 */ .topnav a:hover { background-color: #ddd; color: black; } /* 创建并排的三个不相等的列 */ .column { float: left; padding: 10px; } /* 左右列 */ .column.side { width: 25%; } /* 中间列 */ .column.middle { width: 50%; } /* 清除列之后的浮动 */ .row:after { content: ""; display: table; clear: both; } /* 响应式布局 - 创建堆叠而非并排的三列 */ @media screen and (max-width: 600px) { .column.side, .column.middle { width: 100%; } } </style> </head> <body> <div class="header"> <h1>Header</h1> <p>请调整浏览器窗口的大小以查看响应效果。</p> </div> <div class="topnav"> <a href="#">Link</a> <a href="#">Link</a> <a href="#">Link</a> </div> <div class="row"> <div class="column side"> <h2>Side</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit..</p> </div> <div class="column middle"> <h2>Main Content</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque vehicula, risus eget aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit. Praesent scelerisque tortor sed accumsan convallis.</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque vehicula, risus eget aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit. Praesent scelerisque tortor sed accumsan convallis.</p> </div> <div class="column side"> <h2>Side</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit..</p> </div> </div> </body> </html>