html - 在屏幕中心居中导航栏标题
问题描述
我试图将导航栏的标题放在屏幕中间。有没有办法在没有自定义边距/填充值的情况下做到这一点(试图保持响应)?在下图中,我想在屏幕中间居中显示“标题文本在此处”。
<header>
<nav class="nav">
<img class="nav-logo" src="./jumbotron.jpg" alt="logo">
<h1 class="nav-title">Title text goes here</h1>
<ul class="nav-links">
<li class="nav-link"><a href="https://www.google.com">nav link 1</a></li>
<li class="nav-link"><a href="https://www.google.com">nav link 2</a></li>
<li class="nav-link"><a href="https://www.google.com">nav link 3</a></li>
</ul>
</nav>
</header>
/*****************************************************************************/
/* Navbar */
/*****************************************************************************/
.nav {
display: flex;
flex-direction: row;
justify-content: center;
background-color: white;
}
.nav-logo {
width: 10%;
}
.nav-title {
flex: 1;
margin: auto 0;
text-align: center;
font-size: 30px;
}
.nav-links {
flex: 0.5;
display: flex;
width: 100%;
padding: 20px;
justify-content: flex-end;
list-style: none;
}
解决方案
您可以使用 flexbox 来实现这一点。通过在标题和标题上设置align-items
为,标题将扩展以覆盖徽标和链接未占用的任何空间。然后,将您的标题文本居中:stretch
flex-grow
1
text-align:center
.nav {
display: flex;
flex-direction: row;
align-items: stretch
background-color: white;
}
.nav-logo {
width: 10%;
}
.nav-title {
flex-grow: 1;
margin: auto 0;
text-align: center;
font-size: 30px;
text-align: center;
}
.nav-links {
display: flex;
width: 100%;
padding: 20px;
justify-content: flex-end;
list-style: none;
}
推荐阅读
- email - Phpmailer 使用自己的域
- python - 从 1919 年到 1930 年按国家/地区的最大数量排序前 2 个国家
- c++ - 查找最大乘积子数组
- node.js - AWS Elasticsearch Service 抛出的“没有可用于处理请求的服务器”
- python - 使用共享 yaxes 但“链接”xaxes 进行绘图
- python - 如何将集合中的相同单词组合成一个单词
- amazon-web-services - 通过过滤将细粒度消息从 Amazon SNS 路由到 SQS
- database - 如何解决访问中的延迟按键问题
- python - Python 中的 C++ 扩展“DLL 加载失败”
- django - 如何在 Django 中应用 SQL 查询?