2-Layer Method For Image Overlays For Translucent Coloring

I found this page

Tthe 2nd and 3rd boxes of code are supposed to be a different method for layering translucent color. The example they have is supposed to be the footer on the page.

I put their code in a txt file

head
style
.element-with-background-image {
    background-image:
    linear-gradient(rgba(red, red, red, 0.6), rgba(red, red, red, 0.6)), 
    url(https://upload.wikimedia.org/wikipedia/commons/1/1d/Martinique_Beach_%28Salines%29.jpg)  }```

/style
/head

body
div class="element-with-background-image"
/div
/body
/html

and ran the file through Lint5 and got no errors, renamed the file to html and got nothing to show in a browser. Can anyone see why the code above won’t create anything?

Thanks,

Chris

Why did you rename the CSS to html instead of to css?

Write the code in a working page so it will demonstrate something, preferably whatever it is supposed to do or almost do.

It would make it much easier to read the code in your post if you wrote it and formatted it correctly. You can highlight your code, then use the </> button in the editor window, which will format it, or you can place three backticks ``` (top left key on US/UK keyboards) on a line before your code, and three on a line after your code.

1 Like

Hi there Chris77,

here is your code, amended so as to meet the prevailing conditions. :mask:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>untitled document</title>
<!--<link rel="stylesheet" href="screen.css" media="screen">-->
<style media="screen">
.element-with-background-image {
    width: 60%;
    padding-top: 45%;
    margin: auto;
    background: linear-gradient(rgba(255, 0, 0, 0.1), rgba(255, 0, 0, 0.6)),
                url(https://upload.wikimedia.org/wikipedia/commons/1/1d/Martinique_Beach_%28Salines%29.jpg);
    background-size: 100% auto;
 }
</style>
</head>
<body>
 <div class="element-with-background-image"></div>
</body>
</html>

coothead

2 Likes

Thanks Coothead!! I went through your code taking stuff out to see where my code failed and foud that what my code needed was

padding-top: 45%;

Why ie this necessary?

<!DOCTYPE html>
<html lang="en">
<head>

<style media="screen">
.element-with-background-image {

 width: 60%;   
    background: linear-gradient(rgba(255, 0, 0, 0.6), rgba(255, 0, 0, 0.1)),
                url(https://upload.wikimedia.org/wikipedia/commons/1/1d/Martinique_Beach_%28Salines%29.jpg);
    background-size: 100% auto;
 }
</style>
</head>
<body>
 <div class="element-with-background-image"></div>
</body>
</html>

Hi there Chris77,

[quote]padding-top: 45%;

Why is this necessary? [/quote]

The containing element does not expand vertically for “background-image”. :mask:
So to a method to contain it responsively is required. :sunglasses:

The image in question has an aspect ratio of 4:3, so setting “padding-top”
to 45% with a “width” off 60% will maintain that aspect ratio as the window
width is increasesdor decreased.

I trust, hopefully, that this explanation will suffice. :ok_hand:

coothead

p.s.

Your code…

background-image:
linear-gradient(rgba(red, red, red, 0.6), rgba(red, red, red, 0.6))

…was also a cause of fatal failure. :scream:

2 Likes

Coots,
You write, "The containing element does not expand vertically for “background-image”.
why does the containing eleemnt (the div) need to expand (vertically) for the background image and why doesn’t it expand for the background-image and why does something simple like adding a little padding to the top of the div get the background-image to expand.

I know your coding is correct because it works, but adding a little padding to get it to work seems unlikely to have worked since padding just adds blank space and doesn’t seem to expand images - it doesn’t create space that an image would be allowed to fill, or does it?.

Thanks

Hi there Chris77,

Contrarily to your thoughts, it does. :sunglasses:

The CSS “background-image” unlike the HTML “img element”
has absolutely no effect on the element that contains it.

To make the containing element display a “background-image”
fully, requires that it’s dimension matches the aspect ratio of the
image that is to be used as the background.

You image’s dimension is 2,304×1,728 and the aspect ratio is 4:3.

One way to to use this information to set the dimension of the
containing element, is to first set an apppropriate percentage width
to it and then calculate 3/4 of that value for the “padding-top” value.

You are probably wondering why “padding-top” is chosen instead of
“height”. :no_mouth:

Well, that is because an element’s percentage width and padding are
both related to it’s container’s width - in this instance the “body element”.

Using “background-size:100% auto” ensures that the “background-image”
will now fit exactly into the space that has been so carefully prepared for it. :ok_hand:

coothead

4 Likes

No kidding. I would have never thought that. Thanks so much Coothead.

I am very sorry, but I was just kidding. :sunglasses:

coothead

2 Likes

I was working on keeping the image full size because it becomes too small when the browser is narrowed a lot. I tried removing the “auto” part on background-size but it didn’t work. The only other thing I can think of that could create variablility is percent and if I remove width: 60%; padding-top: 45%; I end up with a blank page, what I had for the same reason at the beginning of this thread. Is a scaling image inherent some how with this layering technique?

Thanks,

Chris.

Hi there Chris77,

we are not tied to just using percentage values. :mask:

Fixed values can also be employed and then, at
strategic positions, the values can be changed. :sunglasses:

Here is a one possible scenario…

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>untitled document</title>
<!--<link rel="stylesheet" href="screen.css" media="screen">-->
<style media="screen">
.element-with-background-image {
    width: 48em;
    padding-top: 36em;
    margin: auto;
    background: linear-gradient(rgba(255, 0, 0, 0.1), rgba(255, 0, 0, 0.6)),
                url(https://upload.wikimedia.org/wikipedia/commons/1/1d/Martinique_Beach_%28Salines%29.jpg);
    background-size: 100% auto;
 }
@media screen and (max-width: 50em) {
.element-with-background-image {
    width: 40em;
    padding-top: 30em;
  }
 }
@media screen and (max-width: 42em) {
.element-with-background-image {
    width: 32em;
    padding-top: 24em;
  }
 }
@media screen and (max-width: 34em) {
.element-with-background-image {
    width: 24em;
    padding-top: 18em;
  }
 }
@media screen and (max-width: 26em) {
.element-with-background-image {
    width: 16em;
    padding-top: 12em;
  }
 }
</style>
</head>
<body>
 <div class="element-with-background-image"></div>
</body>
</html>

coothead

1 Like

Chris, if you are still having a problem understanding coothead’s post #5 example and subsequent explanations, I have taken the liberty of modifying his example slightly for the purpose of clarifying the math.

Instead of demonstrating the efficient use of one box does all, I have created a second box.

An outer container, .wrapper, is used to set the width of the inner image box, and thus the display.

The width of that inner box is therefore 100% by default. It doesn’t really have to be mentioned in the CSS.

The height of the inner box is determined by a percent padding-top setting because vertical padding is computed based on the width of the parent container… my reason for using a .wrapper as a parent container is simply that the padding-top in percent is very easy to calculate based on a width of 100% and the width of the display can be changed independently from the width of the page without needed to recalculate the percent padding top.

To use this example,
(1) open it in your browser and see a red dashed line near the top of the page.
(2) change the width of the browser window and see the width of the red line remain 60% of the width of the page.
(3) delete the commented line to reveal the inner box as remarked.
(4) experiment with any values that you wish to see how that inner box is affected.
(5) delete the next commented line as remarked to add the background-image and gradients. (don’t overlook deleting the closing comment mark just above the last curly brade).

Hope this helps If it doesn’t, then we will need to see more of the context surrounding your image.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
    <title>constant aspect ratio container</title>
<!--
https://www.sitepoint.com/community/t/2-layer-method-for-image-overlays-for-translucent-coloring/235774/5
Chris77
code by coothead + ron
-->
    <style media="screen">
.wrapper {
    width:60%;  /* This determines the width of the image box. */
    margin:0 auto;  /* This centers the image box on the page.  Optional. */
    outline:1px dashed red;  /* Temporary Outline for testing purposes.  To Be Deleted. */
}

/* delete this line including the open comment mark after expeimenting with the .wrapper box and its red dashed line.  Deleting this line will reveal the inner box.

.element-with-background-image {
    width:100%;  /* This container will be the same width as the .wrapper container */
    padding-top:75%;  /* padding-top gives this inner box a responsive height.  Vertical padding is computed based on the width of the containing element, .wrapper in this case.  A percent value creates a box with a constant aspect ratio. 75% = 4:3 */

/* above, we created an inner box with a predictable aspect ratio.  Experiment with browser width and percent height as desired to see how the height of the box is affected.  Delete this line and the closing comment mark above the closing brace when ready to add the background image, slider, or vid :eek: with the 4:3 aspect ratio.

    background:linear-gradient(rgba(255, 0, 0, 0.1), rgba(255, 0, 0, 0.6)),
               url(https://upload.wikimedia.org/wikipedia/commons/1/1d/Martinique_Beach_%28Salines%29.jpg);  /* 2304x1728 original image dimensions from which the 4:3 aspect ratio is determined */
    background-size:100% auto;
*/
}
    </style>
</head>
<body>

<div class="wrapper">
    <div class="element-with-background-image"></div>
</div>

</body>
</html>
4 Likes

Let me work on these a bit. Thanks Coot and Ron.

Using px instead of % is the answer, and the px, of course, is the diimensions of my image. But now I’m spoiled. I like the image large when the browser is large, but can I set a minimum image size for when the width and padding-top start makng the image too small? I’d rather not use another media query. I tried min-width bit it had no effect.

Using px instead of % where?

Can you post a working page with the code that you are happy with, please? I need to get my head around what you have decided works best for you before I can answer your min-width question.

<!DOCTYPE html>
<html lang="en">
<head>

<style media="screen">
.element-with-background-image {
padding-top: 405px; 
 width: 540px;   
    background: linear-gradient(rgba(255, 0, 0, 0.6), rgba(255, 0, 0, 0.1)),
                url(https://upload.wikimedia.org/wikipedia/commons/1/1d/Martinique_Beach_%28Salines%29.jpg);
    background-size: 100% auto;
 }
</style>
</head>
<body>
 <div class="element-with-background-image"></div>
</body>
</html>

(width 60% was changed to 540px, padding top 45% changed to 405px)
It would be nice if the image grew larger than these px dimensions when the browser is wide, but when the browser
is made narrow the image never becomes smaller than these px dimensions. Can it be done with a minimum size function, maybe one within a media query

@media when width less than 541px don’t use 60% use width 540px, padding top 405px

Thanks

But that will create horizontal scrollers on small screens. Surely width: auto would be better.

I tried it, on the code juist above, and as the browser narrowd the image became smaller and smaller.

That’s responsive. If the element stays at 540px and the screen is only 320px it creates scrolls, that’s bad.
If you want the background image to not shrink too much, set the background-size to what you want in the query, but make its container auto to fit on the screen properly.