Creating a border indentation for a table row using the <tr> tag

In my database, there is a table that shows the parent-child relationship.

Check out this link for more details

The HTML structure of the table is as follows:

<table>

<tr class="parent_row">
    <td >1</td>
    <td>2</td>
    <td>3</td>
    <td><a>Link</a></td>
    <td style="width:20px;"></td>
</tr>
<tr class="child_row">
    <td >1.1</td>
    <td>2.1</td>
    <td>3.1</td>
    <td><a>Link_child</a></td>
</tr>
<tr class="parent_row">
    <td >1</td>
    <td>2</td>
    <td>3</td>
    <td><a>Link</a></td>
    <td style="width:20px;"></td>
</tr>
<tr class="child_row">
    <td >1.2</td>
    <td>2.2</td>
    <td>3.2</td>
    <td><a>Link_child</a></td>
    <td style="width:20px;"></td>
</tr>
</table>

Here's the CSS used for styling the table:

table{
    margin:0;
    padding:0;
    width:100%;
    border-collapse: collapse;
    border-spacing: 0;
}

tr{
    border-color: #D8D8D8;
    border-style: solid;
    border-width: 1px 0 0;
    line-height:2em;
    font-size:14px;
}

td:first-child{
    padding-left:20px;
}

.child_row{
    border-style:dotted;
}

The current setup displays borders for both parent and child rows, with solid borders for parents and dotted borders for children. The issue arises where the child row's border starts from the left edge instead of aligning correctly with the text.

I attempted to address this by using a background image positioned 20px from the left for tr elements, but it didn't work due to repeat-x setting for handling various screen sizes.

Is there an alternative solution to ensure the borders in parent and child rows behave as expected?

Updated the interactive demo on jsfiddle

This solution needs to be compatible across different browsers including IE8, Chrome, Safari, and Firefox.

Answer №1

Unfortunately, I am unable to create an indentation in a tr element using margin or padding.

One alternative solution is to use the td element with the first-child selector. Here's an example:

tr.child_row td:first-child { padding-left:20px; }

If you prefer to indent every cell in the child row, you can simply remove :first-child like this:

tr.child_row td { padding-left:20px; }    

Check out this updated jsFiddle link for a visual representation: http://jsfiddle.net/ZPSVg/23/

Answer №2

To properly structure the layout, you will want to include an empty td element with a specified width in the child section and set the colspan attribute on the parent element.

<tr>
     <td colspan="2">Parent</td>
</tr>
<tr>
     <td style="width: 20px;"></td><td>Child</td>
</tr>

Answer №3

Try using the CSS property text-indent

<tr>
     <td colspan="2">Mother</td>
</tr>
<tr>
     <td style="text-indent: 1em;"></td><td>Child</td>
</tr>

Answer №4

Here is a method to create a proper right or left margin on a table row that will function on both modern and outdated browsers.

1/ Table WITHOUT borders or background color:

<table>
    <tr>
        <td>AA</td>
        <td>BBBBBBBBB</td>
        <td>CCCC</td>
    </tr>
    <tr class="leftMargin">
        <td>A</td>
        <td>B</td>
        <td>C</td>
    </tr>
    <tr> 
        <td>A</td>
        <td>B</td>
        <td>C</td>
    </tr>
</table>
.leftMargin td {  
    position: relative;  
    left: 60px;  
    z-index: 1;  
}  

2/ Table WITH cell borders and background color:

<table>  
    <tr>  
        <td>AA</td>  
        <td>BBBBBBBBB</td>  
        <td>CCCC</td>  
    </tr>  
    <tr class="leftMargin">  
        <td><div class="column1">A</div></td>  
        <td><div>B</div></td>  
        <td><div>C</div></td>  
    </tr>  
    <tr>  
        <td>A</td>  
        <td>B</td>  
        <td>C</td>  
    </tr>  
</table>  
table{  
    border-collapse: collapse;  
}  

td {  
    border: solid 1px red;  
    background-color:yellow;  
}  

.leftMargin td {  
    border: none;  
    background-color: transparent;  
    padding: 0 0;  
}  

.leftMargin div {  
    position: relative;  
    left: 60px;  
    z-index: 1;  
    border-top: solid 1px red;  
    border-right: solid 1px red;  
    border-bottom: solid 1px red;  
    background-color: yellow;  
    margin: -1px 0;  
}  

.leftMargin div.column1 {  
    border-left: solid 1px red;  
}  

Browser support:

This HTML/CSS code has been tested successfully on various desktop and mobile browsers as listed below.

Desktop browsers:

  • Windows:
    IE 9
    IE 11 + IE 5,7,8,9,10 emulation
    Edge 42
    Chrome 5, 70
    Firefox 3.6, 64
    Safari 4.0.3

  • Tested using :
    IE 5.5

  • Tested using :
    Opera 10

Mobile browsers:

  • Android 4.1.2 (Jelly Bean):
    Safari 4.0
    Chrome 26
    Firefox 58

  • iPhone 6.1.6 (iPhone 3GS with most recent OS update):
    Safari 6.0
    Chrome 29

  • iPad Air OS 12.1.1:
    Safari 12

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

What could be causing the Bootstrap ProgressBar to not show up?

Having an issue with the ProgressBar component from react-bootstrap: <ProgressBar now={dynamicValue} /> The value dynamicValue changes for each component. However, I am unable to get the progressBar to display in my case. ...

The functionality of Bootstrap 5's modal-dialog-scrollable seems to be malfunctioning

I recently created a Student Add form using bootstrap 5 <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dial ...

What are the advantages of importing CSS files from JS source code with webpack, rather than building CSS in isolation as traditionally done?

If you're looking for information on how to load CSS with webpack, check out the documentation here: https://webpack.js.org/guides/asset-management/#loading-css1 The webpack documentation suggests importing CSS files from JavaScript code and using ex ...

Pictures glide within containers

Hey, I'm having an issue with my images. They're not floating like they should be, even though they are centered in the div. I really want them to float and be centered. Here's the code snippet I've been working with. HTML <div cla ...

Parent div's overflow due to its flexbox properties

I am currently facing a dilemma with my nested flexbox setup. The primary objective is to create two responsive columns: .ColWrap, { width: 100%; box-sizing: border-box; background-color: #fff; padding: 50px 10px 50px 25px; display: fl ...

The column is not properly aligned

I am facing a challenge in aligning 3 elements using bootstrap while working with Angular 8. To include only the necessary CSS from Bootstrap (such as col-md and col classes), I have added the following to my styles array: "styles": [ "src/ ...

Get notified about the number of cells you've selected by simply dragging on a table

Is there a way to display the number of selected cells in a table when dragging with the mouse? I have a table set up, and I'd like to create an alert that shows how many cells are selected while dragging. Here is the link to my fiddle: http://jsfiddl ...

Filtering elements upon loading

I have successfully implemented data filtering on my website using data-filter. While everything is working great, I am interested in displaying items with specific attributes upon loading. For instance, I want to load only green objects by default inste ...

Cursor hovers over button, positioned perfectly in the center

I am facing a challenge with implementing a hover function for a set of buttons on the screen. The goal is to display the mouse pointer at the center of the button upon hovering, rather than the actual position of the cursor being displayed. I have tried u ...

Exploring the wonders of Bootstrap 3 panels and stylish floating images

Is it possible to create a responsive design using Bootstrap 3 panel along with a floating image positioned next to it? I want the panel to seamlessly fit into the available space, without overlapping the image. Can anyone provide guidance on achieving thi ...

Is there a way to utilize either css3 or css in order to change the icon displayed on a button

Currently, I am working on a button that contains a span element. I am interested in finding a way to change the background of the span when the button is clicked and have it remain changed. Is there a way to achieve this using CSS only? I am aware that ...

The video element in HTML5 is not showing up on Safari browsers, but it is functioning properly on Chrome

I have set up a video slider on my webpage. You can view the site The code is functioning perfectly on Chrome and iOS Safari, but there seems to be an issue on desktop browsers. When inspecting the page in Safari, the video elements are present in the HTM ...

What are some techniques for concealing a CSS transition beneath another element in both directions?

Witness the magic in action! By clicking on the black box below, a larger black box will gracefully emerge from beneath the sidebar. While it may not be as clear in jsfiddle, rest assured that this effect is more pronounced in browsers. Thanks to some clev ...

Changing the color of MUI Typography when selected

Struggling to modify the styling of the Typography component nested inside a MenuItem. Unable to apply styles to ListItem as intended Check out my code here: https://codesandbox.io/s/dztbc?file=/demo.tsx:1481-1804 Desired behavior: Change styling on sele ...

Displaying a Facebook tab exclusively within the Facebook platform

Hello Everyone I have been utilizing woobox to generate Facebook tabs for my pages and I have a query: I have created an HTML page on my personal server, which I am using as a Facebook tab (created with the help of woobox). Now, is there a method whe ...

What is the best way to access dropdown sub-menu options from a complex multi-level navigation bar

Struggling to figure out how to open my dropdown sub-menu using CSS. Hoping to make it so the user can open it by hovering over the corresponding tag. I attempted to use #lablinksDD to trigger the opening of #ddSbMenu when hovered over #menuDD with #labLin ...

The CSS code functions properly in Firefox, however, it does not seem to be functioning in

The menu I created in Magento is functioning correctly in Firefox. When I hover over the menu in Firefox, it works fine, but I am not seeing any hover effects in Chrome. Here is how my hover style is defined: #nav { width:965px; margin ...

Require the capability to bypass inline CSS styling

Currently facing an issue with a wordpress website I am constructing using iThemes Builder. The problem arises when integrating the plugin Jquery Megamenu, as the drop-down menu gets truncated due to an overflow:hidden property that cannot be overridden i ...

Tips on adjusting the color of a link once it has been clicked?

Looking for a link with a specific style? a { color: #999; &:hover { color: #008da0; } &:visited { color: #999; } /* I added this to test if it's gonna fix it, but it didn't */ } Upon clicking the link, it turns blue and remains s ...

Implementing jQuery selector for CSS and properly handling special characters in the code

I've been attempting to use jQuery to change the background color of a radio button. I provided the CSS code below, but even after escaping special characters in jQuery as shown below, the solution still isn't working. #opt5 > [type="radio"]: ...