Selecting the first child within a table using the first-child

I recently set up a login portal page for three different websites. The layout consists of three separate login portals displayed neatly in two columns within a table named "loginPortals". I am currently working on styling the page using CSS, aiming to enhance my skills and keep the code streamlined.

To give you an idea of the structure, here is a snippet of the HTML:

<table class="loginPortals">
    <tbody>
        <tr>
            <td>Picture 1</td>
            <td>Login form 1</td>
        </tr>
        <tr>
            <td>Picture 2</td>
            <td>Login form 2</td>
        </tr>
        <tr>
            <td>Picture 3</td>
            <td>Login form 3</td>
        </tr>
    </tbody>
</table>

Although I have started working on the CSS, it's far from complete. One issue I am facing is with applying borders between each row. My current attempt is not achieving the desired outcome, as it adds unwanted thickness in the middle of rows due to top border being applied to all cells.

.loginPortals{
    width:100%;
}
    .loginPortals tbody:first-child td{
        border-top:1px solid #000;
    }
    .loginPortals tbody tr td{
        border-bottom:1px solid #000;
        padding:1em 0;
    }

The use of the first-child selector did not work as intended, resulting in thicker borders than expected. How can I resolve this issue without adding more classes or inline styles?

Any input would be greatly appreciated!
Joe

Answer №1

To ensure your table borders are tidy, it is important to set them to collapse:

.loginPortals{
    width:100%;
    border-collapse: collapse;
}

Once you have done this, you can skip the :first-child rule and simply add top and bottom borders to all rows in your table:

.loginPortals tbody tr td{
    border-top:1px solid #000;
    border-bottom:1px solid #000;
    padding:1em 0;
}

Additionally, it's worth noting that this selector:

.loginPortals tbody:first-child td

Actually means:

Select any td element
within a tbody
that is the first child of its parent.

Not:

Select any td element
within the first child of a tbody.

Since the only child of .loginPortals is the tbody, all td elements within their parent tr will be selected. For a visual guide, refer to this answer. If you were looking for this specific selector, you would want:

.loginPortals tbody tr:first-child td

However, by setting your table borders to collapse, this becomes unnecessary.

Answer №2

If my understanding is correct, you are dealing with 3 forms but only wish to have two visible lines - this is the impression I got from your statement "I want a border of some sort appearing between each row". If that's the case, you can try the following code snippet for a solution.

.customForms tr:first-child td{
    border-top:none;
}
.customForms tbody tr td{
    border-top:1px solid #000;
    padding:1em 0;
}​

Check out this JSFiddle link for a live example.

Answer №3

In my opinion, this solution seems viable...

th {border-bottom:1px solid #333;}

Answer №4

When looking to enhance your CSS abilities, it is vital to move away from using tables for layout purposes.

Additionally, the :first-child pseudo class applies to elements that are the initial child within their parent element. Considering you have only one tbody element and it serves as the first child of the table, using either tbody or tbody:first-child will produce the same outcome.

Note: If you intend to specifically target only the cells in the first row of the table, you can utilize the selector tr:first-child td.

To address this issue, you could apply a border-bottom to all td elements. Alternatively, if a top border is also required, you have the option of setting it on the actual table, or giving each td both a border-top and border-bottom. To prevent the double border effect, consider implementing border-collapse: collapse to make the borders collapse together.

In summary: Use

table {border-collapse: collapse}
to manage table borders effectively. However, refrain from relying on tables for layout purposes.

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

"Utilizing Bootstrap to create a space-saving table layout with an

Using Bootstrap on a table causes an unexpected empty column to appear on the right side (as shown in the screenshot). This issue is strange because it works fine with another table using the same setup... but this particular table seems to have a mind of ...

What are some of the top languages, libraries, and modules used to create a web application for constructing templates?

Our company specializes in creating websites for membership-based associations. Currently, we utilize custom DLL's created with CodeGear Delphi to connect with a SQL Server database and implement a unique HTML template language. Essentially, our templ ...

Leverage Bootstrap 4 for achieving flexible layouts and centering content with two elements using flex and justify-content

By using the d-flex, justify-content-center, and flex-grow-1 classes, I was able to achieve my desired layout with just 3 divs. <div class="row bg-primary d-flex justify-content-center"> <div class="col-auto"> LEFT </div> < ...

Is it better to store CSS and JavaScript in separate files or within the main HTML document

While the best practice is to place JavaScript and CSS code in separate files (such as .js and .css), many popular websites like Amazon, Facebook, etc. often include a large portion of their JavaScript and CSS code directly within the main HTML page. Whic ...

Issue with aligning CSS in Internet Explorer 7

Everything looks great on Firefox and IE8, but on IE 7 the middle "search input text field" is not aligned properly with the other two fields. It seems to be dropping down slightly. Here is the code - thank you for your help: <!DOCTYPE html PUBLIC "-/ ...

What is the best way to layer multiple navigation menus on top of each other using z-index?

I'm facing a challenge trying to position or overlap two navigation menus in my project. The technologies I am using are Bootstrap, HTML, and CSS. It seems like I might need to utilize the z-index property, but I'm unsure about the exact impleme ...

Is there a way to ensure consistent heights for various elements within a column, even if their heights are

Currently, I am utilizing the flex property to create uniform columns and vh to ensure they have the same height. This setup is functioning properly, but within each column, there could be a varying number of items. I am interested in having the elements w ...

use angularjs directive to position a div absolutely aligned with the right edge of another div

I am attempting to use an angularjs directive to align the right side of an absolute div with another div. The code is working, but I am encountering an issue where the children divs are slightly wider than their parent, causing the offsetWidth value to be ...

Correctly align the div on the screen as the viewport is scrolled

I am currently working on a parallax website where the sliders are designed to slide from the left and align within the viewport as the user scrolls. However, I have encountered an issue where multiple scroll actions are required for the slide to align pro ...

What is the correct way to utilize ng-if/ng-show/ng-hide to hide or show HTML elements within the app.run function

I am currently working on developing an app that loads views correctly. HTML: <body> <loading outerWidth='1000' outerHeight='1000' display='isReady'></loading> <div class='wrapper' ng-sho ...

Is it possible to style an element that has extended beyond its parent container using CSS?

Is it possible to select items that have a float:right property and appear on the next line due to reasons like excessive content on the left or a window size too small? This is necessary because the parent item has a background image that looks good, but ...

Achieving a centered Title while aligning Nav all the way to the right using flexbox: tips and tricks

I am currently using flexbox to align the h1 and nav on the same line with display:flex. I want the title to be centered and the navigation to be on the right side. However, I am struggling to figure out how to move the navigation all the way to the right. ...

What is the best way to create a div that maintains a consistent size while displaying images inside?

Struggling with a project and attempting to create a fixed div container for uniform picture sizes, I've tested various methods recommended by others but have had zero success in altering the size of my div container. Could it be due to my use of boot ...

What is the process for developing multiple screens or views in PhoneGap?

Currently working on a reading app and decided to give phoneGap a shot for the first time. I'm pretty proficient in HTML, CSS, and JS, but not very familiar with xCode. In this app, I plan to have a button on the home screen that redirects users to an ...

Creating a dynamic trio of graphs with HTML5, CSS3, and Vanilla JavaScript

I successfully created a tree graph using HTML5 and CSS3, but currently the nodes are static. I am looking to enhance the graph by making it dynamic. By dynamic, I mean that if the number of nodes increases or there are multiple children added, the graph ...

Different options exist for top layers with flexible widths aside from using position: absolute

The only method I'm aware of to place a layer on top is by using position: absolute. https://i.sstatic.net/R4j8i.jpg (favoring top, disliking bottom) However, this approach often prevents dynamic scaling with the rest of the page. You can attempt ...

Glitch in jQuery causing multiple instances of text being added to a textarea consecut

I created a response system where users can reply to comments by clicking on the reply button. Once clicked, it triggers a function that adds @user at the beginning of their comment: $('#inputField').focus(); $('#inputField').text(&apo ...

Tips for integrating CSS keyframes in MUI v5 (using emotion)

Hey there! I'm currently working on adding some spinning text, similar to a carousel, using keyframes in my React app. The setup involves MUI v5 with @emotion. Basically, I want a "box" element to show different words every few seconds with a rotating ...

What is the process for outputting JSON data from a script page?

I had a JSON file named results.json which is displayed below. Additionally, I had an HTML file containing some script that is used to retrieve data from this JSON file. Upon entering the HTML page, a script function get_machFollow(que_script) is called to ...

"Email signature design with sleek black borders incorporated for a professional touch in responses

At my workplace, I use an HTML file as my email signature footer with IBM Notes. Everything looks great when I create a new email - the logo is positioned correctly, text is styled how I want it, and the overall layout is fine. However, things start to lo ...