javascript - How can I remove element's height when its value is empty? - Stack Overflow

I have a HTML structure like this:li{list-style: none;border-bottom: 1px solid;padding:4px;}div{backgr

I have a HTML structure like this:

li{
    list-style: none;
    border-bottom: 1px solid;
    padding:4px;
}

div{
    background-color: #eee;
    padding: 6px 0;
}
<ul>
  <li>
    <span>something</span>
    <div></div>
  </li>
  <li>
    <span>something</span>
    <div>something else</div>
  </li>
</ul>  

I have a HTML structure like this:

li{
    list-style: none;
    border-bottom: 1px solid;
    padding:4px;
}

div{
    background-color: #eee;
    padding: 6px 0;
}
<ul>
  <li>
    <span>something</span>
    <div></div>
  </li>
  <li>
    <span>something</span>
    <div>something else</div>
  </li>
</ul>  

And this is expected result:

I guess I can do that by JS, something like this:

if ( $('div').val() == '' ) {
    $('div').hide();
}

But I want to know can I do that by pure HTML and CSS ?

Share Improve this question asked Aug 20, 2016 at 0:07 stackstack 10.2k22 gold badges70 silver badges130 bronze badges 2
  • Well why my question has earned one vote to be close as unclear ? Really is it unclear? I've provided a screenshot of the expected result... still it is unclear ?! – stack Commented Aug 20, 2016 at 0:11
  • 1 Possible duplicate of How to hide/remove a DIV when empty – Calum Commented Aug 20, 2016 at 0:13
Add a ment  | 

4 Answers 4

Reset to default 10

You can use the :empty pseudo selector:

 div:empty {
    display: none;
 }

li{
    list-style: none;
    border-bottom: 1px solid;
    padding:4px;
}

div{
    background-color: #eee;
    padding: 6px 0;
}

div:empty {
    display: none;
}
<ul>
  <li>
    <span>something</span>
    <div></div>
  </li>
  <li>
    <span>something</span>
    <div>something else</div>
  </li>
</ul>  

If you need old browser support, you could just use line height for spacing instead.

 li{
      list-style: none;
      border-bottom: 1px solid;
      padding:4px;
    }
    
    div{
      background-color: #eee;
      // padding: 6px 0;
      line-height: 2;
    }
<ul>
  <li>
    <span>something</span>
    <div></div>
  </li>
  <li>
    <span>something</span>
    <div>something else</div>
  </li>
</ul> 

use :empty | CSS-Trick

 div:empty{
       display:none;
      }

reference from :empty | css-Trick

You can use Jquery like

$('div:empty').hide();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742311790a4420038.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信