精确格式化狭窄浏览器窗口的演员表
我需要在网页上格式化一个“铸件列表”(一系列人/角色对),以近似于专业的结果,当行的文本比浏览器窗口中的可用空间宽时。但是我不确定我是否应该查看CSS Flexbox,网格,JavaScript或其他内容,或者是否有能力生成与我想要的任何东西的生成。
以下是同一演员表列表的一些示例,该示例以不同的宽度为手动格式。 (我在此处使用固定宽度字体,以使显示对齐方式更易于显示,但希望一种可与可变宽度字体一起使用的方法。)
Wolfgang Amadeus Mozart ................. Court Composer
Antonio Salieri ........ Assistant to the Court Composer
Tim ... Assistant to the Assistant to the Court Composer
John Jacob Jingleheimer Schmidt ................. Intern
John Jacob Jingleheimer Schmidt Jr. ....... Secretary to
the Assistant to the Court Composer
Wolfgang Amadeus Mozart ..... Court Composer
Antonio Salieri ..... Assistant to the Court
Composer
Tim ...... Assistant to the Assistant to the
Court Composer
John Jacob Jingleheimer Schmidt ..... Intern
John Jacob Jingleheimer
Schmidt Jr. ............ Secretary to the
Assistant to the Court Composer
Wolfgang Amadeus Mozart ... Court ; not ideal, better to break the longer name
Composer
Antonio Salieri ....... Assistant
to the Court Composer
Tim ............ Assistant to the
Assistant to the Court Composer
John Jacob Jingleheimer
Schmidt ............... Intern
John Jacob Jingleheimer
Schmidt Jr. ..... Secretary to
the Assistant to the
Court Composer
Wolfgang Amadeus
Mozart .... Court Composer
Antonio Salieri ... Assistant
to the Court Composer
Tim ........ Assistant to the
Assistant to the Court
Composer
John Jacob Jingleheimer
Schmidt ........... Intern
John Jacob Jingleheimer
Schmidt Jr. .... Secretary
to the Assistant to the
Court Composer
表达此布局方案背后逻辑的一种方法可能是:
- 对于每个人/角色对,尝试将文本设置为一行。如果它太宽了:
- 计算“人”部分中的最后一个单词的宽度,以及“角色”部分中的第一个单词。
- 将“人”部分分解为多行,因此每行不超过可用宽度的70%,最后一行不比“角色”部分中的第一个单词的宽度宽(加上一个常数缩进和点)。
- 将“角色”部分分为多行,因此每行不超过可用宽度的70%,而第一行不比“人”部分中的最后一个单词的宽度宽(加上常数) 。
- 设置“人”部分,随后的线缩进。
- 在“人”部分的最后一行之后,将“角色”部分的第一行设置为右侧,并用点填充额外的空间。
- 设置“角色”部分的其余线,正确。
仅使用CSS,是否可以像上面的结果一样获得结果?还是可以在JavaScript中执行此类操作,测量单词并手动定位线路断裂?有人做了类似的事情吗?任何指针都赞赏。
I need to format a "cast list" (a series of person/role pairs) on a web page in a way that approximates professional-looking results when a line's text is wider than the available space in the browser window. But I'm not sure if I should be looking at CSS flexbox, grid, JavaScript, or something else, or if any of these are even capable of generating anything close to what I want.
Here are some examples of the same cast list, manually formatted for different widths. (I'm using a fixed-width font here to make it easier to show alignment, but would prefer a method that works with variable-width fonts.)
Wolfgang Amadeus Mozart ................. Court Composer
Antonio Salieri ........ Assistant to the Court Composer
Tim ... Assistant to the Assistant to the Court Composer
John Jacob Jingleheimer Schmidt ................. Intern
John Jacob Jingleheimer Schmidt Jr. ....... Secretary to
the Assistant to the Court Composer
Wolfgang Amadeus Mozart ..... Court Composer
Antonio Salieri ..... Assistant to the Court
Composer
Tim ...... Assistant to the Assistant to the
Court Composer
John Jacob Jingleheimer Schmidt ..... Intern
John Jacob Jingleheimer
Schmidt Jr. ............ Secretary to the
Assistant to the Court Composer
Wolfgang Amadeus Mozart ... Court ; not ideal, better to break the longer name
Composer
Antonio Salieri ....... Assistant
to the Court Composer
Tim ............ Assistant to the
Assistant to the Court Composer
John Jacob Jingleheimer
Schmidt ............... Intern
John Jacob Jingleheimer
Schmidt Jr. ..... Secretary to
the Assistant to the
Court Composer
Wolfgang Amadeus
Mozart .... Court Composer
Antonio Salieri ... Assistant
to the Court Composer
Tim ........ Assistant to the
Assistant to the Court
Composer
John Jacob Jingleheimer
Schmidt ........... Intern
John Jacob Jingleheimer
Schmidt Jr. .... Secretary
to the Assistant to the
Court Composer
One way to express the logic behind this layout scheme might be:
- For each person/role pair, try to set the text in a single line. If it's too wide for that:
- Compute the width of the last word in the "person" part, and the first word in the "role" part.
- Break the "person" part into multiple lines, so that each line is no wider than 70% of the available width, and the last line is no wider than the width of the first word in the "role" part (plus a constant to account for indenting and dots).
- Break the "role" part into multiple lines, so that each line is no wider than 70% of the available width, and the first line is no wider than the width of the last word in the "person" part (plus a constant).
- Set the "person" part, with subsequent lines indented.
- After the final line of the "person" part, set the first line of the "role" part, flush right, filling in the extra space with dots.
- Set the remaining lines of the "role" part, flush right.
Is it possible to get results like the above, using just CSS? Or is it possible to do this kind of thing in JavaScript, measuring words and manually positioning line breaks? Has anyone done anything similar? Any pointers appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然您几乎只能使用CSS到达那里,但是需要计算第二列第一行所需的凹痕,而javaScript必须完成。
该片段在包含文本的第一列Div中的每个列中都有两个跨度。第一个跨度允许白色背景仅用于文本,第二个跨度允许计算实际文本的末尾。从那里可以找到第二列的第一行所需的凹痕。
由于点只是一个视觉线索,它们可以通过伪元素包含,重复径向梯度制作点。
While you can get almost there with just CSS, there is a need to calculate the indent needed on the second column first line which has to be done by JavaScript.
This snippet has two spans within each of the first column divs containing the text. The first span allows a white background to just the text and the second allows calculation of the end of the actual text. From there the indent required on the first line of the second column can be found.
As the dots are just a visual clue they can be included through a pseudo element, repeated radial gradients making the dots.