This question already has answers here:
Can a CSS class inherit one or more other classes?
(29 answers)
Closed 7 years ago.
I have 5 CSS classes. They are completely identical except one line. Is there a way to create one CSS class, then have the 5 other CSS classes inherite from the one and just add it's own specific's?
As you can see below the only line that is different is this line...
background-image: url("../Images/vertTabsButton2.gif");
.divMasterCol1Button1 {
float: left;
border-style: none;
border-width: thin;
background-image: url("../Images/vertTabsButton1.gif");
background-repeat: no-repeat;
background-position-x: center;
background-position-y: top;
width: 215px;
height: 700px;
margin: 0px auto;
padding: 0px;
text-align: center;
}
.divMasterCol1Button2 {
float: left;
border-style: none;
border-width: thin;
background-image: url("../Images/vertTabsButton2.gif");
background-repeat: no-repeat;
background-position-x: center;
background-position-y: top;
width: 215px;
height: 700px;
margin: 0px auto;
padding: 0px;
text-align: center;
}
Without using a pre-compiler like SASS you cannot achieve inheritance. However you could accomplish something like what you want by splitting the common properties out into a single class and applying the remaining unique properties through some other ID or class.
.commonProperties {
float: left;
border-style: none;
border-width: thin;
background-repeat: no-repeat;
background-position-x: center;
background-position-y: top;
width: 215px;
height: 700px;
margin: 0px auto;
padding: 0px;
text-align: center;
}
.divMasterCol1Button2 {
background-image: url("../Images/vertTabsButton2.gif");
}
.divMasterCol1Button1 {
background-image: url("../Images/vertTabsButton2.gif");
}
Here's your HTML
<button class="commonProperties divMasterCol1Button1"/>
<button class="commonProperties divMasterCol1Button2"/>
Here's your CSS
.commonProperties {
float: left;
border-style: none;
border-width: thin;
background-repeat: no-repeat;
background-position-x: center;
background-position-y: top;
width: 215px;
height: 700px;
margin: 0px auto;
padding: 0px;
text-align: center;
}
.divMasterCol1Button2 {
background-image: url("http://www.gettyimages.ca/gi-resources/images/CreativeImages/Hero-527920799.jpg");
}
.divMasterCol1Button1 {
background-image: url("http://www.gettyimages.ca/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg");
}
I recommend using SASS or LESS. Here's an example of it would look like for both pre-compiler languages with your class
SCSS
.divMasterCol1Button1 {
float: left;
border-style: none;
border-width: thin;
background-image: url("../Images/vertTabsButton1.gif");
background-repeat: no-repeat;
background-position-x: center;
background-position-y: top;
width: 215px;
height: 700px;
margin: 0px auto;
padding: 0px;
text-align: center;
}
.divMasterCol1Button2 {
#extend .divMasterCol1Button1;
background-image: url("../Images/vertTabsButton2.gif");
}
and here is LESS
.divMasterCol1Button1 {
float: left;
border-style: none;
border-width: thin;
background-image: url("../Images/vertTabsButton1.gif");
background-repeat: no-repeat;
background-position-x: center;
background-position-y: top;
width: 215px;
height: 700px;
margin: 0px auto;
padding: 0px;
text-align: center;
}
.divMasterCol1Button2 {
.divMasterCol1Button1;
background-image: url("../Images/vertTabsButton2.gif");
}
I prefer SCSS because bootstrap 4 is beginning to use it for their framework.....
Simply use CSS:
.c1,.c2,.c3,.c4,.c5{
//common styles
}
.c1{
//c1 special style
}
...
.c5{
//c5 special style
}
See example http://jsfiddle.net/qj76455e/
I wanted to make the principle readable, therefore I used short class names c1,...,c5 instead of divMasterCol1Button1 etc.
Use 2 or more classes. OOCSS, SMACSS or BEM will be great tool.
.button {
...
}
.button--red { color: red; }
Or you can achieve this with #extend operator in sass, less or stylus.
Related
I want to insert an image in CSS bubble, the code is correct but nothing appears,
can you please check how to solve this one
.triangle-isosceles {
position: relative;
padding: 15px;
margin: 1em 0 3em;
color: #000;
background: #f3961c;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
/*border-radius: 10px;*/
background: linear-gradient(top, #f9d835, #f3961c);
}
now before
.triangle-isosceles:before {
content: "";
display: block; /* reduce the damage in FF3.0 */
position: absolute;
bottom: -15px;
left: 50px;
width: 0;
border-width: 15px 15px 0;
border-style: solid;
border-color: #f3961c transparent;
}
the last one is after
.triangle-isosceles:after{
content: url("/web/resources/images/paul.png");
display: block;
position: absolute;
bottom: -130px;
left: 12px;
width: 110px;
height: 110px;
border:0;
background: #3365d4;
-moz-border-radius: 70px;
-webkit-border-radius: 70px;
border-radius: 70px;
}
In the "after" version, you have two errors:
content: This is not the correct tag to specify the background, the correct one would be simply "background"
background: As you may figure by reading the first error, the background property you have defined is the one who is specifiying the background. This tag has to be removed as it will be overwritten if is not deleted.
So the CSS should loke like this:
.triangle-isosceles:after {
background: url("/web/resources/images/paul.png");
display: block;
position: absolute;
bottom: -130px;
left: 12px;
width: 110px;
height: 110px;
border:0;
-moz-border-radius: 70px;
-webkit-border-radius: 70px;
border-radius: 70px;
}
You should also consider to add compatibility with more browsers. You can find more information at this page: https://www.w3schools.com/cssref/css3_pr_background-size.asp
EDIT: This is an example of a working CSS tag that add a background image to a "div":
#headerwrap {
background: url(../images/homepage/header.jpg) no-repeat center top;
margin-top: 60px;
padding-top: 20px;
text-align: center;
background-attachment: relative;
background-position: center center;
min-height: 600px;
width: 100%;
-webkit-background-size: 100%;
-moz-background-size: 100%;
-o-background-size: 100%;
background-size: 100%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Hope this helps :)
You need to add content also, Without content after and before won't work
Like this :
.triangle-isosceles:after {
content :'';
background: url("/web/resources/images/paul.png");
display: block;
position: absolute;
bottom: -130px;
left: 12px;
width: 110px;
height: 110px;
border:0;
-moz-border-radius: 70px;
-webkit-border-radius: 70px;
border-radius: 70px;
}
I am currently having issues with Safari not displaying background images and background colors in my css. Apparently its just the homepage.
#footer {
background-color: #00594c;
}
#home_widget_1 {
background-color: #00594c;
color: white;
height: 485px;
border-right: 1px solid white;
background-image: url(https://www.fvcolumbus.org/stage/wp- content/uploads/2016/12/independent-living1.png);
background-size: 510px 282px;
background-repeat: no-repeat;
margin-right: 0px !important;
background-position: center top;
}
#home-featured-image {
height: 540px;
width: 100%;
display: block;
max-width: 1280px;
margin-right: auto;
margin-left: auto;
background-image: url(../stage/wp-content/uploads/2016/10/10192016_homepage.jpg);
background-repeat: no-repeat;
}
Apparently I had caching issues.
I put together a button using :before and :after elements and IE10/9 are ignoring them completely, as far as I can tell they should be working perfectly in at least those 2 versions.
.buttonSML {
background-position:-35px -432px;
background-color: transparent;
border: none;
text-transform: uppercase;
font-size: 2.9rem;
font-weight: #font-bold;
height: 55px;
padding: 0 5px;
position: relative;
.text-shadow(0,0,4px);
cursor: pointer;
}
.buttonSML:before, .buttonSML:after {
content: " ";
position: absolute;
top: 0;
height: 55px;
width: 20px;
display: inline-block;
visibility: visible
}
.buttonSML:before {
background-image: url('../images/sprite.png');
background-position: 0px -432px;
background-repeat: no-repeat;
background-color: transparent;
left: -20px;
}
.buttonSML:after {
background: url('../images/sprite.png');
background-position: -394px -432px;
background-repeat: no-repeat;
background-color: transparent;
right: -20px;
}
Added a jsfiddle so you can see the end result http://jsfiddle.net/7D4kG/1/
Not really sure what up so would appreciate any advice you guys can provide.
After some work I found 2 solutions.
FIrst is with the help of jquery, you can replace with and add
$('#button-id').click(function ()
{
$('#form-id').submit();
});
Works well, but you loose HTML5 form validations.
To keep the validations you can skip the jquery and just add "overflow: visible" to your buttons style. Have only tested it in IE10 so far, will test the rest later when I republish.
http://jsfiddle.net/3MHHs/1/
I have made some changes and it works for me in Chrome and IE10. I completely removed the positioning, because that is very ...advanced thing in pseudos. Browsers fail on simpler cases too. I have removed the 5px padding too.
.sprite {
background-image: url('https://dl.dropboxusercontent.com/u/6374897/sprite.png')
}
.buttonSML {
background-position: -35px -432px;
background-color: transparent;
border: none;
text-transform: uppercase;
font-size: 2rem;
height: 55px;
padding: 0;
cursor: pointer;
}
.buttonSML:before, .buttonSML:after {
content: "";
width: 20px;
height: 55px;
font-size: 2rem;
display: inline-block;
vertical-align: middle;
background-repeat: no-repeat;
background-color: white; /* sorry */
background-image: url('https://dl.dropboxusercontent.com/u/6374897/sprite.png');
}
.buttonSML:before {
background-position: 0px -432px;
}
.buttonSML:after {
background-position: -394px -432px;
}
I've been trying to create a small social media bar for my website.For some reason which I can't work out the images which I want to use to create a sprites won't load. I should also mention that I have had similar problems with background-image not displaying.
HTML call is this:
<div id="socialbar">
<div class="plus">Google Plus</div>
<div class="twitter">Twitter</div>
<div class="facebook">Facebook</div>
<div class="youtube">YouTube</div>
<div class="flickr">Flickr</div>
</div>
CSS looks like this:
#socialbar
{
width: 245px;
margin: 0px;
display: inline;
border: 0px;
}
#socialbar a.plus
{
display: block;
float: left;
height: 32px;
width: 32px;
text-indent: -9999px;
background-image: url('http://www.mediafire.com/conv/9452d5f77f6e5e7e91294b54b9a74d18029cbeae2075ef9d12787e7631f849226g.jpg');
background-position: 0px 0px;
margin-left: 5px;
margin-right: 5px;
}
#socialbar a:hover.plus
{
background-image: url('http://www.mediafire.com/conv/9452d5f77f6e5e7e91294b54b9a74d18029cbeae2075ef9d12787e7631f849226g.jpg');
background-position: 0px -32px;
}
#socialbar a.twitter
{
display: block;
float: left;
height: 32px;
width: 32px;
text-indent: -9999px;
background-image: url('http://www.mediafire.com/conv/d2133ecf83fc76b78008ed1357a8fb8213e6c2300c26f6af3b1758c576ed17db6g.jpg');
background-position: 0px 0px;
margin-left: 5px;
margin-right: 5px;
}
#socialbar a:hover.twitter
{
background-image: url('http://www.mediafire.com/conv/d2133ecf83fc76b78008ed1357a8fb8213e6c2300c26f6af3b1758c576ed17db6g.jpg');
background-position: 0px -32px;
}
#socialbar a.facebook
{
display: block;
float: left;
height: 32px;
width: 32px;
text-indent: -9999px;
background-image: url('http://www.mediafire.com/conv/df04514676d02be959e52f440b9c10e82d09eefdb82d34fdd336a896be551d026g.jpg');
background-position: 0px 0px;
margin-left: 5px;
margin-right: 5px;
}
#socialbar a:hover.facebook
{
background-image: url('http://www.mediafire.com/conv/df04514676d02be959e52f440b9c10e82d09eefdb82d34fdd336a896be551d026g.jpg');
background-position: 0px -32px;
}
#socialbar a.youtube
{
display: block;
float: left;
height: 32px;
width: 32px;
text-indent: -9999px;
background-image: url('http://www.mediafire.com/conv/6de4cbb7ecf8e81a25e0ebe35e2cb82af5859b4b2cb3ac9c3ec2fe8c62348c296g.jpg');
background-position: 0px 0px;
margin-left: 5px;
margin-right: 5px;
}
#socialbar a:hover.youtube
{
background-image: url('http://www.mediafire.com/conv/6de4cbb7ecf8e81a25e0ebe35e2cb82af5859b4b2cb3ac9c3ec2fe8c62348c296g.jpg');
background-position: 0px -32px;
}
#socialbar a.flickr
{
display: block;
float: left;
height: 32px;
width: 32px;
text-indent: -9999px;
background-image: url('http://www.mediafire.com/conv/dbab33ccef1c60b2974d10a0323f7bb04f594f3b57468dc1a7be8551eb9d1b176g.jpg');
background-position: 0px 0px;
margin-left: 5px;
margin-right: 5px;
}
#socialbar a:hover.flickr
{
background-image: url('http://www.mediafire.com/conv/dbab33ccef1c60b2974d10a0323f7bb04f594f3b57468dc1a7be8551eb9d1b176g.jpg');
background-position: 0px -32px;
}
Your find my code here also:
http://jsfiddle.net/Yx8tT/122/
(Please note that i am using another host for this as the host for my website does not allow direct links to images etc for anything other than my actual website)
And the site in question being:
http://www.darrenlumber.co.uk
Thanks inadvance!
You aren't applying your css to the plus elements.
<div class="plus">Google Plus</div>
#socialbar a.plus
{
display: block;
float: left;
height: 32px;
width: 32px;
text-indent: -9999px;
background-image: url('http://www.mediafire.com/conv/9452d5f77f6e5e7e91294b54b9a74d18029cbeae2075ef9d12787e7631f849226g.jpg');
background-position: 0px 0px;
margin-left: 5px;
margin-right: 5px;
}
The selector should be:
#socialbar .plus a
and so on for each of the others.
Just redoing our captcha.
Demo: http://sitehelp.com.au/demos/ttt/422.php
Anyhoo the css is as follows:
.funcaptcha { width: 81px; overflow: hidden; float: left; margin-right: 0px; }
.funcaptcha a { display: inline-block; background: url("http://sitehelp.com.au/demos/ttt/images/ttt-small.png"); margin: 2px 2px 0 0; float: left; }
.funcaptcha a.circle { background-position: 0px -29px; }
.funcaptcha a.cross { background-position: -29px -83px; }
.funcaptcha a.empty { background-position: -0px 0px; }
.funcaptcha a.marked,
.funcaptcha a:hover { background-position: -29px 0px; }
.funcaptcha .box { display: block; cursor: pointer; overflow: hidden; width: 25px; height: 25px; }
Contains php, so unable to do a fiddle. But My css positioning is just off a tad, any suggestions ?
the ttt-small.png is:
So that it can be marked answered....
Bottom layer is 25 pixels tall and the top layer is 26. Sprite needs to be fixed.
Here's an editable jsFiddle with the HTML and CSS: http://jsfiddle.net/WhG5L/