Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only child in CSS with a text sibling

I want to make the following inner span disappear, if it's an only child:

<div class="foo">
  <span></span>  
</div>

i.e. the selector should not work in this case:

<div class="foo">
  <span></span>  
  Some text
</div>

I tried :only-child and :last-child which don't work, I assume because of that "Some text" text.

like image 923
h bob Avatar asked Oct 26 '15 09:10

h bob


People also ask

How do I target a sibling in CSS?

Adjacent Sibling Selector (+) The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element, and "adjacent" means "immediately following".

Can I use CSS only child?

The :only-child CSS pseudo-class represents an element without any siblings. This is the same as :first-child:last-child or :nth-child(1):nth-last-child(1) , but with a lower specificity.


1 Answers

I think this cannot be done with only CSS. You should use jQuery or JavaScript by making some conditions.

if ($(".foo").text().length < 1 && $('.foo span').is(':empty')) {
    //hide your element here
    $('.foo').hide();
}
like image 163
Ionut Avatar answered Oct 06 '22 05:10

Ionut