Tips for centering text within a description list using CSS

I need to figure out how to align the spans of the dt and dd elements horizontally in an HTML description list that includes an icon inside the dt element.

<dl>
        <div>
            <dt><span class="ic"></span><span>Dt</span></dt>
            <dd><span>Desc</span></dd>
        </div>
    </dl>

Because the icon has a fixed width, I am facing alignment issues between the span elements. Is there a way to achieve horizontal alignment in this scenario? For reference, please see this HTML Output

I have researched solutions using CSS flexbox or grid layouts but have not found a suitable answer yet.

Answer №1

*In response to the previous question, I believe it is still centered around: How can spans from different parent elements be horizontally aligned in HTML/CSS

It is possible to align the "term" and "desc" spans horizontally without the use of JavaScript by utilizing display: table, table-row, and table-cell CSS properties.

Here's a suggestion:

.list-item dt {
  display: table-cell;
  vertical-align: middle;
}

.list-item dd {
  display: table-cell;
  margin-left: 10px; /* Add spacing between term and desc */
  vertical-align: middle;
}

.list-item .term {
  display: inline-block;
  font-weight: bold;
}

.list-item .icon {
  display: inline-block;
  width: 20px; /* Adjust size as necessary */
  height: 20px; /* Adjust size as necessary */
  background-color: red; /* For illustration purposes */
  margin-right: 5px; /* Add space between icon and term */
}

.list-item div {
  display: table-row;
}

We have set the display property of dt and dd elements to table-cell, making them act like cells in a table. Additionally, the display property of the parent div is set to table-row to behave like a row in a table.

By default, table cells vertically align their contents to the middle. The vertical-align property helps align the content of "term" and "desc" spans vertically to the middle of their cells.

Furthermore, the display property of "term" span is changed to inline-block to apply the font-weight property. Similarly, the display property of "icon" span is set to inline-block with specified width and height for spacing. Margin-right property adds some space between "icon" and "term" spans.

You may adjust the sizes of "icon" and other elements according to your design requirements.

I hope this explanation proves helpful!

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

transforming unicode into regular characters prior to being presented on a webpage

I am currently utilizing the openinviter class to import contacts from emails. Sadly, it is showing Unicodes of non-English characters, like Polish, such as u0117 (and similar code types), instead of regular characters. Is there a way to convert these Unic ...

What is the best way to alter the font size in an SVG?

I have incorporated an SVG icon into my website and obtained the following code from Adobe Illustrator: <svg id="Livello_1" data-name="Livello 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448.05 436.7"><path d="M190.5,66.9l22.2-22.2a23.9, ...

Effective ways to transmit a variable array from an HTML document to a PHP server

Is there a better method for transferring a variable array from HTML to PHP? I attempted to use the serialize function, but it doesn't seem to be functioning correctly. Any suggestions would be greatly appreciated. //HTML var arrayTextAreasNames = [ ...

Managing traffic in Google Kubernetes Engine (GKE)

I am encountering an issue with our website deployment on GKE, which consists of 10 pods. When deploying a new version, we use MAXsurge=1 and MAXunavailable=0. Upon trying to access the website during a new deployment, I sometimes only see the header in t ...

Top-notch tools and guides for front-end web development

As I prepare to transition to a new role focused on front-end web development, specifically CSS and jQuery, I am seeking recommendations for valuable resources to enhance my skills in these areas. Whether it be books, websites, blogs, or any other medium, ...

Utilize CSS animation classes on multiple elements throughout a single webpage

Trying to create a share button for social media with an animated button using CSS. However, encountering issues when trying to display multiple buttons on the same page for different content. The problem arises when clicking the second button, as it trigg ...

Creating an interactive date selection feature with a calendar icon in MVC 5

I currently have a textbox that displays the datepicker when clicked. However, there is now a requirement to add a calendar icon inside the textbox. The datepicker should be displayed when either the calendar icon or the textbox is clicked. Below is the co ...

When TTF fonts are installed on the device, they take precedence over Google fonts

I have successfully implemented the Ubuntu font from Google Fonts on my website: <link href='http://fonts.googleapis.com/css?family=Ubuntu:300,400,300italic,400italic,500,500italic,700,700italic' rel='stylesheet' type='text/css ...

Looking for some guidance with Bootstrap column layouts

I'm in the process of creating an address field layout. My goal is to have the State and Country sections positioned on the right side of the city and postal code fields. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3 ...

Element with negative z-index overlapping background of parent element

I am currently working on creating a CSS3 animated menu, but I am facing an issue where elements with low z-index values are displaying on top of their containing elements. This is causing the "sliding out" effect to be ruined. Here is the HTML: <html ...

Fuzzy text in drop-down box on Chrome, clear on Firefox

I've encountered an issue with a dropdown menu in Google Chrome where the content appears blurry, while it displays correctly in Firefox. The problem arises when the dropdown exceeds a certain height, and I've tried setting a max-height with over ...

Switch between using the useState hook by toggling it with the MUI Switch

Looking to implement a custom Dark Mode feature for a specific element on my create-react-app website using MUI. I have successfully implemented the Switch to change the state on toggle, but I am having trouble figuring out how to toggle it back and fort ...

Adjust the size of the Div and its content to fit the dimensions of

Currently, I have a div containing elements that are aligned perfectly. However, I need to scale this div to fit the viewport size. Using CSS scale is not an option as it does not support pixel values. https://i.stack.imgur.com/uThqx.png I am looking to ...

Div behaving as a radio input

I have customized radio buttons to look like buttons using JavaScript. However, on page load, both buttons automatically receive the 'active' class instead of only when they are selected. Additionally, the fadeToggle function in the if-statement ...

The duplication and multiplication of margin-left is occurring

I am baffled by the fact that margin-left is only affecting certain elements and causing frustration. .setting-container { position: relative; top: 60px; left: 0px; width: 100%; height: calc(100% - 60px); } .setting-topnav { width: 100%; h ...

Utilizing HTML5/JavaScript to send a text message from a mobile device

I am developing a mobile web application to be downloaded from various markets with a mini web server, capable of running on any operating system such as iOS, Android, Windows8, and more. My goal is to make the application as OS-independent as possible by ...

Best practices for displaying output in Python Flask after submitting an HTML form

Embarking on my first web development project, I delved into Flask. Recently, I created a basic temperature converter, running it on my local host. The webpage featured a form input for entering a value, two radio buttons for selecting Fahrenheit or Celsiu ...

Tips for addressing color contrast accessibility problems with inactive buttons

An issue has arisen with a button that is conditionally disabled and appears greyed out. The background color is #e0e0e0 and the text color is #a6a6a6, resulting in a contrast ratio of 1.84, which falls short of the required 4.5 minimum. Is there a way to ...

AngularJS bracket-enhanced template

Why is AngularJS giving an error when brackets are used inside ng-template content? I am trying to create an input field that should accept an array, but I keep getting this error message: "Error: Syntax Error: Token ']' not a primary expression ...

Elements overlapped with varying opacities and responsive to mouse hovering

In this Q/A session, we will explore a JS solution for managing the opacity of overlapping elements consistently during hover. Objective Our goal is to create two transparent and overlapping elements, similar to the red boxes showcased below. These eleme ...