Centering floating divs within another div

前端 未结 7 2033
自闭症患者
自闭症患者 2020-11-29 14:58

I\'ve searched other questions and, while this problem seems similar to a couple of others, nothing I\'ve seen so far seems to address the issue that I\'m having.

I

相关标签:
7条回答
  • 2020-11-29 15:40

    The following solution does not use inline blocks. However, it requires two helper divs:

    1. The content is floated
    2. The inner helper is floated (it stretches as much as the content)
    3. The inner helper is pushed right 50% (its left aligns with center of outer helper)
    4. The content is pulled left 50% (its center aligns with left of inner helper)
    5. The outer helper is set to hide the overflow

    .ca-outer {
      overflow: hidden;
      background: #FFC;
    }
    .ca-inner {
      float: left;
      position: relative;
      left: 50%;
      background: #FDD;
    }
    .content {
      float: left;
      position: relative;
      left: -50%;
      background: #080;
    }
    /* examples */
    div.content > div {
      float: left;
      margin: 10px;
      width: 100px;
      height: 100px;
      background: #FFF;
    }
    ul.content {
      padding: 0;
      list-style-type: none;
    }
    ul.content > li {
      margin: 10px;
      background: #FFF;
    }
    <div class="ca-outer">
      <div class="ca-inner">
        <div class="content">
          <div>Box 1</div>
          <div>Box 2</div>
          <div>Box 3</div>
        </div>
      </div>
    </div>
    <hr>
    <div class="ca-outer">
      <div class="ca-inner">
        <ul class="content">
          <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
          <li>Nullam efficitur nulla in libero consectetur dictum ac a sem.</li>
          <li>Suspendisse iaculis risus ut dapibus cursus.</li>
        </ul>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-29 15:53

    Solution:

    <!DOCTYPE HTML>
    <html>
        <head>
            <title>Knowledge is Power</title>
            <script src="js/jquery.js"></script>
            <script type="text/javascript">
            </script>
            <style type="text/css">
                #outer {
                    text-align:center;
                    width:100%;
                    height:200px;
                    background:red;
                }
                #inner {
                    display:inline-block;
                    height:200px;
                    background:yellow;
                }
            </style>
        </head>
        <body>
            <div id="outer">
                <div id="inner">Hello, I am Touhid Rahman. The man in Light</div>
            </div>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 15:56

    In my case, I could not get the answer by @Sampson to work for me, at best I got a single column centered on the page. In the process however, I learned how the float actually works and created this solution. At it's core the fix is very simple but hard to find as evident by this thread which has had more than 146k views at the time of this post without mention.

    All that is needed is to total the amount of screen space width that the desired layout will occupy then make the parent the same width and apply margin:auto. That's it!

    The elements in the layout will dictate the width and height of the "outer" div. Take each "myFloat" or element's width or height + its borders + its margins and its paddings and add them all together. Then add the other elements together in the same fashion. This will give you the parent width. They can all be somewhat different sizes and you can do this with fewer or more elements.

    Ex.(each element has 2 sides so border, margin and padding get multiplied x2)

    So an element that has a width of 10px, border 2px, margin 6px, padding 3px would look like this: 10 + 4 + 12 + 6 = 32

    Then add all of your element's totaled widths together.

    Element 1 = 32
    Element 2 = 24
    Element 3 = 32
    Element 4 = 24
    

    In this example the width for the "outer" div would be 112.

    .outer {
      /* floats + margins + borders = 270 */
      max-width: 270px;
      margin: auto;
      height: 80px;
      border: 1px;
      border-style: solid;
    }
    
    .myFloat {
        /* 3 floats x 50px = 150px */
        width: 50px;
        /* 6 margins x 10px = 60 */
        margin: 10px;
        /* 6 borders x 10px = 60 */
        border: 10px solid #6B6B6B;
        float: left;
        text-align: center;
        height: 40px;
    }
    <div class="outer">
      <div class="myFloat">Float 1</div>
      <div class="myFloat">Float 2</div>
      <div class="myFloat">Float 3</div>
    </div>

    0 讨论(0)
  • 2020-11-29 15:58

    display: inline-block; won't work in any of IE browsers. Here is what I used.

    // change the width of #boxContainer to 
    // 1-2 pixels higher than total width of the boxes inside:
    
    #boxContainer {         
        width: 800px; 
        height: auto;
        text-align: center;
        margin-left: auto;
        margin-right: auto;
    }
    
    #Box{
        width: 240px; 
        height: 90px;
        background-color: #FFF;
        float: left;
        margin-left: 10px;
        margin-right: 10px;
    }
    
    0 讨论(0)
  • 2020-11-29 16:01

    First, remove the float attribute on the inner divs. Then, put text-align: center on the main outer div. And for the inner divs, use display: inline-block. Might also be wise to give them explicit widths too.


    <div style="margin: auto 1.5em; display: inline-block;">
      <img title="Nadia Bjorlin" alt="Nadia Bjorlin" src="headshot.nadia.png"/>
      <br/>
      Nadia Bjorlin
    </div>
    
    0 讨论(0)
  • 2020-11-29 16:01

    I accomplished the above using relative positioning and floating to the right.

    HTML code:

    <div class="clearfix">                          
        <div class="outer-div">
            <div class="inner-div">
                <div class="floating-div">Float 1</div>
                <div class="floating-div">Float 2</div>
                <div class="floating-div">Float 3</div>
            </div>
        </div>
    </div>
    

    CSS:

    .outer-div { position: relative; float: right; right: 50%; }
    .inner-div { position: relative; float: right; right: -50%; }
    .floating-div { float: left; border: 1px solid red; margin: 0 1.5em; }
    
    .clearfix:before,
    .clearfix:after { content: " "; display: table; }
    .clearfix:after { clear: both; }
    .clearfix { *zoom: 1; }
    

    JSFiddle: http://jsfiddle.net/MJ9yp/

    This will work in IE8 and up, but not earlier (surprise, surprise!)

    I do not recall the source of this method unfortunately, so I cannot give credit to the original author. If anybody else knows, please post the link!

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