sam
sam

Reputation: 127

How to make the button size increase automatically when the element size in the button is increased

I am using paragraph tag and img tag inside a button tag and added some css properties to button so it look like the below image. The problem is when i increase text in paragraph the text is overflowing out of the button, I want to wrap it inside the button. Below is my code, enter image description here

<button class="white-btn" id="btn_one" onclick="myFunction()">
        <img src="./icon1.png"/>
        <p>Bauleistungenfdssfdsfasadsf</p>
    </button>

.white-btn {
width: 20%;
height: 151px;
background: #ffffffb8;
border-radius: 12px;
text-align: center;
display: inline-flex;
justify-content: center;
align-items: center;
flex-grow: 1;
border: none;

Upvotes: 1

Views: 391

Answers (5)

Michael Kolesidis
Michael Kolesidis

Reputation: 360

You do that with

.white-btn {
   max-width: fit-content;
}

Upvotes: 0

dita lubis
dita lubis

Reputation: 184

Here is my try, I have added some explanation comments in the code, hope this help!

.white-btn {
  min-width: 20%;  /*Changed this line*/
  max-width: 100%; /*optional, this is to make sure it's not more than the container*/
  min-height: 151px; /*optional, so the height also can increase following the content of the button*/
  background: red; /*I just change this as example purposes, to make the button visible to me*/
  border-radius: 12px;
  text-align: center;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  flex-grow: 1;
  border: none;
  word-break: break-word; /*Added this line*/
}
img {
  width: 150px;  /*this is optional*/
}
<button class="white-btn" id="btn_one" onclick="myFunction()">
    <img src="https://jenmulligandesign.com/wp-content/uploads/2017/04/pexels-beach-tropical-scene-free-stock-photo.jpg"/>
    <p>Bauleistungenfdssfdsfasadsf Bauleistungenfdssfdsfasadsf Bauleistungenfdssfdsfasadsf</p>
</button>

Upvotes: 1

joohong89
joohong89

Reputation: 1271

Try using word-break with max-width. You will be able to set a max length for button to 'grow, before it hits a limit and breaks to the next line.

Update: I just realized word-break: break-word has been deprecated. Added several other examples to show the 'break' with other properties.

.white-btn {
  width: 20%;
  height: 151px;
  background: red;
  border-radius: 12px;
  text-align: center;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  flex-grow: 1;
  border: none;
  min-width: 250px;
}

.width-auto {
  width: auto !important;
  max-width: 40%;
  word-break: break-all;
}

.width-overflow-wrap-break-word {
  width: auto !important;
  max-width: 30%;
  overflow-wrap: break-word;
}

.width-overflow-wrap-anywhere {
  width: auto !important;
  max-width: 40%;
  overflow-wrap: anywhere;
}
.width-fix {
  width: 300px;
  word-break: break-all;
}
  
  <p> Original </p>
  <button class="white-btn" id="btn_one" onclick="myFunction()">
      <img src="https://via.placeholder.com/150"/>
      <p>Bauleistungenfdsdfasdfsadfsdfasdfssfdsfasadsf</p>
  </button>
  
  <br>
  <p> word-break: break-all; </p>
  <button class="white-btn width-auto" id="btn_one" onclick="myFunction()">
      <img src="https://via.placeholder.com/150"/>
      <p>Bauleistungenfdsdfasdfsadfssdfasdfasdasdfasdfasdffdfasdfssasdfsdfasfasffdsfasadsf</p>
  </button>
   
  <br>
  <p> overflow-wrap: break-word; </p>
  <button class="white-btn width-overflow-wrap-break-word" id="btn_one" onclick="myFunction()">
      <img src="https://via.placeholder.com/150"/>
      <p>For long sentence with multiple breaks inbetween.</p>
  </button>
  <br>
  
  <p> overflow-wrap: anywhere; </p>
  <button class="white-btn width-overflow-wrap-anywhere " id="btn_one" onclick="myFunction()">
      <img src="https://via.placeholder.com/150"/>
      <p>ForAVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongSentence</p>
  </button>
  <br>
  
   <p> word-break: break-all; </p>
  <button class="white-btn width-fix" id="btn_one" onclick="myFunction()">
      <img src="https://via.placeholder.com/150"/>
      <p>Bauleistungenfdsdfasdfsadfsdfasdfssfdsfasadsf</p>
  </button>

Upvotes: 1

Smit
Smit

Reputation: 32

Try this hope it's works

.white-btn {
width: 20%;
height: 151px;
background: blue;
border-radius: 12px;
text-align: center;
display: inline-flex;
justify-content: center;
align-items: center;
flex-grow: 1;
border: none;
  word-break: break-word;
  
}
<button class="white-btn" id="btn_one" onclick="myFunction()">
        <img src="./icon1.png"/>
        <p>Bauleistungenfdssfdsfasadsf</p>
    </button>

Upvotes: 1

raielly
raielly

Reputation: 188

Add word-break: break-all; in your white-btn class and its all good now.

Upvotes: 1

Related Questions