CSS: Make two floating elements overlap

前端 未结 8 1559
臣服心动
臣服心动 2021-01-03 19:28

I have two divs within a container. One floats left and one floats right. Both are about 60% as wide as the container and are designed such that they overlap in the middle (

相关标签:
8条回答
  • 2021-01-03 19:51

    You could create the divs with absolute position and add a positive z-index to the one you want to be in front.

    <div id="container">
        <div id="left">left</div>
        <div id="right">right</div>
    </div>
    
    #container {
        width: 400px;
        background-color: #eee;
        position: relative;
    }
    
    #left {
        width: 250px;
        border: 1px solid #ccc;
        display: block;
        position: absolute;
        top: 0px;
        left: 0px;
    }
    
    #right {
        width: 250px;
        border: 1px solid #ccc;
        display: inline;
        position: absolute;
        top: 0px;
        right: 0px;
        z-index: 1;
    }
    
    0 讨论(0)
  • 2021-01-03 19:52

    You can only do that with positioning.

    <div id="container">
        <div id="left">left</div>
        <div id="right">right</div>
    </div>
    
    #container {
        width: 400px;
        background-color: #eee;
        position: relative;
    }
    
    #left {
        width: 250px;
        border: 1px solid #ccc;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1;
    }
    
    #right {
        width: 250px;
        border: 1px solid #ccc;
        position: absolute;
        right: 0;
        top: 0;
        z-index: 2;
    }
    
    0 讨论(0)
  • 2021-01-03 19:59

    Excellent Solution: http://jsfiddle.net/A9Ap7/237/


    So, dont use:

      MARGIN-LEFT:100px...
    

    == or similar commands.
    The problem is that, if the left elements size is changed, if window is resized or etc,,, then it will make you problems! so, dont use such custom dirty "tricks", but make a normal structure inside html, so they should be naturally ordered.

    0 讨论(0)
  • 2021-01-03 20:00

    Try this one:

    <div id="container"> 
        <div id="left">left</div> 
        <div id="right">right</div> 
    </div> 
    <style>
    #container { 
        width: 400px; 
        background-color: #eee; 
    } 
    
    #left { 
        width: 250px; 
        border: 1px solid #ccc; 
        float: left; 
    } 
    
    #right { 
        width: 250px; 
        border: 1px solid #ccc; 
        margin-left: 150px;
        position: absolute;
    }
    </style>
    
    0 讨论(0)
  • 2021-01-03 20:03

    Use a negative margin-right on the left box so that the right box is allowed to overlap:

    #left {
        width: 250px;
        border: 1px solid #ccc;
        display: inline;
        float: left;
        margin-right:-104px;
    }
    

    The 104 pixels is the overlap amount plus 4px for borders.

    Here's a jsfiddle.

    0 讨论(0)
  • 2021-01-03 20:08

    Can you add an extra div in there?

    <div id="container"> 
        <div id="left">
            <div id="left-inner">left</div>
        </div> 
        <div id="right">right</div> 
    </div> 
    <style>
    #container { 
        width: 400px; 
    } 
    
    #left { 
        float: left;
        width: 0px;
        overflow:visible; 
    } 
    
    #left-inner { 
        float: right;
        width: 250px; 
    } 
    
    #right { 
        width: 250px; 
    }
    </style>
    
    0 讨论(0)
提交回复
热议问题