Categorizing dl elements

Imagine having a definition list with multiple sets. Here's an example:

<dl>
  <dt>Colors
  <dd>Red
  <dd>Blue

  <dt>Shapes
  <dd>Circle
</dl>

Now, what if you want these definitions to be displayed horizontally like this:

Colors    Shapes
Red       Circle
Blue

And even when the browser window is resized, it should wrap neatly like this:

Colors
Red
Blue

Shapes
Circle

Is there a clever way to achieve this layout without breaking the semantic structure of the dl tag?

It seems that only dt and dd elements are allowed within a dl list. Divs or other wrappers can't be used. Splitting into separate lists wouldn't be ideal either. Any suggestions?

Answer №1

The guidelines suggest that each group within a dl should be enclosed in a div element:

To add microdata attributes, global attributes that affect entire groups, or for aesthetic reasons, it is recommended to wrap each group in a dl element with a div element. This will not alter the meaning of the dl element.

https://html.spec.whatwg.org/multipage/grouping-content.html#the-dl-element

Utilize this div for styling purposes such as implementing flex properties.

Answer №2

There isn't a designated tag to enclose both dt and dd tags together. An element called "di" was suggested for XHTML 2.0, but the standard was unsuccessful.

Unlike a ul where each li tag can serve as an inner wrapper for individual list items, in this case each item has a pair of inner tags, making it different from that structure.

Essentially, you should either use a dl or a ul for each column. While semantic markup is preferred, a single dl alone is not optimal for this layout without the presence of a di tag.

Update:

As pointed out by @biziclop, once CSS3 Multiple-column layout becomes standardized and widely supported, splitting columns may be achievable at specified points (with limited additional layout options). Ultimately, the code snippet might resemble the following:

dl {column-width:100px; column-fill:auto;}
dt {break-before:always;}

Answer №3

Experimenting with the less commonly supported multi-column feature in CSS3:

Drawbacks:

  • Requires a specified height
  • Does not automatically wrap text (unless utilizing media queries)

http://jsfiddle.net/kERsT/8/

dl {
    -webkit-column-width: 150px;
       -moz-column-width: 150px;
            column-width: 150px;
    /* Must have explicitly defined height */
    height: 250px;
}

dt ~ dt {
    /* Intentionally causing column-break-before behavior */
    margin-top: 1200px;
}

Answer №4

In order to resolve the issue at hand, I successfully utilized an unordered list system. The implementation can be seen by following this link: http://jsfiddle.net/radialglo/pXLB4/. It should be noted that specifying the width is optional, but for the sake of demonstration, I assigned a sufficiently large width so as to force content onto the next line. Feel free to manipulate the html viewport on the provided fiddle.

<div>
    <ul>Authors
        <li>John</li>
        <li>Luke</li>
    </ul>
    <ul>
    <li>Editor</li>
    <li>Frank</li>
    </ul>
</div>​

The necessary styling code:

div ul{
    float: left;
    list-style: none;
    width: 100px;
    margin-bottom: 1em;
}​

If you have a strong preference for maintaining semantic structure, it may prove challenging to find a suitable solution. This is due to the fact that the organization of each term definition and its associated definitions does not lend itself well to easy stylization.

Answer №5

One way to achieve this is by utilizing absolute positioning and nth-of-type selectors, although these may not be supported by older browsers. You can see a demonstration here: http://jsfiddle.net/PrJWJ/6/

dt,dd{margin:0;}

@media (min-width:20em) /* 10em per column */
{
    /* Enable positioning*/
    dl{position:relative;}
    dt,dd{position:absolute;}

    dt{top:0;} /* All terms at the top */
    dd{top:1.3em;}  /* 1st dd under a term at 1.3em (factoring in line spacing) */
    dd+dd{top:2.6em;} /* 2nd dd under a term */

    /* 1st column */
    dt:nth-of-type(1),
    dt:nth-of-type(1)~dd
    {left:0;}

    /* 2nd column */
    dt:nth-of-type(2),
    dt:nth-of-type(2)~dd
    {left:10em;}
}

@media (max-width:19.99em)
{
    /* Space before a new term */
    dd+dt{margin-top:1em;}
}

It's important to customize and expand on this based on the maximum number of rows and columns you anticipate needing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customizing hover color with tailwind CSS

How can I change the color of an icon on mouseover using Tailwind CSS? I have tried to go from this https://i.sstatic.net/ObW6C.png to this https://i.sstatic.net/Tagp3.png, but it's not working. .btn { @apply agt-h-10 agt-w-10 agt-bg-zinc-100 agt- ...

Is there a way to extract a particular value from a JSON URL and transfer it to a JavaScript variable?

I am looking to extract current exchange rates from a JSON URL for implementation in a webpage. Specifically, I want to retrieve a particular exchange rate (such as the US Dollar) and store it in a variable for use within a JavaScript function. Below is a ...

Applying Bootstrap's inline styling to adjust the width of a label

I'm having trouble adjusting the width of a Bootstrap label using inline styles. Here is my code: <td><span class="label label-info" style="width:30px; text-align:center; background-color:#6d8cbf"> TEXT </span></td> Unfort ...

jQuery body background changer/utilizer

I am currently developing a website that requires the functionality to switch between background images with the ability for users to navigate through them using back and forth arrows. Each background image should have a unique dynamic description associa ...

Using Chrome to gather data from a website's tables

I am trying to scrape table data from a website using Selenium with the Chrome browser. I have written the code below, but it seems to be not working as expected. Sub Chartinka() Dim bot As New WebDriver, posts As WebElements, post As WebElement, i ...

One of the paragraphs refuses to align with the others by floating left

Greetings everyone! This is my first time posting here, although I have been a regular visitor looking for answers on this site. Hopefully, someone can assist me with my current issue. Let me explain my problem: I've been attempting to align all the ...

The `<Outlet/>` from react-router is being rendered in a location outside of its intended wrapper div

Utilizing MUI for crafting a straightforward dashboard featuring an <AppBar>, a side <Drawer>, my component appears as follows: <> <AppBar> // code omitted <AppBar/> <Drawer> // code omitted <Drawer/> / ...

What is the best way to implement NgClass on a single iteration within NgFor while utilizing parent/child components?

My goal is to toggle the visibility of tiered buttons when a parent button is clicked. I am using ngFor to generate three buttons on tier 1, but I'm struggling to select only the desired tier when clicked instead of affecting all of them. I've m ...

Guide to incorporating a fadeIn effect in Jquery triggered by a change event

Looking for some guidance on implementing a fade-in effect on a <p> tag with relevant CSS after making an AJAX call in response to a change event. Below is the current code snippet: $('#scenarioDropdownList').change(function() { var sc ...

Confirm the Text Box with JavaScript

I'm struggling with validating the textbox on my login Xhtml. Once I finally cracked the code, I encountered an issue with the 'container' class in the section. How can I properly write the code and successfully validate the textbox? <h ...

What is the best way to display the output after retrieving an array?

Database : --> product table P_id P_name P_uploadKey 1 Cemera 7365 2 Notebook 7222 3 Monitor 7355 4 Printer 7242 --> buy table B_id P_id B_nam ...

I am having trouble properly positioning an icon next to its corresponding text within a menu item

I'm a newcomer to the world of HTML5 + CSS3 and I'm having trouble with aligning menus, text, and icons within the menu. Here's the issue: Each line of the menu consists of an icon and a text description. The problem is that they are too clo ...

The CSS overflow property is a great tool to demonstrate how a box can be neatly cut off at its

According to the specifications outlined in this resource, In situations where overflow occurs, the 'overflow' property determines if a box is clipped to its padding edge. Additionally, it specifies whether or not a scrolling mechanism will b ...

What is the best way to determine if a character is valid for use in HTML rendering?

There are certain characters, like ordinal 22 or 8, that do not appear correctly in HTML (when using Chrome for example when copying and pasting them into this 'Ask question' editor; assuming utf-8 encoding). How can I identify which characters a ...

Spring Boot application on Prime Faces is not displaying CSS after deployment to Heroku

I'm currently using a combination of Spring 3.1.3, Prime Faces 3.4.2, Hibernate 3.6.8, and Pretty Faces 2.0.4. When I deploy my application on a local server (Apache Tomcat 7), the CSS displays properly. However, when I deploy the same project to Her ...

Tips for employing e.preventDefault within the onChange event handler for a select element

Is there a way to prevent the select tag value from changing using preventDefault? I have successfully prevented input from changing the value with event.preventDefault, but have not been able to do the same for the select tag. Here is an example code sni ...

A PHP drop-down menu maintains a fixed position

I have set up a database called pogoda with a city table containing columns for city and cityid. Using PHP, I am creating a dynamic list and submitting the chosen value into subsequent code. To ensure that the drop-down list has a default starting positio ...

Spinning an object using JQuery

I am currently working on a project to test my skills. I have set up a menu and now I want to customize it by rotating the icon consisting of three vertical lines by 90 degrees every time a user clicks on it. This icon is only visible on smartphones when t ...

Transforming into a serialized division

I am working on creating a custom WISYWIG editor that generates a div with specific inner elements. The goal is to design the div (along with its inner structure), serialize it, store it in a database as a string or JSON format, and later insert it into th ...

What is the method for absolutely positioning an element with separate targets for the `left` and `right` properties?

Seeking a complex CSS result that may seem impossible. Consider the following scenario: <div class="div1"> <div class="div2"> <div class="div3"> <div class="div4"></div> ...