Opacity for only a part of background image - CSS - css

Do you maybe know how (and if) I can add an opacity for the background image but only to PART of it?
The effect should be like this one: https://i.stack.imgur.com/HYvaU.png.
I have only added the image as a background but I cannot find any solution for this oppacity.
My HTML:
<header>
<img src="images/logo.svg" />
<h1>A history of everything you copy</h1>
<p>
Clipboard allows you to track and organize everything you copy.
Instantly access your clipboard on all your devices.
</p>
</header>
And CSS:
body {
font-family: "Bai Jamjuree", sans-serif;
text-align: center;
}
#media (min-width: 1200px) {
header {
width: 100%;
background-image: url(images/bg-header-desktop.png);
background-size: 100%;
background-repeat: no-repeat;
padding-top: 50px 150px;
}
}
h1 {
color: hsl(210, 10%, 33%);
font-size: 35px;
margin-bottom: 15px;
}
header > p {
color: hsl(201, 11%, 66%);
font-size: 18px;
}
Thank you in advance!
I have tried to use mask-image but it didn't work:
#media (min-width: 1200px) {
header {
width: 100%;
background-image: url(images/bg-header-desktop.png);
mask-image: linear-gradient(180deg, rgba(0, 0, 0, 1), transparent 74%);
background-size: 100%;
background-repeat: no-repeat;
padding-top: 50px 150px;
}
}
Do you have maybe any idea if I can give an opacity only for the bottom part of this background image using CSS?

With more than 1 background, you can put image in 1 and opacity on the other.
You can change 2nd background color as you want. It's opacity value is given by the RGBA background color (here 0.75 in the snippet).
body {
margin: 0;
padding: 0;
}
.wrapper {
position: absolute;
width: 100vw;
height: 50vh;
top: 50%;
transform: translateY(-50%);
font-size: 10em;
font-weight: bold;
font-family: sans-serif;
background: url("https://picsum.photos/id/22/1280/600");
}
.wrapper1 {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
clip-path: inset(50% 0 0 0);
background-color: rgba(255, 255, 255, 0.75);
display: flex;
justify-content: center;
align-items: center;
}
.wrapper2 {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
z-index: 2;
}
<div class="wrapper">
<div class="wrapper1"></div>
<div class="wrapper2">Hello World!</div>
</div>
Look at the snipper in full scree, for this demo I put width 100vw, so in small result is "strange"

Related

Are there any way to change color of part of a string?

I'am beginner at frontend, and got some design-layout to train. Designer expects that on hover part of string or even letter will change color Example
I thought about CSS 'clip', but doubt
I change the snippet. Play with font-size.
body {
margin: 0;
padding: 0;
}
.wrapper {
position: absolute;
width: 100vw;
height: 50vh;
top: 50%;
transform: translateY(-50%);
font-size: 3em;
font-weight: bold;
font-family: sans-serif;
background-color: red;
}
.wrapper1 {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
clip-path: inset(0 50% 0 0);
background-color: blue;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper2 {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
clip-path: inset(0 0 0 50%);
background-color: green;
display: flex;
justify-content: center;
align-items: center;
}
<div class="wrapper">
<div class="wrapper1">Hello World!</div>
<div class="wrapper2">Hello World!</div>
</div>
As G-Cyrillus has pointed out, background-clip with value text can be used, it will 'cut out' characters from the background.
In this simple snippet the background is half white, half black and the blue/white background is supplied in a pseudo before element.
Note that the property requires a -webkit- prefix in some browsers.
* {
margin: 0;
}
div::before {
background-image: linear-gradient(to right, blue 0 50%, white 50% 100%);
content: '';
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
div {
position: relative;
width: 100vw;
height: 500px;
font-size: 50px;
text-align: center;
rmix-blend-mode: difference;
rcolor: white;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
background-image: linear-gradient(to right, white 0 50%, black 50% 100%);
}
<div>Hello how are you?</div>
So, Thanks for your help, A Haworth , G-Cyrillus! I think, I've found the solution. I experimented with background-clip:text, but in my case it was excess, but I used mix-blend-mode, thanks. I've found an article Taming Blend Modes: difference and exclusion, where explained filter:invert(1). Tried to show in snippet. When hover the cell part of title change color to white. But color of title and hovering background should be the same.My realized layout from designer
.block {
width: 100%;
height: 200px;
padding-top: 10px;
position: relative;
filter: invert(1);
}
h1 {
position: relative;
color: #091C91;
text-align:center;
font-size: 2rem;
z-index: 5;
mix-blend-mode:difference;
filter: invert(1);
}
.list {
display: flex;
justify-content: center;
border: 1px solid red;
height: 130px;
width: 100%;
position: absolute;
top: 0;
left: 0;
}
.column {
color: white;
flex: 0 1 25%;
border: 1px solid black;
filter: invert(1);
}
.column:hover {
background: #091C91;
}
<div class="block">
<h1>Snippet for Inverting colores</h1>
<div class="list">
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
<div class="column">Column 4</div>
</div>
</div>

Text filled with blurred background image

I'm trying to create a title that has a glass looking visual which blurs the background image.
I was able to do it but I had to make references to the background image 3 times and it doesn't seem very optimal.
Here is the code I used.
body {
background: blue url("https://images5.alphacoders.com/318/318370.jpg") no-repeat center center fixed;
background-size: cover;
}
.bimage {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: url("https://images5.alphacoders.com/318/318370.jpg") no-repeat center center fixed;
filter: blur(10px);
background-size: cover;
}
.title {
position: relative;
width: 100%;
height: 500px;
overflow: hidden;
}
h2 {
font-family: Verdana;
position: absolute;
top: 0;
left: 0;
//display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 500px;
margin: 0;
font-weight: bold;
font-size: 18vw;
text-align: center;
text-transform: uppercase;
mix-blend-mode: screen;
}
.mask {
background: #fff;
}
#h2 {
background: url("https://images5.alphacoders.com/318/318370.jpg") no-repeat center center fixed;
color: rgba(255, 255, 255, 1);
background-size: cover;
mix-blend-mode: multiply;
-webkit-text-stroke-width: 1px;
-webkit-text-stroke-color: rgba(255, 255, 255, 0.4);
text-shadow: 3px 3px 10px black;
}
<div class="title">
<div id="bimage" class="bimage"></div>
<h2 class="mask">MARIO</h2>
<h2 id="h2">MARIO</h2>
</div>
https://codepen.io/marceltoma/pen/zYWGYwp
I wonder if there's a better way to do this.
Looking at your codepen, you can start reusing css in many places.
This is just an example for you to get started:
/* Reusing background properties */
#h2, body, .bimage {
background: url("https://images5.alphacoders.com/318/318370.jpg") no-repeat center center fixed;
background-size: cover;
}
/* Reusing with children selector */
.title, .title * {
width: 100%;
position: absolute;
top: 0;
left: 0;
}
body {
background-color: blue
}
.bimage {
height: 100%;
filter: blur(10px);
}
.title {
position: relative;
height: 500px;
overflow: hidden;
}
h2 {
font-family:Verdana;
//display: flex;
align-items: center;
justify-content: center;
height: 500px;
margin: 0;
font-weight: bold;
font-size: 18vw;
text-align: center;
text-transform: uppercase;
mix-blend-mode: screen;
}
.mask {
background: #fff;
}
#h2 {
color: rgba(255,255,255,1);
mix-blend-mode: multiply;
-webkit-text-stroke-width: 1px;
-webkit-text-stroke-color: rgba(255, 255, 255, 0.4);
text-shadow: 3px 3px 10px black;
}
<div class="title">
<div id="bimage" class="bimage"></div>
<h2 class="mask">MARIO</h2>
<h2 id="h2">MARIO</h2>
</div>
Please give it a try, and keep DRY!

How to create a slanted Progress Bar using CSS?

I'm making progress bar with CSS. I want to make the end of the section inside the bar oblique. How can I do as pictured?
.progress-bar{
height: 34px;
background: #0C0C0C;
display: flex;
flex: 1 auto;
width: 100%;
position: relative;
}
.progress-bar div{
width: 69%;
background: #1EA614;
height: 100%;
}
.progress-bar div span{
position: absolute;
font-size: 24px;
width: 100%;
left: 0;
justify-content: center;
align-items: center;
display: flex;
height: 100%;
}
<div class="progress-bar">
<div>
<span>Level 5</span>
</div>
</div>
Here is a simplified version with less of code where you can easily control the curve, the color and the percentage of the progress
.progress-bar {
--s:20px; /* define the curve (make bigger to increase the curve, smaller to reduce)*/
--p:50; /* percentage of the progress without unit */
--c:#1EA614; /* color */
height: 34px;
line-height:34px;
background:
linear-gradient(to bottom right,var(--c) 49%,transparent 50%)
calc(1%*var(--p) + var(--p,0)/100*var(--s)) 0 / var(--s) 100%,
linear-gradient(var(--c) 0 0)
0 / calc(1%*var(--p)) 100%,
#0C0C0C;
background-repeat:no-repeat;
text-align: center;
font-size: 24px;
color:#fff;
margin:5px;
}
<div class="progress-bar">
Level 5
</div>
<div class="progress-bar" style="--p:30;--c:red;--s:10px">
Level 5
</div>
<div class="progress-bar" style="--p:70;--c:lightblue;--s:40px">
Level 5
</div>
<div class="progress-bar" style="--p:100;--s:40px">
Level 5
</div>
You could use a gradient to accomplish this, as I do below.
The only line that I've changed is
background-image: linear-gradient(105deg, #1EA614 0%, #1EA614 85%, transparent 85%);
Essentially, we declare a gradient background that's on an angle which roughly lines up with the photo you gave. Then, we set the stops like so:
At 0% (all the way to the left) it's fully green;
85% of the way through we ensure that it's still fully green. This means that there is no graduation between the original green and the black, and thus that we get a sharp transition.
85% of the way through (so directly after) we make it black. Because this is so close to the previous stop, the transition between them is instant, and we get the effect you're looking for.
Note that this number, 85%, does need some tweaking to make sure that the cutoff is the same all of the way through.
Here's your demo again, but with this code added in. I've also added an animation, so that you can see it works at all width stages of the bar.
.progress-bar{
height: 34px;
background: #0C0C0C;
display: flex;
flex: 1 auto;
width: 250px;
position: relative;
}
.progress-bar div{
width: 100%;
background-size: cover;
background-position: -250px 0;
background-repeat: no-repeat;
background-image: linear-gradient(105deg, #1EA614 0%, #1EA614 96%, transparent 96%);
height: 100%;
animation: bar 2s linear infinite;
}
.progress-bar div span{
position: absolute;
font-size: 24px;
width: 100%;
left: 0;
justify-content: center;
align-items: center;
display: flex;
height: 100%;
}
#keyframes bar {
0% {
background-position: -250px 0;
}
100% {
background-position: 0 0;
}
}
<div class="progress-bar">
<div>
<span>Level 5</span>
</div>
</div>
If you're having issues with it not filling the entire bar, you could try just moving the gradient across the bar, rather than changing the bar's width. I've updated my example to do this.
References:
https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
.progress{
height: 34px;
background: #0C0C0C;
display: flex;
flex: 1 auto;
width: 100%;
position: relative;
}
.progress .progress-bar{
width: 60%;
background: #1EA614;
height: 100%;
position: relative;
overflow: hidden;
}
.progress span{
position: absolute;
font-size: 24px;
width: 100%;
left: 0;
justify-content: center;
align-items: center;
display: flex;
height: 100%;
}
.progress .progress-bar:after{
content: "";
position: absolute;
top: -1px;
right: -6.5px;
height: 115%;
width: 13px;
background: #0C0C0C;
transform: rotate(20deg);
}
<div class="progress">
<div class="progress-bar"></div>
<span>Level 5</span>
</div>

Unable to add image in the bg with the background-image property-CSS

I'm using the background-image prop to get an image in the bg and a text on the foreground:
fiddle:
https://jsfiddle.net/zvy0j3r1/5/
however I dont see any image getting displayed. i'm not sure what I'm I missing here
CSS:
.main {
padding: 40px 70px;
display: inline-block;
width: 100%; //customizable user controlled width (not necessarily be 100% all time)
color: #AFBEC6;
text-align: center;
border: 3px solid #E7ECEE;
background-color: #F7F8F9;
}
.icon {
background-image: url(https://mdn.mozillademos.org/files/7693/catfront.png);
width: 100%;
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.text {
font-size: 24px;
position: relative;
top: -18px;
}
Just set the .main as relative and .icons as absolute.
.main {
padding: 40px 70px;
display: inline-block;
width: 100%;
color: #AFBEC6;
text-align: center;
border: 3px solid #E7ECEE;
background-color: #F7F8F9;
position: relative;
}
.icon {
background-image: url(https://mdn.mozillademos.org/files/7693/catfront.png);
width: 100%;
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: absolute;
left: 0;
top: 0;
}
.text {
font-size: 24px;
position: relative;
top: -18px;
}
<div class="main">
<div class="icon"></div>
<div class="text">No Data available</div>
</div>
The background image is not showing because the element doesn't have any height. You might think that using height: 100% to the element, would make it take up the same height of it's parent, but it doesn't work like that.
When a child element has height: 100%, it will only take up 100% of it's parent if the parent has an explicit height set, like with pixels, ems, vm, etc.

CSS z-index property doesn't work

I'm trying to give an hover effect to a div (a background picture) with the "filter" property. But it has got an influence on another div (my text) which is supposed to be over.
I applied the z-index property (without forgetting the position of my divs) but it doesn't work. You'll see, my text is blurring too and is not supposed to. Could you tell me please what's wrong with my code ?
Here is the codepen showing my problem : https://codepen.io/Gillian-D/pen/qmqEQy
HTML
<div class="container">
<div class="row">
<div class="col-sm-6 left_part-padding">
<div class="left_part">
<div class="left_part_content">
Left part
</div>
</div>
</div>
</div>
CSS
.container {
margin-right: auto;
margin-left: auto;
}
.left_part {
height: 40vh;
position: relative;
z-index: 0;
background-image: url(img/book2.jpeg);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
background-color: #000;
box-shadow: 0 50px 60px 0 rgba(0,0,0,.35);
border-radius: 10px;
}
.left_part:hover {
-webkit-filter: blur(5px);
filter: blur(5px);
-webkit-transition: .5s ease-in-out;
position: relative;
z-index:0;
}
.left_part_content:hover {
color: #fed136;
position: relative;
z-index: 2;
}
.left_part-padding, .right_part-padding, .bottom_part-padding {
padding-left: 10px;
padding-right: 10px;
}
.left_part_content {
color: white;
font-family: "Raleway", Helvetica, Arial, sans-serif;
text-transform: uppercase;
font-size: 1.2rem;
font-weight: 400;
letter-spacing: .300em;
margin-right: -.300em;
text-align: center;
vertical-align: middle;
line-height: 40vh;
position: relative;
z-index: 2;
}
Thanks a lot!
When you add an effect on an element, in most cases their children gets affected as well, so in this case, you could use a pseudo element for the background-image
Updated codepen
Changed CSS rules
.left_part {
height: 40vh;
position: relative;
background-color: #000;
box-shadow: 0 50px 60px 0 rgba(0,0,0,.35);
border-radius: 10px;
}
.left_part::before {
content: '';
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
background-image: url(http://lorempixel.com/500/200/nature/1/);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
background-color: #000;
border-radius: 10px;
}
.left_part:hover::before {
-webkit-filter: blur(5px);
filter: blur(5px);
}
Have a look here:
https://codepen.io/anon/pen/EmNPVZ
I took the text part element out of the to-be-blurred background div, now both have the same parent. Next I solved the centering differently, the usual way (absolute position, top and left 50%, transform: translate(-50%, -50%). I then applied pointer-events: none; to the text layer so the hover below it could word. And I added a different hover rule that affects the text - see my codepen. HTH

Resources