CSS Display Property Tutorial 🎯

beginner
13 min

CSS Display Property Tutorial 🎯

Welcome to our CSS Display Property tutorial! This lesson will guide you through understanding and mastering one of the fundamental CSS properties that help you control the layout and visibility of HTML elements. Let's dive in! 🐳

What is the CSS Display Property? 📝

The display property in CSS is a powerful tool that allows you to define how a block-level element will be rendered on the page. You can change the nature of an element from an inline box to a block box, an inline-block, or even something more complex like a table cell or an inline-flex container.

Basic Display Values 💡

display: block

When you set an element's display property to block, it will render as a standalone block-level box. A block-level box will start on a new line and take up the full width available, extending to the left and right margins.

css
div { display: block; border: 1px solid red; padding: 10px; width: 200px; height: 100px; }

display: inline

When you set an element's display property to inline, it will flow along the line with other inline elements, taking up as much space as necessary.

css
span { display: inline; border: 1px solid blue; padding: 10px; margin-right: 10px; }

Other Useful Display Values 💡

display: inline-block

The inline-block value treats an element as if it were inline but allows you to set width, height, margin, and padding like a block-level element.

css
div { display: inline-block; border: 1px solid green; padding: 10px; width: 200px; height: 100px; margin-right: 10px; }

display: flex

The flex value enables Flexbox, a powerful and flexible (pun intended) layout model that allows you to create complex layouts easily.

css
div { display: flex; border: 1px solid purple; padding: 10px; }

Quiz 💡

Quick Quiz
Question 1 of 1

Which display value makes an element a standalone block-level box?

Quick Quiz
Question 1 of 1

Which display value treats an element as if it were inline but allows you to set width, height, margin, and padding like a block-level element?

Happy learning, and stay curious! 🌟