Text Wrapping around an absolute positioned div

后端 未结 10 1818
感动是毒
感动是毒 2020-12-03 13:25

I know there are a few questions about similar topics but they mostly amount to floating the div/image. I need to have the image (and div) positioned absolutely (off to the

相关标签:
10条回答
  • 2020-12-03 14:06

    I think the best option is to add an additional div after the float content, but still inside the parent to clear previous styles.

    <div class="clear"></div>
    

    And CSS:

    .clear
    {clear:both;}
    
    0 讨论(0)
  • 2020-12-03 14:14

    I needed a similar solution to float a pullout quote (not an image) which would have variable length text inside. The pullout quote needed to be inserted into the HTML at the top (outside the flow of the text) and float down into the content with text that wraps around it. Modifying Leonard's answer above, there is a really simple way to do this!

    See Codepen for Working Example: https://codepen.io/chadwickmeyer/pen/gqqqNE

    CSS

    /* This creates a space to insert the pullout content into the flow of the text that follows */
    .pulloutContainer:before {
      content: '' ;
      display:block;
      float: right;
      /* The height is essentially a "margin-top" to push the pullout Container down page */
      height: 200px;
    }
    
    .pulloutContainer q {
      float:left;  
      clear:both;
      /* This can be a set width or percent, if you want a pullout that floats left or right or full full width */
      width: 30%;
      /* Add padding as you see fit */
      padding: 50px 20px;
    }
    

    HTML

    <div id="container">
    
      <div class="pulloutContainer">
          <!-- Pullout Container Automatically Adjusts Size -->
          <q>Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet.</q>
        </div>
    
        <div class="content">
           <h1>Sed Aucteor Neque</h1>
           <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in dui mauris. Vivamus hendrerit arcu sed erat molestie vehicula. Sed auctor neque eu tellus rhoncus ut eleifend nibh porttitor. Ut in nulla enim. Phasellus molestie magna non est.</
    
          ...INSERT MORE TEXT HERE...
    
      </div>
    </div>
    
    0 讨论(0)
  • 2020-12-03 14:15

    There is an easy fix to this problem. It's using white-space: nowrap;

    <div style="position:relative">
     <div style="position: absolute;top: 100%; left:0;">
      <div style="white-space:nowrap; width: 100%;">
        stuff
      </div>
     </div>
    </div>
    

    For example I was making a dropdown menu for a navigation so the setup I was using is

    <ul class="submenu" style="position:absolute; z-index:99;">
       <li style="width:100%; display:block;">
          <a href="#" style="display: block;width: 100%;white-space: nowrap;">Dropdown link here</a>
       </li>
    <ul>
    

    Image Examples

    Without Nowrap enabled

    With Nowrap enabled

    Also if you still can't figure it out check out the dropdowns on bootstrap templates which you can google. Then find out how they work because they are using position absolute and getting the text to take up 100% width without wrapping the text.

    0 讨论(0)
  • 2020-12-03 14:20

    In my opinon, the "Absolute" trait is poorly named, because its position is actually relative to the first parent whos position is not static

    <div class="floated">
     <div style="position: relative;">
      <div class="AbsoluteContent">
        stuff
      </div>
     </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题