How to style dt and dd so they are on the same line?

后端 未结 18 2194
滥情空心
滥情空心 2020-11-27 10:12

Using CSS, how can I style the following:

Mercury
Mercury (0.4 AU from the Sun) is the closest planet to th
相关标签:
18条回答
  • 2020-11-27 10:28

    This works on IE7+, is standards compliant, and allows differing heights.

    <style>
    dt {
        float: left;
        clear: left;
        width: 100px;        
        padding: 5px 0;
        margin:0;
    }
    dd {
        float: left;
        width: 200px;
        padding: 5px 0;
        margin:0;
    }
    .cf:after {
        content:'';
        display:table;
        clear:both;
    }
    </style>
    
    <dl class="cf">
        <dt>A</dt>
        <dd>Apple</dd>
        <dt>B</dt>
        <dd>Banana<br>Bread<br>Bun</dd>
        <dt>C</dt>
        <dd>Cinnamon</dd>
    </dl>        
    

    See the JSFiddle.

    0 讨论(0)
  • 2020-11-27 10:30

    jsFiddle Screenshot

    See jsFiddle demo

    I needed a list exactly as described for a project that showed employees at a company, with their photo on the left, and information on the right. I managed to accomplish the clearing by using psuedo-elements after every DD:

    .myList dd:after {
      content: '';
      display: table;
      clear: both;
    }
    

    In addition, I wanted the text to only display to the right of the image, without wrapping under the floated image (pseudo-column effect). This can be accomplished by adding a DIV element with the CSS overflow: hidden; around the content of the DD tag. You can omit this extra DIV, but the content of the DD tag will wrap under the floated DT.

    After playing with it a while, I was able to support multiple DT elements per DD, but not multiple DD elements per DT. I tried adding another optional class to clear only after the last DD, but subsequent DD elements wrapped under the DT elements (not my desired effect… I wanted the DT and DD elements to form columns, and the extra DD elements were too wide).

    By all rights, this should only work in IE8+, but due to a quirk in IE7 it works there as well.

    0 讨论(0)
  • 2020-11-27 10:34

    CSS Grid layout

    Like tables, grid layout enables an author to align elements into columns and rows.
    https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout

    To change the column sizes, take a look at the grid-template-columns property.

    dl {
      display: grid;
      grid-template-columns: max-content auto;
    }
    
    dt {
      grid-column-start: 1;
    }
    
    dd {
      grid-column-start: 2;
    }
    <dl>
      <dt>Mercury</dt>
      <dd>Mercury (0.4 AU from the Sun) is the closest planet to the Sun and the smallest planet.</dd>
      <dt>Venus</dt>
      <dd>Venus (0.7 AU) is close in size to Earth, (0.815 Earth masses) and like Earth, has a thick silicate mantle around an iron core.</dd>
      <dt>Earth</dt>
      <dd>Earth (1 AU) is the largest and densest of the inner planets, the only one known to have current geological activity.</dd>
    </dl>

    0 讨论(0)
  • 2020-11-27 10:36

    Depending on how you style the dt and dd elements, you might encounter a problem: making them have the same height. For instance, if you want to but some visible border at the bottom of those elements, you most probably want to display the border at the same height, like in a table.

    One solution for this is cheating and make each row a "dl" element. (this is equivalent to using tr in a table) We loose the original interest of definition lists, but on the counterpart this is an easy manner to get pseudo-tables that are quickly and pretty styled.

    THE CSS:

    dl {
     margin:0;
     padding:0;
     clear:both;
     overflow:hidden;
    }
    dt {
     margin:0;
     padding:0;
     float:left;
     width:28%;
     list-style-type:bullet;
    }
    dd {
     margin:0;
     padding:0;
     float:right;
     width:72%;
    }
    
    .huitCinqPts dl dt, .huitCinqPts dl dd {font-size:11.3px;}
    .bord_inf_gc dl {padding-top:0.23em;padding-bottom:0.5em;border-bottom:1px solid #aaa;}
    

    THE HTML:

    <div class="huitCinqPts bord_inf_gc">
      <dl><dt>Term1</dt><dd>Definition1</dd></dl>
      <dl><dt>Term2</dt><dd>Definition2</dd></dl>
    </div>
    
    0 讨论(0)
  • 2020-11-27 10:40

    this works to display them as table, with border, it should be responsive with 3em the width of the first column. The word-wrap just breaks any words wider than the column

     dl { display:block;
          border:2px solid black;
          margin: 1em;}  
     dt { display:inline-block;
          width:3em;
          word-wrap:break-word;} 
     dd { margin-left:0;
          display:inline;
          vertical-align:top;
          line-height:1.3;} 
     dd:after { content:'';display:block; } 
    

    Comparison of <table> with <dl>:

    <!DOCTYPE html>
    <html>
    <style>
    
    dl { display:block;border:2px outset black;margin:1em; line-height:18px;}  
    dt { display:inline-block;width:3em; word-wrap:break-word;} 
    
    dd { margin-left:0; display:inline; vertical-align:top; line-height:1.3;} 
    dd:after { content:'';display:block; } 
    
    
    .glosstable { border:2px outset #aaaaaa;margin:1em; text-align:left}  
    .glosstable, table, tbody,  tr,  td, dl, dt {font-size:100%; line-height:18px;}
    
    .glossaz { font-size:140%;padding-left:2em;font-weight:bold;color: #00838c; } 
    td.first {width: 2.5em;} 
    </style>
    <body>
    Table<br>
    <table class="glosstable">
      <tr><td class="first">Milk</td>
      <td class="glossdata">Black hot drink</td>
    </tr>
      <tr><td class="first">Coffee2</td>
      <td class="glossdata">Black hot drink</td>
    </tr>
      <tr><td>Warm milk</td>
      <td class="glossdata">White hot drink</td>
    </tr>
    </table>
    DL list <br>
    <dl class="glosstablep">
      <dt>Milk</dt>
      <dd class="glossdata">White cold drink</dd>
      <dt>Coffee2</dt>
      <dd class="glossdata">Black cold drink</dd>
      <dt>Warm Milk</dt>
      <dd class="glossdata">White hot drink</dd>
    </dl>
    
    </body>
    </html>

    0 讨论(0)
  • 2020-11-27 10:40

    I usually start with the following when styling definition lists as tables:

    dt,
    dd{
        /* Override browser defaults */
        display: inline;
        margin: 0;
    }
    
    dt  {
        clear:left;
        float:left;
        line-height:1; /* Adjust this value as you see fit */
        width:33%; /* 1/3 the width of the parent. Adjust this value as you see fit */
    }
    
    dd {
        clear:right;
        float: right;
        line-height:1; /* Adjust this value as you see fit */
        width:67%; /* 2/3 the width of the parent. Adjust this value as you see fit */
    }
    
    0 讨论(0)
提交回复
热议问题