如何精确获取虚构文本内容宽度中的边距?

huangapple go评论55阅读模式
英文:

How can I get margin exactly in the width of imaginary text content?

问题

以下是代码部分的翻译:

这是我正在使用的列表:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

    * {
      margin: 0;
      padding: 0;
      font-family: Arial;
    }

    ol {
      counter-reset: item;
    }

    li {
      display: block;
    }

    li:before {
      content: counters(item, ".") " ";  /* 这里是CSS代码 */
      counter-increment: item;
    }

    ol li ol li {
      margin-left: 80px; /* 左边距应与一个数字和一个   的宽度完全相同 */
    }

<!-- language: lang-html -->

    <ol>
      <li>Coffee
        <ol>
          <li>One</li>
          <li>Two</li>
          <li>Three</li>
        </ol>
      </li>
      <li>Tea
        <ol>
          <li>One</li>
          <li>Two</li>
          <li>Three</li>
        </ol>
      </li>
      <li>Milk</li>
      <li>Cool</li>
    </ol>

<!-- end snippet -->

希望这有所帮助。

英文:

This is a list I am working with:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  font-family: Arial;
}

ol {
  counter-reset: item;
}

li {
  display: block;
}

li:before {
  content: counters(item, &quot;.&quot;) &quot; &quot;;
  counter-increment: item;
}

ol li ol li {
  margin-left: 80px; /* The left margin should have exactly the width of one number and a &amp;emsp; */
}

<!-- language: lang-html -->

&lt;ol&gt;
  &lt;li&gt;Coffee
    &lt;ol&gt;
      &lt;li&gt;One&lt;/li&gt;
      &lt;li&gt;Two&lt;/li&gt;
      &lt;li&gt;Three&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Tea
    &lt;ol&gt;
      &lt;li&gt;One&lt;/li&gt;
      &lt;li&gt;Two&lt;/li&gt;
      &lt;li&gt;Three&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Milk&lt;/li&gt;
  &lt;li&gt;Cool&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

The ol li ol li currently has margin-left: 80px;. Now it would be possible to manually adjust the px value in a way that for example 1.1 is perfectly left aligned with Coffee. But to make sure it will be always perfectly aligned, also with other fonts, I would like to insert an invisible space of one number and a &amp;emsp;. With that, it should be perfectly aligned on one line.

Here is an image to show how it should look like:
如何精确获取虚构文本内容宽度中的边距?

How can this be done? I would be very thankful for help.

答案1

得分: 2

Float可以做到这一点。您可以使用一个带有微小底边距的浮动元素来使第一个数字浮动,并且它将通过其宽度将一切推到右边。

英文:

Float can do it. You make the first number float with a tiny bottom margin and it will push everything to the right by its width.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  font-family: Arial;
}

ol {
  counter-reset: item;
}

li {
  display: block;
}

li:before {
  content: counters(item, &quot;.&quot;) &quot; &quot;;
  counter-increment: item;
}

/* select li that has &quot;ol&quot; */
li:has(ol):before {
  float: left;
  margin-bottom: 5px; /* small value */
}

ol ol {
  overflow: hidden; /* create a formatting context for the float */
}

<!-- language: lang-html -->

&lt;ol&gt;
  &lt;li&gt;Coffee
    &lt;ol&gt;
      &lt;li&gt;One&lt;/li&gt;
      &lt;li&gt;Two&lt;/li&gt;
      &lt;li&gt;Three&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Tea
    &lt;ol&gt;
      &lt;li&gt;One&lt;/li&gt;
      &lt;li&gt;Two&lt;/li&gt;
      &lt;li&gt;Three&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Milk&lt;/li&gt;
  &lt;li&gt;Cool&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 19:54:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定