A guide on dynamically adjusting text size based on viewport width and height in React with TailwindCSS

Utilizing React and Tailwind, I am working towards creating two overlapping rectangles with text. The top rectangle's text should be aligned to the left, while the bottom rectangle's text should be aligned to the right. Regardless of window size adjustments, the font size should dynamically change to maintain consistent proportions within each box. Referencing the provided screenshot, it is crucial that:

  1. The bottom text must always start at the same position as the 't' in 'first'
  2. The spacing between the first and second sentences must remain constant.

Currently, my code fails to maintain these desired positions and spacings.

const Hero = ({ ...props }: HeroProps): JSX.Element => {
  const textStyle = {
    fontSize: `8vw`
  };
    return (
        <div className='h-2/5 w-full'>
          <div className="w-5/6 h-1/2 bg-red-500 flex items-center">
            <h1 className="font-impact whitespace-nowrap w-full h-full" style={textStyle}>The first sentence</h1> 
          </div>
          <div className="w-5/6 h-1/2 bg-blue-500 ml-auto flex items-center">
           <h1 className="font-impact text-right whitespace-nowrap w-full h-full" style={textStyle}>The second sentence</h1>
           </div>
        </div>
  );
};

This implementation is set for the initial section of my webpage, as indicated by:

<div className='h-2/5 w-full'>

Here's a snapshot displaying the current layout, which does not adjust correctly: https://i.stack.imgur.com/43STe.png

I am grateful for any assistance with this matter.

Answer №1

Utilize vw for custom text sizes.

<div class="h-2/5 w-full">
  <div class="flex h-1/2 w-5/6 items-center bg-red-500">
    <h1 class="font-impact h-full w-full whitespace-nowrap text-[5vw]" style="{textStyle}">The first sentence</h1>
  </div>
  <div class="ml-auto flex h-1/2 w-5/6 items-center bg-blue-500">
    <h1 class="font-impact h-full w-full whitespace-nowrap text-right  text-[5vw]" style="{textStyle}">The second sentence</h1>
  </div>
</div>

https://play.tailwindcss.com/EogMqGkKK2

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

Exploring server side rendering using DevExtreme and Material UI in conjunction with React 16

Upon refreshing the page and navigating through my SSR, it seems that the CSS is not being applied unless I interact with certain elements in the app. Are there any examples available of how to correct this issue? I am using the same code as the controlle ...

Cross-browser compatibility issue: Chrome displays image class in HTML CSS, while Firefox does not

I am building a webpage and have encountered an issue with displaying images in different browsers. Specifically, the image appears correctly in Chrome and Safari but doesn't show up in Firefox. By "doesn't show," I mean that the image is not vi ...

Enhance your Bootstrap Progress Bar by incorporating the width value within the CSS using CoffeeScript or jQuery

I'm attempting to create a function where every time the button is clicked, the progress bar increases by an extra 12.5% out of the total 100%. (Therefore, eight clicks will complete the progress bar to 100%) Currently, it updates the text content bu ...

Concealing an input field in AngularJS

I am currently developing a login page with register and login options using AngularJS. The interface includes three input fields: username, password, and name. I aim to display the name field upon clicking the register button and hide it upon clicking t ...

if the checkbox is selected, navigate to the displayed section

In my scenario, I have a total of 5 checkboxes and corresponding to each checkbox, there are 5 div elements. The functionality that I need is that when a checkbox is checked, the associated div should be displayed; otherwise, it should be hidden. The check ...

Updates made to the stylesheet in Tailwindcss do not immediately appear in the browser

After adding a class, I find myself having to stop the Nuxt server, reload VS Code, and then run "npm run dev" in order to see the changes take effect. Below is my tailwind.config.js : /** @type {import('tailwindcss').Config} */ module.expo ...

Improving how React deals with routes

Is there a more efficient way to manage react routes for my book store website? I want each book to have its own page with author information and purchase options. Here are my current routes: <Routes> {/* DASHBOARD */} < ...

Error: The Vue bind object property cannot be read because it is undefined in the class binding

I'm attempting to associate a class with an object property, but I encounter an issue when trying to trigger the @click event on the list object - the console states that it cannot read the isSelected property of the object. My goal is to activate a c ...

Utilize custom scrollbar design exclusively for Windows operating system

My goal is to customize the scrollbar style of a div specifically for Windows systems. While most mobile devices have invisible scrollbars that I want to keep hidden, the aesthetics of Mac OS scrollbars are acceptable to me. What I really need is to make t ...

A versatile text input component designed to manage the validation of passwords, facilitate secure login processes, and validate

Query I appreciate the assistance with props - is it possible to create a single textInput custom component that can handle various validations? Snippet Below you will see the basic Login screen, featuring 2 text inputs. import React, { PureComponent } ...

Positioning the subheading "perfectly beneath the main heading" for a clean and professional look

I'm feeling a bit lost when it comes to tasks like this. While I can easily design an entire website using html and css, I start to question my approach when faced with challenges like these. Could someone offer some guidance? My goal is to have a H1 ...

Encountered issue while initializing object from controller in AngularJS

Here is the demonstration on how the fiddle appears: var app = angular.module('testApp', []); app.controller = angular.('testAppCtrl', function ($scope) { $scope.vehicle = { type: 'car', color: 're ...

The value of React state may be unpredictable, even if it appears to be defined on the

<li className="field-title">Role: </li> {console.log(this.state.userData.roles)} {this.state.userData.roles.map((role) => { return ( <li>{role.name}</li> ) })} Even though when I console log the state ...

The alignment of Material-UI Typography in AppBar appears to be off

Having trouble centering text in an AppBar? You're not alone. Despite trying various methods like using Typography element, align="center", textAlign="center", and style={{ align: "center" }}, among others: class CustomApp extends React.Component { ...

Adjusting the size of the iframe to match the dimensions of the window

Is there a way to make the iframe automatically fill up the entire space? For example, only opens the iframe up to half of the window in Mozilla Firefox and IE6. How can I ensure that it takes the maximum size of the screen? Are there any CSS or JavaScr ...

Is there a CSS3-compatible CSS parser available in C/C++?

Are there any C/C++ CSS parsers that currently support CSS3? I have come across a few, but none of them seem to offer full CSS3 compatibility. ...

Encountering an error message in Material UI that reads: "Error with prop type: isValidElement is not a function" specifically while implementing the mui

Recently, I encountered a problem with MUI's Accordion component in my next.js app. It started throwing errors related to Failed prop type: isValidElement is not a function. These errors seem to be coming from various components of the MUI Accordion l ...

Ways to resolve the issue "How to address the React hook error stating "React Hook 'React.useState' is being called conditionally"?"

I encountered an issue while attempting to build my next.js project. How can I resolve it? It seems that the error is related to using hooks conditionally. ./src/lib/session.js 6:29 Error: React Hook "React.useState" is called conditionally. R ...

Updating the total on the Infusionsoft order form can be achieved using either Jquery or custom code

Is there a way to automatically calculate and update the total amount on the order form based on user input in two quantity fields? The price should increase as the user adjusts the quantity. I've been searching for a solution, but haven't found ...

Activate inactive html button using javascript

One of the challenges I am facing is testing forms where the client specifically requested that the submit button be disabled by default. I have been exploring ways to dynamically replace the disabled="" attribute with enabled using JavaScript within a s ...