我認(rèn)為這可能對你有幫助:CSS @media Rule。正如你在教程中所看到的,CSS有一種方法可以檢查螢?zāi)坏膶挾取?
@media only screen and (max-width: 600px) { body { background-color: lightblue; } }
在這個(gè)例子中,當(dāng)寬度小於600像素時(shí),背景顏色將被設(shè)定為淺藍(lán)色。使用這個(gè)規(guī)則,你可以相應(yīng)地改變你的CSS。
你應(yīng)該嘗試自己實(shí)現(xiàn)它,而不僅僅是從網(wǎng)路上複製貼上。去嘗試,享受使用CSS的樂趣;這是學(xué)習(xí)它的唯一方法。
這是修改後的CSS程式碼,用於實(shí)現(xiàn)響應(yīng)式漢堡選單:
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; /* 在較大屏幕上默認(rèn)隱藏菜單圖標(biāo) */ } .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; } /* 移動(dòng)設(shè)備的媒體查詢 */ @media only screen and (max-width: 767px) { .menu { display: none; /* 在小屏幕上默認(rèn)隱藏菜單 */ flex-direction: column; background-color: #333; position: absolute; top: 50px; right: 0; width: 100%; } .menu.active { display: flex; /* 激活時(shí)顯示菜單 */ } .menu li { width: 100%; text-align: center; } .menu-toggle { display: block; /* 在小屏幕上顯示菜單圖標(biāo) */ } }