When you add CSS font-size to the body element, how can you effectively manage the nesting of tags and the concept of inheritance within

Currently revamping a website loaded with old content. The main change in design is to make the site adaptable to different screen sizes. I am utilizing the font-size in the body element for this purpose and setting all measurements to ems. While this method works well, I encounter issues when dealing with nested elements that also have font-size specified. For instance, when there's a span tag within a p tag with a class containing a font-size property, the font-size ends up being inherited and applied twice. How should I address this issue? This problem frequently arises with links, lists, and styles involving bold text.

The rationale behind setting the body to 1em is to be able to detect screen resolution and adjust the font-size value to make the overall design scale proportionally. All design elements have been converted from pixels to ems.

For an informative example, check out:

I believe the only solution is to edit existing posts, updating the styles to new ones while taking these factors into account. Given the large number of posts, I'm truly hoping there might be a simpler approach I'm overlooking.

This serves as a basic example. Any suggestions?

    <style type="text/css">
    html {
        font-size:100%;
    }
    body {
        font-size:1em;
    }
    .smalltext, ul, a {
        font-family: "Century Gothic", Arial, sans-serif;
        font-size: .75em;
    }
    .boldNavy {
        font-family: "Century Gothic", Arial, sans-serif;
        font-size: .75em;
        font-weight:bold;
        color:navy;
    }
    </style>

    <div>
      <p class="boldNavy">Showing bold text as intended.</p>
      <p><a href="#">Link appearing as intended</a></p>
      <p class="smalltext">Text displaying correctly, observe what happens with nested tags.</p>
      <p class="smalltext"><span class="boldNavy">Bold text appears smaller here due to inheriting the font-size from the p tag.</span> Regular text follows <a href="#">along with a link</a>. Paragraph continues...</p>
      <ul>
        <li><span class="boldNavy">Bullet List 1</span><br />
          This is the description and <a href="#">a link</a></li>
        <li><span class="boldNavy">Bullet List 2</span><br />
          This is the description and <a href="#">a link</a></li>
      </ul>
    </div>

Answer №1

To ensure compatibility with IE9 and above, I recommend using the unit "rem," which stands for root em. By doing this, you can avoid the cascading effect that occurs when using em values. For more information, please refer to this article:

Answer №2

Applying styles like font-size: .75em actually prevents inheritance rather than causing it. In this case, the font size is set to three-quarters of the parent element’s font size. While this may not be practical (as it can make the text too small), it's important to avoid setting font sizes in a way that contradicts your intentions.

There is no one-size-fits-all solution for setting font sizes, despite the abundance of false promises out there. For instance, if you want to decrease the font size for a list of links—although it's generally advised against—you should set the font-size on the ul element containing the list and leave it at that. It would serve no purpose (and might even be nonsensical) to also decrease the font size for a elements within that list.

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

Upon executing `$('div')`, an empty array is returned

My goal is to retrieve all the div classes on a page using jQuery $('div');. However, the page does not natively support jQuery, so I am loading it through external links until $ === jQuery evaluates to true. Even on Stack Overflow where jQuery ...

When elements are arranged side by side without using floats, the alignment at the top is not correct

I am having trouble aligning multiple divs next to each other using inline-flex. They are not lining up properly on top (refer to the screenshot). Interestingly, if I remove the svg, the divs align correctly. ...

Crafting a horizontal sub-menu that sits between the parent element and the company logo

Seeking assistance with designing a navigation menu that places the navigation bar above the site logo. I envision the sub-menu expanding horizontally from the navigation bar and positioning itself between the parent navigation bar and the logo. The sub- ...

Creating a curved edge for a shape in React Native can add a stylish and

Issue Description I am working on an app and struggling with styling the bottom navigation bar. It's more complex than what I've done before, especially the curved top edge of the white section and the area between the blue/green circle and the ...

Strange behaviors when using setinterval with classes

Currently working on enhancing a slider functionality, and I have observed that everything is functioning smoothly - function Slider(element) { this.i = 0; this.element = element; var self = this; this.timer = window.setInterval(functi ...

The PHP script is failing to send out email notifications

I am facing an issue with my website's contact form that is supposed to send out an email after the user fills it out and clicks submit, but it does not appear to be working. Unfortunately, I do not have access to the mail server since it is hosted e ...

Issue with Twitter Bootstrap 3 Sticky Footer functionality in IE11 and IE Edge

My website works perfectly on Chrome, but I'm having issues on Internet Explorer. I would really appreciate your help. The website I am working on is: I am trying to implement a sticky footer on IE, but for some reason, it's not working as expe ...

Leveraging the :checked state in CSS to trigger click actions

Is there a way to revert back to the unchecked or normal state when clicking elsewhere in the window, after using the :checked state to define the action for the clicked event? ...

Need help addressing a sliding page issue in my Upwork clone script using JavaScript

For those familiar with Upwork's UI, you may have noticed a small feature I implemented. When a user clicks on a freelance job offer, another page opens from the left side using transform: translate(120%) to transform: translate(0). The issue arises ...

What is the best way to customize the appearance of the initial row in each tr cluster?

Looking to add a button for expanding child rows within a datatable, with each group in the table having the same child elements.... The provided CSS currently displays the icon on every row throughout the entire table. td.details-control { backg ...

PHP DOMDocument - Script tag containing Chinese characters is improperly formatted

Can anyone help me figure out why my PHP DomDocument is converting Chinese characters inside a script tag to strange symbols in the output? <?php $html = <<<EOD <!DOCTYPE html> <html> <head> <script> ...

What is the best way to include overflow-hidden in the image displayed by the tailblock component?

I have implemented the following Tailblock component, which is built on tailwind.css. My goal is to achieve a hover effect where the image scales up within its original dimensions. I want the image to zoom in without changing its height and width. I atte ...

Unable to play GSM extension files on a web browser due to compatibility issues

I've been attempting to play an audio file with a (.gsm) extension using <audio>, object tags, and JavaScript. <script> var myAudio = new Audio(); // creating the audio object myAudio.src = "test.gsm"; // assigning the audio file t ...

Unleash the power of jQuery by incorporating the Ajax functionality with a hover option to enhance user interactivity. Utilize the .ajax

On my website, I have a calendar displayed with dates like "11/29/2014" stored in an attribute called "data-date". The goal is to check the server for a log file corresponding to that date and change the CSS of the div on mouse hover. Here is the current ...

What is Causing The Card to Not Expand?

I wanted to enhance my project by incorporating a responsive card template, so I turned to one of the templates on CodePen for help. However, after copying the code into my own platform, I encountered an issue where the card wouldn't expand when click ...

Implementing user-driven filtering in a React table

When a user clicks on the submit button, data will be loaded. If no filter is applied, all data will be displayed. const submit = async (e: SyntheticEvent) => { e.preventDefault(); const param = { ...(certificateNo && ...

Change the z-index of divs when clicked

When a button is clicked, I want to iterate through a group of absolutely positioned children divs with varying z-indexes. The goal is for the z-index of the currently visible div to decrease on each click, creating a looping effect where only one div is v ...

Use the angular cli to incorporate the CSS styles found in the node_modules directory

After successfully installing a new library with npm, I now face the challenge of importing the CSS into my project. It seems unwise to directly link to the node_modules folder. Can anyone suggest an easy way to import this CSS into my Angular CLI project? ...

I need help figuring out how to showcase the following object in an Angular 5 HTML file

https://i.sstatic.net/XXm3W.png The console screenshot above shows an object with two values: users and tickers, each being an array of values. How can I display these values in an Angular 5 HTML template similar to the screenshot above? I attempted to ...

Creating responsive data tables in HTML

My e-shop features a product description block using the code snippet below. While it looks great on larger screens, such as a 17" desktop monitor, it appears poorly on smaller smartphone screens. Friends have suggested that I learn Bootstrap/Flexbox and ...