Just add a display: table; to the element.
The code is here: https://codepen.io/cuteyangyang/pen/ZEeGMaq

<!DOCTYPE html>
<html>
<head>
    <title></title>
<style type="text/css">
    .outer,outer2{
      width: 100px;
      overflow: hidden;
    }
    .inner{
      white-space: nowrap;
    }
    .inner2{
      white-space: nowrap;
      display: table;
    }    
</style>
</head>
<body>
    <div class="outer">
      <div class="inner">很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。</div>
    </div>
    <div class="outer">
      <div class="inner2">很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。很长的文字。</div>
    </div>
    <script type="text/javascript">
        console.log(document.querySelector('.inner').offsetWidth) // 100
        console.log(document.querySelector('.inner2').offsetWidth) // 1536
    </script>
</body>
</html>

Or change to
float: left
position: absolute
display: inline-block

Reference link:
https://stackoverflow.com/questions/4014401/css-white-space-nowrap-doesnt-increase-width
https://stackoverflow.com/questions/22260240/width-of-element-with-white-space-nowrap-doesnt-adjust-to-child-content
https://stackoverflow.com/questions/37186433/white-space-nowrap-affects-width


周羊羊
923 声望22 粉丝

一只可爱的羊(๑•.•๑)