How can I dynamically adjust the font size of content in a React Material-UI Button when it's unexpectedly

In my web application project, I have created multiple choice questions where each answer is displayed within a single button:

<Button 
    fullWidth={true}
    variant="contained"
    onClick={(answer) => this.handleAnswer(this.state.answers[0].text)}
>
    {this.state.answers[0].text}
</Button>

When clicked, the user can see if they have selected the correct answer. The issue arises when some answers are too long for the button's width, causing the text to break into multiple lines and increase the button's height.

https://i.stack.imgur.com/a3TWT.png

I am looking for a solution to automatically resize the text inside the Button in order to prevent it from breaking down and accommodate longer answer content effectively.

Answer №1

One way to achieve this effect is by using CSS properties like text-overflow: ellipsis; in conjunction with

overflow: hidden; white-space: nowrap;

.btn {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  border-radius: 8px;
  width: 150px;
  background-color: #eee;
  font-size: 24px;
  font-family: Helvetica, sans-serif;
  padding: 0.5em 1em;
  margin-bottom: 1em;
  text-align: center;
}
<div class="btn">test text</div>
<div class="btn">test text</div>
<div class="btn">test text</div>
<div class="btn">test text test text test text test text test text test text</div>

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

Obtain URL parameters prior to rendering with Next.js on the server side

Looking to retrieve the parameters from a URL coming from the Spotify API, for example: http//link.com/?code=examplecode. Is there a way to extract the value of "code" prior to rendering so that I can redirect it and transfer the code value to another p ...

Discover the child of the delegated event div using jQuery

Can you assist me in resolving this issue? My HTML structure appears as follows: <div id="main-randompart"> <!--Inserted into the DOM during runtime--> <div class="close"> X </div> <img src="somesrc..."> ...

Tips for creating a fluid slide-up animation

I've been tasked with creating a multi-step accordion similar to a stepper form. Each accordion will contain forms with next and previous buttons inside. When the next button is clicked, a new accordion will open from the bottom of the page, initial ...

Using $app->render() in Slim PHP allows you to specify the relative path to external dependencies

When rendering an actual HTML site that includes external dependencies like JavaScript and CSS using PHP, there may be issues with resolving the dependency paths when using methods such as $app->render("./somewhere/index.html") or simply echoing out the ...

I am currently working on creating a drag select feature in React completely from scratch, but I'm facing some challenges with

Check out this code I created for drag selection: Here's the item generation code: const items = [ 1, 2, 3, ...94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, ].map((i) => ({ item: i, selected: i === 1 })); This is the actual code responsi ...

Ways to differentiate between buttons in a button cluster?

I have created a set of 3 buttons using the code from this resource. However, the buttons are currently positioned vertically close to each other as shown in the source code. I would like to adjust the spacing between them. Is there a way to achieve this ...

Retrieve the input value closest to the selected row

I need to extract the price and currency value of a checked row containing a checkbox. The price is specified in a text box while the currency is selected from a dropdown menu. $(document).ready(function() { $('#test').click(function(){ ...

What could be the reason for my new course not being recognized?

Looking to modify the behavior of a button where, when clicked, it hides a specific div element, changes its text display, and toggles between classes using addClass/removeClass for subsequent click events. Unfortunately, the intended functionality is not ...

GET requests will not be allowed

Having an unusual issue where the GET parameters are not being passed properly. <!DOCTYPE html> <html> <body> <?php var_dump($_GET); var_dump($_POST); ?> <form action="test.php?ds=1" m ...

What causes an :before content element positioned absolutely to exceed the boundaries of its parent when set as an inline element?

Check out this CodePen: https://codepen.io/jossnaz/pen/BMwpjR HTML <br> <div class="" style=" "> <span class="" style=""> Lorem ipsum nunc hendrerit imperdiet aliquet class massa suspendisse libero, enim condimentum himenaeos h ...

Looking to have the image float after reducing the size of the windows in html?

For my webpage, I want an image to appear on the extreme left when the browser is maximized, but then move to the extreme right when the browser is minimized. I've tried using float, but it doesn't seem to be working. Any suggestions on how to ac ...

Positioning a Material UI Menu item underneath its parent element using CSS styling

I have created a Material UI dialog that features some text and an Icon with a dropdown menu option. You can check out the demo here: https://codesandbox.io/s/prod-rain-1rwhf?file=/src/App.js My goal is to properly position the Menu component so that it a ...

Having trouble accessing static files with express? If you see the message "Cannot Get /", this might be the

After trying to learn how to read static files with Express from a reliable website, I encountered an issue. My terminal indicated that the operation was successful, but when I checked the website, it displayed "Cannot Get /" (as shown in the image). Despi ...

What is the best way to add borders to table cells that have a non-zero value

I am trying to create a table using a function and it currently looks like this: table { border: 1px solid; } td { width: 30px; height: 30px; text-align: center; } <table> <tr> <td>2</td> <td>0</td> ...

Is the history object automatically passed to child components within the Router in react-router-dom?

Is it normal for the Router component to automatically pass down the history object to child components? To showcase this concept, consider the following code snippet: App.js ; const App = () => { return ( <> <h1>Hello</h1&g ...

Using a Component's Variable in Another Component in React Native

Hello there! Thank you so much for offering your help. I have a component called PlayerWidget and would like to utilize its state variable isPlaying value in the AlbumHeader Component. Here is the code for the PlayerWidget: import React, { useContext, use ...

Error message 'Module not found' occurring while utilizing dynamic import

After removing CRA and setting up webpack/babel manually, I've encountered issues with dynamic imports. The following code snippet works: import("./" + "CloudIcon" + ".svg") .then(file => { console.log(file); }) However, this snip ...

Enhancing Material UI datepicker with custom CSS styles, including cursor pointer effects

I am facing an issue with my Material UI datepicker component in React. The cursor is not showing and the icon I added is not clickable, resulting in the datepicker dialog not appearing. Despite trying inline styling and styled-components, I have been una ...

What is the method to merge min and max validation errors when using React Hook Form?

<input {...register("subject", { maxLength: 50, minLength: 2, required: true, })} disabled={isLoading} id="subject" autoComplete=&q ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...