如何在表格标题内垂直居中对齐图标

时间:2019-03-11 09:05:14

标签: html css css3

我有一个动态表,其中没有动态的列和行。在表头中,我需要固定排序图标垂直居中。即使表头的文本大小是动态的,行数也没有增长。在文本大小保持增加的情况下垂直对齐排序图标?

jsfiddle

<table style="width:100%">
  <tr>
    <th><div class='filter-container header-container'> <div class='header-text'>  Firstname Firstname Firstnam eFirstnam eFirstname Firstname Firstname Firstname Firstname</div><div class='icon-right'> <span    class= "fa fa-sort-asc fa-lg sort-icon "></span><span   class= "fa fa-sort-desc fa-lg sort-icon  "></span></div></div></th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>80</td>
  </tr>
</table>

CSS:

table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}

th, td {
  padding: 5px;
}

.filter-container {
  width: 100%;
  height: 100%;
  position: relative;
  padding: 0px !important;
  margin: 0px !important;
}

.header-container {
  height: 100%;
  vertical-align: middle;
  text-align: center;
}

.header-text {
  font-size: 22px;
  line-height: 24px;
  color: #000000;
  font-weight: normal;
  width: 90%;
  float: left;
  letter-spacing: 0;
  text-align: left;
}

.sort-icon {
  float: right !important;
  clear: right;
  height: 1px !important;
}

4 个答案:

答案 0 :(得分:3)

使用整个表头单元格内容的包装器。在其中使用两个元素。文字和图标:

<th>
  <div class="aligner">
    <div class="text">any text here can be as long as you want</div>
    <div class="icon">your icon here</div>
  </div>
</th>

并使用:

th .aligner {
  display: flex;
  align-items: center;
}

显然,两个对齐的元素都必须具有相等的垂直paddingbordermargin s。


使用刚刚添加的示例,这是一个入门示例:https://jsfiddle.net/websiter/h94yen82/

我删除了排序器的样式,并添加了:

.header-container {
  justify-content: space-between;
}
.sort-icon {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 0;
}

答案 1 :(得分:0)

怎么样?

.header-container {
  height: 100%;
  vertical-align: middle;
  text-align: center;
  overflow: hidden;
}

.icon-right {
  position: absolute;
  top: 50%;
  right: 0;
  margin-top:-10px;
}

https://jsfiddle.net/56y4wmkz/

答案 2 :(得分:0)

尝试

 .header-container {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

答案 3 :(得分:0)

尝试这些属性,我认为这对您很有用。

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.icon-right {
  margin-top: -15px;
}

table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
th, td {
  padding: 5px;
}

.filter-container {
	width: 100%;
	height: 100%;
	position: relative;
	padding: 0px !important;
	margin: 0px !important;
}
.header-container {
	height: 100%;
	vertical-align: middle;
	text-align: center;
}

.header-text {
    font-size: 22px;
    line-height: 24px;
}
.header-text {
    color: #000000;
    font-weight: normal;
}

.header-text {
	width: 90%;
	float: left;
	text-align: start;
	letter-spacing: 0;
	text-align: left;
}
.sort-icon {
	float: right !important;
	clear: right;
  height: 1px !important;
}

 .header-container {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

.icon-right {
    margin-top: -15px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<table style="width:100%">
  <tr>
    <th><div class='filter-container header-container'> <div class='header-text'>  Firstname Firstname Firstnam eFirstnam eFirstname Firstname Firstname Firstname Firstname</div>
      <div class='icon-right'> <span    class= "fa fa-sort-asc fa-lg sort-icon "></span><span   class= "fa fa-sort-desc fa-lg sort-icon  "></span></div></div></th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>80</td>
  </tr>
</table>

这是我的密码笔链接

https://codepen.io/arshiyakhanam_786/pen/JzyvKe

相关问题