How to display items side-by-side without using tables?

后端 未结 9 1802
野性不改
野性不改 2020-12-08 16:14

For example you want to display an image beside a text, usually I would do this:

相关标签:
9条回答
  • 2020-12-08 16:33

    All these answers date back to 2016 or earlier... There's a new web standard for this using flex-boxes. In general floats for these sorts of problems is now frowned upon.

    HTML

    <div class="image-txt-container">
      <img src="https://images4.alphacoders.com/206/thumb-350-20658.jpg">
      <h2>
        Text here
      </h2>
    </div>
    

    CSS

    .image-txt-container {
      display: flex;
      align-items: center;
      flex-direction: row;
    }
    

    Example fiddle: https://jsfiddle.net/r8zgokeb/1/

    0 讨论(0)
  • 2020-12-08 16:34

    The negative margin would help a lot!

    The html DOM looks like below:

    <div class="main">
      <div class="main_body">Main content</div>
    </div>
    <div class="left">Left Images or something else</div>
    

    And the CSS:

    .main {
      float:left;
      width:100%;
    }
    
    .main_body{
      margin-left:210px;
      height:200px;
    }
    .left{
      float:left;
      width:200px;
      height:200px;
      margin-left:-100%;
    }
    

    The main_body will responsive it's with, may it helps you!

    0 讨论(0)
  • 2020-12-08 16:38

    It depends on what you want to do and what type of data/information you are displaying. In general, tables are reserved for displaying tabular data.

    An alternate for your situation would be to use css. A simple option would be to float your image and give it a margin:

    <p>
        <img style="float: left; margin: 5px;" ... />
        Text goes here...
    </p>
    

    This would cause the text to wrap around the image. If you don't want the text to wrap around the image, put the text in a separate container:

    <div>
        <img style="float: left; margin: ...;" ... />
        <p style="float: right;">Text goes here...</p>
    </div>
    

    Note that it may be necessary to assign a width to the paragraph tag to display the way you'd like. Also note, for elements that appear below floated elements, you may need to add the style "clear: left;" (or clear: right, or clear: both).

    0 讨论(0)
  • 2020-12-08 16:44

    Yes, divs and CSS are usually a better and easier way to place your HTML. There are many different ways to do this and it all depends on the context.

    For instance, if you want to place an image to the right of your text, you could do it like so:

    <p style="width: 500px;">
    <img src="image.png" style="float: right;" />
    This is some text
    </p> 
    

    And if you want to display multiple items side by side, float is also usually preferred.For example:

    <div>
      <img src="image1.png" style="float: left;" />
      <img src="image2.png" style="float: left;" />
      <img src="image3.png" style="float: left;" />
    </div>
    

    Floating these images to the same side will have then laying next to each other for as long as you hava horizontal space.

    0 讨论(0)
  • 2020-12-08 16:47

    You should float them inside a container that is cleared.

    Example:

    https://jsfiddle.net/W74Z8/504/

    A clean implementation is the "clearfix hack". This is Nicolas Gallagher's version:

    /**
     * For modern browsers
     * 1. The space content is one way to avoid an Opera bug when the
     *    contenteditable attribute is included anywhere else in the document.
     *    Otherwise it causes space to appear at the top and bottom of elements
     *    that are clearfixed.
     * 2. The use of `table` rather than `block` is only necessary if using
     *    `:before` to contain the top-margins of child elements.
     */
    .clearfix:before,
    .clearfix:after {
        content: " "; /* 1 */
        display: table; /* 2 */
    }
    
    .clearfix:after {
        clear: both;
    }
    
    /**
     * For IE 6/7 only
     * Include this rule to trigger hasLayout and contain floats.
     */
    .clearfix {
        *zoom: 1;
    }
    ​
    
    0 讨论(0)
  • 2020-12-08 16:47

    these days div is the new norm

    <div style="float:left"><img.. ></div>
    <div style="float:right">text</div>
    <div style="clear:both"/>
    
    0 讨论(0)
提交回复
热议问题