Tips for automatically adjusting the width of dt and dd elements using CSS

My approach to creating a paper form involves using dt and dd elements.

The width of dt should align with the word, while dl should occupy the remaining space.

Currently, I manually adjust each line, but I am looking for a way to automate this using CSS.

div {
  width: 200px;
}


/* Styling for dl, dt, and dd on the same line */

dl {
  width: 100%;
  overflow: hidden;
  padding: 0;
  margin: 0
}

dt {
  float: left;
  /* Adjust the width; ensure the total of both is 100% */
  padding: 0;
}

dd {
  float: left;
  /* Adjust the width; ensure the total of both is 100% */
  margin: 0;
  box-sizing: border-box;
  border-bottom: 1px solid black;
}


/* Setting specific widths for dt and dd */

dl dt:nth-of-type(1) {
  width: 36%;
}

dl dd:nth-of-type(1) {
  width: 64%;
}

dl dt:nth-of-type(2) {
  width: 26%;
}

dl dd:nth-of-type(2) {
  width: 74%;
}

dl dt:nth-of-type(3) {
  width: 14%;
}

dl dd:nth-of-type(3) {
  width: 86%;
}
<div>
  <dl>
    <dt>aaabbbccc:</dt>
    <dd>&nbsp;</dd>
    <dt>aaabbb:</dt>
    <dd>&nbsp;</dd>
    <dt>aaa:</dt>
    <dd>&nbsp;</dd>
  </dl>
</div>

Answer №1

From a semantic standpoint, using a ul element would be more appropriate for the content you are presenting. In this scenario, the dd is primarily being used for layout purposes and does not contain any relevant definition content. By applying a flex display to the li, we can effectively target an empty italics element to create the necessary bottom border underline, adjusting its length dynamically as needed.

ul, li {
  margin: 0;
  padding: 0;
  list-style: none;
}

div {
   width: 200px; 
}

div li {
  display: flex;
}

div li i {
  border-bottom: 1px solid;
  flex-grow: 1;
}
<div>
  <ul>
    <li><span>aaabbbccc:</span><i></i></li>
    <li><span>aaabbb:</span><i></i></li>
    <li><span>aaa:</span><i></i></li>
  </ul>
</div>

Link to jsFiddle

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

JavaScript code to modify a table row

I have a dynamic table that increases with Firebase data, and I am trying to incorporate a delete and edit button on each row. While the remove button is working fine, I am facing issues with implementing the edit functionality. Although I came across some ...

Parsing Images Using JSON and Ajax

I am facing an issue with extracting an image from a JSON file located at this link using jQuery/ajax and inserting it into a div. Despite my efforts, I am struggling to achieve this task due to being new to this field. Your assistance is greatly apprecia ...

Building a chat console resembling IRC using JavaScript

I am a novice in HTML and JavaScript. I am currently working on creating a simple program that takes input from the user via the command line and displays it in a large console window. However, when I enter a simple text, nothing is displayed in the box. D ...

I am working on an HTML form that is designed vertically, but I am unsure of how to arrange two text fields side by side on the same line

I'm struggling with formatting my HTML form to have two text fields on the same line instead of stacked vertically. In the example below, I want the Size, Width, and Height fields to be aligned horizontally rather than one below the other. <form c ...

What is the best way to eliminate base tags from jQuery mobile scripts?

In jQuery Mobile, how can I remove the base href tags from the page and disable base href? Code Snippet: // Function to test for dynamic-updating base tag support function baseTagTest() { var fauxBase = location.protocol + "//" + location.host + l ...

Tips for making an interactive slider using JQuery

I need to create dynamic sliders within tabs that can change content dynamically. My development platform is ASP.NET MVC 5 and I'm using JSON data format. What's the best way to implement this dynamic slider feature for each tab? <div class ...

Optimize your website by caching static pages and content using Node.js

When it comes to caching static content using nodejs, there seem to be two main methods that can be utilized: The first method involves using nodejs and utilizing the following code: app.use(express.static(path.join(__dirname, 'public'), { max ...

Manipulating a cloned object using jQuery

var clonedForm = $('form').clone(); //clonedForm.find('input[type="hidden"]').remove(); clonedForm.find('select').each(function(){ $($(this).val()).insertAfter($(this)); $(this).remove(); }); Atte ...

InvalidSelectorError: The specified selector is not valid //button[contains(., 'buttonText')] while running JavaScript code

Here's an example of HTML code that I am working with: <button id="toolbar-item-17-update-id" type="submit" name="action" value="update" class="button-action-update btn btn-secondary"> Ed ...

Practical applications of flexible layouts in everyday life

I'm currently on the hunt for real-world examples of elastic layouts, specifically those based on using em's for widths. Surprisingly, I haven't been able to find much out there. If anyone knows of any examples, I would greatly appreciate it ...

Share an image using only the publish_actions authorization

There are some apps I've come across that only request access to users_photos and publish_actions (not publish_stream or photo_upload). Surprisingly, they are able to upload photos without being approved for User Generated Photos (e.g. Instagram). Wh ...

Is there a way for me to utilize VB to remotely click a button on a webpage

I've been searching for a solution to my issue but none of the suggestions from other posts seem to work for me. I am attempting to write VB code that will modify the year dropdown, populate the address field, and then initiate a search when a button ...

What is the best way to adjust the height of my website to properly display on all screen sizes

Is there a way to make my web page fit perfectly on the screen in terms of both width and height? I'm familiar with using Media Queries for adjusting widths, but how can I also control the height? Any suggestions on setting the height? I would great ...

Unable to remove the vertical dropdown menu padding issue in Firefox

issue: https://i.stack.imgur.com/qisVn.jpg My website appears correctly on IE, Opera, and Chrome, but in Firefox, there is a spacing problem between each image in the dropdown menu. I suspect the issue lies within the CSS code. The same spacing issue occ ...

Using regular expressions in JavaScript, how can one extract the content along with the HTML tags from within the tags?

Below is the text to be analyzed: how much production in batu The text is displayed with HTML tags where each word is wrapped in a span with a specific style or class. For example: '<span style="">how &nbsp;</span><span style ...

Assigning a download attribute to a file that originates from a different source

Is there a workaround for using the "download" attribute to give an image a specific name, such as in the example below? The issue I am encountering may be related to the explanation provided in this discussion. The image will always be hosted on a differ ...

Tips for aligning all elements in-line using Bootstrap CSS

I am trying to make all elements display in a line in my HTML tags. How can I achieve this using Bootstrap? Currently, the page size is displaying on different lines. Thank you, Austin TX <div class="col-xs-12 col-md-8 form-inline"> <span c ...

Does Google Chrome have a strange way of interpreting CSS?

Almost done with my website and it looks great on Firefox and IE 8, but Chrome is causing issues. Here's how it appears on FF: And here's Chrome: Notice how Chrome resizes the first image even though the code is the same as the one below? Che ...

What is the contrast between specifying a MIME type and omitting it?

If I were to write a stylesheet in an HTML document, it's worth noting that since the introduction of HTML5, the type attribute is no longer necessary as text/css has become the default and only possible value: <style type='text/css'> ...

My goal is to adjust the height of every text box on the page

Seeking guidance here. Within the webpage I'm developing, there are multiple 'Divs' containing 'text boxes' and 'background images' within them. I aim to adjust the height of each text box on the page. The height of each ...