Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set CSS style for child nodes in style attribute

Tags:

html

css

Is it possible to set children nodes's styles from within the style of a parent without using the <style> element in <head>?

E.g. I have a table where I have a row (first row below), and I want all text in the TD elements to be font size 7pt.

Something like:

  <table>
    <tr style='font-size:7pt;'>  <--- How do I tell it to apply to child TD elements
      <td>cell 1 should be formatted to 7pt font</td>
      <td>cell 2 should be formatted to 7pt font</td>
    </tr>
    <tr>
      <td>cell without format</td>
      <td>another cell without format</td>
    </tr>
  </table>

Thanks, Grant

like image 320
grantstead Avatar asked Jul 05 '12 14:07

grantstead


People also ask

How do I apply CSS from parent to child?

It's easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinators (>), which are placed between two CSS selectors.

How do you write a CSS selector for child elements?

The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

How do I use child tags in CSS?

The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.


1 Answers

<table>
<tr class="format">
  <td>cell 1 should be formatted to 7pt font</td>
  <td>cell 2 should be formatted to 7pt font</td>
</tr>
<tr>
  <td>cell without format</td>
  <td>another cell without format</td>
</tr>

<style>.format>td{font-size:7pt;}</style>
like image 170
Alexandre Avatar answered Sep 28 '22 00:09

Alexandre