如何在不改变边框高度的情况下居中文字

时间:2017-08-07 13:27:05

标签: html css flexbox height border

body {
  background-color: #660033;
  padding: 0;
  margin: 0;
}

nav {
  background-color: #FFF033;
  height: 100px;
  display: flex;
}

ul {
  margin: auto;
  display: flex;
  margin: 0px;
  padding: 0px;
  list-style-type: none;
  width: 100%;
}

ul>li {
  height: 100%;
  margin: auto;
  text-align: center;
  flex: 1;
  border-right: 2px dashed #660066;
}

ul>li:first-child {
  border-left: 2px dashed #660066;
}
<meta http-equiv="X-UA-COMPATIBLE" content="IE=edge.chrome=1" />
<link rel="stylesheet" href="style.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=MedievalSharp" rel="stylesheet">

<header>
  <nav>
    <ul>
      <li><a href="#">Games</a></li>
      <li><a href="#">Music</a></li>
      <li><a href="#">Films</a></li>
      <li><a href="#">Photos</a></li>
    </ul>
  </nav>
  <header>

如何在不更改右边框高度的情况下将文本置于ul列表中? 当我尝试删除高度:100%时,右边框的高度也发生了变化,为什么我改变了

flex:1;
例如

flex:2;

Nothig正在发生。有什么问题?

2 个答案:

答案 0 :(得分:1)

查看此Fiddle! 我更改了显示属性,我不知道它对你是否合适。我甚至不知道我是否回答你的问题,我想你想要实现垂直对齐:中间,但你没有指定它!希望我帮忙。

HTML:

<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=MedievalSharp" rel="stylesheet">

<header>
  <nav>
    <ul>
      <li><a href="#">Games</a></li>
      <li><a href="#">Music</a></li>
      <li><a href="#">Films</a></li>
      <li><a href="#">Photos</a></li>
    </ul>
  </nav>
  <header>

CSS:

body {
  background-color: #660033;
  padding: 0;
  margin: 0;
}

nav {
  background-color: #FFF033;
  height: 100px;
  display: flex;
}

ul {
  margin: auto;
  margin: 0px;
  padding: 0px;
  list-style-type: none;
  width: 100%;
  display:table;
  height:100%;
}

ul>li {
  height: 100%;
  text-align: center;
  border-right: 2px dashed #660066;
  display:table-cell;
  vertical-align:middle;
}

ul>li:first-child {
  border-left: 2px dashed #660066;
}

答案 1 :(得分:0)

要使文本居中,您可以使用<?php $message = $_POST["email"]; echo $message; ?> 将您的Flex项目变为嵌套的Flex容器并添加display: flex,我们也可以将align-items: center替换为text-align: center

演示:

&#13;
&#13;
justify-content: center
&#13;
body {
  background-color: #660033;
  padding: 0;
  margin: 0;
}

nav {
  background-color: #FFF033;
  height: 100px;
  display: flex;
}

ul {
  margin: auto;
  display: flex;
  margin: 0px;
  padding: 0px;
  list-style-type: none;
  width: 100%;
}

ul > li {
  height: 100%;
  margin: auto;
  flex: 1;
  border-right: 2px dashed #660066;
  display: flex; /* new */
  align-items: center; /* new */
  justify-content: center; /* new */
}

ul > li:first-child {
  border-left: 2px dashed #660066;
}
&#13;
&#13;
&#13;