Simple two column html layout without using tables

后端 未结 12 1706
夕颜
夕颜 2021-01-30 05:17

I\'m looking for a super easy method to create a two column format to display some data on a webpage. How can i achieve the same format as:


    &l         
相关标签:
12条回答
  • 2021-01-30 05:42

    If you want to do it the HTML5 way (this particular code works better for things like blogs, where <article> is used multiple times, once for each blog entry teaser; ultimately, the elements themselves don't matter much, it's the styling and element placement that will get you your desired results):

    <style type="text/css">
    article {
      float: left;
      width: 500px;
    }
    
    aside {
      float: right;
      width: 200px;
    }
    
    #wrap {
      width: 700px;
      margin: 0 auto;
    }
    </style>
    
    <div id="wrap">
      <article>
         Main content here
      </article>
      <aside>
         Sidebar stuff here
      </aside>
    </div>
    
    0 讨论(0)
  • 2021-01-30 05:43

    All the previous answers only provide a hard-coded location of where the first column ends and the second column starts. I would have expected that this is not required or even not wanted.

    Recent CSS versions know about an attribute called columns which makes column based layouts super easy. For older browsers you need to include -moz-columns and -webkit-columns, too.

    Here's a very simple example which creates up to three columns if each of them has at least 200 pixes width, otherwise less columns are used:

    <html>
      <head>
        <title>CSS based columns</title>
      </head>
      <body>
        <h1>CSS based columns</h1>
        <ul style="columns: 3 200px; -moz-columns: 3 200px; -webkit-columns: 3 200px;">
          <li>Item one</li>
          <li>Item two</li>
          <li>Item three</li>
          <li>Item four</li>
          <li>Item five</li>
          <li>Item six</li>
          <li>Item eight</li>
          <li>Item nine</li>
          <li>Item ten</li>
          <li>Item eleven</li>
          <li>Item twelve</li>
          <li>Item thirteen</li>
        </ul>
      </body>
    </html>
    
    0 讨论(0)
  • 2021-01-30 05:44

    I know this is an old post, but figured I'd add my two penneth. How about the seldom used and oft' forgot Description list? With a simple bit of css you can get a really clean markup.

    <dl>
    <dt></dt><dd></dd>
    <dt></dt><dd></dd>
    <dt></dt><dd></dd>
    </dl>
    

    take a look at this example http://codepen.io/butlerps/pen/wGmXPL

    0 讨论(0)
  • 2021-01-30 05:49

    a few small changes to make it responsive

    <style type="text/css">
    #wrap {
        width: 100%;
        margin: 0 auto;
        display: table;
    }
    #left_col {
       float:left;
       width:50%;
    }
    #right_col {
       float:right;
       width:50%;
    }
    @media only screen and (max-width: 480px){
        #left_col {
           width:100%;
        }
        #right_col {
           width:100%;
        }
    }
    </style>
    
    <div id="wrap">
        <div id="left_col">
            ...
        </div>
        <div id="right_col">
            ...
        </div>
    </div>
    
    0 讨论(0)
  • 2021-01-30 05:51

    There's now a much simpler solution than when this question was originally asked, five years ago. A CSS Flexbox makes the two column layout originally asked for easy. This is the bare bones equivalent of the table in the original question:

    <div style="display: flex">
        <div>AAA</div>
        <div>BBB</div>
    </div>
    

    One of the nice things about a Flexbox is that it lets you easily specify how child elements should shrink and grow to adjust to the container size. I will expand on the above example to make the box the full width of the page, make the left column a minimum of 75px wide, and grow the right column to capture the leftover space. I will also pull the style into its own proper block, assign some background colors so that the columns are apparent, and add legacy Flex support for some older browsers.

    <style type="text/css">
    .flexbox {
        display: -ms-flex;
        display: -webkit-flex;
        display: flex;
        width: 100%;
    }
    
    .left {
        background: #a0ffa0;
        min-width: 75px;
        flex-grow: 0;
    }
    
    .right {
        background: #a0a0ff;
        flex-grow: 1;
    }
    </style>
    
    ...
    
    <div class="flexbox">
        <div class="left">AAA</div>
        <div class="right">BBB</div>
    </div>
    

    Flex is relatively new, and so if you're stuck having to support IE 8 and IE 9 you can't use it. However, as of this writing, http://caniuse.com/#feat=flexbox indicates at least partial support by browsers used by 94.04% of the market.

    0 讨论(0)
  • 2021-01-30 05:51
    <div id"content">
    <div id"contentLeft"></div>
    <div id"contentRight"></div>
    </div> 
    
    #content {
    clear: both;
    width: 950px;
    padding-bottom: 10px;
    background:#fff;
    overflow:hidden;
    }
    #contentLeft {
    float: left;
    display:inline;
    width: 630px;
    margin: 10px;
    background:#fff;
    }
    #contentRight {
    float: right;
    width: 270px;
    margin-top:25px;
    margin-right:15px;
    background:#d7e5f7;
    } 
    

    Obviously you will need to adjust the size of the columns to suit your site as well as colours etc but that should do it. You also need to make sure that your ContentLeft and ContentRight widths do not exceed the Contents width (including margins).

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