Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break word if text inside Angular Material button is longer than the width of the button?

How do I make sure that the text inside an Angular Material Button does not overflow, by wrapping the text inside the button. I have tried the following:

HTML

<div class="cotnainer">
 <button mat-raised-button color="accent">Click me! This is a long text, but I want to to wrap if content is overflowing
 </button>
</div>

CSS

.container{
  width: 200px;
  height:200px;
  background-color: silver;
}
button{
  max-width: 100%;
}

span, .mat-button-wrapper{ // I have tried all of the following options, but it does not work.
  max-width: 100% !important;
  word-break: break-all !important;
  overflow: hidden !important;
}

Here is a Stackblitz

EDIT Current result:

Current result

Desired result: (sorry for my poor image editing skills) Desired result

like image 795
John Avatar asked Nov 27 '18 10:11

John


People also ask

How do you break a text button?

The easiest way to have a line break is using the <br> tag on your text. It is used for inserting a single line break.

How do you break a long word in HTML?

Use word-wrap:break-word; It even works in IE6, which is a pleasant surprise. word-wrap: break-word has been replaced with overflow-wrap: break-word; which works in every modern browser.

How do I make text fit a button in CSS?

Remove the width and display: block and then add display: inline-block to the button. To have it remain centered you can either add text-align: center; on the body or do the same on a newly created container.


1 Answers

You can achieve this with this code:

Hiding:

button {
  overflow-x: hidden !important;
}

Word Break:

button{
  white-space: pre-wrap !important;
}
like image 179
Brunhack Avatar answered Sep 21 '22 16:09

Brunhack