0

It took me like 20 minutes to find this, so I'm asking and answering my question.

So, say I have a div:

<div id="foo"> Lorem ipsum dolor sit amet consecuter Lorem ipsum dolor sit amet, consectetur adipiscing 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>

And I'm not sure how much stuff is in the div. It could be one sentence or an entire scientific article.

So, if there's not much in the div, I want it to be just as wide as it needs to be. But, if there's a lot of stuff in the div, I want it to be, say, width: 75%.

I.e., if it fits within 75% of your browser window, it's one line. If it doesn't fit, the lines wrap at 75%.

How do I do this?

Cameron Hurd
  • 4,398
  • 1
  • 17
  • 28
SIGSTACKFAULT
  • 957
  • 1
  • 15
  • 24

1 Answers1

1

Easy peasy.

#foo {
    width: max-content;
    max-width: 75%;
}

So if the content fits within 75% of your broswer window, It'll be on one line. If it doesn't fit, it'll line wrap at 75%.

If your browser doesn't support max-width yet, it will pin at 75%, which is fine.


An overcomplicated jsfiddle example doohickey:

var data;
var delay = 150;

async function thingy() {
  $("#controls").hide();
  $("#magic").text("");
  var i;
  for (i = 0; i < data.length; i++) {
    $("#magic").append(data[i] + " ");
    await sleep(delay);
  }
  $("#controls").delay(1000).fadeIn();
}

function updatedelay() {
  $("#delay").text(delay + "ms");
}

$(function() {
  data = $("#data").text().split(" ");
  thingy();
});


// grrrr
// Credit: http://stackoverflow.com/a/39914235/2729876
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
#magic {
  border: solid 1px black;
  padding: 10px;
  margin: auto;
  width: max-content;
  max-width: 75%;
}

#controls {
  display: none;
  border: solid 1px blue;
  width: 50%
}

#data {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center>
  <div id="magic"> </div>

  <br><br><br>

  <div id="controls">
    <button onclick="thingy();">Again!</button>
    <br><br>
    Delay:<span id="delay">150ms</span><br>
    <button onclick="$('#delay').text((delay+=100)+'ms')">++</button>
    <button onclick="$('#delay').text((delay+=10)+'ms')">+</button> | 
    <button onclick="$('#delay').text((delay-=10)+'ms')">-</button>
    <button onclick="$('#delay').text((delay-=100)+'ms')">--</button>
  </div>
</center>


<div id="data">Lorem ipsum dolor sit amet, consectetur adipiscing 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>
AGE
  • 3,389
  • 3
  • 34
  • 56
SIGSTACKFAULT
  • 957
  • 1
  • 15
  • 24
  • Why do this? You can achieve the same with `display: inline-block` and you won't have any [browser support issues](http://caniuse.com/#feat=intrinsic-width)? – Jayx May 08 '17 at 16:06
  • Hush, this way is cooler. Anyways, on browsers that don't support it, it'll just pin at 75%, which is fine. – SIGSTACKFAULT May 08 '17 at 18:14