Using the & operator in Scss - html

HTML:
<div id="carousel" class="slider__container">
<div class="slider__slide">
<span>Slide One</span>
</div>
<div class="slider__slide--active">
<span>Slide Two</span>
</div>
<div class="slider__slide">
<span>Slide Three</span>
</div>
</div>
SCSS:
body {
background-color: #7e57c2;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.slider__container {
background: red;
min-height: 250px;
width: 200px;
position: relative;
}
.slider__slide {
background: blue;
position: absolute;
height: 100%;
width: 100%;
opacity: 0;
&--active {
opacity: 1; // Why doesn't this inherit the background colour and all other styles?
}
}
I don't know if I'm using the & operator the wrong way, but if you look at the styling for slider__slide and the &--active inside of it. Why doesn't &--active not inherit all the other styles defined in slider__slide?
You can checkout the codepen here

Because there are two different classes, .slider__slide and .slider__slide--active.
You have to inherit parent class in this case
.slider__slide {
background: blue;
position: absolute;
height: 100%;
width: 100%;
opacity: 0;
&--active {
#extend .slider__slide;
opacity: 1;
}
}
or another way, you have to use two classes for modifying element:
<div class="slider__slide slider__slide--active"

This is not how it works. Applying the & operator means, the additional class has to be set as well! It's not a string concatenation.
so this code:
.slider__slide {
&.--active {
opacity: 1;
}
}
will be converted into this css:
.slider__slide.--active {
color: blue;
}
which will apply to this markup code:
<!--markup with two classes 'slider__slide' and '--active'-->
<div class="slider__slide --active">
but NOT this markup code:
<!--markup with single class 'slider__slide--active'-->
<div class="slider__slide--active">
Also see the updated pen

You need to add the full name in active and it will work
.slider__slide {
background: blue;
position: absolute;
height: 100%;
width: 100%;
opacity: 0;
&.slider__slide--active {
opacity: 1;
}
}

For simple explanation
SCSS
.some-class {
width: 100px;
height: 200px;
&--modified {
height: 300px;
}
}
This will be analog of SCSS code in CSS
.some-class {
width: 100px;
height: 200px;
}
.some-class--modified {
height: 300px;
}
If you use only ".some-class--modified" in HTML obviously it will not add "width" from ".some-class" because they are completely different classes.

It's because you have applied background: blue; on class .slider__slide and not on .slider__slide--active.
You should be using parent class as well and then the modifier with it
<div class="slider__slide slider__slide--active"> so it inherits the parent styles as well.

use &:active inside the class in which you want to use active.
.slider__slide {
background: blue;
position: absolute;
height: 100%;
width: 100%;
opacity: 0;
&:active {
//styles when active
}
&:hover{
// styles when hovered
}
}

Related

How do I show an ::after element above everything regardless of overflow and opacities of filters of siblings and nephews

So I have an element that I want to show a tooltip for. But of course, to show this tooltip, I need to make it go above everyhing, no matter what (including overflow: hidden parents). Right now I'm making this happen with the :hover::after rule, but the element's been hidden beneath siblings with a backdrop-filter: blur(...) style.
Minimal reproducable example:
.nephew {
backdrop-filter: blur(10px);
background-color: #0005;
width: 100px;
height: 100px;
}
.nephew.tooltip {
position: relative;
}
.tooltip:hover::after {
content: 'This is a very long tooltip';
background-color: white;
z-index: 999999999;
position: absolute;
width: max-content;
}
.main {
display: flex;
}
<div class="main">
<div class="nephew"></div>
<div class="nephew tooltip"></div>
<div class="nephew"></div>
</div>
Please tell me how to fix this problem. I'm also open to new solutions, since I'm aware this is a bodgy way of implementing this feature.
You need to bring the container div forward too and not just the pseudo element.
Include this CSS and it should now work fine:
.nephew:hover {
z-index: 99999999;
}
Updated example:
.nephew {
backdrop-filter: blur(10px);
background-color: #0005;
width: 100px;
height: 100px;
}
.nephew:hover {
z-index: 99999999;
background-color: #0004; /* just added this to highlight the container */
}
.nephew.tooltip {
position: relative;
}
.tooltip:hover::after {
content: 'This is a very long tooltip';
background-color: white;
z-index: 999999999;
position: absolute;
width: max-content;
}
.main {
display: flex;
}
<div class="main">
<div class="nephew"></div>
<div class="nephew tooltip"></div>
<div class="nephew"></div>
</div>
One approach to solving this might be to apply to the ::after pseudo-element something like:
transform: translateY(-24px)
to raise the tooltip above the row of squares so that it isn't automatically truncated by the next square in the row.
Working Example:
.main {
display: flex;
width: 600px;
margin-top: 24px;
}
.nephew {
position: relative;
width: 100px;
height: 100px;
background-color: rgb(0, 0, 127);
}
.nephew.tooltip {
background-color: rgb(63, 63, 127);
}
.nephew::after {
content: '';
position: absolute;
top: 0;
left: 0;
background-color: white;
white-space: nowrap;
}
.nephew.tooltip:hover::after {
content: 'This is a very long tooltip';
transform: translateY(-20px);
}
<div class="main">
<div class="nephew tooltip"></div>
<div class="nephew"></div>
<div class="nephew tooltip"></div>
<div class="nephew"></div>
<div class="nephew tooltip"></div>
<div class="nephew"></div>
</div>

:hover to change other element, but it triggers from the target as well [duplicate]

This question already has answers here:
CSS disable hover effect
(14 answers)
Closed 3 years ago.
I have a problem. I have made a targeted hover with .trigger:hover .target {} .. my problem though is that it triggers from the .target as well..
.trigger {
width: 300px;
height: 300px;
background: #444;
position: relative;
}
.target {
position: absolute;
width: 100%;
height: 100%;
background: #111;
transform: translate3d(100%, 0, 0);
}
.trigger:hover .target {
border-radius: 50%;
}
<div class="trigger">
<div class="target"></div>
</div>
Example: https://jsfiddle.net/Ls4ejw60/
how does one prevent this? .. i have seen it being done with this method of .trigger:hover .target {}, but i haven't figured out how to prevent it from triggering off the target.
You can use pointer-events: none; on the .target:
.trigger {
width: 300px;
height: 300px;
background: #444;
position: relative;
}
.target {
position: absolute;
width: 100%;
height: 100%;
background: #111;
transform: translate3d(100%, 0, 0);
pointer-events: none;
}
.trigger:hover .target {
border-radius: 50%;
}
<div class="trigger">
<div class="target"></div>
</div>
Another option is to move it out of the .trigger, and use the adjacent sibling combinator (+), but then you'll have to define it's size explicitly, and position it beside the trigger in another way (I've used display: flex; on the body):
body {
display: flex;
}
.trigger, .target {
width: 300px;
height: 300px;
}
.trigger {
background: #444;
position: relative;
}
.target {
background: #111;
}
.trigger:hover + .target {
border-radius: 50%;
}
<div class="trigger"></div>
<div class="target"></div>

CSS "Glass Shadow" Effect

I'm trying to achieve this shadow effect, where the shadow is like a blurred version of the image itself:
I was able to achieve it by stacking the same image twice and applying filter: blur(20px) to the one under, but that feels like an inefficient way of doing it:
.cover-wrapper {
position: relative;
margin: 50px;
width: 200px;
height: 200px;
}
.cover {
z-index: 1;
border-radius: 10px;
position: absolute;
width: 200px;
height: 200px;
}
.cover-shadow-wrapper {
position: absolute;
filter: blur(20px);
overflow: hidden;
height: 200px;
width: 200px;
}
.cover-shadow {
transform: scale(1.5)
}
<div class="cover-wrapper">
<img src="https://picsum.photos/200/200?image=871" alt="cover" class="cover">
<div class="cover-shadow-wrapper">
<img src="https://picsum.photos/200/200?image=871" alt="shadow" class="cover-shadow">
</div>
</div>
Is there a more efficient way of doing this? Also, what is this effect actually called?
does it have to be an image tag?
you can use a single div and its ::after pesudo, give them both the same background image and attributes, then blur the ::after, you'd have a single element in html which is simpler
.glassy-img, .glassy-img::after {
background-image: url(https://picsum.photos/200/200?image=871);
border-radius: 10px;
background-size: cover;
background-position: center;
width: 200px;
height: 200px;
}
.glassy-img::after {
content: '';
filter: blur(20px);
display: block;
position: relative;
z-index: -1;
}
<div class="glassy-img"></div>

CSS circle with inner circle and image

I'm trying to achieve the following:
A background circle with a smaller colored circle inside of it, which must be centered
A small centered image inside of both circles
All of these items needs to be placed in a single div
I'm trying to do this with the minimum amount of code. I want to avoid duplication as much as possible. I believe that all of this can be achieved using before and after selectors, but I'm not sure how to get this done
Here's what I have so far:
CSS:
.grid-container {
display: grid;
grid: 100px / 100px;
}
.circle {
border-radius: 50%;
width: 100%;
height: 100%;
background-color: #e4e4e7;
}
.circle:before {
content: "";
border-radius: 50%;
width: 80%;
height: 80%;
top: 10%;
left: 10%;
background-color: blue;
display: block;
position: relative;
}
.image-one:before {
content: url("https://stackoverflow.com/favicon.ico");
}
.circle-01 {
grid-column: 1 / 2;
grid-row: 1 / 2;
}
HTML:
<div class="grid-container">
<div class="circle-01 circle image-one"></div>
</div>
I need a structure whereby I can easily change the color of the inner circle and/or image
Example
<div class="grid-container">
<div class="circle-01 circle image-one yellow"></div>
<div class="circle-01 circle image-two blue"></div>
<div class="circle-01 circle image-three green"></div>
</div>
You can do it with a pseudo element like this, putting the pseudo element on top of the main element and using borders and a background-image. You can even use a background color behind the image if it doesn't fill the whole pseudo element (note the no-repeat, the size and position settings for the background):
.x1 {
width: 300px;
height: 300px;
position: relative;
border-radius: 50%;
border: 10px solid #22f;
margin: 30px;
background: yellow;
}
.x1:after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 220px;
height: 220px;
border-radius: 50%;
border: 6px solid #f22;
background: #3d3 url(http://placehold.it/200x200/fa0/?text=this_is_an_image) center center no-repeat;
background-size: 100px 100px;
}
<div class="x1"></div>
Note: the orange square is an image, the green color around it is the background color, the red circle is the border of the pseudo element, the yellow area is the background color of the main element and the blue circle is the border of the main element. Each of these could as well be white or transparent.
ADDITION after additional question in comment:
You can also change the background-colors by adding seperate classes. In the following snippet I added two classes to the div, one that affects the background in the main element and one that affects the background-color of the pseudo element. In the latter case you have to make sure to use the background-color property, not background in the CSS rule - otherwise the background-image would disappear:
.x1 {
width: 300px;
height: 300px;
position: relative;
border-radius: 50%;
border: 10px solid #22f;
margin: 30px;
background: yellow;
}
.x1:after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 220px;
height: 220px;
border-radius: 50%;
border: 6px solid #f22;
background: #3d3 url(http://placehold.it/200x200/fa0/?text=this_is_an_image) center center no-repeat;
background-size: 100px 100px;
}
.aqua-outer-bg {
background: aqua;
}
.pink-inner-bg:after {
background-color: pink;
}
<div class="x1 aqua-outer-bg pink-inner-bg"></div>
Note: The original CSS rules remained unchanged, their background colors are overwritten by the additional classes.
ONE MORE ADDITION after additional question in comment from OP on September 18th:
Yes, you can also split that in two classes as I did below (.x1a and .x1b). I simply added both classes to the HTML tag and split up the CSS from x1:after into two rules, one for .x1a:after and one for .x2a:after
.x1a {
width: 300px;
height: 300px;
position: relative;
border-radius: 50%;
border: 10px solid #22f;
margin: 30px;
background: yellow;
}
.x1a:after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 220px;
height: 220px;
background: #3d3 url(http://placehold.it/200x200/fa0/?text=this_is_an_image) center center no-repeat;
background-size: 100px 100px;
}
.x1b:after {
border-radius: 50%;
border: 6px solid #f22;
}
.aqua-outer-bg {
background: aqua;
}
.pink-inner-bg:after {
background-color: pink;
}
<div class="x1a x1b aqua-outer-bg pink-inner-bg"></div>
Try running this snippet:
$(document).ready(function() {
var sourceIndex = 1;
var colorIndex = 1;
var colors = [
"rgb(0, 132, 203)",
"rgb(255, 192, 203)",
"rgb(50, 192, 103)",
"rgb(255, 165, 0)"
];
var sources = [
"https://www.linkedin.com/favicon.ico",
"https://www.google.com/favicon.ico",
"http://jsfiddle.net/favicon.ico",
"https://getbootstrap.com/favicon.ico",
"https://www.facebook.com/favicon.ico"
];
$("button").click(function() {
changeStuff($(this).hasClass("changeImage") ? sources : colors, $(this));
function changeStuff(list, selector) {
counter(list, selector);
if (list == sources) {
selector
.prev()
.prev(".outer-circle")
.find(".inner-circle")
.find("img")
.attr("src", list[sourceIndex]);
} else {
if (
selector
.prev(".outer-circle")
.find(".inner-circle")
.css("background-color") == colors[colorIndex]
) {
selector
.prev(".outer-circle")
.find(".inner-circle")
.css("background-color", "tan");
} else {
selector
.prev(".outer-circle")
.find(".inner-circle")
.css("background-color", colors[colorIndex]);
}
}
}
});
function counter(list, selector) {
if (list == sources) {
sourceIndex == list.length - 1 ? (sourceIndex = 0) : sourceIndex++;
} else {
colorIndex == list.length - 1 ? (colorIndex = 0) : colorIndex++;
}
}
});
.container {
display: flex;
align-items: center;
flex-direction: column;
}
.box {
display: flex;
}
.inner-circle {
border-radius: 50%;
width: 80%;
height: 80%;
display: flex;
align-items: center;
justify-content: center;
}
.box:first-child .inner-circle {
background-color: blue;
}
.box:nth-child(2) .inner-circle {
background-color: black;
}
.box:nth-child(3) .inner-circle {
background-color: maroon;
}
.outer-circle {
border-radius: 50%;
width: 100px;
height: 100px;
background-color: #e4e4e7;
display: flex;
justify-content: center;
align-items: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="box">
<div class="outer-circle">
<div class="inner-circle">
<img src="https://stackoverflow.com/favicon.ico" alt="">
</div>
</div>
<button class='changeColor'>Change Color</button>
<button class='changeImage'>Change Image</button>
</div>
<div class="box">
<div class="outer-circle">
<div class="inner-circle">
<img src="https://stackoverflow.com/favicon.ico" alt="">
</div>
</div>
<button class='changeColor'>Change Color</button>
<button class='changeImage'>Change Image</button>
</div>
<div class="box">
<div class="outer-circle">
<div class="inner-circle">
<img src="https://stackoverflow.com/favicon.ico" alt="">
</div>
</div>
<button class='changeColor'>Change Color</button>
<button class='changeImage'>Change Image</button>
</div>
</div>
Abracadabra
div {
border-radius: 50%
}
#a {
display: flex;
justify-content: center;
align-items: center;
height: 64px;
width: 64px;
border: 2px solid green;
}
img {
align-self: auto;
border: 2px solid blue;
border-radius: 50%;
padding:5%;
}
<div id="a">
<img src="https://rack.pub/media/janus.png" height="48">
</div>

CSS text not changing color

So when I tried to put a white color on some text it did not change in the browser. When I user inspect on google chrome it shows that the text is "color: rgb(34, 34, 34);" When in the code I put "color:white;"
HTML:
<div id="features">
<div id="features-filter"></div>
<div id="features-text">
<h1>Features</h1>
</div>
</div>
CSS:
#features {
background: url("img/server-room.jpg") no-repeat center;
background-size: cover;
position: relative;
height: 70%;
z-index: 99;
}
#features-filter {
position: absolute;
background: url("img/dot.png") repeat;
z-index: 999;
width: 100%;
height: 100%;
}
#features-text {
position: absolute;
z-index: 999;
}
#features-text h1 {
color: white;
}
It could be a caching issue.
Alternatively, there might be a rule within your CSS (or another style sheet) that defines the style of the element after you change the color to white.
When you inspect it, where is the "color: rgb(34, 34, 34);" style defined from? Is it defined from the #features-text h1 that you've written, or is it being defined by another rule, elsewhere in the stylesheet (or another stylesheet?) If it's being defined elsewhere - find out where and make sure this rule isn't overwriting the style you've defined.
Or, if it's being applied by a style sheet that you can't access/edit and you need to define it in your style sheet specifically, try using:
#features-text h1 {
color: white !important;
}
Well here it works try to set it important maybe something else in your code overwrites it
body{background: gray;}
#features {
background: url("img/server-room.jpg") no-repeat center;
background-size: cover;
position: relative;
height: 70%;
z-index: 99;
}
#features-filter {
position: absolute;
background: url("img/dot.png") repeat;
z-index: 999;
width: 100%;
height: 100%;
}
#features-text {
position: absolute;
z-index: 999;
}
#features-text h1 {
color: white !important;
}
<div id="features">
<div id="features-filter"></div>
<div id="features-text">
<h1>Features</h1>
</div>
</div>
Something like this should do it. Obviously I changed the color to blue.
<!DOCTYPE html>
<html>
<head>
<div id="features">
<div id="features-filter"></div>
<div id="features-text">
<h1>Features</h1>
</div>
</div>
<style>
#features {
background: url("img/server-room.jpg") no-repeat center;
background-size: cover;
position: relative;
height: 70%;
z-index: 99;
}
#features-filter {
position: absolute;
background: url("img/dot.png") repeat;
z-index: 999;
width: 100%;
height: 100%;
}
#features-text {
position: absolute;
z-index: 999;
}
#features-text h1 {
color: blue;
}</style>
</html>
Its always better to use Hex Code instead of color name. This way, you make sure every browser supports your code:
#features-text h1 {
color: #FFFFFF !important;
}
Cheers
Wrap your css code in style tags, then it'll work.
<style>
#features {
background: url("img/server-room.jpg") no-repeat center;
background-size: cover;
position: relative;
height: 70%;
z-index: 99;
}
#features-filter {
position: absolute;
background: url("img/dot.png") repeat;
z-index: 999;
width: 100%;
height: 100%;
}
#features-text {
position: absolute;
z-index: 999;
}
#features-text h1 {
color: white;
}
</style>
You can change text color by using this css code
h1{
color:#FFF;
}
try hex code and add !important:
h1{
color:#FFFFFF !important;
}
the hex code have a RGB value. it's like RR-GG-BB.
On normal number, it's count like this: 0123456789
You can count hex number like this: 0123456789ABCDEF