搜索
制作一个响应式的下拉菜单的方法
P粉521013123
P粉521013123 2023-09-04 15:47:42
[CSS3讨论组]
<p>汉堡菜单图标应该在右侧,图片应该在左侧。</p> <p>当我们点击菜单打开时,它应该展开一个下拉菜单,里面应该有logo home <em>About us</em> gallery contact。</p> <pre class="brush:html;toolbar:false;">&lt;div class=&quot;rectangle1&quot;&gt; &lt;ul class=&quot;dropdown&quot;&gt; &lt;li class=&quot;li1&quot;&gt;&lt;a href=&quot;home.html&quot;&gt;&lt;img class=&quot;img&quot; src=&quot;YOGI Logo.svg&quot; alt=&quot;&quot;&gt; &lt;/a&gt;&lt;/li&gt; &lt;li class=&quot;li2&quot;&gt;&lt;a class=&quot;a1&quot; href=&quot;#home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li class=&quot;li3&quot;&gt;&lt;a class=&quot;a2&quot; href=&quot;aboutus.html&quot;&gt;关于我们&lt;/a&gt;&lt;/li&gt; &lt;li class=&quot;li4&quot;&gt;&lt;a class=&quot;a3&quot; href=&quot;gallery.html&quot;&gt;图库&lt;/a&gt;&lt;/li&gt; &lt;li class=&quot;li5&quot;&gt;&lt;a class=&quot;a4&quot; href=&quot;contact.html&quot;&gt;联系我们&lt;/a&gt;&lt;/li&gt; &lt;a class=&quot;Contact_Us&quot; href=&quot;contact.html&quot;&gt; &lt;img class=&quot;Contact-us-img&quot;src=&quot;Vector.svg&quot; alt=&quot;&quot;&gt; &lt;span class=&quot;Contact-us-text&quot;&gt;联系我们&lt;/span&gt; &lt;/a&gt; &lt;/ul&gt; &lt;/div&gt; </pre> <p>我只需要将其转换为CSS。</p> <p>我需要它适应 iPhone 14 Pro 的响应式设计,以成为一个汉堡菜单。</p>
P粉521013123
P粉521013123

全部回复(2)
P粉748218846

我认为这可能对你有帮助:CSS @media Rule。正如你在教程中所看到的,CSS有一种方法可以检查屏幕的宽度。

@media only screen and (max-width: 600px) {
  body {
    background-color: lightblue;
  }
}

在这个例子中,当宽度小于600像素时,背景颜色将被设置为浅蓝色。使用这个规则,你可以相应地改变你的CSS。

你应该尝试自己实现它,而不仅仅是从互联网上复制粘贴。去尝试,享受使用CSS的乐趣;这是学习它的唯一方式。

P粉738821035

这是修改后的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; /* 在小屏幕上显示菜单图标 */
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板