Weird CSS effect with box shadows - how to solve? - html

See this example:
I have several boxes with white background and huge black, translucent box shadows that overlap the boxes above. However, this leads to an irritating behavior: While the white background gets darker through the overlapping box shadows, nested objects, like text or other boxes, don't!
Could anybody tell me why this occurs? I guess it has something to do with z-index. I would like prevent this - the nested objects should become darker as well. Any solutions?
Thanks in advance!
Here's the code: https://jsfiddle.net/xq20hvp4/3/
<div>Coloured text <span>Box with background</span></div>
<div>Coloured text <span>Box with background</span></div>
<div>Coloured text <span>Box with background</span></div>
<div>Coloured text <span>Box with background</span></div>
<div>Coloured text <span>Box with background</span></div>
CSS:
div {
margin: 20px;
box-shadow: 0 0 0 250px rgba(0, 0, 0, 0.3);
font-size: 25px;
color: red;
font-weight: bold;
font-family: Consolas, Arial, sans-serif;
background-color: #ffffff;
}
div span {
background-color: #e7e7e7;
color: #555555;
font-weight: normal;
font-size: 17px;
padding: 1px 5px;
}

It's because those elements are on top of the div with the shadow. In order to put them behind, you can use position: relative; on the background element and give it z-index: 1:
div {
margin: 20px;
box-shadow: 0 0 0 250px rgba(0, 0, 0, 0.3);
font-size: 25px;
color: red;
font-weight: bold;
font-family: Consolas, Arial, sans-serif;
background-color: #ffffff;
/* Add this */
position: relative;
z-index: 1;
}
div .box {
background-color: #e7e7e7;
color: #555555;
font-weight: normal;
font-size: 17px;
padding: 1px 5px;
}
Here's an updated fiddle: https://jsfiddle.net/6wwz8usw/.

https://jsfiddle.net/fd7tx2c2/
div {
z-index: 1;
position: relative;
}
Z-index
Position

Related

How to create inner shadow effect on text in CSS?

I'm trying to convert a Figma design into code but it has some text that uses an inner-shadow effect
I tried to style it using plain and clipping the text-shadow CSS property but the result doesn't quite match the design in which the shadow kind of clips/insets the text.
h1 {
font-family: 'Poppins';
font-style: normal;
font-size: 80px;
text-align: center;
color: #6225E6;
text-shadow: -6px 0px 0px #D63737;
background-color: #151717;
}
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<h1>This is some text<h1/>
You can get closer using mask (doesn't work in Firefox)
h1 {
font-family: 'Poppins';
font-size: 80px;
letter-spacing: 5px;
text-align: center;
color: #0000;
text-shadow:
5px 0 #6225E6,
0 0 red;
-webkit-mask: linear-gradient(#000 0 0);
-webkit-mask-clip: text;
mask-clip: text;
}
body {
background:#000;
}
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<h1>This is some text<h1/>
For better support you can duplicate the text and try like below:
h1 {
font-family: 'Poppins';
font-size: 80px;
letter-spacing: 5px;
text-align: center;
color: #0000;
text-shadow:
5px 0 #6225E6,
0 0 red;
position: relative;
}
h1:before {
content: attr(data-text);
position: absolute;
inset: 0;
background: #000;
color: #fff;
mix-blend-mode: darken;
text-shadow: none;
}
body {
background: #000;
}
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<h1 data-text="This is some text">This is some text</h1>
This kind of works, I guess depending on who is looking at it and what they're interpreting as the shadow versus the fill. The slight blur is me trying to throw the viewer off a bit but unfortunately, it doesn't give you much room to move the shadow either. I think any solution anybody comes up with is going to require some form of optical illusion to make it work.
body {
background-color: #151717;
}
h1 {
font-family: 'Poppins';
font-style: normal;
font-size: 120px;
background-color: #D63737;
color: transparent;
letter-spacing: 2px;
text-shadow: 2px 2px 1px #6225E6, 1px 1px 1px #D63737;
-webkit-background-clip: text;
-moz-background-clip: text;
background-clip: text;
}
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<h1>This is some text</h1>
added this CSS property called letter-spacing
that makes every letter have some little gap between them, so the shadow doesn't touch the other letters.
letter-spacing: 6px;
documentation: The letter-spacing CSS property sets the horizontal spacing behavior between text characters. This value is added to the natural spacing between characters while rendering the text. Positive values of letter-spacing causes characters to spread farther apart, while negative values of letter-spacing bring characters closer together.
h1 {
font-family: 'Poppins';
font-style: normal;
font-size: 80px;
text-align: center;
color: #6225E6;
text-shadow: -6px 0px 0px #D63737;
background-color: #151717;
letter-spacing: 6px;
}
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<h1>This is some text</h1>

Add outside stroke to the text with css

How to add outside stroke for the text.
I tried with -webkit-text-stroke: 10px black; and text-shadow but my text becomes thinner, I want to have font size 24px.
p {
color: #fff;
text-shadow: -1px -1px 0 #844733, 1px -1px 0 #844733, -1px 1px 0 #844733, 1px 1px 0 #844733;
font-size: 24px;
}
<p>my text</p>
here is the image -> [1]: https://i.stack.imgur.com/5xMgn.png
I wanted very thick stroke and only outlide
You can use a pseudo element with identical text content to the main text and give the pseudo element the text-stroke. It was shown on CSS Tricks.
p {
color: #fff;
font-size: 24px;
position: relative
}
p:after {
content: attr(data-text);
-webkit-text-stroke: 6px #844733;
color: #844733;
position: absolute;
left: 0;
z-index: -1
}
<p data-text="my text">my text</p>
#import "compass/css3";
-webkit-text-stroke: width of stroke;
this one is pretty smooth

How can my button's position be centered without any kind of "margin cheat"?

How can I place my button in the center without any kind of "margin cheat" (for example setting margin-left: 525px;)?
HTML
<div id="banner">
<div id="bannerContainer">
<h1>H1 tag</h1>
Products
</div>
</div>
CSS
.bannerButton {
border: 2px solid #fff;
color: #fff;
font-weight: 300;
font-family: 'Raleway';
font-size: 20px;
display: inline-block;
background-color: rgb(63, 127, 191);
padding: 18px 60px 18px 50px;
margin-bottom: 50px;
margin-top: 50px;
position: relative;
margin-left: 525px;
}
.bannerButton:hover {
text-decoration: none;
background: #eaf;
color: #fff;
}
I've tried making it sit in the center but it didn't work out so well without me setting margin-left; 525px;, which in my case, centers the button under the text, please help me remove this "margin cheat" and do it in the right way.
The a act like text it means when you give text-align:center; to its parent, it will be placed in center of its parent.
You don't need to give margin to the a element. You can use text-align:center;.
#bannerContainer
{
text-align:center;
}
.bannerButton {
border: 2px solid #fff;
color: #fff;
font-weight: 300;
font-family: 'Raleway';
font-size: 20px;
display: inline-block;
background-color: rgb(63, 127, 191);
padding: 18px 60px 18px 50px;
margin-bottom: 50px;
margin-top: 50px;
position: relative;
}
.bannerButton:hover {
text-decoration: none;
background: #eaf;
color: #fff;
}
<div id="banner">
<div id="bannerContainer">
<h1>H1 tag</h1>
Products
</div>
</div>
If you set the position of the button to absolute, give it a set width, and add this it should center:
left: 50%; right: 50%;
Have you try this:
<center>Products</center>
I am not sure whether it is helpful to you..

Center align text inside a div

I need to center vertically the text inside a div but I am having a problem. This is the situation:
There is an hover attribute that I added in the CSS and in fact, when the mouse goes on the div, it changes the background and the text goes at the center of the div.
I would like the text to be centered also in the first case shown in the picture (when the mouse in not over the div). You can find the fiddle with the code here: Fiddle
.tab {
float: left;
margin: 0px;
height: 50px;
display: table-cell;
line-height: 50px;
}
This is the code that I have used for the div. And when the mouse goes over:
.tab:hover {
cursor: pointer;
box-shadow: inset 0 0 10px #999;
-moz-box-shadow: inset 0 0 10px #999;
-webkit-box-shadow: inset 0 0 10px #999;
background-color: #555;
line-height: 50px;
}
I have used the line-height in both cases but it works only in .tab:hover. Any idea?
That happens because you are setting after a declaration for the font:
.font_header {
font: 19px Century Gothic, sans-serif;
color: #EEEEEE;
}
This CSS is after and then the specificity goes with the declaration here. If you just change the order it will work, since the last has more precedence:
.font_header {
font: 19px Century Gothic, sans-serif;
color: #EEEEEE;
}
.tab {
float: left;
margin: 0px;
height: 50px;
display: table-cell;
line-height: 50px;
}
UpdatedFiddle
You need to set the line-height: 50px; for .font_header as well.
Example fiddle: http://jsfiddle.net/6tzwc17c/2/
At least in Chrome it works if you just split the font declaration, like:
font-family: Century Gothic, sans-serif;
font-size: 19px;
Instead of:
font: 19px Century Gothic, sans-serif;

CSS Tooltip has different position in different browsers. How can I account for this?

I have adjusted my bottom attribute to display how I want it in Firefox, but the spacing is all warped in Chrome and Safari. Is there a way to sett a different "top" attribute for each browser?
HTML
<a class="tooltip" href="#"> <!-- this tag activates my tool tip -->
<div class="handsevent" > <!-- this div contains everything that activates the tool tip when hovered over-->
<div class="handswrapper">
<div class="handshour" >
<h3>8:00am-noon</h3>
</div>
<div class="handsspeaker">
<h3>Speaker 3</h3>
</div>
</div>
<div class="handstitle">
<p>Description</p>
</div>
</div>
<span class="classic"> <!-- this span contains everything that pops up in the tool tip -->
<h3>Title Bar</h3>
<br />lots of descriptive text
</span>
</a>
CSS
/* HOVER WINDOW */
.tooltip {
color: #000000; outline: none; font-style:bold;
cursor: help; text-decoration: none;
position: relative;
}
.tooltip span {
margin-left: -999em;
position: absolute;
}
.tooltip:hover span {
border-radius: 30px 30px; -moz-border-radius: 30px; -webkit-border-radius: 30px;
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1); -webkit-box-shadow: 5px 5px rgba(0, 0, 0, 0.1); -moz-box-shadow: 5px 5px rgba(0, 0, 0, 0.1);
font-family: Arial, Tahoma, Helvetica, sans-serif;
position: auto; left: 1em; top: 2em; z-index: 99; <!-- the 'top' attribute on this line is where I have been adjusting the display position it affects the position differently in different browsers -->
margin-left: 0; width: 700px; margin-top:-10px;
}
.tooltip:hover img {
border: 0; margin: -30px 0 0 90px;
float: right; position:fixed;
z-index: 99;
}
.tooltip:hover em {
font-family: Arial, Tahoma, Helvetica, sans-serif; font-size:14px; font-weight: bold; color:005DA4;
display: block; padding: -0.2em 0 0.6em 0;
}
.classic { padding: 0.8em 1em; }
* html a:hover { background: transparent; }
.classic {background: #ffffff; border: 1px solid #ffffff; }
I have tried pixels, em, and percentage. None of these have been consistent. Are there browser specific settings I could use?
here is a demo link for reference.
Using a CSS Reset will help to eliminate differences between browsers.
I switch the top attribute in ".tooltip:hover span" to "auto" and that seems to be working.