CSS override or inheritance - css

I have a really basic question about CSS. If there are two CSS files.
base.css contains:
.awesome-style {
height: 10px;
weight: 20px;
border: 5px;
}
child.css contains:
#import "base.css";
.awesome-style {
height: 15px;
weight: 20px;
padding: 10px;
}
When I will use the awesome-style class, what will be the applied style? My guess is that
.awesome-style {
height: 15px;
weight: 20px;
border: 5px;
padding: 10px
}
Am I right? Could somebody give me a description or good examples about how these "overrides" work? Thank you.

You are correct in what you believe will be the final* style applied to the element:
.awesome-style {
height: 15px;
weight: 20px;
border: 5px;
padding: 10px
}
When you do an #import, think of it as adding the CSS before what is in the CSS file you are adding it to. (Note: #import must be before any other styles in your CSS)
This is where it gets opinionated. If you have .awesome-style in both CSS files base.css and child.css, you might want to consider only adding the properties that are different than what is defined in base.css - since your browser will read them both and will apply whatever properties don't change or come last.
For instance, in your example, you could do:
base.css
.awesome-style {
height: 10px;
weight: 20px;
border: 5px;
}
child.css
#import base.css
.awesome-style {
height: 15px;
padding: 10px
}
This is because only the height and padding are changing. This is easier to see when you consider how the CSS file will look to the browser:
.awesome-style {
/* browser reads this, but it will get changed */
height: 10px;
/* this stays the same, but will still get 'overwritten' since it's defined again in the next rule */
weight: 20px;
/* this doesn't get defined again, so the browser will use this */
border: 5px;
}
.awesome-style {
/* height changed */
height: 15px;
/* weight stayed the same, but is still read from this rule because it comes after the previous rule */
weight: 20px;
/* padding wasn't defined prior, so it will use this rule */
padding: 10px;
}
And then this really becomes a question about CSS specificity
*final as in your current question - another rule may override what you have

Related

Is there a shorthand css property like "all:" but for unspecified properties?

Let's say I have two different stylesheets that are setting values to unique properties on the div element with class="example".
Stylesheet A:
.example {
background-color: #000;
border-radius: 50%;
color: #fff;
width: 25px;
height: 25px;
line-height: 25px;
display: inline-block;
text-align: center;
}
Stylesheet B:
div{
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
vertical-align: baseline;
background: transparent;
}
Is there a shorthand property and value similar to all: revert; that I could add to the Stylesheet A to set all undeclared properties to the browser default and essentially overwrite/erase the Stylesheet B? In my mind, it would be something like remaining: revert;.
If there is not such a shorthand property, is there some other trick I could do without explicitly having to list all the properties that haven't been declared in Stylesheet A and applying 'revert'? Bonus points if I could apply this trick recursively to the elements within the div with > and/or * selectors.
Thanks in advance.
There isn't a one-size-fits-all way to do this due to how cascade resolution for the all property works (so the bonus question doesn't have a universal answer, it depends entirely on the project). The CSS cascade does not compartmentalize rules and declarations by the stylesheet they appear in — all author-origin styles are consolidated to a single logical "stylesheet" in the order they appear in, and cascade resolution is performed accordingly. This means equally specific styles in later stylesheets will take precedence, whereas more specific styles in earlier stylesheets will take precedence.
In this case, because .example happens to be more specific than div, placing an all: reset declaration at the top of the .example rule will give you the desired effect (assuming there are no other style declarations elsewhere you would like to preserve, or more specific declarations elsewhere you would like removed as those would continue to apply even if you do this):
.example {
all: revert;
background-color: #000;
border-radius: 50%;
/* ... */
Everything in .example appearing after the all: revert declaration will be unaffected by it.
In the following code snippet you can compare the results of applying all: revert this way, against an element without the declaration:
.example:where(:first-of-type) {
all: revert;
}
.example {
background-color: #000;
border-radius: 50%;
color: #fff;
width: 25px;
height: 25px;
line-height: 25px;
display: inline-block;
text-align: center;
}
div{
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
vertical-align: baseline;
background: transparent;
}
<p>Inspect each element's styles to see the effects of <code>all: revert</code>
<div class="example">1</div>
<div class="example">2</div>
Or, see the following screenshots of Firefox's Inspector:
Element 1:
Element 2:

replacing global css with local css

I am trying to edit the style of a popover. In this case I want to edit the width of the display. However, I have a global popover style sheet that applies to all popovers in the application.
/* ========================================================================
Component: popovers
========================================================================== */
.popover {
border-radius: 0;
border: none;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
.arrow {
margin-left: -7px !important;
}
.popover-header {
padding: 1rem 0.75rem 0.5rem;
display: block;
background-color: $secondary;
border-bottom: none;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
.popover-body {
padding: 0.5rem 1rem;
color: $body-color;
}
.popover-close {
position: absolute;
top: -2px;
right: 0;
padding: 0 0.5rem;
color: $gray-lighter;
font-size: 2rem;
font-weight: 400;
line-height: 1;
text-shadow: 0 1px 0 #fff;
&:hover {
cursor: pointer;
color: $body-color;
}
}
I would like to know how in my local css file I can overwrite/add to these inherited styles.
Local Style Sheet:
.team-activity-container {
.icon {
background-image:
background-repeat: no-repeat;
display: inline-block
}
.icon .icon-team {
background-position: -5px -5px;
width: 25px;
height: 25px
}
}
.popover {
background-color: aqua !important;
width: 500px;
}
Edit: The local stylesheet is in the container that displays the popover, and the classes in the html are all related to the contents that fill the popover. While the popover and it's stylesheets are at the global level. how can I edit the popover at the local level without having to touch the global style sheet.
Also, The popover is from ng-bootstrap and I believe the problem is I can't overwrite the default width that bootstrap sets
I don't know all the context you are in, but there are 3 main ways to overwrite existing CSS rules:
add a new stylesheet with the new rules after the existing ones;
if you have control over the new popover HTML, adding a class (for instance version2 so you can edit your variant in a meaningful way as .popover.version2 inheriting what is already sets and changing just what you need);
add "!important" to the rules you add and are intended to overwrite the others, but notice that if the existing rules have already that, it's not going to work.
Depends on the context there could be other solutions like leverage on HTML tags or HTML tags properties if your new popover has some difference with the previous for examples.
I hope this helps.
EDIT: I saw you have edited your question adding stuff.
Looking at the global CSS, if your popover is in a particular container just bind the new rules to the container like this:
.team-activity-container .popover {...rules};

How to override Foundation accordion styles?

I'm using foundation sites in a WordPress theme, I am trying to override the style for the accordion component:
In particular I want all the items to have a border-radius of 10px.
There seems to be a couple of mixins that apply specifically to the first and last elements on the list which change the border radius:
/// Adds styles for the accordion item. Apply this to the list item within an accordion ul.
#mixin accordion-item {
&:first-child > :first-child {
border-radius: $global-radius $global-radius 0 0;
}
&:last-child > :last-child {
border-radius: 0 0 $global-radius $global-radius;
}
}
The problem I seem to be having is I am unable to override the borders in this mixin, I have tried re-declaring the mixin and changing the 0's to $global-radius.
I have changed the global Radius to 10px styles defined:
.di-accordian-title {
background-color: $blue;
color: $white !important;
padding: 5px;
border-radius: 10px;
margin-top: 5px;
width: 100%;
clear: both;
float: left;
font-size: 14px;
}
.di-accordian-title:hover, .di-accordian-title:focus {
background-color: $light-blue;
color: $white !important;
padding: 5px;
border-radius: 10px;
margin-top: 5px;
width: 100%;
clear: both;
float: left;
font-size: 14px;
}
I just can't seem to override the first and last items border-radius.
This is my first time using sass in a project, first time using sass at all really.
What is the correct way to override the defaults set up in the accordion mixin?
You shouldn't need to override the mixin, the specificity of your own styles or settings should override it.
There are two easy ways of doing this:
Via "settings"
In your _settings.scss file (which should be referenced from app.scss) there is a setting for $global-radius which by default is set to 0. Simply set this to 10px or 0.6rem or whatever you fancy and the global radius will be changed to that amount.
N.B. This is the radius for all foundation elements, not just the accordion.
Via specificity
Alternatively if you want to keep a global radius of 0 (or whatever) and just want the accordion to have a 10px radius:
.my-accordion.accordion {
.accordion-item {
&:first-child > :first-child {
border-radius: 10px 10px 0 0;
}
&:last-child > :last-child {
border-radius: 0 0 10px 10px;
}
}
}
This would go in one of your projects scss files, loaded after the Foundation SCSS, which are also #imported from the app.scss file
Edit: Where .my-accordion is the class you have for that specific accordion, though it also works for all accordions if you just use the .accordion-item part of the scss above.

Main CSS override properties

I am using the CMS called WebsiteBaker, now I wrote a module for it, a droplet, that uses it's own CSS.
It seems to be that the CSS of the droplet cannot be overriden by the main CSS.
I assume that's because the droplet (and along with it, the css) is loaded after everything else.
And I cannot just modify the CMS files as workaround, as it's a module.
Any ideas for a workaround or something similar?
--
Main CSS (override attempt, included in the header)
#programma_tab {
width: 300px;
}
Droplet CSS Example (included in the body, there is no other way)
#programma_tab {
padding: 5px;
font-size: 12px;
margin-top: -15px;
width: 200px;
}
I have a couple of ideas.
1 - Try making the selectors more specific. Something like body should override the original
body #programma_tab {
padding: 5px;
font-size: 12px;
margin-top: -15px;
width: 200px;
}
2 - try adding !important after any CSS property.
#programma_tab {
padding: 5px !important;
font-size: 12px !important;
margin-top: -15px !important;
width: 200px !important;
}
If you are using the DropletsExtension for WebsiteBaker the CSS for your Droplet can be automatically loaded in the head section of the template and you don't need to put your CSS hardcoded into the body.
Just add a !important statement to the CSS commands like:
#programma_tab {
padding: 5px !important;
font-size: 12px !important;
margin-top: -15px !important;
width: 200px !important;
}
I know its certainly not the most elegant way, but it works.

CSS - Extending class properties

I'm pretty new to CSS and have been finding my way around so far.
I am creating these button like links with shadows and stuff. Now there are several such buttons required on the site - everything about the buttons is same - except few properties change like width and font size.
Now instead of copying the same code - over and over for each button - is there a way of extending the button and adding just the properties that change.
Example of two buttons - css
.ask-button-display {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
width:350px;
font-size: 20px;
font-weight: bold;
padding:10px;
}
.ask-button-submit {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
font-weight: bold;
width:75px;
font-size: 12px;
padding: 1px;
}
And this is how I'm currently using it in my html
Ask a Question Submit
So I'm wondering if there is a cleaner way to do this - like
.button {
/* PUT ALL THE COMMON PROPERTIES HERE */
}
AND THEN SOMEHOW EXTEND IT LIKE
.button #display {
/* THE DIFFERENT PROPERTIES OF Display BUTTON */
}
.button #ask {
/* THE DIFFERENT PROPERTIES OF Ask BUTTON */
}
But I'm not sure how to do this.
Thanks for your inputs
You can add multiple classes to one element, so have one .button class which covers everything, then a .button-submit class, which adds things in.
For example:
.button {
padding: 10px;
margin: 10px;
background-color: red;
}
.button-submit {
background-color: green;
}​
See a live jsFiddle here
In your case, the following should work:
.button {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
width:350px;
font-size: 20px;
font-weight: bold;
padding:10px;
}
.button-submit {
width:75px;
font-size: 12px;
padding: 1px;
}​
See a live jsFiddle here
You might want to try this:
.button {
padding: 10px;
margin: 10px;
background-color: red;
}
.button.submit {
background-color: green;
}
.button.submit:hover {
background-color: #ffff00;
}
This way you avoid repeating the word and will able to use the classes in the elements like this:
Ask a Question
Submit
See the example in JSFiddle (http://goo.gl/6HwroM)
Rather than repeat the common "Add a button class to the element" answer I'm going to show you something new in the weird and whacky world of new age CSS, or better known as SCSS!
This reuse of code in stylesheets can be achieved with something called a 'Mixin'. What this allows us to do is reuse certain styles by using the '#include' attribute.
Let me give you an example.
#mixin button($button-color) {
background: #fff;
margin: 10px;
color: $color;
}
and then whenever we have a button we say
#unique-button {
#include button(#333);
...(additional styles)
}
Read more here: http://sass-lang.com/tutorial.html.
Spread the word!!!
You can do this since you can apply more than one class to an element. Create your master class and and other smaller classes and then just apply them as needed. For example:
Ask a Question Submit
This would apply the button and submit classes you would create while allowing you to also apply those classes separately.
Modify your code example along the lines of:
.master_button {
/* PUT ALL THE COMMON PROPERTIES HERE */
}
AND THEN SOMEHOW EXTEND IT LIKE
.button_display {
/* THE DIFFERENT PROPERTIES OF Display BUTTON */
}
.button_ask {
/* THE DIFFERENT PROPERTIES OF Ask BUTTON */
}
And apply like:
Ask a Question
Submit
.ask-button-display,
.ask-button-submit {
/* COMMON RULES */
}
.ask-button-display {
}
.ask-button-submit {
}
You may want to look into Sass. With Sass you can basically create variables in your css file and then re-use them over and over. http://sass-lang.com/
The following example was taken from Sass official website:
$blue: #3bbfce;
$margin: 16px;
.content-navigation {
border-color: $blue;
color:
darken($blue, 9%);
}
.border {
padding: $margin / 2;
margin: $margin / 2;
border-color: $blue;
}
Add a button class to both links for the common parts
.button {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
}
Keep in your other classes the rules that aren't common.
And your HTML will be
Ask a Question
Submit
Without changing/ adding new classes you can add styles to all elements with a class name starting with "ask-button"... (whatever the elements with the class are; button, anchor, div etc.) let's say your buttons are divs then:
div[class^="ask-button"] {
// common css properties
}
You can also list all classes that will have common properties like this:
.ask-button-display,
.ask-button-submit {
// common css properties here
}
And then you add the separate styling for each button:
.ask-button-display{
// properties only for this button
}
.ask-button-submit {
// properties only for this button
}

Resources