I am trying to select a previous sibling in CSS using the class name of its following sibling and I am struggling to figure it out. My objective is to remove the bottom-margin
from the title
element via the h1-no-margin
class
body,
html,
h1 {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 200px;
}
.container h1.title {
margin-bottom: 2.5rem;
}
<!-- I cannot edit the HTML for the container class -->
<div class="container">
<!-- I cannot edit the HTML for the title class-->
<h1 class="title">This is a title</h1>
<!-- I can edit the HTML for the below div, so I can add and remove classes on it -->
<div class="info h1-no-margin"></div>
</div>
.
Few things to note:
- I don't have access to any JavaScript
- I don't have access to certain parts of the HTML
- I haven't had any success on other solutions presented on SO
Here is the HTML:
<!-- I cannot edit the HTML for the container class -->
<div class="container">
<!-- I cannot edit the HTML for the title class-->
<h1 class="title">This is a title</h1>
<!-- I can edit the HTML for the below div, so I can add and remove classes on it -->
<div class="info h1-no-margin"></div>
</div>
Here is the CSS:
body, html, h1 {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 200px;
}
.container h1.title {
margin-bottom: 2.5rem;
}
The idea behind this is that I can use the h1-no-margin
class on the div with the info
class to add and remove the margin on different pages.
Any help would be appreciated, thanks.