Hide Empty Elements

CSSCSSBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the :empty pseudo-class in CSS, which allows us to select and hide elements that have no content. By applying this technique, we can improve the overall design and readability of our web pages by removing clutter and empty spaces. Through a series of exercises, we will learn how to effectively use the :empty pseudo-class to hide empty elements and create cleaner and more polished web pages.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL css(("`CSS`")) -.-> css/BasicConceptsGroup(["`Basic Concepts`"]) css(("`CSS`")) -.-> css/CoreLayoutGroup(["`Core Layout`"]) css(("`CSS`")) -.-> css/IntermediateStylingGroup(["`Intermediate Styling`"]) css/BasicConceptsGroup -.-> css/selectors("`Selectors`") css/CoreLayoutGroup -.-> css/display_property("`Display Property`") css/IntermediateStylingGroup -.-> css/pseudo_classes("`Pseudo-classes`") subgraph Lab Skills css/selectors -.-> lab-35208{{"`Hide Empty Elements`"}} css/display_property -.-> lab-35208{{"`Hide Empty Elements`"}} css/pseudo_classes -.-> lab-35208{{"`Hide Empty Elements`"}} end

Hide Empty Elements

index.html and style.css have already been provided in the VM.

To hide elements with no content, use the :empty pseudo-class. For example, if you have the following HTML code:

<p>Lorem ipsum dolor sit amet. <button></button></p>

You can use the following CSS code to hide the button element with no content:

p:empty {
  display: none;
}

Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.

Summary

Congratulations! You have completed the Hide Empty Elements lab. You can practice more labs in LabEx to improve your skills.

Other CSS Tutorials you may like