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

displaying a pair of inputs next to each other

Is it possible to display two input fields side by side? I am using Angular's matInput forms, but struggling to position the second input next to the first. What I would like to achieve is to have "input1 , input2" on the same line. Here is my code: ...

Shifting the position of an HTML page to one direction

I'm currently working on adding a sidebar to my Twitter Bootstrap 3 project. The goal is to have a fixed positioned nav nav-pills nav-stacked show up on the left side of the page when a button is clicked. I've set its z-index to 1000 so it appear ...

Locate the final flexbox in the initial row and the initial flexbox in the concluding row

How can I identify the last element of the first row and the first element of the last row in the given code to make it round from all corners? It should be noted that the column number will vary as well as the last element of the first row and first eleme ...

Eliminate gaps between lines in a wrapped Flexbox layout

I am trying to eliminate the spaces between rows in a horizontal list flexbox. I am creating an alphabetical list and I want it to flow seamlessly without any gaps. This is what I have so far: #example { display: block; margin-left: auto; margin-r ...

Customizing Paths in Express Handlebars

I have a query regarding setting up custom folders in Express.js Here's my situation: I need to implement logic where if a specific name is present in my database, the paths for CSS and JS files should be altered before rendering. By default, my pat ...

Generating a Popup Alert with a DIV

Looking at this instance: HTML / CSS Popup div on text click I am curious about how to have this display on the main page upon initial visit to the website. I prefer it to load automatically without requiring a button click, and once the content is read, ...

What could be causing the video to extend beyond the limits of the container?

When extending the result window, the video ends up overlapping the section below it. I am looking to keep the video within the confines of the section's height, which is set to height:100vh. Is there a way to accomplish this? Check out this jsFiddl ...

Tips for customizing the appearance of a jQuery sortable target list prior to dropping items

When using jquery sortable, I am able to customize a placeholder inside a connected list specified in the connectWith option to visualize where the content will be dropped. However, I am struggling to find a way to style the actual list that contains the p ...

What is the best way to allow wider list items to overflow their absolutely positioned container?

My challenge involves a list that is dynamically populated, and I require it to be wider than its absolutely-positioned parent (specifically a custom <select> element implementation). #wrapper { position: relative; border: 1px ...

Enlarge an element to the extent of filling the list item

I am facing a challenge in expanding the a elements within this jsfiddle to fully occupy the li element. The use of display:block does not seem to be effective since I am utilizing a table for spacing. What steps can I take to achieve this while ensuring ...

Struggling to keep up with responsive behavior on a page featuring vertically and horizontally centered content that spans the full height

I'm working on a full-height website and the first section needs to have both vertical and horizontal centered content. The issue I'm facing is that there's an image included, which doesn't scale responsively when the screen size change ...

Is there a way to stop objects in a particular list from being dragged into another nested ul using jquery sortable?

I am faced with a challenge regarding nested lists, specifically when it comes to preventing parent-parent list items from dropping into an inner <div> within an <li> <div> that already contains its own <ul> <li> list. Additio ...

Simple method to toggle between elements using jQuery

After creating a script using jQuery to show/hide content when tabs are clicked (which also changes the color of the clicked tab), everything is functioning smoothly. However, I believe there may be a more efficient way to switch between content that allow ...

"How can you enhance the performance of JavaScript and CSS in a Chrome Extension without using exclude_matches/globs or excluding domains

I have been in the process of creating a Chrome Extension, and unfortunately, when I tried to make it work on specific URLs, I encountered an issue. While Chrome has options like exclude_matches and exclude_globs for this purpose, there seems to be a bug i ...

Eliminate the mystery overflow in your CSS styling

My website is experiencing overflow on the x axis, even though all vw/width properties are set to 100. I suspect that the .logout element may be causing this issue, but I'm not sure what exactly needs to be modified to resolve it. Any assistance would ...

Can anyone shed some light on why the CSS code is not functioning properly within my HTML file?

My CSS doesn't seem to be working in my HTML. I have linked it correctly, but it's not displaying properly - even showing empty in the CSS tab of Chrome Inspector. The links are there, so I'm not sure why it's not functioning correctly. ...

Troubleshoot: Issue with Multilevel Dropdown Menu Functionality

Check out the menu at this link: The issue lies with the dropdown functionality, which is a result of WordPress's auto-generated code. Css: .menu-tophorizontalmenu-container { margin: 18px auto 21px; overflow: hidden; width: 1005px; ...

Is there a way to create an animation for changing .text in response to an on click event?

I'm currently developing a simple calculator that increases a variable when a button is clicked, displaying the updated value in a div. Each click on the 'amount-upwards' button adds 200 to the displayed number. Below is the jQuery code I a ...

Browser displaying a CSS error: "Invalid property name" while applying pseudo-element :after

I encountered an issue in Chrome and Explorer while attempting to set a CSS property for a pseudo element :after. (I'm trying to create a styled burger nav icon) The error message I received was: 'Unknown property name' This happened wh ...

Customize the appearance of a React component depending on its unique identifier

After creating a simple TODO app with drag and drop functionality, I am now looking to apply a line-through CSS style to any task added or dragged into the second column of my app. What is the best way to target these tasks using their unique ID: <div c ...