Utilizing React with Emotion to Customize Font Sizes

In my React app, I am using emotion to style and display the formula for compound interest. Within my render() method, I am returning the following:

<div css ={equation}>
  <p>
  P (1 +</p>
  <p css={fraction}>
    <span className="num">1</span>
    <span className="symbol">/</span>
    <span className="bottom">2</span>
  </p>
  )
   <sup>(nt)</sup>
</div>

Additionally, outside of the component I have defined the following:

const fraction = css`
    display: inline-block;
    position: relative;
    vertical-align: middle;
    letter-spacing: 0.0001em;
    text-align: center;

    .num {
      display: block;
      padding: 0;
      height: 12px;
      line-height: 12px;
    }

    .bottom {
      border-top: thin solid black;
    }

    .symbol {
      display: none;
    }
`
const equation = css`
font-size: 100%;
.p{
    font-size:large !important;
  }
`;

While the fraction styling is working as expected, I am encountering issues with changing the font size of the p elements. I have tried various methods, including using the font-size property within the emotion css styling, but so far have been unsuccessful. I managed to change the font size by switching p to h1 elements, but I would prefer to find a solution that allows me to specify the font size within the emotion styling for p elements.

Answer №1

I have identified a couple of issues in your code that need to be addressed:

  • The use of .p { ... inside the equation is incorrect; it should be an element selector like p { ...
  • Your react div requires the use of className to apply the desired effect

Below are the updated code changes in the sandbox: https://codesandbox.io/s/emotion-xh53z?fontsize=14

Here's the updated code for your reference:

import React from "react";
import { render } from "react-dom";
import { css } from "react-emotion";

const fraction = css`
  display: inline-block;
  position: relative;
  vertical-align: middle;
  letter-spacing: 0.0001em;
  text-align: center;
  .num {
    display: block;
    padding: 0;
    height: 30px;
    line-height: 12px;
  }
  .bottom {
    border-top: thin solid black;
  }
  .symbol {
    display: none;
  }
`;

 // Corrected p inside the equation
const equation = css`
  p { 
    font-size: large !important;
  }
`;

// Use className={} instead of css={}
const App = () => (
  <div className={equation}>
    <p>
      P (1 +
      <p className={fraction}>
        <span className="num">1</span>
        <span className="symbol">/</span>
        <span className="bottom">2</span>
      </p>
      )<sup>(nt)</sup>
    </p>
  </div>
);

render(<App />, document.getElementById("root"));

The closing </p> tag has been moved to ensure proper application with inline-block

Update 1:

Check out the latest updated version here: https://codesandbox.io/s/emotion-1tjc7

Following their latest blog, you should use:

import styled from '@emotion/styled'
import { css } from 'emotion'

instead of

import styled, { css } from 'react-emotion'

Update 2:

If you are unable to use vanilla emotion, consider the following alternative:

import styled from "@emotion/styled";
/** @jsx jsx */
import { css, jsx } from "@emotion/core";

According to their documentation:

There are two ways to get started with the css prop.

  • Babel Preset (Not usable in Create-React-App or CodeSandbox)
  • JSX Pragma (Just add the specified lines of code)

The codesandbox has also been updated: https://codesandbox.io/s/emotion-0z3vz

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 showcasing a complete background image in a div even if the content inside is minimal

Check out the header image on ink.talentosoft.com if you get a chance. The height of the header image is precisely 388 px. I am looking to have the background of this div fully displayed. Currently, the setup for the div looks like this: background: url ...

Navigating the maze of Material UI in React with TypeScript

I have a functioning code, but I am trying to incorporate Material UI into it. However, when I replace 'input' with 'TextField', I encounter the following error: Uncaught (in promise) Error: Request failed with status code 422 at cr ...

What causes offsetHeight to be less than clientHeight?

INFORMATION: clientHeight: Retrieves the height of an element, taking into account padding offsetHeight: Obtains the height of an element, considering padding, border, and scrollbar Analyzing the Data: The value returned by offsetHeight is expected to ...

Issues arise when implementing a sticky position using CSS with incomplete functionality

I want to create a website with a menu bar that stays at the top even on long pages. However, when I use position: sticky in CSS, the menu disappears after scrolling a certain distance. Does anyone know how to fix this issue? HTML: <nav> <ul cla ...

Content does not display within a list element (ul)

I am attempting to display captions beneath the thumbnail slider in list format. However, the text is not appearing unless I switch the <ul> tag to <div>. You can locate the thumbnail here. It is the first thumbnail. Much appreciated. ...

Navigate between pictures using hover in jQuery

I am working on creating a feature where images cycle through individually every 2 seconds, but switch to the right image when its associated link is hovered. So far, I have managed to make the images show up on hover, but I am struggling with getting them ...

The transformOrigin property of the MUI popover does not seem to be functioning as expected

When a button is clicked, a popover opens with a table inside. The transformOrigin property of the popover is not functioning as expected. Upon initial reload, the popover displays in the correct position (and when resizing the window while the popover is ...

Tips for customizing the color of icons in material UI

Why am I unable to change the color of the search icon using sx in React? I attempted changing the color through CSS using the class name 'searchIcon', but it seems ineffective. import React from 'react' import './Header.css' ...

The concealment of the container is ineffective when attempting to hide it until all cached images are

My website has a background and a main container. I wanted to hide the container until the entire page had loaded, so I included #cover{opacity:0} at the beginning of the page and $(window).load(function() { $('#cover').css('opacity&apo ...

Error Alert: Attempting to access the 'prototype' property of an undefined value has resulted in a TypeError after switching to react-scripts version 4.0.3

I encountered a problem where I received the following error message: (Uncaught Error: Module build failed (from ./node_modules/source-map-loader/dist/cjs.js)). After attempting to resolve it by using npm install, a new error popped up in the console: path ...

Applying a CSS class to a newly generated row with JavaScript

I have a JSP page where I dynamically add rows to a table using a different Javascript function than in my previous query. While I can add elements to the table columns, I'm unable to apply a style class that is defined in a CSS file. Here is my Java ...

CSS Techniques for Smooth Transitions

When the hamburger icon is pressed, a menu appears over the current page with CSS from Glamor. The menu slides in perfectly from the right side of the screen, but I am having trouble getting it to slide out when any part of the menu is clicked. I have def ...

What is the best way to hide the jQuery modal I created?

Hello everyone! Currently, I am working on coding a simple JS modal that can be opened and closed smoothly. The issue I am facing is related to adding a click function to (.black-overlay) in order to fade out everything and close the modal. <div class ...

The page background appears to be incomplete in its coloring

As I work on creating a web application for personal use, my goal is to ensure it is mobile-friendly. However, I've encountered an issue where the background of my language page isn't displaying in full blue color as intended. Despite trying diff ...

Mini-navigation bar scrolling

I am trying to create a menu where I want to hide elements if the length of either class a or class b is larger than the entire container. I want to achieve a similar effect to what Facebook has. How can I make this happen? I have thought about one approac ...

How to Stop Fixed Elements in iOS from Zooming

I'm currently working on a responsive website with a fixed header and navigation. Is there a way to allow users to zoom in on the page's content without affecting the fixed elements? Thanks for any help! ...

Verify the version of NextJs the project is built on

Currently in the midst of a NextJs project, I am eager to determine the exact version of NextJs that is currently installed. Can anyone point me in the right direction to find this information? ...

Incorporating an HTML Canvas element within a CSS grid design

I've been attempting to integrate an html canvas that I created into one of the css grid classes specified in my code. The code runs when the "Calculate" button is clicked. Upon clicking the button, the <div> section designated for the canvas r ...

Incorporate my personal design flair when utilizing third-party React.js component libraries

Incorporating Material UI as a component library in my React project has been beneficial. However, I am facing a challenge where the inline styling provided by Material UI clashes with my existing custom styles that I have developed over time. Is there a ...

Implement variable tailwind styling in a react component using Next.JS, Tailwind, and TypeScript

Having some difficulty assigning dynamically tailwind classes to a react component. The theme colors have been extended in the tailwind.config.js file as shown below: ... theme: { extend: { colors: { blueGray: { 50: '#f6 ...