There are two types of merging to think about when dealing with CSS:
css_merge()
style_merge()
color: blue !important; font-size: 12px;
, and the inline style attribute might want to style as color: red;
.Essentially these are the same operation, with the css_merge()
being a nested version of style_merge()
.
Merge complete stylsheets with css_merge()
css1 <- cssparser::read_css('
#main {
color: red !important;
font-size: 11px;
margin-top: 10px;
}
.footer {
color: #888888;
font-size: 6px;
}
.soft {
color: #aaa;
}
')
css2 <- cssparser::read_css('
#main {
color: blue;
font-size: 14px;
background-color: white;
margin-top: 14px;
}
.footer {
background-color: #bbb;
}
.emph {
font-weight: bold;
}
')
css_final <- cssparser::css_merge(css1, css2)
css_pretty_print(css_final)
#main {
color: red !important;
font-size: 14px;
margin-top: 14px;
background-color: white;
}
.footer {
color: #888888;
font-size: 6px;
background-color: #bbb;
}
.soft {
color: #aaa;
}
.emph {
font-weight: bold;
}
Merge styles for an individual element with style_merge()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Pull styles from 2 different stylesheets, and the inline style
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
style1 <- css1[['#main']]
style2 <- css2[['#main']]
style3 <- cssparser::read_inline_style("color: yellow; font-size: 18px;")
style_pretty_print(style1)
color: red !important;
font-size: 11px;
margin-top: 10px;
style_pretty_print(style2)
color: blue;
font-size: 14px;
background-color: white;
margin-top: 14px;
style_pretty_print(style3)
color: yellow;
font-size: 18px;
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Merge these 3 styles, with inline style taking highest prioirty
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
style_final <- style_merge(style1, style2, style3)
style_pretty_print(style_final)
color: red !important;
font-size: 18px;
margin-top: 14px;
background-color: white;