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

The dilemma of selecting objects in Three.js

Currently, I am developing a small web application that utilizes three.js. However, I have encountered an issue: I created a prototype with my three.js content and everything functions correctly (the canvas size in the prototype matches the browser window ...

Display the result of a function within a component

I'm currently working on a React component that has the following structure: const myAge = () => { var diff = Date.now() - new Date(1991, 2, 1); var age = new Date(diff); return age; }; export const About = (props) => { return ( &l ...

What steps can I take to personalize Material UI within a React application?

As someone who is new to this UI framework and React, I have been tasked with developing an application that requires more design patterns. I specifically chose a framework that does not depend on jQuery code. However, I am facing challenges when it comes ...

Adjusting the properties of a <span> tag when hovering over a different element

Query: I am trying to dynamically change the color of the span element with classes glyphicon and glyphicon-play when the user hovers over the entire element. How can I achieve this? Recommendation: .whole:hover > span.glyphicon.glyphicon-play { c ...

The vertical menu was added, but the text did not align properly

I pasted a sidebar from a different source https://i.stack.imgur.com/jkYWz.png My goal is to have the text appear at the top of the page, similar to this example https://i.stack.imgur.com/1aR5s.png After adding this line, my text is displaying at the b ...

Tips for integrating a vertical menu with button icons and subcategories

Currently working on a project with Twitter Bootstrap and looking to create a specific sidebar setup. My goal is to include subcategories using an accordion and a collapsible menu for mobile devices. I came across a similar implementation at http://jsfiddl ...

Trigger a function in AngularJS when a div is scrolled to within a specific number of pixels from the bottom of the screen

I am experimenting with the AngularJS infinite-scroll directive. Below is the code snippet: angular.module('infiniteScroll', []) .directive('infiniteScroll', [ "$window", function ($window) { return { link:funct ...

Transferring SetState from a parent component to a child component in React Native

When working with React js, passing setState from parent components to child components is done like this. However, in React Native setABC is undefined. What is the recommended approach for achieving similar functionality in React Native? Parent.js: funct ...

Steps for updating the <option> selection in the third <select> menu when the second <select> menu changes, with the second menu being populated by altering the value of the first dropdown through jQuery AJAX

I'm facing a unique situation in my school management project and have been unable to find a solution online. Here's the issue: I have three <select> elements - the first one is for selecting a class, the second one is for selecting a secti ...

Displaying a collection of information in a material UI ReactJS component

My goal is to display a list of movies using Redux and Material UI. FilmsService : export const FilmServices = { getAllFilms, }; function getAllFilms(){ return axios.get(config.baseUrl).then((response)=>{ return response; }).ca ...

Is there a method for redirecting my page to a specific href link without triggering a page reload?

This is my HTML code. <a href="http://127.1.1.0:8001/gembead/emstones.html?car=36">Car</a> I am trying to redirect to a specific page with parameters without fully reloading the current page. Is there a way to achieve this task? I believe the ...

Keep scrolling! There's no need to worry about reaching the end of the page and having to start over

I'm experiencing an issue where scrolling only works once when the page is initially loaded. I need it to be able to repeat from the beginning each time. <div class="content"> <div class="next-section blue">First Section</div> & ...

Directing a webpage to a particular moment within that page

Is it possible to automatically redirect users to a new page at a specific time? I would like visitors to be redirected to a new site starting from 12:00AM on December 31st, without allowing them to access the current site after that time. Is there a way ...

Prevent clicking through slides on React by disabling the Swiper feature

Is there a way to handle the global document click event (using React hook useClickAway) so that it still fires even when clicking on a slide? For example, think of a circle in the header like an avatar dropdown - when you click on it, a menu pops up. Th ...

Is there an automatic bottom padding feature?

Currently, I am facing a challenge in fitting the loader into the container without it being overridden by the browser. Using padding-bottom is not an ideal solution as it results in the loader appearing un-resized and unprofessional. Any suggestions or co ...

Troubleshooting: Unable to preview Facebook Page plugin

When I visit the Facebook developer site to get the code for a Facebook page plugin, I use this link. The preview feature works perfectly with pages like "facebook.com/Nike", but when I try it with my own page, "facebook.com/BargainHideout", it doesn&apos ...

In a React app, there are instances where `localstorage.getitem('key')` may result in returning null

I've encountered a strange issue while building a login form that sets a JWT token in localstorage. The problem arises when, despite being able to see the token in my console.log, setting localstorage.getitem('idToken') sometimes returns nul ...

Tips for implementing this code for effective DAO and CRUD operations

As I venture into coding CRUD operations for the first time, I encountered an issue while testing the update functionality: The error message "Dao.get() missing 1 required positional argument: 'id'" is puzzling me. Where could I have gone wrong? ...

Getting this error in React Recoil - "Type 'any[]' is not compatible with type 'never[]' parameter"?

Currently, I am working through the React Recoil Todo List tutorial, however, I am encountering some type errors while following along and I am not sure how to resolve them effectively. Below is the code snippet: export const todoListAtom = atom({ key: ...

Firebase generated an error message that reads "TypeError: currentUser.updateProfile is not a function" following the merging of a Firebase user with another object

I've been working on updating a user's profile using the updateProfile() function. However, when I try to call this function following the example from the provided link, I encounter an error message saying TypeError: currentUser.updateProfile i ...