Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 162861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:27:58+00:00 2026-05-11T11:27:58+00:00

I heard you should define sizes and distances in your stylesheet with em instead

  • 0

I heard you should define sizes and distances in your stylesheet with em instead of in pixels. So the question is why should I use em instead of px when defining styles in CSS? Is there a good example that illustrates this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:27:58+00:00Added an answer on May 11, 2026 at 11:27 am

    The reason I asked this question was that I forgot how to use em’s as it was a while I was hacking happily in CSS. People didn’t notice that I kept the question general as I wasn’t talking about sizing fonts per se. I was more interested in how to define styles on any given block element on the page.

    As Henrik Paul and others pointed out em is proportional to the font-size used in the element. It’s a common practice to define sizes on block elements in px, however, sizing up fonts in browsers usually breaks this design. Resizing fonts is commonly done with the shortcut keys Ctrl++ or Ctrl+-. So a good practice is to use em’s instead.

    Using px to define the width

    Here is an illustrating example. Say we have a div-tag that we want to turn into a stylish date box, we may have HTML-code that looks like this:

    <div class='date-box'>     <p class='month'>July</p>     <p class='day'>4</p> </div> 

    A simple implementation would defining the width of the date-box class in px:

    * { margin: 0; padding: 0; }  p.month { font-size: 10pt; }  p.day { font-size: 24pt; font-weight: bold; }  div.date-box {     background-color: #DD2222;     font-family: Arial, sans-serif;     color: white;     width: 50px; } 

    The problem

    However, if we want to size the text up in our browser the design will break. The text will also bleed outside the box which is almost the same what happens with SO’s design as flodin points out. This is because the box will remain the same size in width as it is locked to 50px.

    Using em instead

    A smarter way is to define the width in ems instead:

    div.date-box {     background-color: #DD2222;     font-family: Arial, sans-serif;     color: white;     width: 2.5em; }  * { margin: 0; padding: 0; font-size: 10pt; }  // Initial width of date-box = 10 pt x 2.5 em = 25 pt // Will also work if you used px instead of pt 

    That way you have a fluid design on the date-box, i.e. the box will size up together with the text in proportion to the font-size defined for the date-box. In this example, the font-size is defined in * as 10pt and will size up 2.5 times to that font size. So when you’re sizing the fonts in the browser, the box will have 2.5 times the size of that font-size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read that you should define your JavaScript functions in the <head> tag, but
I've developed a little screenshot application but I've heard that I should profile my
I have heard umpteen times that we 'should not mix business logic with other
I've heard that the static_cast function should be preferred to C-style or simple function-style
I have heard people state that Code Generators and T4 templates should not be
I have heard/read the term but don't quite understand what it means. When should
I heard that if you use port 443 (the port usually used for https)
I would like to hear other people's advice on when one should build a
It's fall of 2008, and I still hear developers say that you should not
Hello I am working on this design, but there seems to be a problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.