Best Website-BuildersBest Website-Builders
    What's Hot

    Venture Capital Investors Are Trying to Take Over Pandemic Response

    March 12, 2023

    Judge in abortion-pill lawsuit schedules hearing but delays announcing it

    March 12, 2023

    I Tried Bing AI’s Celebrity Mode on 7 Famous Stars

    March 12, 2023
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    Best Website-BuildersBest Website-Builders
    • Home
    • CSS

      Best AI Tools for Web Designers (2023)

      March 12, 2023

      PSPad 5.0.7.770 | Neowin

      March 11, 2023

      Battle of Memphis

      March 11, 2023

      How to create a recipe book using HTML, CSS and JavaScript

      March 11, 2023

      Cubist Systematic Strategies LLC Increases Holding in Hennessy Capital Investment Corp.

      March 11, 2023
    • Joomla

      Pros, Cons, & Pricing Compared

      March 11, 2023

      Give your website a place to call home for a lifetime of web hosting for just $100

      March 11, 2023

      12 Best Free Web Hosting Sites to Choose From

      March 10, 2023

      cPanel vs SPanel: Which is the Better Web Hosting Control Panel?

      March 10, 2023

      Web Content Management Systems Market Business Growth Potential 2023-2030

      March 6, 2023
    • PHP

      Man sues ex-wife’s friend for helping ex-wife get abortion

      March 11, 2023

      Perfect indoor and outdoor slippers to wear around the house or on errands

      March 11, 2023

      Review: Maggie Milner’s ‘Couplets’

      March 11, 2023

      “Hasta Cuando” by Kari Uchis, everyone’s favorite

      March 11, 2023

      TikToker changed vintage dresses and people got really mad

      March 11, 2023
    • UX

      New York City worker saw company hiring for her job but paid $90,000 more

      March 11, 2023

      Best March Madness Apps of 2023: NCAA, ESPN, CBS

      March 11, 2023

      Best March Madness Apps of 2023: NCAA, ESPN, CBS

      March 11, 2023

      I found my job listing with a much higher salary, so I reapplied

      March 10, 2023

      MD&M Minneapolis Calls for Speakers for 2023 Conference

      March 10, 2023
    • Web Builders
      1. Web Design
      2. View All

      What Comes First in Website Development — Design or Copy?

      February 2, 2023

      Modern Campus Honors Best Higher Education Websites of 2022

      February 2, 2023

      Premier SEO Consultant in Las Vegas, Nevada with Unparalleled Customer Service

      February 2, 2023

      Can Religious Freedom Be Saved? This group is racing the clock to teach America’s first freedom

      February 2, 2023

      How i Create New Google Account

      February 7, 2023

      CWT powers tools for meeting and event planners

      January 31, 2023

      Best Website Builder – Website Builders

      January 24, 2023

      Is There A Market For Rap-Themed Slot Games? – Rap Review

      January 19, 2023
    • WordPress

      Quordle today – Tips and Answers for Sunday, March 12 (Game #412)

      March 12, 2023

      This little-known outfit sells for just $2,700 on a 98-inch screen with a laser pointer.

      March 12, 2023

      Netflix finally gets around to adding subtitle options for TV

      March 11, 2023

      3 reasons why now is a good time to buy a new vacuum cleaner

      March 11, 2023

      Samsung Galaxy Z Fold 5 leak shows no main camera upgrade

      March 11, 2023
    • Realtoz
      • Our Other Sites
    • More News
    Best Website-BuildersBest Website-Builders
    Home » Which CSS unit should I use?
    CSS

    Which CSS unit should I use?

    websitebuildersnowBy websitebuildersnowOctober 18, 2022No Comments6 Mins Read
    Facebook Twitter LinkedIn Telegram Pinterest Tumblr Reddit WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    You will learn some CSS units for customizing the font size of text when creating web pages. There are many units such as pt, pc, ex, but most of the time you should focus on the three most common units: px, em, and rem. Many developers usually don’t understand the difference between these units. So below is a detailed description of these units.


    Before proceeding, note that there are two types of unit length. absolutely and Relative.


    absolute length

    Absolute length units are fixed, and a length represented by one of these will appear exactly as its size.

    Screen sizes vary greatly, so we don’t recommend using absolute length units with screens. However, if you know the output medium, such as print layout, you can use it.

    unit

    explanation

    cm

    Centimeter

    mm

    mm

    of

    inches (1in = 96px = 2.54cm)

    pixel *

    Pixel (1px = 1/96th of an inch)

    point

    Point (1pt = 1/72nd of an inch)

    computer

    Picas (1pc = 12pt)

    relative length

    A relative length unit specifies a length relative to another length property. Relative length units scale well between different rendering media.

    unit

    in relation to

    M*

    relative to the element’s font-size (2em means twice the size of the current font)

    Yuan

    Relative to current font’s x-height (rarely used)

    Channel

    Based on the width of “0” (zero)

    Rem*

    relative to the font-size of the root element

    vw

    For 1% of viewport width*

    vh

    Relative to 1% of viewport height*

    vmin

    For 1% of *smaller dimension of viewport

    vmax

    For 1% of the *large dimension of the viewport

    %

    relative to parent element

    1.Px (pixel)

    Pixels are probably the most used unit in CSS and are very popular for setting the font size of text on web pages. One pixel (1px) is defined as 1/96th of an inch on the print media.

    However, computer screens generally have nothing to do with actual measurements such as centimeters or inches. They are only defined to be small but visible. What is considered displayed depends on the device.

    Different devices have different pixels per inch on the screen. This is called pixel density. If you use the physical number of pixels on a device’s screen to determine the size of content on that device, it’s difficult to make it look the same on all screen sizes.

    That’s where the device’s pixel ratio comes into play. It’s basically just a way to calculate how much space a CSS pixel (1 pixel) occupies on a device’s screen so that it looks the same size when compared to another device.

    An example is shown below.

     <div class="container">
         <div>
           <h1>This is a paragraph</h1>
           <p>
             Lorem ipsum, dolor sit amet consectetur adipisicing elit.
             Reprehenderit incidunt perferendis iure veritatis cupiditate delectus
             omnis at! Officiis praesentium officia, nemo veniam consequatur
             nostrum sunt aliquid ipsam, corporis quas quaerat. Lorem ipsum dolor
             sit amet consectetur adipisicing elit. Hic quam beatae voluptatibus
             amet possimus iure impedit assumenda distinctio aliquid debitis, autem
             vel ullam aut, quod corporis ratione atque ducimus dolorum.
           </p>
         </div>
       </div>
     .container {
      width: 100%;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .container div {
      max-width: 500px;
      padding: 5px 20px;
      border: 1px grey solid;
      border-radius: 10px;
    }
    p {
      font-size: 16px;
    }

    output

    web-content-sized-in-pixel-css-unit-of-measurement

    The top box is what it looks like when viewed on a large screen such as a laptop, and the bottom box is what it looks like when viewed on a small screen such as a phone.

    Notice that the text in both boxes is the same size. This is basically how pixels work. This ensures that web content (not just text) looks the same size across devices.

    2. M (M)

    The em unit got its name from the uppercase “M” (em), since most CSS units come from typography. Uses the parent element’s current font size as a base. Can be used to increase or decrease an element’s font size based on the font size inherited from its parent.

    Let’s say you have a parent div with a font-size of 16px. If you create a paragraph element in that div and give it a font-size of 1em, the font-size of the paragraph will be 16px.

    However, if you specify a font-size of 2em for another paragraph, it will be converted to 32px. Consider the following example.

        <div class="div-one">
          <p class="one-em">1 em based on 10px</p>
          <p class="two-em">2 em based on 10px</p>
      </div>
        <div class="div-two">
         <p class="one-em">1 em based on 10px</p>
         <p class="two-em">2 em based on 10px</p>
        </div>
       </div>
     .div-one {
      font-size: 15px;
    }
    .div-two {
      font-size: 20px;
    }
    .one-em {
      font-size: 1em;
    }
    .two-em {
      font-size: 2em;
    }

    output

    Web content resized in em css units of measure

    You can see how the em scales the size of the text and how the current font size inherited from the parent container affects it. Using em is not recommended, especially on pages with complex structures.

    Improper use can lead to scaling issues where elements are not properly sized based on the complex inheritance of DOM tree sizes.

    3. Rem (Root Em)

    Rem works in much the same way as em, the main difference is that rem only refers to the font size of the root element of the page, not the font size of its parent. The root font-size is the default font-size specified by the user in the browser settings or by the developer.

    The default font size for web browsers is usually 16px, so 1rem is 16px and 2rem is 32px. But if for example the root font-size is changed to 10px then: 1rem is 10px, 2rem is 20px.

    Here’s an example for clarity:

        <div class="div-one">
         
         <p class="one-em">1 em based on container (40px)</p>
         <p class="two-em">2 em based on container (40px)</p>
         
         <p class="one-rem">1 rem based on root (16px)</p>
         <p class="two-rem">2 rem based on root (16px)</p>
       </div>
     .div-one {
      font-size: 40px;
    }
    .one-em {
      font-size: 1em;
    }
    .two-em {
      font-size: 2em;
    }
    .one-rem {
      font-size: 1rem;
    }
    .two-rem {
      font-size: 2rem;
    }

    output

    Web content sized with the rem css unit of measure

    As you can see, paragraphs defined in REM units are completely unaffected by the font size declared in the container and are rendered strictly relative to the root font size defined in the HTML element selector.

    Px vs. Em vs. Rem: Who is the strongest unit?

    Em is not recommended as it can have complex hierarchies of nested elements. REM will generally scale well with the new default/base font size specified in your browser settings as opposed to PX. This explains why you should use REM when working with textual content in web pages. REM wins the race. Using REM to improve content styling and scaling adds flair to your site as it is ideal for website authors. Proper measurement of your content will determine the look and feel of your website, but you have to be creative.



    Source link

    Share this:

    • Tweet
    • Email
    • Pocket
    • Mastodon
    • WhatsApp
    • Telegram
    • Share on Tumblr
    • Print
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleUX design and research gain momentum as a key career role
    Next Article Best website builders for musicians and bands 2022
    websitebuildersnow
    • Website

    Related Posts

    Best AI Tools for Web Designers (2023)

    March 12, 2023

    PSPad 5.0.7.770 | Neowin

    March 11, 2023

    Battle of Memphis

    March 11, 2023
    Add A Comment

    Leave a Reply Cancel reply

    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    Advertisement
    Demo

    This website provides information about CSS and other things. Keep Supporting Us With the Latest News and we Will Provide the Best Of Our To Makes You Updated All Around The World News. Keep Sporting US.

    Facebook Twitter Instagram Pinterest YouTube
    Top Insights

    Venture Capital Investors Are Trying to Take Over Pandemic Response

    March 12, 2023

    Judge in abortion-pill lawsuit schedules hearing but delays announcing it

    March 12, 2023

    I Tried Bing AI’s Celebrity Mode on 7 Famous Stars

    March 12, 2023
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2023 bestwebsite-builders. Designed by bestwebsite-builders.
    • Home
    • About us
    • Contact us
    • DMCA
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.