How to 'append' the background with more styles in CSS? - html

I need a way to 'append' the style to the same div in css. I know this can be done with jQuery, but I wonder if it'll be possible just using the stylesheet.
My div has a class .myClass, and then later I give it a custom attribute "customAttr". I need background image to be put on top of gradient.
In the example below, I actually specify attribute and class at the same time. In the real thing though, I need to be able to add it at a later point, such that image will appear on top of the previously visible background. Also, I would have many more backgrounds specified to accommodate for older browsers.
http://jsfiddle.net/mgboss/eyw4v82f/3/
div {
height: 70px;
width: 70px;
}
div[customAttr="hello"] {
background-image: url("http://s22.postimg.org/5cvkclhi5/bolt.png");
}
div.myClass {
background: background: linear-gradient(red, blue);
}
Thanks!

CSS gradients are also values of background-image (even if specified with background shorthand), so even with correct specificity of selectors you will have to specify both picture and gradient as multiple background:
div {
height: 70px;
width: 70px;
}
.myClass[customAttr="hello"] {
background: url("http://s22.postimg.org/5cvkclhi5/bolt.png"), linear-gradient(red, blue);
}
.myClass {
background: linear-gradient(red, blue);
}
<div class="myClass" customAttr="hello"></div>
Alternatively, you can add extra images with pseudo elements:
div {
height: 70px;
width: 70px;
position: relative;
}
.myClass[customAttr="hello"]::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background: url("http://s22.postimg.org/5cvkclhi5/bolt.png");
}
.myClass {
background: linear-gradient(red, blue);
}
<div class="myClass" customAttr="hello"></div>

You can do it this way too..
div.myClass {
background: url("http://s22.postimg.org/5cvkclhi5/bolt.png") no-repeat,
linear-gradient(red, blue) ;
}
http://jsfiddle.net/umairorana/6mc2v7mx/

Is this the kind of effect you're looking for?
http://jsfiddle.net/ben220/e3912d78/
HTML:
<body>
<div></div>
</body>
CSS:
div {
height: 350px;
width: 500px;
position: relative;
background: linear-gradient(red, blue);
}
div::after {
content:"";
background: url("tree.jpg") no-repeat;
opacity: 0.5;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
The only way I could think of to do this was to add opacity to the background image but there seems to be no conventional way to do this in CSS. You can however use the ::after property was a work-around. This basically allows you to insert content into your webpage without changing the HTML. In this example I've only included one div in the HTML code. The other, which contains the background image, is purely done with css.
I hope this helps.
Ben

Related

How to make background image opacity change without the entire body div changing?

I have created an ID for my HTML body called #indexbody. I put a background image with CSS using background-image:url("hs2.webp");. Because I Have done it this way, is there a way to change the background opacity of my image without dimming the entire body?
CSS:
#indexbody{
background-image:url("hs2.webp");
background-size: 100% auto;
}
If you put the background-image on the before pseudo image rather than the actual body element you can set its opacity down without that affecting the whole body element.
Here's a simple snippet:
body {
width: 100vw;
min-height: 100vh;
margin: 0;
}
body::before {
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url(https://picsum.photos/id/1015/300/300);
background-size: cover;
opacity: 0.4;
position: absolute;
}
<body></body>
There are many different ways to do this. One of the most common is using a pseudo-element. In this case, I used :after to create the background color overtop of the picture then used z-index to make sure my absolutely positioned text elements are layered ahead of the pseudo-element.
#indexbody {
background-image: url("https://dummyimage.com/600x400/000/fff");
/* background-color: rgba(0, 0, 0, .5); --> solution without using psuedo-element */
background-size: cover;
background-position: center;
width: 100%;
height: 500px;
position: relative;
}
#indexbody:after {
content: "";
position: absolute;
background-color: orange;
opacity: .5;
inset: 0;
}
p {
position: absolute;
color: white;
background-color: blue;
z-index: 1;
text-align: center;
}
<div id="indexbody">
<p>absolutely positioned element overtop, unaffected by opacity</p>
</div>
Using the :before or :after CSS pseudo-elements, you apply the div with a background image and set an opacity on it.

How to achieve this low opacity gloomy light for background with HTML and CSS?

I've seen people make designs like this for their website. As you can see those two low opacity blue lights, one at the top right and the other at the bottom left. I am wondering how are they making this in HTML and CSS? I can make PNG out of this, but is there a way that can be done with HTML and CSS? I think it would load faster than a PNG file. Thank you in advance. :)
I tried using this code.
HTML:
<body>
<div></div>
</body>
CSS:
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
div {
width: 100%;
height: 100%;
background-color: #191b1f;
}
div::after,
div::before {
content: "";
display: inline-block;
position: absolute;
background: hsl(199, 56%, 18%);
width: 300px;
height: 300px;
border-radius: 50%;
filter: blur(70px);
mix-blend-mode: lighten;
}
div::before {
top: 0;
right: 0;
transform: translate(50%, -50%);
}
div::after {
top: 50%;
left: 0px;
transform: translateX(-50%);
}
/*With gradient background*/
div {
background: radial-gradient(
circle closest-corner at center 125px,
hsl(199, 56%, 18%),
#191b1f 70%
)
no-repeat;
}
Result:
For this method, the normally used styling is the backdrop filter method. By using that, you can create a frosted glass effect in CSS. First you should create a main div and then a sub div which we should create the backdrop effect. The method which I follows is:
Find a picture with similarity to the background.
Then reduce the brightness of the background image using filter: brightness(48%); and then I use the backdrop-filter: blur(5); to the sub div.
This is the exact same method which I was following for the past few months.

Using an SVG as a background for a specific portion of a website

I'm trying to use an SVG as a background for a specific section on a website I'm creating, but I'm finding it difficult. The main problem I'm having is the SVG covers the text, but in the image, I'll link below the SVG is behind the text.
This is the image
This is some code I wrote which didn't work.
.icons_and_text {
margin-top: 100px;
position: relative;
}
.icons_and_text::before {
content: '';
background-image: url('./images/bg-curvy-desktop.svg');
background-size: cover;
position: absolute;
top: -600px;
left: 10;
height: 400px;
width: 100%;
}
I'm looking for answers which would enable me to solve the problem on my own next time, thank you.
Hope it works for you
header .container {
position: relative;
z-index: 1;
}
This seems to work, although it's not exactly what I want. What I want is a perfect way to position the image.
header.header {
background-color: hsl(217, 28%, 15%);
background-image: url(../images/bg-curvy-desktop.svg);
background-position: center bottom;
background-repeat: no-repeat;
background-size: 100% 23rem;
}

How to clip inside a box in css?

I have a background image of many colors. Header is a white box and i need to write text over this box. So what happens is through this box, text is clipped and background is seen through this white box. How to do this?
You cab do that with background clip: text, but you have only support in webkit
CSS
body {
height: 100%;
background: linear-gradient(0deg, red,white, blue, pink, yellow, green);
}
h3 {
background-image: inherit;
-webkit-background-clip: text;
color: transparent;
font-size: 80px;
top: 0px;
position: relative;
margin-top: 0px;
padding: 0px;
}
h3:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
background-color: white;
z-index: -1;
}
fiddle
Simply use this css code on box:
#box_over {background-color:rgba(255,255,255,0.1);}
Where is 0.1 there is background opacity. You can increase or decrease opacity.
I got your point! Its simple! You can do this with images.
Imagine there is a background colors, and you simply put another div style with image which contains white background with see-trough text! Use photoshop ;)
And also, check this css3 how to do multiple backgrounds in one div:
#box_over{
background:url(clipped_text.png), url(background.png);
background-size:100% 100%;
background-repeat:no-repeat;
}

How to overlay image with color in CSS?

Objective
I want a color overlay on this header element. How can I do this with CSS?
Code
#header {
/* Original url */
/*background: url(../img/bg.jpg) 0 0 no-repeat fixed;*/
background: url(https://fakeimg.pl/250x100/) 0 0 no-repeat fixed;
height: 100%;
overflow: hidden;
color: #FFFFFF
}
<header id="header">
<div class="row">
<div class="col-xs-12">
...
</div>
</div>
</header>
You should use rgba for overlaying your element with photos.rgba is a way to declare a color in CSS that includes alpha transparency support. you can use .row as an overlayer like this:
#header {
background: url(../img/bg.jpg) 0 0 no-repeat fixed;
height: 100%;
overflow: hidden;
color: #FFFFFF
}
.row{
background: rgba(39,62,84,0.82);
overflow: hidden;
height: 100%;
z-index: 2;
}
You can do that in one line of CSS.
background: linear-gradient(to top, #3204fdba, #9907facc), url(https://picsum.photos/1280/853/?random=1) no-repeat top center;
You can also modify the opacity of a color by hovering over it in VS Code and clicking on it to make it a hex color. It can be shortened to (#3204fde6, #9907fae6) instead of the rgba (rgba(48, 3, 252, 0.902), rgba(153, 7, 250, 0.902).
header {
height: 100vh;
width: 100%;
color: white;
font: bold 6.5em/2em monospace;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(to top, #3204fdba, #9907facc), url(https://picsum.photos/1280/853/?random=1) no-repeat top center;
}
<header>Hello World</header>
See here CodePen
You may use negative superthick semi-transparent border...
.red {
outline: 100px solid rgba(255, 0, 0, 0.5) !important;
outline-offset: -100px;
overflow: hidden;
position: relative;
height: 200px;
width: 200px;
}
<div class="red">Anything can be red.</div>
<h1>Or even image...</h1>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a" class="red"/>
This solution requires you to know exact sizes of covered object.
You could use the hue-rotate function in the filter property. It's quite an obscure measurement though, you'd need to know how many degrees round the colour wheel you need to move in order to arrive at your desired hue, for example:
header {
filter: hue-rotate(90deg);
}
Once you'd found the correct hue, you could combine the brightness and either grayscale or saturate functions to find the correct shade, for example:
header {
filter: hue-rotate(90deg) brightness(10%) grayscale(10%);
}
The filter property has a vendor prefix in Webkit, so the final code would be:
header {
-webkit-filter: hue-rotate(90deg) brightness(10%) grayscale(10%);
filter: hue-rotate(90deg) brightness(10%) grayscale(10%);
}
Here's a creative idea using box-shadow:
#header {
background-image: url("apple.jpg");
box-shadow: inset 0 0 99999px rgba(0, 120, 255, 0.5);
}
What's happening
The background sets the background for your element.
The box-shadow is the important bit. It basically sets a really big shadow on the inside of the element, on top of the background, that is semi-transparent
To add an overlay, you can use the CSS background-blend-mode property something like this:
#header {
background: url("img/image.jpg") 0 0 no-repeat fixed;
height: 100%;
overflow: hidden;
background-color: hsl(206, 27%, 38%);
background-blend-mode: multiply;
}
#header.overlay {
background-color: SlateGray;
position:relative;
width: 100%;
height: 100%;
-webkit-opacity: 20%;
opacity: 0.20;
z-index: 2;
}
Something like this. Just add the overlay class to the header, obviously.
Use mutple backgorund on the element, and use a linear-gradient as your color overlay by declaring both start and end color-stops as the same value.
Note that layers in a multi-background declaration are read much like they are rendered, top-to-bottom, so put your overlay first, then your bg image:
#header {
background:
linear-gradient(to bottom, rgba(100, 100, 0, 0.5), rgba(100, 100, 0, 0.5)),
url(../img/bg.jpg) 0 0 no-repeat fixed;
height: 100%;
overflow: hidden;
color: #FFFFFF
}
You can also add an additional class with such settings. Overlay will not overlap content and no additional tag is needed
.overlay {
position: relative;
z-index: 0;
}
.overlay::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: red;
opacity: .6;
/* !!! */
z-index: -1;
}
https://codepen.io/zeroox003/pen/yLYbpOB
If you don't mind using absolute positioning, you can position your background image, and then add an overlay using opacity.
div {
width:50px;
height:50px;
background: url('http://images1.wikia.nocookie.net/__cb20120626155442/adventuretimewithfinnandjake/images/6/67/Link.gif');
position:absolute;
left:0;
top:0;
}
.overlay {
background:red;
opacity:.5;
}
See here: http://jsfiddle.net/4yh9L/
In helpshift, they used the class home-page as
HTML
<div class="page home-page">...</div>
CSS
.home-page {
background: transparent url("../images/backgrounds/image-overlay.png") repeat 0 0;
background: rgba(39,62,84,0.82);
overflow: hidden;
height: 100%;
z-index: 2;
}
you can try similar like this
If you want to just add a class to add the overlay:
span {
padding: 5px;
}
.green {
background-color: green;
color: #FFF;
}
.overlayed {
position: relative;
}
.overlayed::before {
content: ' ';
z-index: 1;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: #00000080;
}
.stand-out {
position: relative;
z-index: 2;
}
<span class="green overlayed">with overlay</span>
<span class="green">without overlay</span>
<br>
<br>
<span class="green overlayed">
<span class="stand-out">I stand out</span>
</span>
Important: the element you put the overlayed class on needs to have a position set. If it doesn't, the ::before element will take the size of some other parent element. In my example I've set the position to "relative" via the .overlayed rule, but in your use case you might need "absolute" or some other value.
Also, make sure that the z-index of the overlayed class is higher than the ones of the eventual child elements of the container, unless you actually want for those to "stand out" and not be overlayed (as with the span with the stand-out class, in my snippet).