CSS 每行一个字符

Ser*_*aro 0 html css flexbox

我需要 ia 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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div></div>
  <div></div>
  <div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Tec*_*nav 6

您可以使用writing-modetext-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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div></div>
  <div></div>
  <div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)