导航栏菜单中的列表项是否可以有不同的对齐方式?
Is it possible to have different alignments for list items within a navbar menu?
我在 CSS 中完成了响应式折叠导航栏菜单。我正在使用 bulma CSS 框架来构建我的页面。
我试图让徽标永久显示在左侧,菜单列表项水平居中。我会使用相同的技术将徽标左对齐,就像将搜索框右对齐一样。
这是我目前的尝试:
body {
font-family: Helvetica, Arial, sans-serif;
background-color: rgb(236, 236, 236);
margin: 0em;
color: rgb(19, 51, 61);
}
p {
font-size: 18px;
line-height: 1.5;
font-weight: bold;
color: rgb(51, 51, 51);
max-width: 55em;
}
p a {
color: rgb(41, 183, 206);
text-decoration: none;
}
strong {
color: rgb(41, 183, 206);
}
nav {
font-size: 12px;
background-color: rgb(19, 51, 61);
box-shadow: 0 1px 2px rgba(19, 51, 61, 0.5);
padding: 0 1em;
height: 44px;
overflow: hidden;
text-align: center;
}
nav img {
text-align: left;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
max-height: 88px;
position: relative;
}
nav li {
display: inline-block;
}
nav a {
display: inline-block;
padding: 0 1em;
color: rgb(236, 236, 236);
font-weight: 700;
letter-spacing: 0.1em;
text-decoration: none;
text-transform: uppercase;
white-space: nowrap;
line-height: 44px;
height: 44px;
}
nav a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav li:last-child {
position: absolute;
right: 0;
bottom: 44px;
background-image: linear-gradient(to right, rgba(19, 51, 61, 0) 0, rgba(19, 51, 61, 1) 2em);
padding-left: 3em;
}
nav li:nth-last-child(2) {
display: none;
}
nav#menu:target {
height: auto;
padding: 0;
}
nav#menu:target ul {
max-height: none;
}
nav#menu:target li {
display: block;
}
nav#menu:target a {
display: block;
padding: 0 2em;
background-color: rgba(255, 255, 255, 0.05);
}
nav#menu:target a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav#menu:target li:not(:first-child) {
margin-top: 2px;
}
nav#menu:target li:last-child {
display: none;
}
nav#menu:target li:nth-last-child(2) {
display: inline-block;
position: absolute;
top: 0;
right: 0;
margin: 0;
border-left: 2px solid rgb(19, 51, 61);
}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">
</head>
<body>
<nav id="menu">
<ul id="menu-closed">
<li><img src="https://camo.githubusercontent.com/971caf673b2a29c7b6d6af6c74b9d7e649214eb1765bfef0848b68d3ce3a40fb/68747470733a2f2f63646e2e737667706f726e2e636f6d2f6c6f676f732f766974656a732e737667" style="max-height: 44px;"></li>
<li><a href="#">Test1</a></li>
<li><a href="#">Test2</a></li>
<li><a href="#">Test3</a></li>
<li><a href="#">Test4</a></li>
<li><a href="#">Test5</a></li>
<li><a href="#menu-closed">× Close menu</a></li>
<li><a href="#menu">☰ Menu</a></li>
</ul>
</nav>
我已经尝试了各种方法,以及一些布尔玛斯定位 类,但无论我做什么,我似乎都无法让它保持在左侧。
这里正确的方法是什么?
额外的问题:如果删除了图像,如何保持列表元素垂直居中?
将您的徽标设置为左侧的绝对位置,并在左侧和右侧的菜单列表中添加等效的相同填充。
body {
font-family: Helvetica, Arial, sans-serif;
background-color: rgb(236, 236, 236);
margin: 0em;
color: rgb(19, 51, 61);
}
p {
font-size: 18px;
line-height: 1.5;
font-weight: bold;
color: rgb(51, 51, 51);
max-width: 55em;
}
p a {
color: rgb(41, 183, 206);
text-decoration: none;
}
strong {
color: rgb(41, 183, 206);
}
nav {
font-size: 12px;
background-color: rgb(19, 51, 61);
box-shadow: 0 1px 2px rgba(19, 51, 61, 0.5);
padding: 0 1em;
height: 44px;
overflow: hidden;
text-align: center;
}
nav img {
text-align: left;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
max-height: 88px;
position: relative;
}
nav li {
display: inline-block;
}
nav a {
display: inline-block;
padding: 0 1em;
color: rgb(236, 236, 236);
font-weight: 700;
letter-spacing: 0.1em;
text-decoration: none;
text-transform: uppercase;
white-space: nowrap;
line-height: 44px;
height: 44px;
}
nav a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav li:last-child {
position: absolute;
right: 0;
bottom: 44px;
background-image: linear-gradient(to right, rgba(19, 51, 61, 0) 0, rgba(19, 51, 61, 1) 2em);
padding-left: 3em;
}
nav li:nth-last-child(2) {
display: none;
}
nav#menu:target {
height: auto;
padding: 0;
}
nav#menu:target ul {
max-height: none;
}
nav#menu:target li {
display: block;
}
nav#menu:target a {
display: block;
padding: 0 2em;
background-color: rgba(255, 255, 255, 0.05);
}
nav#menu:target a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav#menu:target li:not(:first-child) {
margin-top: 2px;
}
nav#menu:target li:last-child {
display: none;
}
nav#menu:target li:nth-last-child(2) {
display: inline-block;
position: absolute;
top: 0;
right: 0;
margin: 0;
border-left: 2px solid rgb(19, 51, 61);
}
nav#menu > ul {
padding-right: 50px;
padding-left: 50px;
}
.logo {
position: absolute;
left: 0;
}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">
</head>
<body>
<nav id="menu">
<ul id="menu-closed">
<li class="logo"><img src="https://camo.githubusercontent.com/971caf673b2a29c7b6d6af6c74b9d7e649214eb1765bfef0848b68d3ce3a40fb/68747470733a2f2f63646e2e737667706f726e2e636f6d2f6c6f676f732f766974656a732e737667" style="max-height: 44px;"></li>
<li><a href="#">Test1</a></li>
<li><a href="#">Test2</a></li>
<li><a href="#">Test3</a></li>
<li><a href="#">Test4</a></li>
<li><a href="#">Test5</a></li>
<li><a href="#menu-closed">× Close menu</a></li>
<li><a href="#menu">☰ Menu</a></li>
</ul>
</nav>
我在 CSS 中完成了响应式折叠导航栏菜单。我正在使用 bulma CSS 框架来构建我的页面。
我试图让徽标永久显示在左侧,菜单列表项水平居中。我会使用相同的技术将徽标左对齐,就像将搜索框右对齐一样。
这是我目前的尝试:
body {
font-family: Helvetica, Arial, sans-serif;
background-color: rgb(236, 236, 236);
margin: 0em;
color: rgb(19, 51, 61);
}
p {
font-size: 18px;
line-height: 1.5;
font-weight: bold;
color: rgb(51, 51, 51);
max-width: 55em;
}
p a {
color: rgb(41, 183, 206);
text-decoration: none;
}
strong {
color: rgb(41, 183, 206);
}
nav {
font-size: 12px;
background-color: rgb(19, 51, 61);
box-shadow: 0 1px 2px rgba(19, 51, 61, 0.5);
padding: 0 1em;
height: 44px;
overflow: hidden;
text-align: center;
}
nav img {
text-align: left;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
max-height: 88px;
position: relative;
}
nav li {
display: inline-block;
}
nav a {
display: inline-block;
padding: 0 1em;
color: rgb(236, 236, 236);
font-weight: 700;
letter-spacing: 0.1em;
text-decoration: none;
text-transform: uppercase;
white-space: nowrap;
line-height: 44px;
height: 44px;
}
nav a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav li:last-child {
position: absolute;
right: 0;
bottom: 44px;
background-image: linear-gradient(to right, rgba(19, 51, 61, 0) 0, rgba(19, 51, 61, 1) 2em);
padding-left: 3em;
}
nav li:nth-last-child(2) {
display: none;
}
nav#menu:target {
height: auto;
padding: 0;
}
nav#menu:target ul {
max-height: none;
}
nav#menu:target li {
display: block;
}
nav#menu:target a {
display: block;
padding: 0 2em;
background-color: rgba(255, 255, 255, 0.05);
}
nav#menu:target a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav#menu:target li:not(:first-child) {
margin-top: 2px;
}
nav#menu:target li:last-child {
display: none;
}
nav#menu:target li:nth-last-child(2) {
display: inline-block;
position: absolute;
top: 0;
right: 0;
margin: 0;
border-left: 2px solid rgb(19, 51, 61);
}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">
</head>
<body>
<nav id="menu">
<ul id="menu-closed">
<li><img src="https://camo.githubusercontent.com/971caf673b2a29c7b6d6af6c74b9d7e649214eb1765bfef0848b68d3ce3a40fb/68747470733a2f2f63646e2e737667706f726e2e636f6d2f6c6f676f732f766974656a732e737667" style="max-height: 44px;"></li>
<li><a href="#">Test1</a></li>
<li><a href="#">Test2</a></li>
<li><a href="#">Test3</a></li>
<li><a href="#">Test4</a></li>
<li><a href="#">Test5</a></li>
<li><a href="#menu-closed">× Close menu</a></li>
<li><a href="#menu">☰ Menu</a></li>
</ul>
</nav>
我已经尝试了各种方法,以及一些布尔玛斯定位 类,但无论我做什么,我似乎都无法让它保持在左侧。
这里正确的方法是什么?
额外的问题:如果删除了图像,如何保持列表元素垂直居中?
将您的徽标设置为左侧的绝对位置,并在左侧和右侧的菜单列表中添加等效的相同填充。
body {
font-family: Helvetica, Arial, sans-serif;
background-color: rgb(236, 236, 236);
margin: 0em;
color: rgb(19, 51, 61);
}
p {
font-size: 18px;
line-height: 1.5;
font-weight: bold;
color: rgb(51, 51, 51);
max-width: 55em;
}
p a {
color: rgb(41, 183, 206);
text-decoration: none;
}
strong {
color: rgb(41, 183, 206);
}
nav {
font-size: 12px;
background-color: rgb(19, 51, 61);
box-shadow: 0 1px 2px rgba(19, 51, 61, 0.5);
padding: 0 1em;
height: 44px;
overflow: hidden;
text-align: center;
}
nav img {
text-align: left;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
max-height: 88px;
position: relative;
}
nav li {
display: inline-block;
}
nav a {
display: inline-block;
padding: 0 1em;
color: rgb(236, 236, 236);
font-weight: 700;
letter-spacing: 0.1em;
text-decoration: none;
text-transform: uppercase;
white-space: nowrap;
line-height: 44px;
height: 44px;
}
nav a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav li:last-child {
position: absolute;
right: 0;
bottom: 44px;
background-image: linear-gradient(to right, rgba(19, 51, 61, 0) 0, rgba(19, 51, 61, 1) 2em);
padding-left: 3em;
}
nav li:nth-last-child(2) {
display: none;
}
nav#menu:target {
height: auto;
padding: 0;
}
nav#menu:target ul {
max-height: none;
}
nav#menu:target li {
display: block;
}
nav#menu:target a {
display: block;
padding: 0 2em;
background-color: rgba(255, 255, 255, 0.05);
}
nav#menu:target a:hover {
background-color: rgba(255, 255, 255, 0.08);
}
nav#menu:target li:not(:first-child) {
margin-top: 2px;
}
nav#menu:target li:last-child {
display: none;
}
nav#menu:target li:nth-last-child(2) {
display: inline-block;
position: absolute;
top: 0;
right: 0;
margin: 0;
border-left: 2px solid rgb(19, 51, 61);
}
nav#menu > ul {
padding-right: 50px;
padding-left: 50px;
}
.logo {
position: absolute;
left: 0;
}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">
</head>
<body>
<nav id="menu">
<ul id="menu-closed">
<li class="logo"><img src="https://camo.githubusercontent.com/971caf673b2a29c7b6d6af6c74b9d7e649214eb1765bfef0848b68d3ce3a40fb/68747470733a2f2f63646e2e737667706f726e2e636f6d2f6c6f676f732f766974656a732e737667" style="max-height: 44px;"></li>
<li><a href="#">Test1</a></li>
<li><a href="#">Test2</a></li>
<li><a href="#">Test3</a></li>
<li><a href="#">Test4</a></li>
<li><a href="#">Test5</a></li>
<li><a href="#menu-closed">× Close menu</a></li>
<li><a href="#menu">☰ Menu</a></li>
</ul>
</nav>