P粉7482188462023-09-05 09:49:42
我認為這可能對你有幫助:CSS @media Rule。正如你在教程中所看到的,CSS有一種方法可以檢查螢幕的寬度。
@media only screen and (max-width: 600px) { body { background-color: lightblue; } }
在這個例子中,當寬度小於600像素時,背景顏色將被設定為淺藍色。使用這個規則,你可以相應地改變你的CSS。
你應該嘗試自己實現它,而不僅僅是從網路上複製貼上。去嘗試,享受使用CSS的樂趣;這是學習它的唯一方法。
P粉7388210352023-09-05 00:41:11
這是修改後的CSS程式碼,用於實現響應式漢堡選單:
CSS(styles.css):
body { margin: 0; font-family: Arial, sans-serif; } header { background-color: #333; color: #fff; padding: 10px; display: flex; align-items: center; } .container { display: flex; justify-content: space-between; align-items: center; } .logo { color: #fff; text-decoration: none; font-size: 24px; } .menu-toggle { width: 30px; height: 30px; background-color: #fff; cursor: pointer; display: none; /* 在较大屏幕上默认隐藏菜单图标 */ } .menu-toggle::before, .menu-toggle::after { content: ""; display: block; width: 100%; height: 3px; background-color: #333; } .menu { display: flex; align-items: center; } .menu ul { list-style: none; margin: 0; padding: 0; display: flex; } .menu li { padding: 10px; } .menu a { color: #fff; text-decoration: none; font-size: 18px; } /* 移动设备的媒体查询 */ @media only screen and (max-width: 767px) { .menu { display: none; /* 在小屏幕上默认隐藏菜单 */ flex-direction: column; background-color: #333; position: absolute; top: 50px; right: 0; width: 100%; } .menu.active { display: flex; /* 激活时显示菜单 */ } .menu li { width: 100%; text-align: center; } .menu-toggle { display: block; /* 在小屏幕上显示菜单图标 */ } }