I'm styling a web page for displaying articles. The main content is the article body, but there's also a list of links to related articles. I want the list normally to be displayed to one side of the article, but to be displayed underneath the article in either of the following situations:
- The viewport is < 992px wide
- There is wide content in the article, e.g. a large table
I've achieved this with the following CSS...
.article-sidebar {
text-align: left;
}
@media (min-width: 992px) {
.article-sidebar {
float: right;
max-width: 28%;
}
}
@media (max-width: 991px) {
.article-sidebar {
padding-top: 46px;
border-top: 1px solid #eeeeee;
padding-left: 5%;
padding-right: 5%;
}
}
.article-sidebar-bottom {
text-align: left;
padding-top: 46px;
border-top: 1px solid #eeeeee;
padding-left: 5%;
padding-right: 5%;
}
...plus some JavaScript that works out if the article has any "wide children" and removes the 'article-sidebar' class and adds the 'article-sidebar-bottom' class if so.
However, I don't like all the duplication of the styling between >= 991px .article-sidebar
and .article-sidebar-bottom
. I understand that if there was duplication between different media queries for the same class I could combine them as explained here, and if media queries weren't involved I could just comma separate the CSS selectors, but is there any way to avoid repeating myself in this case?