Utilizing CSS to customize individual elements

I'm looking to apply font-variant: small-caps specifically to the text within a TD

However, when using the CSS code below, it ends up applying small-caps to all content inside the TD:

td {
font-variant: small-caps;
}

My goal is to have small-caps applied only to the text inside a TD that does not include any a href or input elements. Unfortunately, I am unable to use classes in this scenario.

I attempted the following approach:

td:not(a) {
font-variant: small-caps;
}

However, this did not yield the desired outcome. Is there a way to achieve this?

Thank you.

Answer №1

In order to customize the font style for child elements like anchors, you will need to specifically target and overwrite the font-variant property.

td {
  font-variant: small-caps;
}

// Update styles for children elements such as anchors and inputs within the cell

td a, td input {
  font-variant: initial;
}

// Alternatively, use simplified syntax

td :is(a, input) {
  font-variant: initial;
}

// Target immediate children only

td > :is(a, input) {
  font-variant: initial;
}

The problem with using td:not(a) is that a table cell (td) cannot also be an anchor (a) element. To achieve a similar effect, separate them by a space to select the children of the cell instead. The success of this approach depends on your HTML structure and whether text-only cells contain paragraphs without anchors or inputs.

Answer №2

One straightforward suggestion I have is:

  • If any text content is directly placed inside a td element, it will be styled with small-caps.
  • If the text content is within a child element of a td, it will have the style of initial.

Here is a functional code snippet. If you need to adjust for specific element types, you can modify the CSS selector accordingly.

table {
  border: 1px solid #ddd;
}

td {
  font-variant: small-caps;
  padding: 16px;
}

td>* {
  font-variant: initial;
}
<table>
  <tr>
    <td>
      All text as a direct child
    </td>
    <td>
      <a href="#">All text inside hyperlink</a>
    </td>
  </tr>
  <tr>
    <td>
      <label for="name">
        <input id="name" name-"name" type="text" placeholder="text input..." >
      </label>
    </td>
    <td>
      Part of the text is <a href="#">inside hyperlink</a>
    </td>
  </tr>
</table>

Answer №3

When executing the following:

td {font-variant: small-caps}
td * {font-variant:normal}

You can expect to see uppercase text if it is directly within the td, but lowercase text for text in tags inside the td.

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

Designing the footer for small devices using Bootstrap 4, including layout, responsiveness, and hiding functionalities

Just diving into the world of Bootstrap 4 and facing a challenge with my page footer layout. I have a mockup that I'm trying to replicate: https://i.sstatic.net/pXyMR.png This is what I've come up with so far: index.html (snippet): <!-- foo ...

"Stylish footer design in CSS featuring a sleek overflow scrollbar

Perhaps the most straightforward way to demonstrate this is by providing a direct link to the website where the addition should be made. I am in desperate need of a basic footer that remains fixed at the bottom of the page, regardless of the amount of con ...

Vue.js component still not transitioning out despite using mode="out-in"

As I navigate my way through learning about vue.js, one issue that has been puzzling me is how to make routed pages fade in and out when a user switches to a new page. Despite thinking it would be a simple task with vue, I have been struggling quite a bit ...

The margin-top and margin-bottom properties are not functioning as expected

Check out this source: <div style="border:1px solid red; margin-bottom:10px">test blockA,</div>; <div style="border:1px solid red; margin-top:10px">test blockB</div>; There is consistently a spacing of 10px between blockA and ...

What is the best way to eliminate the lower margin in the UI-Boostrap Angular Tab directive?

The ui.bootstrap.tabs directive, which can be found here, seems to have a default margin before displaying its content. https://i.stack.imgur.com/NECGA.png Here is the HTML snippet: <uib-tabset active="activeJustified" justified="false" id="tabSet" s ...

What are the steps to stop the left alignment of header text?

As I'm working on designing a unique Markdown theme in CSS, I am currently facing some challenges with regards to the styling of headers. Specifically, the h1 header is styled as follows: h1 { border-top: 1px solid black; width: 10%; margin: 0 ...

Can parameters be included in the HTML class attribute?

Is it possible to embed specific information into HTML elements, like images, without disrupting valid HTML markup? Would the following example be considered valid HTML? <img src="..." class="isearcher:tags(paris+hilton,gary+suneese)" > The idea is ...

Circular center button in the footer UI

Struggling with aligning a button in the footer perfectly? I have two icons on each side and while the UI is almost there, something seems off with the center icon's padding and shadow. I'm currently working with material-ui. Take a look at the i ...

Sometimes, CSS unicode characters may not render correctly and appear as jumbled symbols like "â–¾"

https://i.sstatic.net/eGvzJ.jpg Every now and then, about 1 in every 100 page refreshes, the unicode characters on my page turn into jumbled nonsense (you might need to zoom in to see it clearly). Specifically, the unicode characters are displayed as â ...

Encountering a CSS issue during the edit process with PHP and JavaScript

I'm encountering an issue when clicking on the edit button, as my data fetched from the DB should be displayed inside a text field. However, I'm facing a CSS-related error: Uncaught TypeError: Cannot read property 'style' of null Belo ...

What could be causing the spinner (Bootstrap 5.x) to not show up on the screen

I'm having trouble getting a spinner to show up when a user clicks a form submit button. Below is the HTML code to add a <div> for the spinner: <div class="pageloader"> <img src="../images/loader-large.gif" alt ...

Troubleshoot: Bootstrap Table Alignment Problem

I've been struggling to align my table header and content properly, as shown in the image. I want the header aligned to the right while keeping the rest of the content aligned to the left of the header. Is there a way to achieve this using only CSS wi ...

Customizing the appearance of the 'Submit' button compared to the <a href=""></a> button using CSS

I am experiencing some issues. Even though the CSS code for these two buttons is exactly the same, their appearance is different. I am unable to make the :hover or :active effects work either. My goal is to have the left 'input type="submit' but ...

What is the best way to customize the lower border color of a collapsed Bootstrap navbar with an inverse design

Discussing this issue.. I am noticing two distinct colors. One is darker than the background shade and the other is lighter than the background shade. How can these be set? https://i.sstatic.net/Y39IZ.jpg <nav class="navbar navbar-inverse"> <d ...

Wordpress articles refuse to appear side by side

I've been at it for hours now, trying to solve this issue with no luck. I have a Wordpress loop on my homepage that displays post thumbnails vertically, but I want them to appear in sets of three images before starting a new line. Here's the cod ...

Toggling a div overlay through various user interactions such as clicking, pressing a button, or hitting the escape

I've created a simple div overlay that appears when a user clicks inside an input box: HTML <html> <body> <input id="search" type="text" placeholder="Search..."> </body> </html> CSS #overlay { position: fixed; ...

Menu with option to delete next to each selection item

My task is to create a drop-down menu with a delete 'X' option next to each item. This functionality should work even when the drop-down is dynamically populated, without resorting to using a list as an alternative. UPDATE: The icons next to eac ...

Using the <li> element as the primary container for a series of <li>'s within a <ul> is

For my form requirement, I am utilizing Jotforms. After downloading the code, I am currently in the process of customizing the CSS and HTML to fulfill my design needs. Jotforms uses a set of ul , li for managing its HTML structure. However, I encountered ...

Aligning items in a parent div to ensure that one element takes up the remaining space

I'm facing an issue with a container div called .audiojs that has a width of 100%. Inside this container, there are multiple elements with fixed widths and one element with a dynamic width (.scrubber with a width of 30%). The problem arises when I try ...

Is the CSS property `backface-visibility` causing compatibility issues across different browsers with animations?

I have created a unique flip animation that displays new numbers resembling an analog clock or calendar with a hinge in the middle. The process is simple: using a div, I have: The bottom half of the first number on one side The top half of the second num ...