I need to be able to horizontally align the p elements left, center, or right. There will/could be multiple div elements each with one or more vertical text p elements.
Each time I try , each version has it’s own wrinkle:
-
If I use float then I can’t center them and if a non vertical p element gets added it ends up next to the vertical text (because it doesn’t have clear: both)
-
If I use margin and translateX it doesn’t work with multiple elements.
-
When I tried to get them to add CSS to the parent div (text-align: center) it also changes the alignment on the non vertical p elements.
-
I can do this using flexbox but that causes issues with the CSS that’s currently on the parent div.
How can this be done without flexbox and without setting CSS on the parent div?
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>
2
Answers
Is this the effect you are after?
Taking the idea in point 3, a couple of things can make it less problematic.
First, apply the
text-align: center
only if .vert-con has some vertical elements.Second, make
text-align
to left for the non-vertical elements and usefloat
for vertical elements that are to be left or right.Make every non-vertical element
clear: both
.Note: by being specific – changing things only when vert-con has some vertical elements – it may be that ‘they’ allowing the change to vert-con will not mess with other CSS where no vertical elements are present.