Putting a Border Around Floating Elements

后端 未结 3 790
夕颜
夕颜 2021-02-03 22:13

Say I have something like the following code, where I want to display some text between two images that I am floating left and right.



        
相关标签:
3条回答
  • 2021-02-03 22:30

    Add this line to your CSS properties:

    overflow: hidden
    
    0 讨论(0)
  • 2021-02-03 22:37

    In CSS, floated elements do not add height to a parent by default.

    The solution is simply to set overflow: hidden.

    <div style="border: 2px solid black; overflow: hidden;" 
        <img src="testImage1.png" alt="Test Image 1" style="float:right;" />
        <img src="testImage2.png" alt="Test Image 2" style="float:left;" />
        <p>Test Text</p>
    </div>
    

    fiddle: http://jsfiddle.net/JNets/

    0 讨论(0)
  • 2021-02-03 22:51

    Adding an overflow in this case with a value of hidden or auto remedies the issue.

    Check the fiddle below:

    http://jsfiddle.net/XMrwR/

    Clearing floats the overflow way

    http://www.quirksmode.org/blog/archives/2005/03/clearing_floats.html

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