Virtually infinite container (infinite width)

后端 未结 1 777
眼角桃花
眼角桃花 2021-01-26 01:48

In the following situation, I don\'t want #blah to have linebreaks : I want its only limitation to be max-width: 20em;. It should not break lin

相关标签:
1条回答
  • 2021-01-26 02:15

    Update

    A different post and some comments have been deleted, which indicated that jQuery was okay to use, even though it wasn't tagged in the question.

    My jQuery solution is below, but I've since come up with a much simpler solution: Add a width style to blah, which matches max-width. That way, container will automatically grow as needed:

    #blah { 
      position: absolute;  
      font-size: 100px;
      max-width: 20em;
      width: 20em;
    }
    


    jQuery Solution (which may be useful for similar problems):

    In the snippet below, I changed blah to a class instead of an ID.

    You'll notice that max-width is ignored.

    Click the Grow Container button. Each child of #container is cloned and given a static position, so we can determine its width. #container then grows as needed to match the width.

    Now that #container has a width, its children can widen to match their max-width.

    function growContainer() {
      $('.blah').each(function() {
        var clone= $(this).clone();
        clone.css({position: 'static'});
        $('body').append(clone);
        var right= $(this).offset().left + clone.width();
        $('#container').css({
          width: Math.max($('#container').width(), right)
        });
        clone.remove();
      });
    } //growContainer
    
    $('#BG').click(growContainer);
    #container {
      position: absolute;
    }
    
    #BG {
      position: fixed;
    }
    
    .blah { 
      position: absolute;  
      max-width: 20em;
    }
    
    #blah1 { 
      font-size: 100px;
      top: 50px;
      background: red;
    }
    
    #blah2 { 
      left: 5000px;
      font-size: 50px;
      background: lightblue;
    }
    
    #blah3 { 
      top: 1000px;
      font-size: 20px;
      background: green;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="BG">Grow Container</button>
    <div id="container">
      <div class="blah" id="blah1">
    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>
      <div class="blah" id="blah2">
    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?  </div>
      <div class="blah" id="blah3">
    At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.  </div>
      </div>

    0 讨论(0)
提交回复
热议问题