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

Limiting the size of textboxes in Twitter Bootstrap

I am currently working with bootstrap text boxes in the following manner: <div class="row"> <div class="col-lg-4"> <p> <label>Contact List</label> <select class="form-control" id="list" name="li ...

I'm having trouble getting Jquery's insertAfter() function to function properly

After creating a parent div and using jQuery's insertAfter() method to insert additional divs, an unexpected issue arises. The first record goes to the bottom, while subsequent records are inserted above it. Below is the function in question: functi ...

What is the best way to merge makeStyle classes from both the parent and child components together?

Is it possible to pass makeStyle classes from a parent component to a child component and merge them with the makeStyle classes within the child component? For example, incorporating breakpoint hiding into the child component's style. Here is an exam ...

How can I achieve a full-width stepper in Bootstrap?

.row { background: #f8f9fa; margin-top: 20px; } .col { border: solid 1px #6c757d; padding: 10px; } <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://useloope ...

Error: Namespace declaration does not have a type annotation - TypeScript/Babel

After creating my app using the command npx create-react-app --typescript, I encountered an issue with generated code and namespaces causing Babel to throw an error. Here are the steps I took to try and resolve the issue: I ejected the project Updated b ...

When using axios in React, the REST API endpoint seems to be unresponsive and does not return any data. However

After successfully creating a simple REST API using express and bodyParser, I noticed an issue when trying to consume it in a basic React app. The API endpoint returns the expected JSON object on a GET request made through the browser without any problems: ...

Bootstrap 4's container overflowing feature

I am trying to create a container with both horizontal and vertical overflow using the "auto" value. I want the content inside this container, which will mainly consist of large tables, to be scrollable within a window. It would be ideal if this container ...

Proper method for positioning text in a line

Trying to recreate the image below, but facing alignment issues with the text in my code. How can I vertically align the text so that they are aligned like in the photo? Flexbox hasn't helped due to varying text lengths causing misalignment. const ...

When Socket.emit is called, it outputs <span> elements within a well-structured message

Currently working on a small chat application using Node.js, Express.js, and Socket.IO. However, I'm encountering an issue with formatting. Instead of displaying the message content within <span> tags as intended, it is printing out the actual t ...

What is the best way to locate an item reference for a specific DOM element?

Imagine a vast DOM structure with approximately 10,000 HTML elements, including 1,000 span tags. None of the span elements have any identifiers and are buried deep within other objects with long, complex Xpath paths that are not user-friendly for selectio ...

Using JavaScript, HTML, and CSS to select slices of a donut pie chart created with SVG

I have successfully implemented CSS hover effects and can manipulate the HTML to use the .active class. However, I am facing difficulties in making my pie slices switch to the active state upon click. Moreover, once this is resolved, I aim to enable select ...

JQuery is blocking the submission of an HTML form

During my exploration of an AJAX/JQuery tutorial for a registration script that interacts with PHP/MySQL and is submitted via JQuery, I encountered a recurring issue. The problem lies in the form submitting directly to the action page instead of its intend ...

The appearance of the page varies when viewed on different computers using the same version of Firefox

Previously, I posed a question with a touch of irony regarding Firefox without providing an example. As a result, my question was downvoted and I had to request its removal. Now, I have an example illustrating the issue at hand. It took some time to clean ...

Can I incorporate a new React project into an existing React project or webpage?

I have a question about deploying my React project. After using npm run build, I now have a build folder with all the necessary files. My project is more complex than the simple "like button" example on React.org. It includes multiple sub-components, fetc ...

Guide to Displaying Items in Order, Concealing Them, and Looping in jQuery

I am trying to create a unique animation where three lines of text appear in succession, then hide, and then reappear in succession. I have successfully split the lines into span tags to make them appear one after the other. However, I am struggling to fin ...

What is the typical layout for a React project's directory structure (or component library)?

Searching for the optimal folder structure for my React project has been a challenge. The current webapp layout I'm using looks like this: app/ scripts/ app.jsx Component.jsx __tests__/ Component-test.jsx ...

Chrome fails to read font family correctly

I created a jsfiddle that demonstrates an interesting behavior with setting and reading the font-family using jQuery. When I set the font-family to "Arial . . ." it reads back okay, enclosed in a single set of double quotes. However, when I set the font-fa ...

Utilizing spans to adjust the formatting of text within list items

When it comes to shaping text inside li elements, I've encountered a few issues. One of the problems is that the float-right divs aren't floating to the top right corner but instead leaving a space at the top. Additionally, the text isn't &a ...

Ways to substitute numerous instances of a string in javascript

I have experience in developing websites using reactjs. I usually implement restAPI's with java and work with liferay CMS. In one of my projects, I created a shortcode for accordion functionality like this: ('[accordion][acc-header]Heading 1[/ac ...

Show a PDF document on the webpage by making an ajax request in a C# MVC application

Using ItextSharp, I am creating a Pdf from html and trying to show a preview of the Pdf on the screen for the user to interact with. Although the Pdf generation is correct, I am struggling to display it on the screen. Here is the Ajax call: function Pre ...