Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS element child vs child with text node

I have an element that has a child and another element with the same child, but there is also a text node:

<p><strong>This should be heading</strong></p>
...
<p>There is a sentence that has <strong>strong text</strong> inside it.</p>

I do not have the ability to modify the DOM structure in any way, including no JavaScript. All I can do is edit CSS, but I want to style the inline strong differently than the strong that is the only child.

I thought this might work:

p strong:only-child
{
    color: red;
}

However, both items turn red.

Is there a way to target child nodes that don't have text node siblings, using only CSS?

I don't think it can be done, but I figured I'd ask in case there is some sort of clever work-around.

like image 954
Jeff Jenkins Avatar asked Aug 01 '13 13:08

Jeff Jenkins


People also ask

What is a Childnode?

Any subnode of a given node is called a child node, and the given node, in turn, is the child's parent. Sibling nodes are nodes on the same hierarchical level under the same parent node. Nodes higher than a given node in the same lineage are ancestors and those below it are descendants.

How do I apply CSS to child element based on parent?

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 combinator ( > ), which is placed between two CSS selectors. For example, div > p selects all <p> elements where the parent is a <div> element.

Which is the correct way to select child element?

Child Selector: Child Selector is used to match all the elements which are children of a specified element. It gives the relation between two elements. The element > element selector selects those elements which are the children of the specific parent.

What is a child element in CSS?

Introduction to CSS Child Selector. CSS child Selector is defined as the CSS selector that selects only elements that are direct children which is clearer than the contextual selector.


1 Answers

As per my understanding I think it should be like

p:first-child
{
    color: red;
}

JSFiddle

like image 108
Praveen Avatar answered Nov 15 '22 06:11

Praveen