Design a two-column structure with right-aligned labels and left-aligned values using CSS

Hello everyone,

I am seeking the most straightforward method to achieve a basic layout using HTML and CSS that resembles the image in this link: https://i.sstatic.net/e6kuB.png

To be more specific, I need a label aligned to the right on the left side, and a value aligned to the left on the right side. It is important not to set a fixed width for the left column as it should adjust based on the longest label.

The solution should maintain semantic structure, work well with screen readers (reading label followed by value), and avoid excessive use of extra <div> elements.

This type of layout is quite common, so there must be a very simple way to achieve it. However, I have yet to discover it myself.

While a <table> could handle this layout effectively, I understand the recommendation to avoid using tables for purely visual purposes when the data is not tabular.

Thank you in advance!

Answer №1

There are a few options available with minimal HTML, one utilizing CSS Grid and the other using CSS flex-box layout.

CSS Grid:

/* Simple reset to reset margin, padding,
   and box-sizing: */
*,
 ::before,
 ::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Setting the form element's layout to use
   grid layout: */
form {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5em 1em;
  width: 80vw;
  margin: 0 auto;
}

label {
  text-align: right;
}

label::after {
  content: ':'
}
<form>
  <label for="input1">label</label>
  <input type="text" id="input1" placeholder="input 1">
  <label for="input2">A longer label</label>
  <input type="text" id="input2" placeholder="input 2">
  <label for="input3">Another slightly longer label</label>
  <input type="text" id="input3" placeholder="input 3">
  <label for="input4">A label that goes on, frankly, for quite a bit further than might be common for the average &lt;label&gt; element</label>
  <input type="text" id="input4" placeholder="input 4">
</form>

JS Fiddle demo.

Flexbox:

/* Simple reset to ensure uniform styling: */
*,
 ::before,
 ::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Set form layout to flexbox: */
form {
  display: flex;
  flex-wrap: wrap;
  width: 80vw;
  margin: 0 auto;
}

label,
input {
  flex: 1 1 50%;
  margin: 0.5em 0;
}

label {
  text-align: right;
  margin-right: 1em;
}

label::after {
  content: ':'
}
<form>
  <label for="input1">label</label>
  <input type="text" id="input1" placeholder="input 1">
  <label for="input2">A longer label</label>
  <input type="text" id="input2" placeholder="input 2">
  <label for="input3">Another slightly longer label</label>
  <input type="text" id="input3" placeholder="input 3">
  <label for="input4">A label that goes on, frankly, for quite a bit further than might be common for the average &lt;label&gt; element</label>
  <input type="text" id="input4" placeholder="input 4">
</form>

JS Fiddle demo.

Answer №2

CSS can come to the rescue with tables: https://css-tricks.com/almanac/properties/d/display/

While these may not be traditional tables, they can still emulate table behavior.

.notATable {
  display:table;
  list-style:none;
  padding-left:0;
}

.notATable > li 
 {
  display:table-row;
 }
 
 .notATable > li > *
 {
  display:table-cell;
  padding:5px;
 }
 
 
 .notATable label {
  font-weight:bold;
  text-align:right;
 }
 
 .notATable label:after {
  content: ':';
 }
<ul class="notATable">
   <li><label>Label</label><div>Value</div></li>
   <li><label>Another Label</label><div>Some Value</div></li>
   <li><label>A longer Label</label><div>Another Value</div></li>
   <li><label>Short Label</label><div>A different Value</div></li>
   <li><label>Really, Really Long Label</label><div>Last Value</div></li>
</ul>

You now have the flexibility to use CSS to adjust the layout of your content, such as using media-queries to present labels and values differently on mobile devices.

Answer №3

Is this straightforward?

*{
  padding: 5px;
  margin: 0;
  font-family: verdana;
}

.d-table {
  display:table;
}

.m-auto{
  margin:auto  
}

.d-flex{
    display:flex;
}    
.d-flex > div{
    height: auto;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
}

label{
  display:block;
  text-align:right;
  font-weight: bold;
  margin-right: 5px;
}
<div class="d-table m-auto">
  <div class="d-flex">
    <div>
      <label>Label:</label>
      <label>Another label:</label>
      <label>A long label:</label>
      <label>Short label:</label>
      <label>Really, really long label:</label>
    </div>
    <div>
      <p>Value</p>
      <p>Some value</p>
      <p>Another value</p>
      <p>A different value</p>
      <p>Last value</p>
    </div>
  </div>
</div>

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

Changing text content of an element with dynamic formatting

When a link is clicked on, I want to dynamically set the text. Here is the current code: <a href="www.somelinkhere.com" onclick="getElementById('setText').innerHTML='text to replace'" target="someIFrame" ...

Order of execution in Single Page Applications when using multiple files: understanding the Javascript async defer concept

I am currently working on enhancing the performance of my webpage, which is built using EmberJS. One strategy I'm considering is utilizing `asyc` and `defer` attributes for our Javascript files. I have already implemented other optimizations such as ...

Guide to incorporating JSON data into HTML through JavaScript

As I attempt to load data from a JSON file to .js and then to an HTML file, I am facing a challenge. While I can successfully load the JSON values into .js, I am unable to transfer the same values to HTML. Below is the code snippet - could anyone provide a ...

Creating fixed and scrollable columns using either Flexbox or Bootstrap

I'm currently facing an issue with creating a responsive webpage layout. My goal is to have a fixed left column that consists of an image taking up 66% of the page, with the remaining 33% dedicated to scrollable content on the right side. To achieve t ...

Retaining specific items in a collapsed Bootstrap Navbar

I am looking to keep certain items in my navigation bar visible, but also have a collapse toggle when the window becomes too small on the right side. Example: LOGO item item item Sign In Sign Up When collapsed: LOGO ...

Using table-responsive causes a modal to break inside because of overflow-scrolling touch in the CSS

When implementing a modal within a table that is inside a table-responsive container, the modal appears behind the table. To address this issue, I have tried using table-backdrop="false", but it is not an ideal solution as it restricts the full functional ...

Grab cell information from a dynamic table using Ruby and Selenium

I am attempting to retrieve a property of cells within a table. <table id="m-103" class="m-row" cellspacing="0"> <a name="2"></a> <table id="m-108" class="m-row " cellspacing="0"> <a name="3"></a> <table id="m-191" ...

The jquery and css3 slider smoothly slides in one direction, but struggles to move in the opposite

I have successfully implemented a feature in my code where an item slides to the left and gets deleted. However, I am facing an issue where after deleting the element, a new element is created on the right side of the screen but it appears in the center wi ...

Designing a space for textual content using HTML coding

Here is my plan: https://i.sstatic.net/dPGsG.png I'm looking for help on adding text boxes where I can type. Currently, I am using field set but having trouble positioning it. If anyone has any ideas on how to accomplish this, please share with me. ...

"Enhancing Your Web Pages: Generating and Inserting HTML Elements on the Fly Using

For my master's degree project, I am working on developing a shopping web app. The issue I am facing is that I need assistance with creating a div and adding it to the HTML file with an ID matching the category name from the .ts file when the addCate ...

What are the standard stacking order rules for elements in HTML?

Trying to understand the default HTML rules for element stacking order. Are there any restrictions regarding the order of parents and children or sibling elements in a single row? Or is it solely dependent on CSS? For clarification, consider this example ...

Customize MUI Tabs to resemble the design of Bootstrap Nav Tabs

Looking to customize Material UI Tabs to resemble Bootstrap Tabs. Made significant changes, but struggling with removing the border-bottom on the activeTab. In Bootstrap, they use marginBottom: -1px on the active tab, but it doesn't work for me. Any s ...

Designing a versatile DIV that sorts through components with the power of HTML, CSS, and JavaScript

I have been exploring a tutorial on W3 about filtering elements using JavaScript. Here is the link to the tutorial: W3 filter elements After following the tutorial, I noticed that the implementation leaves white space on the right side of the elements whe ...

Enhancing the security of login procedures

My current admin login code allows access to loginhome.php only if the correct username and password are entered. However, I have realized that this approach is not secure as anyone can directly visit mywebsite/loginhome.php without logging in or use the b ...

Switching the background image of a div by clicking on a different div

To start, you can locate all of my code right here. http://jsfiddle.net/yfukm8kh/1/ The issue I'm encountering pertains to the following section. var changePic = function (direction, id, array) { var intID = parseInt(id); var intDir = pars ...

Creating an eye-catching display with Bootstrap 4: Achieving a row of cards with varying heights

Is there a way to set up two cards next to each other in a row without one resizing to match the other? Whenever I resize the page, the cards no longer stay adjacent and collapse onto separate rows with different sizes. Manually setting the height works, b ...

Creating a Background Cutout Effect with CSS Using Elements instead of Images

I'm working on creating a unique visual effect that simulates a "window" where the div placed above all other elements acts like a window, allowing the background color to show through. Take a look at this example for reference. My goal is to make th ...

Angular 2's Component background color styling

Within my Home component, there is a carousel component. I want the background color of the home section to be Gray and the background color of the carousel to be blue. Unfortunately, I am having difficulty setting a background color for the body tag of t ...

Cover the whole page in darkness and emphasize the division element

I attempted to implement a solution I found on stackoverflow without success. The issue is that the blackout feature does not seem to activate - nothing happens. Within my JavaScript code (all enclosed in the $(document).ready(function () tag), it appears ...

What causes Safari to display a black border around resized videos?

I've come across a strange issue while using Safari. Whenever I try to play an MP4 video with dimensions of 1920 * 1080, and resize the Safari window to a width of 937px, something strange happens: https://i.stack.imgur.com/oI50i.png A black border ...