代码之家  ›  专栏  ›  技术社区  ›  avernet

如何设置dt和dd的样式,使它们在同一行上?

  •  181
  • avernet  · 技术社区  · 16 年前

    使用CSS,如何设置以下样式:

    <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>
    

    所以 dt 在一列中显示 dd 在另一列中,每个列 日期 以及相应的 在同一条线上?也就是说,生产出如下产品:

    table format

    16 回复  |  直到 7 年前
        1
  •  129
  •   Ariel    10 年前

    dl {
      width: 100%;
      overflow: hidden;
      background: #ff0;
      padding: 0;
      margin: 0
    }
    dt {
      float: left;
      width: 50%;
      /* adjust the width; make sure the total of both is 100% */
      background: #cc0;
      padding: 0;
      margin: 0
    }
    dd {
      float: left;
      width: 50%;
      /* adjust the width; make sure the total of both is 100% */
      background: #dd0
      padding: 0;
      margin: 0
    }
    <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>
        2
  •  106
  •   Navaneeth    9 年前

    我有一个不使用浮点数的解决方案!
    检查这个 codepen

    dl.inline dd {
      display: inline;
      margin: 0;
    }
    dl.inline dd:after{
      display: block;
      content: '';
    }
    dl.inline dt{
      display: inline-block;
      min-width: 100px;
    }
    



    更新- 3 研发 2017年1月: 我已经为这个问题添加了基于flex-box的解决方案。在链接中检查 科德森 &根据需要对其进行优化(amp;F)。谢谢!

    dl.inline-flex {
      display: flex;
      flex-flow: row;
      flex-wrap: wrap;
      width: 300px;      /* set the container width*/
      overflow: visible;
    }
    dl.inline-flex dt {
      flex: 0 0 50%;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    dl.inline-flex dd {
      flex:0 0 50%;
      margin-left: auto;
      text-align: left;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    
        3
  •  54
  •   Edric Prince Bansal    7 年前

    如果使用bootstrap 3(或更早版本)…

    <dl class="dl-horizontal">
        <dt>Label:</dt>
        <dd>
          Description of planet
        </dd>
        <dt>Label2:</dt>
        <dd>
          Description of planet
        </dd>
    </dl>
    
        4
  •  20
  •   ancestral    14 年前

    假设你知道页边宽度:

    dt { float: left; width: 100px; }
    dd { margin-left: 100px; }
    
        5
  •  16
  •   yckart Matthew Crumley    9 年前

    CSS网格布局

    与表格一样,网格布局允许作者将元素对齐到列和行中。
    https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout

    要更改列大小,请查看 grid-template-columns 财产。

    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>
        6
  •  8
  •   Tyler Crompton    11 年前

    因为我还没有看到一个适用于我的用例的例子,这里是我能够认识到的最完整的证明解决方案。

    dd {
        margin: 0;
    }
    dd::after {
        content: '\A';
        white-space: pre-line;
    }
    dd:last-of-type::after {
        content: '';
    }
    dd, dt {
        display: inline;
    }
    dd, dt, .address {
        vertical-align: middle;
    }
    dt {
        font-weight: bolder;
    }
    dt::after {
        content: ': ';
    }
    .address {
        display: inline-block;
        white-space: pre;
    }
    Surrounding
    
    <dl>
      <dt>Phone Number</dt>
      <dd>+1 (800) 555-1234</dd>
      <dt>Email Address</dt>
      <dd><a href="#">example@example.com</a></dd>
      <dt>Postal Address</dt>
      <dd><div class="address">123 FAKE ST<br />EXAMPLE EX  00000</div></dd>
    </dl>
    
    Text

    奇怪的是,它不适用于 display: inline-block . 我想如果你需要设置 dt 元素或 dd 元素,可以设置 dl 显示为 display: flexbox; display: -webkit-flex; display: flex; 以及 flex 的速记 DD 元素和 dt 元素,比如 flex: 1 1 50% display 作为 显示:内联块 . 但我还没有测试过,所以要谨慎行事。

        7
  •  6
  •   thirdender    13 年前

    jsFiddle Screenshot

    jsFiddle demo

    我需要一个与描述的项目完全相同的列表,显示公司的员工,他们的照片在左边,信息在右边。我设法通过使用psuedo元素来完成清除 DD 以下内容:

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

    此外,我希望文本只显示在图像的右侧,而不在浮动图像下进行换行(伪列效果)。这可以通过添加 DIV 带有css的元素 overflow: hidden; 围绕的内容 DD 标签。你可以省略这个额外的 div 但内容 DD 标签将包裹在漂浮物下。 DT .

    玩了一会儿之后,我可以支持多个 dt 元素单位 DD ,但不是多个 DD 元素单位 dt . 我试图在最后一个之后添加另一个可选类来清除 DD ,但随后 DD 包裹在 dt 元素(不是我想要的效果我想要 dt DD 构成列的元素,以及 DD 元素太宽)。

    根据所有权利,这应该只在IE8+中有效,但由于IE7中的一个怪癖,它也可以在那里工作。

        8
  •  5
  •   Astrotim    11 年前

    我需要这样做 <dt> 内容垂直居中,相对于 <dd> 内容。我用过 display: inline-block 一起 vertical-align: middle

    See full example on Codepen here

    .dl-horizontal {
      font-size: 0;
      text-align: center;
    
      dt, dd {
        font-size: 16px;
        display: inline-block;
        vertical-align: middle;
        width: calc(50% - 10px);
      }
    
      dt {
        text-align: right;
        padding-right: 10px;
      }
    
      dd {
        font-size: 18px;
        text-align: left;
        padding-left: 10px;
      } 
    }
    
        9
  •  4
  •   Andrew Downes    12 年前

    这是另一个选项,它通过显示dt和dd内联,然后在dd后面添加一个换行符来工作。

    dt, dd {
     display: inline;
    }
    dd:after {
     content:"\a";
     white-space: pre;
    }
    

    这类似于Navaneeth上面的解决方案,但是使用这种方法,内容不会像表格中那样排列,但是dd将在每行上立即跟踪dt,不管长度如何。

        10
  •  3
  •   Alexey    15 年前

    我找到了一个对我来说似乎完美的解决方案,但它需要额外的 <div> 标签。 结果证明不需要使用 <table> 如表中所示,要对齐的标记足以使用 display:table-row; display:table-cell; 款式:

    <style type="text/css">
    dl > div {
      display: table-row;
    }
    dl > div > dt {
      display: table-cell;
      background: #ff0;
    }
    dl > div > dd {
      display: table-cell;
      padding-left: 1em;
      background: #0ff;
    }
    </style>
    
    <dl>
      <div>
        <dt>Mercury</dt>
        <dd>Mercury (0.4 AU from the Sun) is the closest planet to the Sun and the smallest planet.</dd>
      </div>
      <div>
        <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>
      </div>
      <div>
        <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>
      </div>
    </dl>
    
        11
  •  3
  •   Sirko    13 年前

    根据您对dt和dd元素的样式,您可能会遇到一个问题:使它们具有相同的高度。例如,如果您希望在这些元素的底部只显示一些可见的边框,那么您很可能希望以相同的高度显示边框,就像在表中一样。

    解决这个问题的一个方法是作弊,并使每一行成为一个“dl”元素。 (这相当于在表中使用tr) 我们对定义列表失去了最初的兴趣,但是在对应的列表中,这是一种获得快速且漂亮样式的伪表的简单方法。

    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;}
    

    HTML:

    <div class="huitCinqPts bord_inf_gc">
      <dl><dt>Term1</dt><dd>Definition1</dd></dl>
      <dl><dt>Term2</dt><dd>Definition2</dd></dl>
    </div>
    
        12
  •  2
  •   Mike Godin    10 年前

    我最近需要通过指定类来混合内联和非内联的dt/dd对。 dl-inline <dt> 应后跟inline的元素 <dd> 元素。

    dt.dl-inline {
      display: inline;
    }
    dt.dl-inline:before {
      content:"";
      display:block;
    }
    dt.dl-inline + dd {
      display: inline;
      margin-left: 0.5em;
      clear:right;
    }
    <dl>
        <dt>The first term.</dt>
        <dd>Definition of the first term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
        <dt class="dl-inline">The second term.</dt>
        <dd>Definition of the second term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
        <dt class="dl-inline">The third term.</dt>
        <dd>Definition of the third term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
        <dt>The fourth term</dt>
        <dd>Definition of the fourth term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
    </dl
    
    >
        13
  •  1
  •   Justin    12 年前

    它适用于IE7+,符合标准,允许不同的高度。

    <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 .

        14
  •  1
  •   Mousey    10 年前

    这样可以将它们显示为表格,带有边框,它的响应宽度应为第一列宽度的3em。换行符只会将任何比列宽的单词分隔开。

     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; } 
    

    比较 <table> 具有 <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>
        15
  •  0
  •   pinky00106    8 年前

    当样式定义列表为表时,我通常从以下内容开始:

    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 */
    }
    
        16
  •  -8
  •   RoboTamer    14 年前

    这里推荐的大多数代码都有效,但是您应该只将通用代码放在样式表中,并将特定代码放在HTML代码中,如下所示。否则,您将得到一个膨胀的样式表。

    我是这样做的:

    样式表代码:

    <style>
        dt {
            float:left;
        }
        dd {
            border-left:2px dotted #aaa;
            padding-left: 1em;
            margin: .5em;
        }   
    </style>
    

    您的HTML代码:

    <dl>
        <dt>1st Entity</dt>
        <dd style="margin-left: 5em;">Consumer</dd>
        <dt>2nd Entity</dt>
        <dd style="margin-left: 5em;">Merchant</dd>
        <dt>3rd Entity</dt>
        <dd style="margin-left: 5em;">Provider, or cToken direct to Merchant</dd>
        <dt>4th Entity</dt>
        <dd style="margin-left: 5em;">cToken to Provider</dd>
    </dl>
    

    Looks like this