Aligning an element to the right within an absolutely positioned header

I am facing an issue with keeping the headings fixed in a table while allowing the body to be scrolled using CSS. Specifically, I am struggling to right-align some of the columns because the headings are made up of divs with absolute positioning. Although I copied this code from a solution on the web that I now can't find, it mostly works well but I need help fixing the alignment of the headings.

.css:

.panel
{
    overflow:auto; 
    overflow-x:hidden;
    border: solid 1px #838383;
}

.fixedTableContainer {
    height: 320px;
    border: 1px solid darkgray;
    background-color: white;
    position: relative;
    padding-top: 20px;
    white-space: nowrap;
    width: 100%;
}

.fixedTableContainerInner {
    overflow-x: hidden;
    overflow-y: auto;
    height: 100%;
    width: 100%;
    line-height: 24px;
}

.fixedTableContainerInner th {
    padding: 0 5px;
}

.thInner {
    position: absolute;
    top: 2px;
    line-height: 24px;
    text-align: left;
    margin-left: -5px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    white-space: nowrap;
    font: normal normal 10pt Verdana, Arial, Helvetica, sans-serif;
}
<asp:Panel runat="server" id="Panel1" runat="server" CssClass="panel">
    <asp:ListView runat="server" id="listView" runat="server" DataKeyNames="CompanyAccountID">
        <LayoutTemplate>
            <div class="fixedTableContainer">
                <div class="fixedTableContainerInner">
                    <table style="width: 100%">
                        <tr>
                            <th><div class="thInner">Acct. ID</div></th>
                            <th><div class="thInner">Account Name</div></th>
                            <th><div class="thInner right">Account Code</div></th>
                            <th><div class="thInner">Sort Code</div></th>
                            <th><div class="thInner">Account Number</div></th>
                        </tr>
                        <tr id="itemPlaceHolder" runat="server"></tr>
                    </table>
                </div>
            </div>
        </LayoutTemplate>
        
        
    </asp:ListView>
</asp:Panel>

My aim is to align any of those headings to the right within their container div, however, my attempts so far have been unsuccessful. Any assistance would be greatly appreciated.

NB: This has been edited for more detail as previous solutions did not work for me.

Thank you all.

PS. After extensive searching, I finally found the original post where I got the code from: fiddle

Answer №1

If you want to ensure functionality, make sure to specify the width of the columns. For instance:

<th><div class="thInner" style="width: 100px; text-align: right;">Acct. ID</div></th>
in this case, the account id column header will align correctly on the right.

You have the option to set specific column widths such as 20%, 10%, 30% and DataTables will work accordingly.

On a side note: I'm a big fan of DataTables!

Answer №2

To start, give the parent element (<th>) a relative position. Next, create a new class for titles aligned to the right and apply the class to them like this (I made them red for better visibility):

.thInner {
    position: absolute;
    top: 2px;
    line-height: 24px;
    text-align: left;
    margin-left: -5px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    white-space: nowrap;
    font: normal normal 10pt Verdana, Arial, Helvetica, sans-serif;
}

th {
  position: relative;
}

.right {
  right: 0;
  color: red;
}
<table style="width: 100%">
            <tr>
                <th><div class="thInner right">Acct. ID</div></th>
                <th><div class="thInner">Account Name</div></th>
                <th><div class="thInner">Account Code</div></th>
                <th><div class="thInner right">Sort Code</div></th>
                <th><div class="thInner">Account Number</div></th>
            </tr>
        </table>

Answer №3

To ensure proper alignment, adjust the positioning of the container element to relative and then set the inner heading to absolute within its boundaries.

    .thInner {
    position: absolute;
    top: 2px;
    line-height: 24px;
    text-align: left;
    margin-left: -5px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    white-space: nowrap;
    font: normal normal 10pt Verdana, Arial, Helvetica, sans-serif;
}
th{
    position: relative;
}

Answer №4

Give this approach a try - it's effective.

.thInner {
    position: absolute;
    top: 2px;
    line-height: 24px;
    left:0;
    width:100%;
    text-align: right;
    margin-left: -5px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    white-space: nowrap;
    font: normal normal 10pt Verdana, Arial, Helvetica, sans-serif;
}

.thInner.c1 {
   left:-80%;
}
.thInner.c2 {
  left:-60%;
}
.thInner.c3 {
  left:-40%;
}
.thInner.c4 {
  left:-20%;
}
.thInner.c5 {
  left:0;
}
        <table style="width: 100%">
            <tr>
                <th><div class="thInner c1">Acct. ID</div></th>
                <th><div class="thInner c2">Account Name</div></th>
                <th><div class="thInner c3">Account Code</div></th>
                <th><div class="thInner c4">Sort Code</div></th>
                <th><div class="thInner c5">Account Number</div></th>
            </tr>
        </table>

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

Steps for adding a time display box to a time progress bar

There is a time progress bar with code provided. How can I make sure the yellow bar moves according to the time and display the time in a box? https://i.sstatic.net/ZgPOe.png var timer = 0, perc = 0, timeTotal = 2500, timeCount = 1, cFlag ...

Is the @media rule intended to be applied to all screen sizes, including those less than 768px wide?

There seems to be an issue with the css min-width 768 not working correctly on mobile devices. Interestingly, it works fine in inspect mode for widths less than 768 on the Google Chrome desktop browser. @media only screen and (min-width: 768px){ #main { ...

Adjusting the size of a React element containing an SVG (d3)

In my simple react app, I have the following component structure: <div id='app'> <Navbar /> <Graph /> <Footer /> </div> The Navbar has a fixed height of 80px. The Footer has a fixed height of 40px. The Graph i ...

Is the parent node of the input element the input element itself?

When working with a DOM object obj of type <input>, I encountered an issue where attempting to obtain its parent node using obj.parentNode resulted in the same obj being returned. Is this behavior specific to <input> objects? What other types o ...

Combining data from two separate MySQL queries to create a personalized user feed

I am working on creating a unique personalized feed system for a website using NodeJS+MySQL. Currently, I am fetching posts based on specific tags using the following query: SELECT column1, column2... FROM table WHERE tags = users_tags ORDER BY relevanc ...

Implement Clip Function with Gradient Effect in JavaScript on Canvas

Trying to incorporate the clip() function within the canvas element to create a unique effect, similar to the one shown in the image. I have successfully achieved a circular clip, but I am now aiming for a gradient effect as seen in the example. How can th ...

having trouble displaying album artwork

Encountering an unusual error in my music player implementation on the website. All functions of the music player are working perfectly fine. First, let's take a look at the error: TypeError: Cannot read properties of undefined (reading 'cover&a ...

The elements in the navigation bar stay in view for a short period of time even after the collapsible navbar has been

After collapsing the navbar, the navbar items remain visible for a short period of time, as shown in the second image below. Here is the HTML code: <nav class="navbar navbar-dark fixed-top" style="background-color: transparent;> <a class="navb ...

What is the proper way to connect an external Javascript file to an html document within a Play Framework project?

Below is the content of my scala.html file: @() <!DOCTYPE html> <html> <head> <title> Spin To Win </title> <link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/styles.css")" ...

Expanding Images with React-Bootstrap Cards

I need assistance with setting up cards in React-Bootstrap. The Card component is enlarging my images beyond their original sizes. When I attempt to place the image in an img tag outside of the card, it displays at its normal size. Can someone provide guid ...

The collapsed navbar button in Bootstrap is unresponsive

I run two websites. The Collapsed Navbar functions properly on the first site, located at (Index Site). <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" ...

Exploring the versatility of Angular Directives for numerous selections

My website has a footer section: <footer> <div class="container"> <div class="contents"> <div class="logo-copyright"> <div class="footer-logo"> <i class="footer-logo gradient">< ...

Utilizing CSS to place an image on top of the upcoming <div> container

My goal is to create a layout similar to the one displayed in the linked image. https://i.stack.imgur.com/7DSE9.jpg Currently, I am using Bootstrap 3 and my HTML markup looks like this: <body class="gray-bg"> <section class="white-bg"> <d ...

Stop unwanted overrides of CSS3 blinking background colors

I am facing an issue with a programmatically created table that has n rows. To distinguish between odd and even rows, I programatically add classes to them. Some of these rows have "special" content that needs to be highlighted. Currently, I am accomplishi ...

The Tinymce filemanager is completely unresponsive and the language settings are failing to load

Encountering an issue with tinymce's filemanager functionality. Attempted to establish a connection but ran into some trouble. When selecting text and trying to create a link, upon clicking the "chain" icon and then the "browse" button, the filemanage ...

When using Three.js, adjusting the TranslateX or Y values sometimes results in unexpected rotation instead of a straightforward movement along the axis,

In my Three.js project with TrackballControls, I am working on creating a dynamic 3D scene. One of the key components is the PerspectiveCamera that I have initialized: camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10 ...

Unusual behavior displayed by Android webview

In landscape orientation, I have a horizontal layout with buttons on the left and a webview on the right like this: Everything works fine in the WebView until an absolutely or fixedly positioned element is added to it. For example, this element: <div ...

Tracking the click location inside a div element

Is there a way to accurately determine the position of a click inside a div? I need the mouse cursor to be exactly where the initial click occurred relative to the moving window as the mouse drag moves it. Below is the structure of the window: <div id ...

What causes the image to not appear at the center bottom of the page when using IE for loading?

Why does the loading image not appear at the center bottom of the page in IE? This function loads content when the page is loaded and also loads content when scrolled to the bottom. When you load the page index.php, you will see the loading image at the ...

Tips for evenly spacing items in a navigation bar

I'm having trouble centering the logo on the navbar as it always leans more towards the left. Is there a way to resolve this problem using Bootstrap classes? The navbar consists of a dropdown toggle button on the left, the logo in the center, and two ...