Stacking box-shadows in multiple css selectors - css

I'm making a little game in Eml, and I'm using css for visualization.
I have a grid with cells, all get the class cell.
However there is also one that has the class selected with obvious use.
Now, to visualize this to the user I have the following css:
.game .cell:hover {
box-shadow: inset 0 0 5px rgba(200, 50, 50, 1);
}
.game .cell.selected {
box-shadow: 0 0 5px rgba(50, 50, 200, 1);
}
So this adds a shadow to the cell you are currently hovering over, and a different one if the cell is selected.
However when you hover over the currently selected one, it just shows the "selected-shadow", not both.
A simple fix of course is to add this:
.game .cell.selected:hover {
box-shadow: 0 0 5px rgba(50, 50, 200, 1), inset 0 0 5px rgba(200, 50, 50, 1);
}
Then it works for both, so that's great.
However it's not perfect.
What if I decide to change the hover-effect, then I'll also have to change the selected+hover effect.
So there's duplicated data, and my question is: can the same be achieved without duplicating data?
Note: I'm wondering about a pure css solution, no extra libraries such as SASS or similar if possible.

You can do a little trick and use a pseudo element:
.cell {
width: 100px;
height: 100px;
display: inline-block;
border: solid 1px;
position: relative;
}
.cell:hover {
box-shadow: inset 0 0 5px rgba(200, 50, 50, 1);
}
.cell.selected:after {
content: "";
position: absolute;
top: 0px;
right: 0;
bottom: 0;
left: 0;
box-shadow: 0 0 5px rgba(50, 50, 200, 1);
}
<div class="cell"></div>
<div class="cell selected"></div>

Related

CSS - Create multiple box shadow [duplicate]

This question already has answers here:
Is there a way to use two CSS3 box shadows on one element?
(2 answers)
Closed 4 years ago.
I have code like this
body {
/*some styling for make div on center*/
}
div {
width: 50px;
height: 50px;
background-color: white;
border-radius:50%;
box-shadow: 0 0 5px 7px rgba(230, 230, 230, 0.4);
}
http://jsfiddle.net/zu9rd1jq/68/
I wanna make multiple box-shadow after box-shadow. It can be when we using photoshop, but how we do using css? Any idea, please :)
I Hope you are looking for this
div {
width: 50px;
height: 50px;
background-color: white;
border-radius:50%;
box-shadow: 0 5px 0 rgba(255, 0, 0, 1), 0 10px 0 rgba(0, 255, 0, 1);
}

Add shadow to html element

I need to create an element with shadow like in the mockup:
http://take.ms/UdLFk
But I created only
http://take.ms/lns0J .
I have next styles:
.shadow {
width: 45px;
left: 37px;
position: relative;
box-shadow: 0 0px 2px 2px rgba(0,0,0,0.5);
}
My markup:
<div class=" shadow"></div>
So, how i can get a shadow like in mockup? I searched many articles but they did not help me.
Adding a border-radius (with a small height and a background-color that fits the shadow) to the element will give the shadow a nice rounded effect. Maybe decrease the opacity a little and you'll get pretty close. Also try using z-index: -1 to put the shadow behind the image.
.shadow {
width: 45px;
left: 37px;
position: relative;
box-shadow: 0 0px 4px 2px rgba(0, 0, 0, 0.1);
border-radius: 50%;
height: 3px;
background: rgba(0, 0, 0, 0.1);
}
<div class="shadow"></div>

How to obfuscate background with css3?

I need to shade the background image.
background: linear-gradient(0deg, rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url("background.png");
This code works OK, but is there a simpler way to do it?
You can add a semitransparent pseudoelement covering the whole image:
.element {
position: relative;
}
.element:after {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
background: rgba(0,0,0,0.5);
pointer-events: none; /* if the element below needs to be clickable */
}
That's just one of the ways you can implement it. It'll render faster than a gradient but I'm not sure about simplicity of it ;)
Don't use background images for making shadows. Its increase your site loading time gradually.
Best way for adding this css to your element:
-webkit-box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
-moz-box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
I recommend this site for generating shadows: http://www.cssmatic.com/box-shadow
Why use gradient ?
Just set background like this :
background: url("background.png"), rgba(0, 0, 0, 0.5);
Depending of what you need, you could use box-shadow.
box-shadow: inset 0 0 100px black;
The only problem is that shadows will also come from the sides.

How to achieve this bevel button in CSS?

Is it possible to create a button that look like this in CSS:
Of course I don't mean using an image as background, I can easily do that. I'm talking about the webkit type of solution.
The short answer is yes, it can be done. I went ahead and gave it a shot.
These are the steps I took:
I opened your bitmap in Sketch, my favorite graphical tool for all things web
I zoomed in to your bitmap, traced the outline with a rounded rectangle and gave it the correct color
I started adding box-shadows, both outside and inset, to replicate the bitmap as close as possible. Note that I only used black and white (with varying alpha values) for the box shadows. This way you can easily change the color of the button by just changing the background-color.
I also added two extra shapes for the bottom shadow and the top glow, as I did not manage to get this right with just box shadows. As long as it are just 2 elements that should not be a problem however, you can use the :before and :after pseudo elements to include these in your css.
The resulting image looks something like this (not exact, but pretty close I think):
And then I translated the drawing to css, by choosing 'copy css attributes' and manually adding the :before and :after elements and doing some fine tuning. This is the (unprefixed) css I came up with:
.button {
display: inline-block;
color: #fff;
text-shadow: 0 0 2px rgba(0,0,0,.3);
font-family: sans-serif;
box-shadow:
inset 0 0 2px 0 rgba(255,255,255,.4),
inset 0 0 3px 0 rgba(0,0,0,.4),
inset 0 0 3px 5px rgba(0,0,0,.05),
2px 2px 4px 0 rgba(0,0,0,.25);
border-radius: 4px;
padding: 8px 16px;;
font-size: 12px;
line-height: 14px;
position: relative;
}
.button.red { background: #EA3D33; }
.button.green { background: #7ED321; }
.button.blue { background: #4A90E2; }
.button:before, .button:after {
content: '';
display: block;
position: absolute;
left: 2px;
right: 2px;
height: 3px;
}
.button:before {
top: 0;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
background: rgba(255,255,255,.6);
box-shadow: 0 1px 2px 0 rgba(255,255,255,.6);
}
.button:after {
bottom: 0;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
background: rgba(0,0,0,.15);
box-shadow: 0 -1px 2px 0 rgba(0,0,0,.15);
}
and a fiddle to demonstrate: http://jsfiddle.net/pn4qk3wL/

Minor CSS issue

I am new to the designing/programming world so I am sure the issue is easy to solve. I am trying to add the moz-box-shadow effect to my header. But as soon as I add that component, the header which is taking up space horizontally shortens up. I want the header to be like Twitter's, where they use a shadow effect.
#header {
background-color: #990000;
width:101.3%;
margin-left:-8px;
margin-top:-8px;
height:40px;
-moz-box-shadow: 1px 1px 10px #D7D7D7;
}
Also, the way i have set the width is it likely going to create cross browser issues?
Here's a version similar to what Twitter has:
This is Twitter's version, more or less:
Live Demo (edit)
HTML:
<div id="top-fixed">
<div id="top-bar"></div>
</div>
CSS:
html, body {
margin: 0; padding: 0
}
body {
padding-top: 50px;
background: #c0deed
}
#top-fixed {
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 1000;
}
#top-bar {
height: 40px;
width: 100%;
background-color:#00a0d1;
background-image:-webkit-gradient(linear,0 0,0 100%,from(#00a0d1),to(#008db8));
background-image:-moz-linear-gradient(#00a0d1,#008db8);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#00a0d1',endColorstr='#008db8');
-ms-filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr='#00a0d1',endColorstr='#008db8')";
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
The trick that Twitter is using, is putting in an absolutely positioned box and giving that box a width of 100% and the shadow. Using overflow-x: hidden on it´s parent, you get the effect that you are looking for.
I've been doing shadows with .png's. I see no benefit of using this (esp. since I would assume browsers started supporting .png prior to supporting box shadowssee, for example, Mozila's statement that FF started supporting box shadows in FF3.5,) but of course, if this is better than doing shadows via .png, feel free to leave a comment proving me wrong!

Resources