我需要能够水平对齐的p元素左,中心,或右.将/可能有多个div元素,每个元素都有一个或多个垂直文本p元素.

每次我try ,每个版本都有自己的皱纹:

  1. 如果我使用float,那么我就不能将它们居中,如果添加了一个非垂直的p元素,它会在垂直文本旁边结束(因为它没有clear:both)

  2. 如果我使用margin和margin,它不适用于多个元素.

  3. 当我试图让他们添加CSS到父div(text—loc:center)时,它也改变了非垂直p元素的对齐方式.

  4. 我可以使用flexbox来实现这一点,但这会导致当前父div上的CSS出现问题.

如果没有flexbox,也没有在父div上设置CSS,该如何做到这一点?

p {
  font-family: Georgia, serif;
  font-size: 18px;
}

.vert-con {
  /* This is sample CSS I can't actually change the CSS for this */
  position: absolute;
  left: 10px;
  top: 10px;
  width: 360px;
  height: 300px;
  overflow: auto;
  background-color: beige;
}

.vert-p {
  writing-mode: vertical-rl;
  display: inline-block;
  vertical-align: top;
  white-space: nowrap;
  margin-inline: 0 18px;
}
<div class="vert-con">
  <p>Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p" style="color: red;">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p" style="color: blue;">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p" style="color: green;">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p>Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
</div>

推荐答案

从第3点的观点来看,有几件事可以让问题少一些.

首先,只有当. vert—con有一些垂直元素时才应用text-align: center.

第二,对于非垂直元素,使用text-align到左,对于要向左或向右的垂直元素,使用float.

使每个非垂直元素clear: both.

p {
  font-family: Georgia, serif;
  font-size: 18px;
}

.vert-con {
  /* This is sample CSS I can't actually change the CSS for this */
  position: absolute;
  left: 10px;
  top: 10px;
  width: 360px;
  height: 300px;
  overflow: auto;
  background-color: beige;
}

.vert-p {
  writing-mode: vertical-rl;
  display: inline-block;
  vertical-align: top;
  white-space: nowrap;
  margin-inline: 0 18px;
}


/* you'll have to change something for vert-con, make sure it changes only if it has vertical elements*/

.vert-con:has(.vert-p) {
  text-align: center;
}

.vert-con:has(.vert-p) p:not(.vert-p) {
  text-align: left;
  clear: both;
}

p.vert-p.left {
  float: left;
}

p.vert-p.right {
  float: right;
}
<div class="vert-con">
  <p>Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p left">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p center" style="color: red;">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p center" style="color: blue;">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p class="vert-p center right" style="color: green;">Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
  <p>Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12 Text 12</p>
</div>

注:通过具体的—只有当vert—con有一些垂直元素时才改变东西—这可能是'他们'允许改变到vert—con不会扰乱其他没有垂直元素的CSS.

Html相关问答推荐

卸载伪元素 destruct 了CSS

打印pdf时100 vh迪夫有半页高

删除第一个列表项上的左边框

在浮动元素旁边垂直居中

无法从路径参数获取ID

风格规则应该适用于响应图像的哪些方面?哪种规则适用于<;图片和gt;,哪种规则适用于<;img>;?

如何保持嵌套进度条的背景色?

如何在网格中拥有离散的行

如何使不断增长的div不溢出其包含固定div的父级

我的Django应用程序中出现模板语法错误

SVG';COLOR&39;属性不优先于通用css';COLOR&39;属性

如何制作带有粘性头和脚的可滚动车身

如何使背景图像在面包屑中相互重叠

HTML 重定向到当前服务器

如何将背景与之前的内容正确对齐

如何使用 CSS 和 HTML 将文本放入图像中?

使用 TailwindCSS 将徽标放在左上角,将菜单放在右上角

使用 Selenium for Python 在网站上查找类名称中包含换行符的元素

每次按下按钮时减小字体大小的 jquery 函数

为什么浏览器在 Select 一个包裹在 标签中的块级元素后包含相邻元素?