简体   繁体   English

如何垂直居中对齐表格内的图标 header

[英]How to vertically center align icon inside table header

I have a dynamic table, with dynamic no of column and rows... In table header, I need to fix sort icon vertically center.. Even table header text size is dynamic and no of lines keeps growing... how to vertically align sort icon while text size keeps increases?我有一个动态表,列和行的动态编号......在表 header 中,我需要修复排序图标垂直居中......即使表 header 文本大小是动态的并且行数不断增长......如何垂直对齐在文本大小不断增加的同时排序图标?

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: 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;
}

Use a wrapper of the entire table header cell contents. 使用整个表头单元格内容的包装器。 Inside it, use two elements. 在其中,使用两个元素。 The text and the icon: 文字和图标:

<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>

and use: 并使用:

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

Obviously, both aligned elements need to have equal vertical padding , border and margin s. 显然,两个对齐的元素都需要具有相等的垂直paddingbordermargin


Using the example you just added, here's a starter: https://jsfiddle.net/websiter/h94yen82/ 使用您刚刚添加的示例,这是一个启动器: https//jsfiddle.net/websiter/h94yen82/

I removed your styles for the sorter and added: 我删除了分拣机的样式并添加了:

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

Quick/simply fix, add this to .icon-right : 快速/简单修复,将其添加到.icon-right

.icon-right {
    ...
    top: calc(50% - 8px);
    position: relative;
}

Edit 编辑

Since you want to apply this to all columns, try this: 由于您要将其应用于所有列,请尝试以下操作:

.icon-right {/* Add this new class */
    top: calc(50% - 8px);
    position: absolute;
    right: 5px;
}

th {/* Add this new class */
    position: relative;
}

.filter-container {
    ...
    /*position: relative;*/ /* remove this line */
    ...
}

Try it jsfiddle . 试试jsfiddle

How about this? 这个怎么样?

.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/ https://jsfiddle.net/56y4wmkz/

try this 试试这个

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

Try These properties I think it's useful for you. 试试这些属性我觉得它对你有用。

.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> 

this is my code pen link 这是我的代码笔链接

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

you can use transform to make div vertically align 你可以使用transform来使div垂直对齐

.icon-right{
  top: 40%;
  transform: translateY(-40%);
  position: relative;
  line-height: 1.8em;
}

Almost answer set height of two icons is 0px or 1px to make two icons is nearly, so cannot click to sort. 几乎答案设置两个图标的高度是0px或1px使两个图标差不多,所以无法点击排序。 I think you should use another icon (or image) width distance to top and bottom rational (like fa-caret ). 我认为你应该使用另一个图标(或图像)宽度距离到顶部和底部理性(如fa-caret )。 How about this? 这个怎么样?

https://jsfiddle.net/2merngco/ https://jsfiddle.net/2merngco/

I don't know how but using absolute position worked for me!我不知道如何使用绝对 position 对我有用!

<th>
 Title <span class="sort-icon"><i class="fa fa-sort"></i></span>
</th>
.sort-icon {
    position: absolute;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM