CSS 每行一个字符

CSS one char per line

我需要在 i a div 中编写的所有文本都是垂直的,例如每行一个字符并居中。

我实现的唯一方法是给 div 固定宽度 width: 7%;word-break: break-all;

有什么方法可以使用 CSS 来实现没有固定宽度的效果吗?

.flex-container {
    display: flex;
    border: 1px solid #000000;
}

.flex-container > div {
    width: 400px;
    height: 300px;
    text-align: center;
    line-height: 75px;
    font-size: 30px;
}

.flex-container > div:nth-child(odd) {
    background-color : #62d962;
}

.flex-container > div:last-child {
    display: flex;
    justify-content: flex-end;
}

.flex-container > div:last-child div {
    width: 7%;
}

.flex-container div:last-child div:nth-child(odd){
    background-color: #65e9e3;
    word-break: break-all;
    line-height: 1em;
    padding: 1em 0;
}
<div class="flex-container">
  <div></div>
  <div></div>
  <div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
  </div>
</div>

css

#onechar {
    width: min-content;
    word-break: break-all;
}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div id="onechar">box</div>
</body>
</html>

希望对您有所帮助:)

可以使用writing-mode and text-orientationCSS属性,实现 所需的输出。

.flex-container {
    display: flex;
    border: 1px solid #000000;
}

.flex-container > div {
    width: 400px;
    height: 300px;
    text-align: center;
    line-height: 75px;
    font-size: 30px;
}

.flex-container > div:nth-child(odd) {
    background-color : #62d962;
}

.flex-container > div:last-child {
    display: flex;
    justify-content: flex-end;
}

.flex-container > div:last-child div {
    writing-mode: vertical-rl;
    text-orientation: upright;
}

.flex-container div:last-child div:nth-child(odd){
    background-color: #65e9e3;
    line-height: 1em;
    padding: 1em 0;
}
<div class="flex-container">
  <div></div>
  <div></div>
  <div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
  </div>
</div>