Introduction
The CSS display property allows you to change how an HTML element behaves. The three most commonly used display values are inline, block, and inline-block. Understanding the difference between these values is essential for creating navigation menus, buttons, forms, cards, and responsive layouts. In this chapter, you’ll practice these display values through simple solved questions that are easy for beginners to understand. CSS Inline Block and Inline-Block practice questions with solutions help to understand the concepts.
Question 1: Display a <span> as a Block Element
Problem Statement
Write CSS to display a <span> element as a block element.
CSS Code
span { display: block;}
Output
The <span> starts on a new line and occupies the available width.
Explanation
display: block;changes an inline element into a block element.- Block elements always begin on a new line.
- They use the available horizontal space by default.
Question 2: Display a <div> as an Inline Element
Problem Statement
Write CSS to display a <div> element as an inline element.
CSS Code
div { display: inline;}
Output
The <div> appears on the same line as surrounding elements.
Explanation
display: inline;removes the block behavior.- Inline elements do not start on a new line.
- Width and height properties do not work as expected on inline elements.
Question 3: Display a <div> as an Inline-Block Element
Problem Statement
Write CSS to display a <div> as an inline-block element.
CSS Code
div { display: inline-block;}
Output
The <div> stays on the same line while supporting custom width and height.
Explanation
inline-blockcombines the features of inline and block elements.- The element stays on the same line.
- Width, height, margin, and padding work normally.
Question 4: Create Horizontal Navigation Links
Problem Statement
Write CSS to display navigation links in a horizontal row.
CSS Code
a { display: inline-block;}
Output
Navigation links appear side by side instead of stacking vertically.
Explanation
inline-blockkeeps links on the same line.- It also allows padding and margin to be applied correctly.
- This is commonly used for navigation menus.
Question 5: Convert a Link into a Block Element
Problem Statement
Write CSS to make every hyperlink behave like a block element.
CSS Code
a { display: block;}
Output
Each hyperlink appears on a new line and occupies the available width.
Explanation
display: block;makes the entire link area clickable.- Each link starts on a new line.
- This style is commonly used for vertical side menus.
Question 6: Display List Items as Inline Elements
Problem Statement
Write CSS to display all list items on the same line.
CSS Code
li { display: inline;}
Output
All list items appear horizontally on a single line.
Explanation
display: inline;makes each list item behave like an inline element.- The items no longer start on a new line.
- This technique is commonly used for simple navigation menus.
Question 7: Create Equal-Width Buttons Using inline-block
Problem Statement
Write CSS to create buttons that appear on the same line with a fixed width.
CSS Code
button { display: inline-block; width: 150px;}
Output
Buttons appear side by side, and each button has a width of 150px.
Explanation
inline-blockkeeps the buttons on the same line.widthgives every button the same size.- This creates a clean and consistent button layout.
Question 8: Compare inline and inline-block
Problem Statement
Write CSS to display one <span> as inline and another as inline-block.
CSS Code
.inline { display: inline;}.inline-block { display: inline-block;}
Output
- The
.inlineelement stays on the same line but cannot use custom width and height. - The
.inline-blockelement also stays on the same line but supports width and height.
Explanation
inlineis suitable for small text elements.inline-blockprovides more layout flexibility.- Both values keep elements on the same line.
Question 9: Display Images as Block Elements
Problem Statement
Write CSS to display every image as a block element.
CSS Code
img { display: block;}
Output
Each image starts on a new line and behaves like a block element.
Explanation
display: block;removes the small space that often appears below inline images.- Images begin on a new line.
- This is useful for banners and large content images.
Question 10: Create a Horizontal Card Layout Using inline-block
Problem Statement
Write CSS to display multiple cards side by side using inline-block.
CSS Code
.card { display: inline-block; width: 200px; margin: 10px;}
Output
The cards appear next to each other with 10px spacing between them.
Explanation
display: inline-block;keeps cards on the same line.widthgives each card a fixed size.marginadds spacing between the cards.
Key Takeaways
display: inline;keeps elements on the same line.display: block;makes elements start on a new line.display: inline-block;combines inline positioning with block sizing.- Block elements support
widthandheight. - Inline elements ignore
widthandheight. inline-blockis useful for menus, buttons, cards, and image galleries.- Choosing the correct display value helps create clean and responsive layouts.
Frequently Asked Questions (FAQs)
1. What is the difference between inline, block, and inline-block elements?
- Inline elements stay on the same line and do not support custom width and height.
- Block elements start on a new line and use the available width.
- Inline-block elements stay on the same line while supporting width and height.
2. Which display value is best for navigation menus?
display: inline-block; is commonly used because it keeps menu items on one line while allowing padding, margin, and width.
3. Can I apply width and height to an inline element?
No. Most inline elements ignore the width and height properties.
4. Why is display: block; used for links?
It makes the entire link area clickable and places each link on a new line, which is useful for side menus.
5. When should I use inline-block?
Use inline-block when you want elements to appear on the same line while supporting width, height, margin, and padding.
6. Why are images sometimes displayed as block elements?
Displaying images as block elements removes the small gap below images and makes layout alignment easier.
7. Which display value is most flexible for beginners?
display: inline-block; is often the most flexible because it combines the advantages of both inline and block elements.x
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
