Tips for choosing every initial td element within a table

Hey there, I'm looking to style only the first <td> (specifically those with the text "label") in each row of a table. If we have a simple HTML structure like this:

<table>
  <tr><td>label</td> <td>value</td></tr>
  <tr><td>label</td> <td>value</td></tr>
  <tr><td>label</td> <td>value</td></tr>
</table>

I want to apply a width of 10% only to the first group of <td></td> elements without using a class selector.

I've tested different selectors for this task:

table.widget tr:first-child td{
    width:10%;
    border:0;   
}

However, this selector only targets the first td in the first tr, not all the TD's. So, I also tried:

table.widget tr td:first-child{
    max-width:10%;
}

Unfortunately, this selects the first child of the TD, rather than the td itself.

Is there a way to achieve this styling effect?

Answer №1

Your second selector is absolutely correct:

Click here to view the example

table.widget tr td:first-child {
    background: orange;
}

If you want to select the first child of each td, use this selector:

table.widget tr td :first-child { /* remember the space after the td */
    // your styles go here
}

However, it's important to mention that in the sample table provided by the OP, the widget class is not being used.

If your table is representing a set of key/value pairs, it might be more suitable to put your label text inside a th:

See this link for an alternative version

table.widget th {
    background: orange;
}

<table class="widget">
  <tr><th>label</th> <td>value</td></tr>
  <tr><th>label</th> <td>value</td></tr>
  <tr><th>label</th> <td>value</td></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

Can a PHP file be used in the src attribute of an HTML5 audio element?

Within my PHP code, I am attempting to include an audio tag like this: '<audio src="song.php" />'.$fallback.'</audio>' I have experimented with various approaches in the "song.php" file, but unfortunately, none of them hav ...

Is it possible to arrange buttons next to each other?

Here is the code for a specific page: HTML: <div class=main> <div class=bttn> <button>abcdef</div> <div class=content> <a href=#!>jksg</a> <a href=#!>jksg</a> & ...

Utilizing a Bootstrap grid system (or an IE-compatible CSS grid) to align elements on the provided

Looking for suggestions on achieving the following layout without using CSS grid, either with Bootstrap or IE compatible CSS grid. On large screens: Head and body stack on the left, with an image on the right covering the height of both. [&mdash ...

Interactive Content Swapping: How to Use Javascript for Dynamic Link Updates

http://jsfiddle.net/bUjx7/42/ <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'> </script> <script type='text/javascript'> $(document).ready(function () { $('.fieldreplace ...

Issues with the display of Bootstrap modal may occur when clicking an <a> tag

Attempting to open a modal by clicking on an <a> tag to display it centered vertically. However, upon clicking, the modal fails to appear at the center with the proper backdrop. In header.blade.php <div> <a class="header-text" href="#" ...

An alternative way to adjust font size without having control over the HTML code

Is there a way to adjust the font size in a web page when only the div or iFrame is editable, not the actual HTML content? Our website allows users to upload their own ebooks, resulting in uncontrolled and complex HTML content for each chapter. We want to ...

Executing a function should be delayed until all necessary information is obtained from the REST Api

I want to create a chart showing data for each month over the course of 12 months. The data for each month will be retrieved from a REST API. Here is my approach: $.getJSON(link, function(data){ // store data in a variable } Repeat this process 12 t ...

Loading Ajax dropdown with a preselected value in the dropdown menu

There are two pages, each containing a form. Page 1 - Form A) Includes a dropdown menu (with values populated from the database) and a continue button. <select name="form[formA][]" id="dropdown1"> <option value="1">Option 01</opti ...

Having difficulty replicating the same effect across all five canvas elements

After following the code provided by j08691 here for canvas, everything worked flawlessly. However, I encountered an error in JavaScript when trying to implement it on multiple canvas elements. As a beginner in JavaScript, I would greatly appreciate any as ...

The code is failing to execute the if statement even when the conditions are satisfied, specifically when both variables are NULL

Running a search query with two dropdown lists, encountering an issue where an if-statement does not execute when conditions are met. Specifically, when both dropdown lists are on the default selected option and return a value of NULL. Below is the code s ...

Design an ARIA menu role with HTML and CSS code

Currently, my code is set up, but I'm unsure about integrating the tab/arrows to navigate to the sub-menu. You can view the code on jsfiddle: https://jsfiddle.net/Fep5Q/60/ This snippet shows a portion of the HTML code I've implemented: <di ...

Designing the Structure for a Private Network System

In the works is a plan to develop a production application catered towards small and medium businesses. The target for this intranet application is 15 to 30 concurrent users. The proposed architecture includes: Client: Firefox browser UI: HTML, JavaScrip ...

Place the video element within the Canvas element

I'm trying to incorporate a video player on Canvas. My video has an alpha channel, and I want to play it over a jpg image. This is the code snippet I've put together: <video id="mainVideo" src="item0.mp4"> </video> <canvas id="m ...

Having trouble retrieving the complete URL on the Login Page within Shopify

I've been working on a feature where users are redirected to different URLs based on the page they click the login button on. For example, I have a registration page and a regular page, both with a login button. When a user clicks the login button on ...

Styling Labels with CSS Wildcard

I have been using the free NinjaForms plugin on my WordPress site. The default styling for labels is bold, but I prefer them to be lighter. Currently, I achieve this through CSS by targeting individual field IDs. However, this method becomes tedious when a ...

Updating the underline style of ant.d's menu item

Currently, I am working with ant.design to build my navbar. By default, the menu items in ant design have a blue-ish underline when selected or hovered over. However, I want to change this underline color to match the overall design of my project. I was su ...

What steps can be taken to repair the cell within the GridView?

My issue is specific to Internet Explorer. I am using the GridView control with a column displaying months (Dec-2011, Jan-2012, etc). In Firefox, they appear on one line as intended: Jan-2012 However, in IE, it breaks them up like this: Jan- 2012 The ...

What is the method for transforming a portion of text from a website into HTML elements?

Currently, my workflow involves copying text from a webpage and then extracting the html tags. I go through the process of selecting the text, visiting wordtohtml.net, pasting it there, and finally copying the output with the necessary html tags and class ...

Dealing with Issues of HTML and CSS Positioning

Why are my buttons not maintaining their positions correctly when the window is resized? I thought it should work as intended. In my previous inquiry, I was able to successfully position elements for window resizing (CSS Top Relative To Screen). However, ...

When Vue 3 is paired with Vite, it may result in a blank page being rendered if the

Issue with Rendering Counter in Vite Project After setting up a new project using Vite on an Arch-based operating system, I encountered a problem when attempting to create the simple counter from the Vue documentation. The element does not render as expec ...