Questions
1 of 30
1How can you centre text vertically and horizontally inside a box?
2What is the use of the white-space property?
3How can you truncate text with an ellipsis (...) in CSS?
4How do you wrap or prevent wrapping of text in a container?
5How do you make text responsive across different screen sizes?
6How do you justify text using CSS?
7What does text-shadow do? Give an example.
8How can you apply multiple shadows to text?
9How do you apply gradients to text in CSS?
10How do you control text overflow in a fixed-width container?
11What is the difference between em, rem, and px when styling text?
12How does font-weight differ from <b> tag in semantic meaning?
13How does accessibility relate to text formatting in HTML?
14What’s the difference between semantic and non-semantic text formatting tags?
15How can you ensure consistent text appearance across different browsers?
16What is the difference between font-style, font-variant, and font-weight?
17What is the role of @font-face in web typography?
18How can you import custom fonts in CSS?
19What’s the impact of using web fonts on performance?
20How can you optimise text rendering for readability?
21How do you make text appear vertically (top-to-bottom) using CSS?
22How do you style only the first letter or first line of a paragraph?
23How do you create a drop cap effect?
24How can you highlight a selected text range dynamically?
25How do you make text glow or have a neon effect using CSS?
26What CSS properties can you use for text animations?
27How can you apply a gradient background only to the text and not the entire element?
28How can you apply different styles to different words in the same line?
29How do you prevent text selection on a webpage?
30How do you implement responsive typography (fluid text size)?
01 / 30

How can you centre text vertically and horizontally inside a box?

Difficulty: 3/10
flexbox, grid, legacy layout

Centering Text in a Box Using CSS

There are several ways to center text both vertically and horizontally inside a container. The best method depends on whether the container has a fixed height or uses flexible layout systems like Flexbox or Grid.

Common Methods for Centering Text
  1. 1

    Using Flexbox: Set the container to display: flex, then use justify-content: center for horizontal alignment and align-items: center for vertical alignment.

  2. 2

    Using Grid: Set the container to display: grid and use place-items: center to center content both vertically and horizontally.

  3. 3

    Using Positioning: With position: relative on the container and position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) on the text, you can center it regardless of container size.

  4. 4

    Using Line Height: For single-line text in a fixed-height box, set line-height equal to the container height to center vertically and use text-align: center for horizontal centering.

Example: Flexbox Centering
Example: Grid Centering
Example: Absolute Positioning Centering

These methods allow precise vertical and horizontal centering, whether you have dynamic content, multiple lines of text, or responsive layouts.

Key Takeaways
  1. 1

    Flexbox and Grid are the most flexible and recommended methods for centering content.

  2. 2

    Absolute positioning works well for fixed-size containers or overlay elements.

  3. 3

    Line-height method is limited to single-line text with a fixed container height.

  4. 4

    Always test centering in responsive scenarios to ensure consistent alignment.

Scenario Questions

0-2 years experience

  1. 1You have a <div class="card"> that contains a single <p>. Write the CSS needed to center the paragraph both vertically and horizontally inside the card.
  2. 2If you try to vertically center the text by setting line-height equal to the container height, what happens when the paragraph wraps onto multiple lines?
  3. 3What does applying only text-align:center do to the text inside a fixed‑height box?

2-5 years experience

  1. 1We need a label centered inside a button whose width and height change based on its content. Which CSS layout method would you pick and why?
  2. 2After adding a 2px border to the container, the text stopped appearing centered. Walk me through how you’d debug that issue.
  3. 3Our design system mixes Flexbox and Grid. How would you decide which to use for a reusable Card component that must center its title text?

5-8 years experience

  1. 1Our page renders thousands of cards in a virtualized list. Discuss any performance implications of using Flexbox versus Grid for centering text in each card.
  2. 2The legacy codebase uses absolute positioning to center text. Propose a migration plan to modern CSS while avoiding layout shifts across browsers.
  3. 3How would you make the centering solution robust for right‑to‑left languages and for users who increase the base font size?

8+ years experience

  1. 1We are merging UI libraries from three product teams into a single design system. How would you define a centering utility that works across React, Angular, and plain HTML, and what governance would you put in place to keep implementations consistent?
  2. 2If we must also support email clients that only understand table‑based layouts, how would you reconcile that with our modern CSS centering approach across the platform?

Follow-up Questions

  • What edge cases does your solution need to handle?
  • How would you verify it works across major browsers and devices?
  • What trade‑offs did you consider when choosing Flexbox vs Grid?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.