简体   繁体   English

通过忽略绝对定位的元素使文本居中对齐

[英]Align text in the center by ignoring the absolutely positioned element

I am developing a flex table from scratch and the table supports filtering and sorting.我正在从头开始开发一个弹性表,该表支持过滤和排序。 The icons for filters and sorting are displayed in the table header (right corner).过滤器和排序的图标显示在表格 header(右上角)中。 Also my table supports that the user can position the header text left/center;我的表也支持用户可以 position header 文本左/中;

My problem:我的问题:

Since the icons are inside the table header, icons as well occupies some space.由于图标在表格 header 内,因此图标也占用了一些空间。 So when i position the elements in the center, i see the alignment gets disturbed as shown below.因此,当我 position 中心的元素时,我看到 alignment 受到干扰,如下所示。

 body { width: 100%; }.table-header, .table-body { display: flex; width: fit-content; border: 1px solid black; padding: 10px; }.header, .data { display: flex; min-width: 150px; width: 150px; border-right: 1px solid black; display: flex; justify-content: center; position: relative; }.header.text { width: 100%; white-space: nowrap; text-align: center; overflow: hidden; text-overflow: ellipsis; }.icons { float: right; right: 0; display: flex; }
 <div class="table"> <div class="table-header"> <div class="header"> <div class="text">Hlkjklkjlkjlkj lkjlkjlkjlkjlkjljlkjlkj</div> <div class="icons"> <span> &#9760</span> <span> &#9760</span> </div> </div> <div class="header">9747 <div class="text">Header 2</div> <div class="icons"> <span>b</span> <span>b</span> </div> </div> <div class="header"> <div class="text">Header 3</div> <div class="icons"> <span>a</span> <span>b</span> </div> </div> <div class="header"> <div class="text">Header 4</div> <div class="icons"> <span>a</span> <span>b</span> </div> </div> </div> <div class="table-body"> <div class="data">123</div> <div class="data">123</div> <div class="data">123</div> <div class="data">123</div> </div> <div class="table-body"> <div class="data">123</div> <div class="data">123</div> <div class="data">123</div> <div class="data">123</div> </div><div class="table-body"> <div class="data">123</div> <div class="data">123</div> <div class="data">123</div> <div class="data">123</div> </div> </div>

Code: Here代码:这里

这里

What i tried我尝试了什么

So since the icons as well take some space, to avoid this i positioned them absolutely relative to the header.因此,由于图标也占用了一些空间,为了避免这种情况,我将它们绝对相对于 header 定位。 So the alignment problem got solved.这样 alignment 问题就解决了。 But for long headers where ellipsis has to be shown, the ellipsis hides behind the icons as shown below但是对于必须显示省略号的长标题,省略号隐藏在图标后面,如下所示这里

Code: Here代码:这里

So what is the solution to this?那么解决这个问题的方法是什么? I want to maintain the center position by reducing the space occupied by the icons.我想通过减少图标占用的空间来保持中心 position 。 Is it possible through CSS?是否可以通过 CSS? Please help.请帮忙。 Thanks:)谢谢:)

If you try to do that with absolute positioning you need to know what exact width your icons can take.如果您尝试使用绝对定位来做到这一点,您需要知道您的图标可以采用的确切宽度。

Then possible solution is to add padding rule (left/right) into table headers, so CSS code should be like this:然后可能的解决方案是将填充规则(左/右)添加到表头中,因此 CSS 代码应如下所示:

body {
  width: 100%;
}

.table-header, .table-body {
  display:  flex;
  width: fit-content;
  border: 1px solid black;
  padding: 10px;
}

.header, .data {
  box-sizing: border-box; 
  padding: 0 30px;
  display: flex;
  min-width: 150px;
  width: 150px;
  border-right:  1px solid black;
  display: flex;
  justify-content: center;
  position: relative;
}

.header .text {
  width: 100%;
  white-space: nowrap;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icons {
  position: absolute;
  right: 0;
}

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

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