自己构建 - 标题
由于你的网站将在不同的设备上查看,因此是时候创建一个可以响应多种屏幕尺寸的页面导航了!
¥Since your site will be viewed on different devices, it’s time to create a page navigation that can respond to multiple screen sizes!
Get ready to…
-
为你的网站创建一个包含导航组件的标题
-
使导航组件具有响应能力
自己尝试一下 - 构建新的 Header 组件
Section titled 自己尝试一下 - 构建新的 Header 组件¥Try it yourself - Build a new Header component
-
Create a new Header component. Import and use your existing
Navigation.astro
component inside a<nav>
element which is inside a<header>
element.Show me the code!
Create a file named
Header.astro
insrc/components/
自己尝试一下 - 更新你的页面
Section titled 自己尝试一下 - 更新你的页面¥Try it yourself - Update your pages
-
On each page, replace your existing
<Navigation/>
component with your new header.Show me the code!
-
Check your browser preview and verify that your header is displayed on every page. It won’t look different yet, but if you inspect your preview using dev tools, you will see that you now have elements like
<header>
and<nav>
around your navigation links.
添加响应式样式
Section titled 添加响应式样式¥Add responsive styles
-
Update
Navigation.astro
with the CSS class to control your navigation links. Wrap the existing navigation links in a<div>
with the classnav-links
. -
Copy the CSS styles below into
global.css
. These styles:- Style and position the navigation links for mobile
- Include an
expanded
class that can be toggled to display or hide the links on mobile - Use a
@media
query to define different styles for larger screen sizes
调整窗口大小并寻找在不同屏幕宽度上应用的不同样式。你的标题现在通过使用 @media
查询来响应屏幕尺寸。
¥Resize your window and look for different styles being applied at different screen widths. Your header is now responsive to screen size through the use of @media
queries.
¥Checklist
¥Resources
-
基于组件的设计 external
-
语义 HTML 标签 external
-
移动优先设计 external