【问题标题】:Equal space to the left and right of text in all navigation menu items所有导航菜单项中文本的左右间距相等
【发布时间】:2017-05-04 23:49:03
【问题描述】:

我正在使用 flexbox 创建导航菜单,我想确保每个单词左右两侧的文本完全一致。

现在,框的响应非常均匀,但是当单词的字符与其他字符不匹配时,单词周围的空间不再均匀。

这是我的小提琴:https://jsfiddle.net/omarel/p204jjnr/2/

header {
  display: flex;
  position: fixed;
  bottom: 0px;
  width: 100%;
  background-color: #000;
  padding: 50px;
}
header .tab {
  color: #fff;
  width: 16.66%;
  height: 70px;
  border: 1px solid red;
  text-align: center;
  font-size: 2.4vw;
  vertical-align: middle;
  line-height: 70px;
}
/* BORDER BOXING  */

header,
header .tab {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
<header>
  <div class="tab">home</div>
  <div class="tab">hello</div>
  <div class="tab">longer word</div>
  <div class="tab">short</div>
  <div class="tab">Neighborhood</div>
  <div class="tab">Floor Plans</div>
  <div class="tab">Views</div>
</header>

更新: 解决方案是以下答案的组合: 添加 flex:auto 和从 header .tab 中删除宽度

【问题讨论】:

  • 我不确定我是否理解。如果选项卡都设置为相同的宽度,并且某些选项卡的文本比其他选项卡多,那么文本周围的空间总会有一些差异......不是吗?

标签: html css sass flexbox


【解决方案1】:

对于 flexbox,您可以使用 flex 属性来指定元素的长度。查看 flex-basis、flex-shrink 和 flex-grow。删除宽度并将您的 flex 设置为自动;

header .tab{
    color: #fff;
    flex: auto;
    height: 70px;
    border: 1px solid red;
    text-align: center;
    font-size: 2.4vw;
    vertical-align: middle;
    line-height: 70px;
}

【讨论】:

  • 我将您的解决方案与删除宽度相结合,并且它有效,因此它是您的答案和删除宽度的组合。非常感谢!
  • 我说在我的答案中删除宽度;),但很高兴它成功了!
【解决方案2】:

而不是为每个项目设置width

width: 16.66%

让宽度为自动(基于内容)并使用padding

width: 16.66% <-- REMOVE
padding: 0 20px

现在,文本左右的空间在所有选项卡中都是一致的。

这是您的代码的简化版本:

header {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  background-color: #000;
  padding: 50px;
}
header .tab {
  padding: 0 20px;
  height: 70px;
  font-size: 2.4vw;
  color: #fff;
  border: 1px solid red;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
<header>
  <div class="tab">home</div>
  <div class="tab">hello</div>
  <div class="tab">longer word</div>
  <div class="tab">short</div>
  <div class="tab">Neighborhood</div>
  <div class="tab">Floor Plans</div>
  <div class="tab">Views</div>
</header>

jsFiddle

【讨论】:

  • 是的!!!做到了,但它只与下面关于添加 flex 的答案一起使用:auto
  • 答案应该可以正常工作。不确定flex: auto 添加了什么。检查我包含的演示。
猜你喜欢
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 2015-01-30
相关资源
最近更新 更多