Before reading my proposed solution, please note that while you specificed:
"This document is generated by a rental software program. The program does not allow us to customize the HTML code."
My solution will only work if you can somehow modify the program to always render the company name field, even if it's empty. I am not sure if that counts as "customizing the HTML code".
Also, the HTML template must always generate the same layout with the exact same order of the fields - but I'm assuming it already does that?
The reasoning behind this is if the company field is also generated, even if it's empty, you can control your CSS selectors 100% accurately.
Looking at the HTML template, it seems like the company field is the third <div>
of the .additional-information
-element.
Knowing this, you can use nth-of-type(3)
to select that field:
.additional-information > div:nth-of-type(3) { }
Furthermore, we can use a combination of :has()
and :empty
(or :blank
) to select the company field if its empty. If its empty, give it display: none
:
.additional-information > div:nth-of-type(3):has(span:empty) {
display: none;
}
If its not empty, apply your desired positioning values:
.additional-information > div:nth-of-type(3) {
top: 10px;
position: absolute;
}
The problem is now that the <div>
s are going to overlap each other:
.additional-information > div > span {
display: block;
}
.additional-information > div:nth-of-type(1) {
position: relative;
left: 0px;
top: -15px;
}
.additional-information > div:nth-of-type(2) {
position: relative;
left: 0px;
top: -15px;
}
.additional-information > div:nth-of-type(3):has(span:empty) {
display: none;
}
.additional-information > div:nth-of-type(3) {
top: 10px;
position: absolute;
}
<div class="additional-information">
<div>
<strong>Address</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 1</span>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 2</span>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 3</span>
</div>
<div>
<strong>Phone number</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">12345678</span>
</div>
<div>
<strong>Company name</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Name of company</span>
</div>
</div>
To prevent this, apply a margin-top
value to the first <span>
inside the address field, equivalent to the height of 2 lines of text ("Company name" and the company name itself):
.additional-information > div:nth-of-type(1) span:first-of-type {
margin-top: 35px;
}
And to prevent a margin-top
value if the <span>
is empty, use a nasty combination of :has()
and :empty
again:
.additional-information:has(div:nth-of-type(3) span:empty) > div:nth-of-type(1) span:first-of-type {
margin-top: 0;
}
The code in action
body {
display: flex;
gap: 100px;
}
.additional-information > div > span {
display: block;
}
.additional-information > div:nth-of-type(1) {
position: relative;
left: 0px;
top: -15px;
}
.additional-information > div:nth-of-type(2) {
position: relative;
left: 0px;
top: -15px;
}
.additional-information > div:nth-of-type(3):has(span:empty) {
display: none;
}
.additional-information > div:nth-of-type(3) {
top: 10px;
position: absolute;
}
.additional-information > div:nth-of-type(1) span:first-of-type {
margin-top: 35px;
}
.additional-information:has(div:nth-of-type(3) span:empty) > div:nth-of-type(1) span:first-of-type {
margin-top: 0;
}
<div class="additional-information">
<div>
<strong>Address</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 1</span>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 2</span>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 3</span>
</div>
<div>
<strong>Phone number</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">12345678</span>
</div>
<div>
<strong>Company name</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Company name</span>
</div>
</div>
<div class="additional-information">
<div>
<strong>Address</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 1</span>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 2</span>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">Address line 3</span>
</div>
<div>
<strong>Phone number</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true">12345678</span>
</div>
<div>
<strong>Company name</strong>
<span data-private="true" data-openreplay-masked="true" data-openreplay-obscured="true"></span>
</div>
</div>
Additional info: