AllThingsSmitty / css-protips

⚡️ A collection of tips to help take your CSS skills pro 🦾
Creative Commons Zero v1.0 Universal
28.12k stars 2.16k forks source link

How do I overlay an image over the background one ? #167

Closed mohakexe closed 8 months ago

NarpatAanjana commented 1 year ago

You can use a

element for this purpose. and following CSS .container { background-image: url('background-image.jpg'); position: relative; }

.overlay { position: absolute; top: 0; left: 0; z-index: 1; }

MegRCooper commented 1 year ago

Hi, I've created a Word doc with a few different examples of an overlay with CSS. Quick Overview (CSS Overlay).docx

DonMiller9294 commented 1 year ago

You can overlay an image over a background using CSS by using the position property and the ::before or ::after pseudo-elements. Here's a basic example:

HTML:

<div class="container">
  <img src="overlay-image.jpg" alt="Overlay Image">
  <p>Content goes here</p>
</div>

CSS:

.container {
  position: relative;
}

.container::before {
  content: "";
  background: url('background-image.jpg');
  background-size: cover;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.5; /* Adjust the opacity as needed */
  z-index: -1; /* Place the overlay behind the content */
}

In this example, the ::before pseudo-element is used to create the overlay. Adjust the background property with the path to your background image, and you can also adjust the opacity to control the overlay's transparency.

Make sure to adjust the paths to your actual image files, and customize the styles to match your design.

happy315 commented 11 months ago

The HTML will look like this:

 <header>
        <section class='hero-header'>
            <h1>Software development</h1>
            <h2>Javascript developer</h2>
            <button>Hire Me</button>
        </section>
    </header>

The css will look like this


 header {
        height: 100vh;
        width: 100%;
        text-align: center;
        padding-top: 100px;
        color: white;
        overflow: hidden;
        background: #c04848; /* fallback for old browsers */
        background: linear-gradient(rgb(72, 0, 72, 0.8), rgb(192, 72, 72, 0.8)),
          url("/luke-peters-B6JINerWMz0-unsplash.jpg");
        background-size: cover;
        background-repeat: no-repeat;
      }
      header h1,
      h2 {
        margin: 2rem;
        font-size: 3rem;
      }
      header button {
        padding: 1.5rem;
        border-radius: 0.2rem;
        background-color: white;
        border: none;
        font-size: 1.5rem;
        color: rgb(72, 0, 72, 0.8);
        font-weight: bold;
      }

The result will look like this

Screenshot 2023-09-25 at 11 00 30 PM