Clashing CSS Styles: Font Size Edition

Can someone explain how CSS font sizes work? I set a specific line to be 200% font size, but changing the body font size affected it. Shouldn't the line maintain its specified size? When the body font size is changed from 12pt to 8pt, the line "There is not much on it" becomes smaller. What is the new font size of the line if the body font size is now 8pt? Is it calculated as 200% * 8/12 = 133%? I'm new to this and confused.

    <!DOCTYPE html>
    <head>
    <link rel="stylesheet" href="mystyle.css">
    <style>
    p.bigger{font-size:200%}
    p.smaller{font-size:10pt}
    #intro{color:red}
    body{font-size:12pt}
    p{color:blue; font:Arial, sans-serif}
    h1{color:green}
    </style>
    </head>
    <body>
    <h1>Introduction</h1>
    <p id="intro">Welcome to my page</p>
    <p class="bigger">There is not much on it</p>
    <p class="smaller">Except this</p>
    <ul>
    <li>Short list</li>
    </ul>
    <h1 style="font-style:italic">Contact</h1>
    Here is my address
    </body>
    <html>

Answer №1

Utilizing the em unit for font sizes is recommended over using percentages.

This is because em values inherit from the parent element, such as the body tag.

For example, setting a font-size:2em is equivalent to 200% compared to a base size of 8px or 12px.


body { font-size:8px; }

thisFontSize { font-size:2em; }

The thisFontSize will be rendered as 16px.

OR

body { font-size:12px; }

thisFontSize { font-size:2em; }

The thisFontSize will be displayed as 24px.


If you need the size to be based on something other than the body tag, you can define it accordingly. Experiment with this sample in JSFiddle for better understanding.

JSFiddle: http://jsfiddle.net/SinisterSystems/7KR9t/

Answer №2

It functions in that manner, although an alternative means of expressing it is by utilizing:

2em

which achieves the identical result as 200%

Answer №3

A percentage represents a relative value, while pt/px indicates an absolute value. For example, if you have 200% of 12pt (24pt) or 200% of 8pt (16pt). It is recommended to assign an absolute value to p.bigger, such as p.bigger { font-size: 16pt }. This way, when adjusting the body font size, it will only impact elements with a percentage or those without a specified font size (h1 in your case). Remember that body encompasses all elements regardless of their sequence in the stylesheet, although it's advisable for it to be placed at the beginning.

Additionally, consider using header tags for titles and subtitles instead of overusing various classes solely for text sizing. Refer to this for an example.

Take a moment to read through this article which discusses the different available measurement units.

Answer №4

If a percentage is used in the font-size property of an element, it will be based on the font size of its parent element.

For example, if the parent element of your .bigger paragraph is body, then the final font-size will be 10pt * 1.5 = 15pt

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for incorporating a CSS class representing an image into a CodeIgniter form submission

<?php $data = array( 'class' => "btn btn-primary btn-lg", 'name' => 'report' ); echo '<span class="glyphicon glyphicon-exclamation-sign"></span> '; echo form_subm ...

Changing the Color of Hamburger Menu Lines in Bootstrap

How can I change the color of the hamburger menu icon's lines for medium and smaller screen sizes? I have attempted changing it through style attributes and using !important in my CSS file, but so far nothing has been successful. ...

Eliminate custom CSS generated by themes on WordPress category pages

I am a newcomer to the world of Wordpress and I'm currently in the process of making adjustments to an existing theme. The theme itself is generating custom CSS code and adding it to the wp_head section. add_action('wp_head', 'custom_c ...

Tips on how to properly position the navigation bar within a wrapper element

I'm having trouble fixing the navigation bar at the top of the wrapper when scrolling down. However, when I try using position: fixed in the #nav ul, the navigation bar extends beyond the wrapper. Below is my HTML code, where the nav bar should be con ...

Creating image links in this jQuery Slider plugin: A beginner's guide

Currently, I am attempting to insert links into each photo within the slider. While I have successfully done this on two other websites, I am encountering difficulties due to variations in the code structure. <a href="http://www.google.com I have expe ...

Why isn't my Bootstrap navbar expanding properly?

I am facing an issue with my toggle menu where it is not dropping down as a solid black block but instead in the center transparent. How can I fix this to make it drop down as a solid block and be positioned to the left? I have a bootstrap navbar that I mo ...

Don't forget the last click you made

Looking for a way to style a link differently after it has been clicked and the user navigates away from the page? Check out this code snippet I've been using: $(document).ready(function(){ var url = document.URL; function contains(search, find) { ...

The embedded CartoDB image header is set to have a style of "display:none"

I'm currently working on a CartoDB map showcasing my readers' summer fishing photos. Each row in the table has an image url, which I've placed at the top of the infowindow display list. I've enabled it and set the image window type to ...

Center-align the table element

I've been struggling to center align this table element, specifically the one containing the square, but nothing seems to be working. Can anyone lend a hand in fixing this issue? <table> <tbody> <tr> <td>TURKEY - SU ...

Issues with Bootstrap column rendering in IE11's Flex layout

I need assistance with the code below: .hero{ background:lightgrey; padding: 50px 0px; } <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E26 ...

Is there a way to showcase a block of Python code using a combination of HTML, CSS, and Javascript to enhance its

On my website, I want to display code blocks similar to how StackOverflow does it. The code block should be properly colored, formatted, and spaced out for better readability. All the code blocks on my site will be in python. def func(A): result = 0 ...

What is the best way to create a toggle button that can show more or show less of a text snippet with animation?

Is it possible for someone to assist me with showing a long text partially and providing a "show more" button to reveal the rest, along with a "show less" option, all with some CSS animation? I was thinking of using a font awesome arrow down icon for expan ...

Revisiting the Issue: Jquery Hover Effect Fails to Function in Internet Explorer

Can anyone provide assistance with this issue? The code snippet works perfectly in all browsers except for IE, where the menu does not fade in and looks unattractive. html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

Webpack fails to handle CSS background images

I'm having trouble with my Webpack configuration as it's not processing CSS images set in the background property: background: url('./hero.jpg') no-repeat right; This is resulting in an error message that reads: ERROR in ./src/app/comp ...

Functionality Issue: Submit Button Not Working on Designed Form Select

Through dedication and hard work, I managed to create a customized form with images that display correctly in Firefox, Chrome, and Internet Explorer's compatibility mode. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

Is it possible to conceal an arrow in a typical dropdown menu?

Is it possible to conceal the arrow in a standard dropdown select fieldset? Check out this Fiddle for more information I am working on an autocomplete feature where users input an organization number and relevant information is fetched from a database. I ...

Is there a way to prevent wrapping in a column, causing the last column to partially extend outside the container when the first column is hovered over?

Is there a way to prevent wrapping of a column so that when I hover over the first column, the last column displays half out of the container? I attempted to achieve this by setting the 25% to flex: 0 0 50%; in my column when hovering on the box-hover cla ...

Is there a way to display the true colors of a picture thumbnail when we click on it?

In my project, I attempted to create a dynamic color change effect when clicking on one of four different pictures. The images are displayed as thumbnails, and upon clicking on a thumbnail, it becomes active with the corresponding logo color, similar to t ...

What is the best way to wrap a div in a vertical orientation?

I'm looking to create a vertical div around an image that fluidly changes width: |_________________| | |XXXXXX| | | |XXXXXX| | | |XXXXXX| | | |XXXXXX| | | |XXXXXX| | | |XXXXXX| | |-----------------| The cont ...

Setting up Scss and purgeCss configuration in Next.js custom postCSS configuration: A step-by-step guide

My current project is using Scss in combination with Bootstrap for design. I have implemented purgeCss to remove unused Css, and customized my postcss.config.js file as follows: module.exports = { plugins: [ "postcss-flexbugs-fixes", [ " ...