I have the following background image that is half gray, and half white.
When elements such as buttons, or text are on the dark side of the background, they appear behind it.
When elements are on the light side it appears in front of the background.
How can I get elements to appear in front of the dark side of the background?
Here is the button code, located outside of the body tag (which my background is located in)
<div class="container">
<div class="row">
<div class="col-xs-12" align="center">
Go
</div>
</div>
</div>
Here is the CSS for the button.
.btn-dark {
border-radius: 0;
color: #fff;
background-color: rgba(0,0,0,0.4);
z-index:1000;
}
.btn-dark:hover,
.btn-dark:focus,
.btn-dark:active {
color: #fff;
background-color: rgba(0,0,0,0.7);
}
.customWidth {
width: 100px !important;
z-index:1000;
}
Here is the code for my background:
.background-picture {
background-image: url("../img/background.png");
background-size: cover;
z-index:1;
}
If you look very closely, you can see that the button actually is above the black part as well. (Depending on the quality of your screen and its calibration, you might actually not be able to see it, but I clearly do.)
The reason is that you have a transparent background color defined (a 60% transparent black) - that's why the "black" button appears grey-ish in front of the white background, but nearly invisible in front of the very dark background.
Simply change it to be an opaque grey, and you're done.
.btn-dark {
/* background-color: rgba(0,0,0,0.4); */
/* about the same color: */
background-color: #999;
}
Also, you definitely should change the font color of the buttons for the dark background to white or a very light grey.
main{position:relative;display:inline-block;border:1px solid red;}main > div{width:200px;height:150px; display:inline-block;}.white{background:#fff;}.black{ background:#000;}button{position:absolute;left:calc(50% - 50px);width:100px;border:none;color:#fff;border:1px solid #ccc;}
#one{
background:#999;
top:10px;
}
#two{
background:rgba(0,0,0,.4);
top:60px;
}
<main>
<div class="white"></div><div class="black"></div>
<button id="two">
Test with rgba color!
</button>
<button id="one">
Test with opaque color!
</button>
</main>
.btn-dark {
border-radius: 0;
color: #fff;
background-color: rgba(0,0,0,0.4);
z-index:1000;
}
.btn-dark:hover,
.btn-dark:focus,
.btn-dark:active {
color: #fff;
background-color: rgba(0,0,0,0.7);
}
.customWidth {
width: 100px !important;
z-index:1001;
}
more the z-index is higer more the button is hover try this code
used background-color: rgba(0,0,0,0.4) for button and make the background black in cause the button will be black and not appear
you can add border or use different background color like gray red ..etc
.btn-dark {
border-radius: 0;
color: #fff;
background-color: rgba(30,30,30,0.4);
border:none;
z-index:10000;
border:1px solid gray;
}
.btn-dark:hover,
.btn-dark:focus,
.btn-dark:active {
color: #fff;
background-color: rgba(0,0,0,0.7);
}
.customWidth {
width: 300px !important;
height:30px;
display:block
}
<div class="container">
<div class="row">
<div class="col-xs-12" align="center" style="background:black">
Go
</div>
<br>
<div class="col-xs-12" align="center" style="background:white">
Go
</div>
</div>
</div>
It is not appearing behind the image, it is on the image only, your colour combination is such that you are seeing the buttons like that. However, if you check the same image uploaded by in some other system with maximum brightness you will see what I'm saying over here. I think if you can just change your colour combination everything will be fine.
Related
I've uploaded a 40x40 square .png.
The square has a white transparent background.
What I would expect when using this image as a custom cursor is that hovering over the red block I would see a faint white transparent square.
What I would expect when hovering over the white block is nothing, as I would assume the square should be "invisible" due to transparent white going over white.
But that's not what we are seeing, the square is actually faintly grey. Where does this conversion come from? I've tried using SVG as well as png.
FIDDLE in case example is not working
https://jsfiddle.net/cx2gu5qb/
.block {
height: 200px;
width: 400px;
cursor: url('https://i.imgur.com/yuDNHeN.png'), auto;
}
.red {
background: red;
}
.white {
border: 1px solid black;
background: white;
}
<div class="block red"></div>
<div class="block white"></div>
The picture you were using in your code as the cursor value url("https://i.imgur.com/yuDNHeN.png") seemed to have a transparent background when opened inside a picture editor but that color was actually: HSV(0,0,100) alpha(128) and as such it's a white with an alpha channel that will blend with the background color below the picture.
So both on red and white colors it showed up as gray.
Well...on red I would expect that color to blend to gray but as you pointed out, on white it shouldn't.
So actually I'm not strictly answering your direct issue but I'm just bringing further evidence to the table that it probably depends on how the browser deal with it when computing the color interpolation.
I made a bunch of attempts using a different image for the cursor and implemented a code snippet where pressing the corresponding button will engage the given class name for each .block element that will set a different value for the cursor property. It includes your original cursor attempt and also a final try using a green transparent background instead of white.
Conclusion so far: not really a very conclusive answer that can explain correctly and in a definitive way how blending works.
First attempt - using a total transparent color
So I attempted to make a new picture from scratch having a transparent background and an opaque red frame for reference.
https://i.ibb.co/WgSYYNW/cursor2.png
As you can see when the cursor shows on the red box, it will look invisible and when you'll hover on the white box, the red frame will be visible.
This comparison is to show how to achieve the real transparency.
Second attempt - using a partially transparent color
Here I tried to use the same picture as before but using a white color with alpha: 130.
https://i.ibb.co/Pg1b6Z1/cursor3.png
And it behaves in the same exact way as your original post did while we expected the translucent white to blend as white over white.
function changeCursor(button){
document.querySelectorAll('.block').forEach(block => {
block.classList.forEach(c => {
if(c.startsWith('cursor'))
block.classList.remove(c);
});
const classname = button.dataset.classname;
block.classList.add(classname);
document.getElementById('status').innerText = `cursor class engaged: ${classname}`;
});
}
button{
display: block;
margin-bottom: .5rem;
cursor: pointer;
padding: .5rem;
}
#status{
border: solid 2px gray;
padding: 1em;
display: block;
width: fit-content;
}
.block {
height: 200px;
width: 400px;
}
.container{
margin: 1rem;
}
/*white alpha 128*/
.cursor1{
cursor: url("https://i.imgur.com/yuDNHeN.png"), auto;
}
/*white alpha 255*/
.cursor2{
cursor: url("https://i.ibb.co/WgSYYNW/cursor2.png"), auto;
}
/*white alpha 130*/
.cursor3{
cursor: url(https://i.ibb.co/Pg1b6Z1/cursor3.png), auto;
}
/*green alpha 75*/
.cursor4{
cursor: url(https://i.ibb.co/w0JYvM1/cursor4.png), auto;
}
.red {
background: red;
}
.white {
border: 1px solid black;
background: white;
}
<div class="container">
<button onclick="changeCursor(this);" data-classname="cursor1">Cursor: white alpha 128</button>
<button onclick="changeCursor(this);" data-classname="cursor2">Cursor: white alpha 255 w/ frame</button>
<button onclick="changeCursor(this);" data-classname="cursor3">Cursor: white alpha 130 w/ frame</button>
<button onclick="changeCursor(this);" data-classname="cursor4">Cursor: green alpha 128 w/ frame</button>
<div id="status"></div>
</div>
<div class="block red"></div>
<div class="block white"></div>
cursor: url('https://i.imgur.com/V8pXcSn.png'), auto;
Your are getting a different color because of the shadow.
I've checked this with popular browsers. The result was exactly the same one.
Check the below image
https://jsfiddle.net/j1wnxzgu/2/
Conclusion
The shadow uses drop shadow algorithm.
As in, the shadow's alpha is proportional to the images alpha channel.
With CSS, we can't change this behavior
Go for JavaScript solutions
If you will make your cursor full white, only then are you going to see the desired result. Like this: fiddle
.block {
height: 100px;
width: 200px;
cursor: url('https://i.imgur.com/V8pXcSn.png'), auto;
}
.red {
background: red;
}
.white {
border: 1px solid black;
background: white;
}
<div class="block red"></div>
<div class="block white"></div>
But if you try to make it transparent, your white color becomes something like #ffffff80 resulting in the greyish color.
i'm wanting my link titles to have a gradient text. currently, when i change the tooltip to be the gradient text, the gradient ends up working fine, but the background is transparent.
i want to have a colored background so you can actually see the text. i understand that the way the gradient is working is by using a gradient background and then cutting out the text to show it, but i think there has to be a way to overlay the whole text over a background.
i'm not sure exactly how to edit tooltips very well, i'm using someone's code and editing it. i'm attaching a picture of what i'm basically trying to do. (the photo is edited because i haven't been able to actually make this happen lmao).
inside of my tooltip, i want to have the gradient text, the color background behind the gradient text, and a border.
there's a picture of what i currently see,
there's a picture of what happens when i change the text to gradient ,
and there's a picture of what i'm trying to get to .
thank you all in advance for any help you can give me!
#s-m-t-tooltip {
font-family:"georgia";
letter-spacing:1px;
text-transform:uppercase;
text-align:center;
position:absolute;
padding:0px 5px 0px 5px;
margin-top:30px;
border:1px solid black;
z-index:9999;
}
edit - added snippet of tooltip code (i'm new at this site so i'm not sure if this will post the code correctly)
One option, embed the tooltip in another div to manage the background:
Updated 3-Aug-22 per #DavidThomas
.col {
display: inline-block;
width: 250px;
padding: 10px;
}
.outer {
position: relative;
}
.note {
font-size: 8pt;
display: inline-block;
line-height: 1.2em;
}
.pos {
position: absolute;
top: 15px;
left: 15px;
border: 1px solid black;
}
.inner {
background-color: white;
}
.kpb {
font-family: "georgia";
font-size: 14pt;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
text-align: center;
padding: 0px 5px;
}
.kpb::before {
content: "Kiwi polar bear";
}
.s-m-t-tooltip {
background: -webkit-linear-gradient(#eee, #333);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.dave-thomas {
color: transparent;
background: linear-gradient(to right, red, orange, yellow, lime, indigo, blue, violet), linear-gradient(to bottom, #FFF, #FFF);
-webkit-background-clip: text, border-box;
}
<div class="col">
<span class="note">Single BG overlaid on white div</span><br>
<div class="outer">
<image src="https://picsum.photos/250/200" />
<div class="pos inner">
<div class="kpb s-m-t-tooltip"></div>
</div>
</div>
</div>
<div class="col">
<span class="note">Per Dave Thomas: Two background layers with paired clipping levels (order matters)</span><br>
<div class="outer">
<image src="https://picsum.photos/250/200" />
<div class="pos kpb dave-thomas"></div>
</div>
</div>
I've read some code and I saw this background-color: transparent.
I don't know what it is, so I searched the internet, but I couldn't find a resource to explain what it means.
What does background-color: transparent mean and/or do?
As the name suggests, background-color: transparent means you can see through the background of the element, i.e. its background color would appear to be identical to the background color seen on its parent element.
Note that this is different from background-color: white, because if the parent element has a background color other than white, given element will have a different color, i.e. white.
Also keep in mind that it is the initial value of background-color property. Meaning, if you do not explicitly specify the background-color, it will take the value transparent.
Here's an example to give you an idea:
.container {
width: 200px;
border: 1px solid black;
}
.container > div {
width: 150px;
margin: 25px;
border: 1px solid black;
height: 50px;
}
.bg-blue {
background-color: aqua;
}
.bg-transparent {
background-color: transparent;
}
.bg-white {
background-color: white;
}
.bg-yellow {
background-color: yellow;
}
<div class="container bg-blue">
<div class="bg-white">White</div>
<div class="bg-transparent">Transparent</div>
</div>
<div class="container bg-white">
<div class="bg-white">White</div>
<div class="bg-transparent">Transparent</div>
</div>
<div class="container bg-yellow">
<div class="bg-white">White</div>
<div class="bg-transparent">Transparent</div>
</div>
If you use background-color:transparent it means, that you don't want to have a background-color for this element. A typical usecase would be: if you are overlapping another element and want to see the underlying element with it's content or color.
Background is used to make the color transparent. Also remember that you can use it in "color", "border" etc.
color:transparent;
border-color:transparent;
background-color:transparent;
I am just playing a little bit around with a landing page.
I just set a red opacity background on "A CATCHY PIECE OF TEXT".
How do I set the width on that the correct way, so I also have the responsive part in mind?
How can I get the font in front, and the red background in the back?
#cathyText {
background-color: red;
padding: 10px;
opacity: 0.1;
}
<h2 class="text-center" id="cathyText">A CATCHY PIECE OF TEXT</h2>
How can I get the font in front, and the red background in the back?
Use rgba():
#cathyText {
padding: 10px;
background: rgba(255,0,0,0.3);
z-index: 1;
}
The reason it works is the first 3 numbers set the red-green-blue of the color, and the last sets the opacity.
If you want to center your <h2> element, one way is to place it in a wrapper and change the display to inline-block:
#cathyText {
background-color: red;
padding: 10px;
background: rgba(255,0,0,0.3);
width:200px;
display:inline-block;
}
.catchy-text-wrapper {
width:100%;
text-align:center;
}
<div class="catchy-text-wrapper">
<h2 class="text-center" id="cathyText">A CATCY PEICE OF TEXT</h2>
</div>
use rgba() for background color
#cathyText {
background-color: rgba(255,0,0,.1);
padding: 10px;
/* opacity: 0.1; */
z-index: auto;
}
I'm trying to have it so that an image of a yellow cog sits inside a div with a yellow border and a white background, and then upon hover the cog changes to white and the background changes to yellow. I'm currently setting the first image as the background of the div, and then using div:hover to change the background on hover, however the spacing etc. doesn't quite work and the border doesn't go around the image properly. Is it possible to have it so the image is inside a link rather than as the background of the div? Here is the code I'm using:
HTML:
<div id="settings">
</div>
CSS:
#settings {
border: 4px solid #ffff00;
padding: 10px 20px 10px 20px;
background: #fff url(img/cog_yellow.png) 0 0 no-repeat;
}
#settings:hover {
background: #ffff00 url(img/cog_white.png) 0 0 no-repeat;
}
Any ideas?
It is possible although not very efficient. You can have two images and show and hide them based on hover:
HTML
<div class="button_link">
<img src="http://www.placecage.com/50/50"/>
<img src="http://www.placecage.com/60/50"/>
</div>
CSS
.button_link a:last-child{
display: none;
}
.button_link:hover a:first-child{
display: none;
}
.button_link:hover a:last-child{
display: block;
}
FIDDLE
If you can post a fiddle recreating the issue with the images you're using there is probably a more efficient way of doing this with just CSS and no additional HTML involved
UPDATE
This is how I would do this, with just CSS:
NEW FIDDLE
You can use this instead:
#settings a{
display:block;
width:100px; /* adjust your width */
height:100px;/* adjust your height */
border: 4px solid #ffff00;
padding: 10px 20px 10px 20px;
background: url(img/cog_yellow.png) 0 0 no-repeat;
}
#settings a:hover {
background: url(img/cog_white.png) 0 0 no-repeat;
}