6

The width of the element changes as I type text between its tags. My question that I want some kind of relation of Width with Height. As the width gets longer, the height increases by the same but I don't want the height to exceed 35px nor start with below 5px.

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 it is possible in CSS to do 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 doesn't seem to follow display:inline. It works when you change the code manually.

Find demo for First code here.

Find demo for Second code here.

Edit:

Understood Meaning: When the button/element has less text, the width is small and the same way, the height should act same but 20% less pixels. When the text is increased, the width increases and the height should also increase. The max length of Height can reach up to 35px but the Width is Infinite by default.

Community
  • 1
  • 1
Shahroz Asif
  • 143
  • 1
  • 10
  • what´s wrong with just `$('.auto').css({'height':Math.max(Math.min(cw, 35), 5)+'px'});` ? – juvian Apr 14 '16 at 17:10
  • 1
    [**css-only relevant link**](http://stackoverflow.com/questions/20456694/grid-of-responsive-squares/20457076#20457076) – L777 Apr 14 '16 at 17:18
  • You don't understand, the code `$('.auto').css({'height':Math.max(Math.min(cw, 35), 5)+'px'});` works with manual changes but not with `display:inline-block`. The Width depends on how many characters of words are displayed. – Shahroz Asif Apr 14 '16 at 18:39
  • Understood Meaning: When the button has less text, the width is small and the same way, the height should be same. When the text is increased, the width increases and the height should also increase. The max length of Height can reach up to 35px but the Width is Infinite by default. – Shahroz Asif Apr 14 '16 at 18:42

2 Answers2

2

I don't know how do you add text on your div. Anyway, on the following snippets the eventlistener trigger is set to input since it's a contenteditable div (the text is added by the user throught keyboard).

Snippet #1:

document.getElementsByTagName("body")[0].onload=function(){equalize()};

var target = document.getElementById("target");

target.addEventListener("input", equalize);

function equalize() {  
var x = target.offsetWidth;
target.style.height = x + "px";
}
#target {
  display: inline-block;
  background: skyblue;  
}
<div id=target contenteditable="true">write here</div>

This second snippet is the same as the previous one, but now the height is 20% smaller than the width (it was equal on the previous example) plus have a max-height limit of 100px.

Snippet #2:

var target = document.getElementById("target");

document.getElementsByTagName("body")[0].onload=function(){equalize()};
target.addEventListener("input", equalize);

function equalize() {  
var x = target.offsetWidth;
var reducedpart = x / 100 * 20;
var result =  x - reducedpart;

  if (result > 100) {
    var result = 100;    
}  
  
target.style.height = result + "px";
}
#target {
  display: inline-block;
  background: skyblue; 
}
<div id=target contenteditable="true">write here</div>

Same as before but using padding instead of height to let the text on center:

Snippet #3:

var target = document.getElementById("target");

document.getElementsByTagName("body")[0].onload=function(){equalize()};
target.addEventListener("input", equalize);

function equalize() {  
var x = target.offsetWidth;
var reducedpart = x / 100 * 20;
var result =  x - reducedpart;

  if (result > 100) {
    var result = 100;    
}  

var secresult = result / 2;

target.style.paddingTop = secresult + "px";
target.style.paddingBottom = secresult + "px";
}
#target {
  display: inline-block;
  background: skyblue; 
  padding-left: 15px;
  padding-right: 15px;
}
<div id=target contenteditable="true">write here</div>

CSS-only workaround using padding instead of height:

Snippet #4:

.container {  
  vertical-align: bottom;
  display: inline-block;
}

#a {
  width: 100%;
  padding-bottom: calc(80% - 1em);
  background: gold;
}

#b {
  width: 100%;
  padding-top: calc(40% - 0.5em);
  padding-bottom: calc(40% - 0.5em);
  background: tomato;
}
<div class=container><div id=a contenteditable="true">write here</div></div>
<div class=container><div id=b contenteditable="true">write here</div></div>
L777
  • 7,719
  • 3
  • 38
  • 63
  • I'm using the third code. If I use custom class to order background, it doesn't appear. Example: CSS: `.bg-red{background-color:#F55C58;} .bg-red:hover{background-color:#F44946;}` – Shahroz Asif Apr 16 '16 at 10:47
  • Why not? I made a test and it does works. [**check this jsfiddle.**](http://jsfiddle.net/kgbv75x5/) Please be aware both colors #F55C58 and #F44946 are very closer one each other so you'll need to pay attention to notice the difference on over hover it. – L777 Apr 16 '16 at 13:12
  • If I remove container and put its properties in one of the ids, it fills up the page. I don't want this to happen – Shahroz Asif Apr 16 '16 at 17:11
  • As I said, it's a 'workaround' and isn't going to work without 2 divs (one inside the other); To do it with a single div you'll need Javascript; – L777 Apr 16 '16 at 17:30
  • Oh, didn't notice that. I tried the second one and when I used `padding-left: 15px` and `padding-right: 15px`, it stopped working. – Shahroz Asif Apr 17 '16 at 11:58
  • second snippet updated with padding-left and padding right of 15px; – L777 Apr 17 '16 at 12:14
  • I want another function. The code you gave worked perfectly. I want the Text to be centered within its boundaries. I tried using padding but it brings the text to the bottom when I shorten the text. – Shahroz Asif Apr 17 '16 at 16:11
  • Or I want the background to make the text look like its centered. – Shahroz Asif Apr 17 '16 at 16:12
  • (the answer got updated with this centered jsfiddle - which is now the snippet #3); – L777 Apr 17 '16 at 17:12
  • 1
    Thanks for much help. If I face further errors, I'll comment here – Shahroz Asif Apr 17 '16 at 17:48
1

In jquery there's a function .height() that get the current computed height for the html element in the given ID or Class. Getting the height value of your div.auto you can monitor the height increase. Given that, you can make a condition if ($('.auto').height() <= 35px) that limit it up to 35px .

Put this <script type="text/javascript" src="jquery-1.12.2.js"></script> in your <head> and that you have the latest JQuery library.

Here's the full implementation of code. Try it in your localhost, because sometimes it doesn't work on jsfiddle or likewise.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <style media="screen">
      .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}
    </style>
    <script type="text/javascript" src="jquery-1.12.2.js"></script>
  </head>
  <body>
    <div class="btn bg-red"><div class="auto">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div></div>
    <script type="text/javascript">
    var cw = $('.auto').width();
    if ($('.auto').height() <= 35px) {
      $('.auto').css({'height':cw+'px'});
    }
    </script>
  </body>
</html>

Hope this will help

Fil
  • 8,225
  • 14
  • 59
  • 85