Center-align the text by disregarding the absolutely positioned element

I have created a custom flex table with filtering and sorting capabilities. The filter and sort icons are located in the right corner of the table header. Additionally, users can choose to align the header text left or center.

The Issue:

When positioning elements in the center, the alignment gets disrupted due to the space occupied by the icons within the table header.

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

https://i.sstatic.net/lOYAM.png

Attempted Solutions:

To address the space occupation issue caused by icons, I positioned them absolutely relative to the header which solved the alignment problem. However, for headers where ellipsis should be displayed, the ellipsis may get hidden behind the icons. You can visualize this here: https://i.sstatic.net/nS2PT.png

Code: Here

If you have any suggestions on how to maintain centered alignment while reducing icon space occupancy through CSS, please advise. Thank you! :)

Answer №1

To achieve the desired outcome using absolute positioning, it is important to determine the exact width that your icons will occupy.

One potential solution involves adding a padding rule (left/right) to the table headers. In this case, the CSS code would look like this:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding a class name to the three active, visible, and shown slides in SwiperJS is a simple process that can be done by

Currently, I am working on customizing a carousel in Swiper with slidesPerView={3}. My goal is to style the sliders that are not visible on the screen. I have made multiple attempts to achieve this: First, I tried adding the attribute slideActiveClass="sw ...

I am looking to enhance the dimensions of my shapes by utilizing the ExtrudeGeometry feature in Three.js. Can anyone provide guidance on

I am currently utilizing three.js to create a line in my project. However, I am facing an issue where the line appears flat like a paper, instead of a three-dimensional wall. I am struggling to increase the height of the line in my code. Below is the code ...

Challenges in creating an alternative path in ExpressJS

I am currently working on a website for my studies. I decided to use nodejs/Express, as the technology is free. The first route /home was successful, but I am having trouble creating more routes. https://i.sstatic.net/6oseq.png Although I thought I had a ...

Content is missing from the response of the AngularJS $http request

Attempting to retrieve content from a website using Angular's $http service. The request seems to be successful as console.log(response.data) displays most of the content but is missing some parts. Check out my code below: $http = angular.element(doc ...

Move the final column from one table to another table given a specific criterion

I have a task where I need to extract the last column from table3 and transfer it into table4. For example, consider table3: Names Process_id Total construction 1 1111 construction_1 1 0000 engineering 1 22 ...

Issues with CSS media queries not being responsive in Safari

Can anyone shed light on why media queries are not functioning properly in Safari? Here is an example: body { background-color:black; } @media screen and (min-width: 1024px) and (max-width: 1300px) { body { background-color:red; } } ...

I cannot understand why I keep receiving <input type="email" value="required"> when all I want is <input type="email" value="" required>

Currently, I am working with PHP code and here is my complete PHP code: echo "<input type=\"email\" class=\"profil_input_text form-control\" name=\"user_email\" pattern=\"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(& ...

Error in Angular: Http Provider Not Found

NPM Version: 8.1.4 Encountered Issue: Error: Uncaught (in promise): Error: Error in ./SignupComponent class SignupComponent_Host - inline template:0:0 caused by: No provider for Http! Error: No provider for Http! The error message usually indicates the a ...

I'm currently checking records of dates: what's the best way to assign a value if a specific date is missing?

Looking to retrieve date-based data from the database. Let's say I need data from June 1st to August 1st from my database. If there are missing records for certain dates, such as the 5th, 6th, and 10th, how can I set the count for those dates to zero ...

`vuetify sidebar with a nested menu``

I am trying to enhance the vuetify sidebar menu I created by adding a submenu to a specific title. Despite editing the code and data as shown below, I am unable to see any changes reflected. Below is the code snippet: <v-list flat class="mt- ...

How can I stop Jquery Mobile from processing the URL hash?

Currently implementing Jquery Mobile in a preexisting web application is posing a challenge. The issue arises as Jquery Mobile is interpreting all URL hashes by default. For example: mysite.com/#foo In this case, the hash 'foo' is being directe ...

Searching for data across multiple tables using the Laravel framework combined with Vue.js and API integration

I am currently implementing a multi-search feature for a single table The search functionality is functioning correctly but only for one column Here is the snippet from my controller: public function index() { return Matter::when(request('sear ...

How can data be transferred from a parent component to a child component using MaterialUI's styling functionality?

Recently delving into the world of reactjs, I've been tinkering with a basic page that incorporates authentication using state. To spruce up the design, I decided to use the MaterialUI framework. The issue I'm facing is related to sending the lo ...

The issue of CSS and PHP header include file not adjusting to device size and exceeding the full width of the screen

This included code is a snippet from a header file for a website. <head> <link rel="shortcut icon" href="favicon.ico"> <meta charset="utf-8" /> <title>Test Site | Foo The Bar </title> <link rel="stylesheet" href="csss_stan ...

"Looking to programmatically close a modal in JavaScript that was opened using the 'data-target' attribute? Here's how you can

I have a modal template embedded in the same HTML file as my controller. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalDeleteItem">Delete selected item</button> < ...

I keep receiving an error in Angular JS but I'm unsure of the reason

I've been working on AngularJS and created a basic module and controller. I'm attempting to show the data of an element inside the controller on the HTML view page, but all I see is {{student.name}} and I'm receiving an error message that sa ...

Bluebird Enthusiastically Predicting the Outcome of a Complex Operation

Lately, I've been heavily utilizing Bluebird in my HAPI API development. However, I've encountered a perplexing issue that has left me puzzled due to either my understanding or lack of experience. Below is an example demonstrating the challenge ...

Graph columns failing to display on Chart.js bar chart

I'm currently facing a challenge while trying to create a bar chart using HTML and JavaScript. Unfortunately, the bars are not showing up for some reason. I have included the code snippet below along with an imagehttps://i.stack.imgur.com/4H7ol.png. ...

Encountering Typescript errors when trying to destructure a forEach loop from the output of

There are different types categorized based on mimetypes that I am currently working with. export type MimeType = 'image' | 'application' | 'text'; export type ApplicationMimeType = '.pdf' | '.zip'; expor ...

Guide to selecting the radio button in group2 by clicking on the radio button in group1

Here is my scenario: I have two sets of radio buttons as shown in the code snippet below: <form name="form" method="post" action=""> <label><input type="radio" name="RadioGroup1" value="radio" id="Radio1">Radio 1</label><br> ...