在不使用 HTML 表的情况下对齐 css 中的键冒号值数据?

发布于 2025-01-10 20:23:14 字数 308 浏览 0 评论 0原文

我有以下要显示的数据。 理想情况下,我希望键和值左对齐,中间用冒号分隔。

我希望结果如下所示:

key1  : value1
key2  : value2
keyAbc: Value Abc
key_N : value N

如下所示:

key1: value1
key2: value2
keyAbc: Value Abc
key_N: value N

如何在 CSS 或 SCSS 中执行此操作,而不使用 HTML 表?

I have the following data that I want to show.
Ideally I want the keys and values left aligned, separated with colons in the middle.

I want the result to be like the following:

key1  : value1
key2  : value2
keyAbc: Value Abc
key_N : value N

And not like the following:

key1: value1
key2: value2
keyAbc: Value Abc
key_N: value N

How to do this in CSS or SCSS, and not using HTML table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

血之狂魔 2025-01-17 20:23:14

您可以使用网格,使列占据内容的最大宽度。

此代码片段将冒号添加到伪元素中,因为它们似乎只是视觉线索而不是数据的一部分。

当然,您可能需要添加一些填充来满足您的特定要求。

.table {
  display: grid;
  grid-template-columns: max-content max-content;
}

 .table > *:nth-child(even)::before {
   content: ":";
}
    
<div class="table">
<div>key1</div>
<div>value1</div>
<div>key2222222</div>
<div>value2</div>
<div>key3</div>
<div>value3</div>
</div>

You can use grid, making the columns take on the max width of content.

This snippet adds the colons in a pseudo element as they seem to be just a visual clue rather than part of the data.

Of course you will want probably to add some padding to suit your particular requirements.

.table {
  display: grid;
  grid-template-columns: max-content max-content;
}

 .table > *:nth-child(even)::before {
   content: ":";
}
    
<div class="table">
<div>key1</div>
<div>value1</div>
<div>key2222222</div>
<div>value2</div>
<div>key3</div>
<div>value3</div>
</div>

夜未央樱花落 2025-01-17 20:23:14

如果您使用 div 结构,则可以使用 display: flex 并执行如下操作:

.css-table {
  display: flex;
}

.key {
  width: 10%;
}

.val::before {
  content: ': ';
}
<div class="css-table">
  <div class="key">key1</div>
  <div class="val">value1</div>
</div>
<div class="css-table">
  <div class="key">key2111</div>
  <div class="val">value2</div>
</div>
<div class="css-table">
  <div class="key">key3</div>
  <div class="val">value3</div>
</div>
<div class="css-table">
  <div class="key">key4</div>
  <div class="val">value4</div>
</div>

如果您不想指定宽度,则需要使用 JavaScript 来计算宽度并内联应用它。

let maxWidth = 0;
  
// Calculate maxWidth
document.querySelectorAll('.css-table').forEach(function(cssTableEl) {
  cssTableEl.querySelectorAll('.key').forEach(function(keyEl) {
    let currWidth = keyEl.clientWidth;

    if (currWidth > maxWidth) {
      maxWidth = currWidth;
    }
  });
});


// Apply maxWidth
document.querySelectorAll('.css-table').forEach(function(cssTableEl) {
  cssTableEl.querySelectorAll('.key').forEach(function(keyEl) {
    keyEl.style.width = maxWidth + 'px';
  });
});
.css-table {
  display: flex;
}

.val::before {
  content: ': ';
}
<div class="css-table">
  <div class="key">key1</div>
  <div class="val">value1</div>
</div>
<div class="css-table">
  <div class="key">key2111</div>
  <div class="val">value2</div>
</div>
<div class="css-table">
  <div class="key">key3</div>
  <div class="val">value3</div>
</div>
<div class="css-table">
  <div class="key">key4</div>
  <div class="val">value4</div>
</div>

If you're using div structure, you can use display: flex and could do something like this:

.css-table {
  display: flex;
}

.key {
  width: 10%;
}

.val::before {
  content: ': ';
}
<div class="css-table">
  <div class="key">key1</div>
  <div class="val">value1</div>
</div>
<div class="css-table">
  <div class="key">key2111</div>
  <div class="val">value2</div>
</div>
<div class="css-table">
  <div class="key">key3</div>
  <div class="val">value3</div>
</div>
<div class="css-table">
  <div class="key">key4</div>
  <div class="val">value4</div>
</div>

If you don't want to specify width, you'll need use JavaScript to calculate the width and apply it inline.

let maxWidth = 0;
  
// Calculate maxWidth
document.querySelectorAll('.css-table').forEach(function(cssTableEl) {
  cssTableEl.querySelectorAll('.key').forEach(function(keyEl) {
    let currWidth = keyEl.clientWidth;

    if (currWidth > maxWidth) {
      maxWidth = currWidth;
    }
  });
});


// Apply maxWidth
document.querySelectorAll('.css-table').forEach(function(cssTableEl) {
  cssTableEl.querySelectorAll('.key').forEach(function(keyEl) {
    keyEl.style.width = maxWidth + 'px';
  });
});
.css-table {
  display: flex;
}

.val::before {
  content: ': ';
}
<div class="css-table">
  <div class="key">key1</div>
  <div class="val">value1</div>
</div>
<div class="css-table">
  <div class="key">key2111</div>
  <div class="val">value2</div>
</div>
<div class="css-table">
  <div class="key">key3</div>
  <div class="val">value3</div>
</div>
<div class="css-table">
  <div class="key">key4</div>
  <div class="val">value4</div>
</div>

财迷小姐 2025-01-17 20:23:14

只需使用内联块并确保主 DIV 的子级也获得它,它就会根据最宽的宽度自动调整大小,就像表格一样。不需要弯曲。

 
.inline-block {
display: inline-block;
}

.inline-block > div {
margin: 2px;
padding: 1px; 
}


    
<div class="inline-block">
<div>key1</div>
<div>keykey2</div>
<div>key3</div>
</div>
    
<div class="inline-block">
<div>:</div>
<div>:</div>
<div>:</div>
</div>

<div class="inline-block">
<div>value1</div>
<div>value2</div>
<div>Value3</div>
</div>

Just use the inline-block and make sure the children of the main DIV gets that too and it will automatically resize based on widest width, much like table. No need for flex.

 
.inline-block {
display: inline-block;
}

.inline-block > div {
margin: 2px;
padding: 1px; 
}


    
<div class="inline-block">
<div>key1</div>
<div>keykey2</div>
<div>key3</div>
</div>
    
<div class="inline-block">
<div>:</div>
<div>:</div>
<div>:</div>
</div>

<div class="inline-block">
<div>value1</div>
<div>value2</div>
<div>Value3</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文