The width of an element changes as you type text between its tags. My question is that I want some kind of ratio of width to height. As the width increases, the height increases by the same, but I do not want the height to exceed 35 pixels or start from less than 5 pixels.
The code I tried:
HTML:
<div class="btn bg-red">This buttons width expands as I write more and more text.</div>
CSS
.btn { padding-left: 10px; padding-right: 10px; display: inline-block !important; border-radius: 6px; color: #ffffff; padding-top: 10px; padding-bottom: 10px; height: relative; } .bg-red{background-color:#F55C58;} .bg-red:hover{background-color:#F44946}
I'm not sure if this is possible in CSS for this.
Then I tried this:
HTML:
<div class="btn bg-red"><div class="auto">This buttons width expands as I write more and more text.</div></div>
CSS
.btn { padding-left: 10px; padding-right: 10px; display: inline-block !important; border-radius: 6px; color: #ffffff; padding-top: 10px; padding-bottom: 10px; } .auto { height: 20% } .bg-red{background-color:#F55C58;} .bg-red:hover{background-color:#F44946}
JavaScript:
var cw = $('.auto').width(); $('.auto').css({'height':cw+'px'});
The second code does not seem to match display:inline . It works when you change the code manually.
Find a demo for the first code here .
Find a demo for the second code here .
Edit:
Understandable value: when a button / element has less text, the width is small and the same, the height should act the same, but 20% less pixels. When the text increases, the width increases, and the height also increases. The maximum height length can reach 35 pixels, but the default width is infinite by default.
source share