【问题标题】:Make element dividing lines equal height with container使元素分割线与容器等高
【发布时间】:2022-01-21 03:44:38
【问题描述】:

我正在尝试做一个简单的导航栏,但边框的大小与文本的大小匹配,而不是其容器的高度。我试图在自己的“矩形”内“关闭”每个文本。我如何做到这一点?

.flex-r {
    display: flex;
    justify-content: space-around;
    justify-items: center;
    flex-wrap: nowrap;
    gap: 1em;
    align-items: center;
    border: 1px solid black;
    min-height: 40px;
}

.nav-item {
    border-right: 1px solid black;
    width: auto;
}
 <div class='flex-r'><a class='nav-item'>Release</a><a class='nav-item'>Statistics</a><a class='nav-item'>Frequent releases</a></div>

【问题讨论】:

  • 您能具体说明您的问题吗? “边框的大小”是什么意思,“‘关闭’每个文本在它自己的‘矩形’内”是什么意思?

标签: html css flexbox navigation navbar


【解决方案1】:

您已将容器设置为 min-height: 40px

将相同的代码添加到项目中。

但这里有一个更好的整体解决方案,可能对您有用:

.flex-r {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;                /* sets the dividing line width */
  background-color: black; /* sets the dividing line color */
  border: 1px solid black; /* sets the border around the container */
  min-height: 40px;
}

.nav-item {
  background-color: white; /* restores background color */
  
  /* center the content */
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
<div class='flex-r'>
   <a class='nav-item'>Release</a>
   <a class='nav-item'>Statistics</a>
   <a class='nav-item'>Frequent releases</a>
 </div>

【讨论】:

  • 感谢您的编辑和解决方案。它有效,但是您在第一行中建议的内容,我之前也尝试过,它使文本转到 div 的顶部,这是我不想要的。
  • 对于该问题(在您的原始代码中),使用我提供的flex 居中解决方案。
猜你喜欢
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 2016-04-20
  • 2021-11-09
  • 2011-10-05
  • 1970-01-01
相关资源
最近更新 更多