Is there a method to transform the lower portion of a background image into a uniform white color?

Hey everyone!

I'm a new user trying to work on my personal project. I was wondering if there's a way to use CSS to turn the bottom half of this image (link: ) completely white. Any tips or suggestions would be greatly appreciated! Thank you!

Answer №1

There may be alternative ways to achieve this effect (such as adjusting the image or container size), but one option could be utilizing a linear-gradient as an additional background image to hide the bottom half.

.sample {
  background-image:
    linear-gradient(transparent 50%, white 50% 100%),
    url('https://images.unsplash.com/photo-1477414956199-7dafc86a4f1a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3870&q=80');
  
  /* Additional styling for display purposes */
  background-size: cover;
  border: 2px solid red;
  aspect-ratio: 1.5/1;
  max-width: 300px;
}
<div class="sample"></div>

Answer №2

There are two simple solutions

  1. Adjust the position of the div following the img tag to be absolute

.post-img{
    position: absolute;
    top: calc(50% - 4px);
    background: red;
    width: -webkit-fill-available;
    height: 50%;
}
img{width:100%}
<html lang="en">
<body>
    <div style="position:relative">
        <img src="https://picsum.photos/id/237/200/300"/>
        <div class="post-img"></div>
    </div>
</body>
</html>


                     OR

  1. This will give you a similar result to what you desire

body {
    height: 100%;
    background: url('https://picsum.photos/id/237/200/300') no-repeat, #f00;
    background-size: 100%;
    background-position-y: 60%;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for excluding a CSS property from a class override

I have a file called _buttons.scss: .btn { display: inline-block; font-family: $btn-font-family; font-weight: $btn-font-weight; color: $body-color; text-align: center; text-decoration: if($link-decoration == none, null, none); white-space: $b ...

Error in the PHP/HTML code

Similar Question: PHP: Dealing with "Undefined variable" and "Undefined index" Notices I'm new to asking questions on this platform. Currently, I'm working on building a basic forum based on this tutorial, but making modifications as needed. ...

The best way to choose an element that lacks a class beginning with a specific prefix

Trying to apply a style for all input elements that do not have a class starting with "border-radius": input:not(class^="border-radius") { This method is not producing the desired result. Any alternative suggestions? ...

Encase every picture within a div and add a numerical label in front of it

I'm working on a function that will wrap each image in a div and add a span label for each image. Here is the code I have written so far: $('#carousel img').each(function(index) { $(this).wrap('<div class="image"></div>& ...

The alarm feature is malfunctioning

I have been struggling with a simple alarm application that I created using jQuery and Javascript. The issue lies in the alarm functionality not working as expected. Below is the relevant code snippet: var today = new Date(); var h = today.getHours(); var ...

Automatically changing the color of the navigation bar text

I am experiencing an issue with the navigation bar on my webpage. Currently, it is styled with white text against a dark background color using the following CSS code snippet: a{ color: white; text-decoration:none; font-weight:bold; font-s ...

Step-by-step guide to creating a clickable div

Having an issue with around 200 clickable buttons on my website, all designed the same: <div> <asp:hyperlink> //or LinkButton </div> The background-style of the button is within the Div and the text of the button is in Hyperlink / LinkB ...

Sorting of boxes is not possible when utilizing the sortable() function

Is there a way to swap the positions of the left and right boxes using the sortable() function from jQuery UI? It seems to work fine for numbers 1 and 2, but not for switching the boxes. $(".sort-me").sortable({ connectWith: ".connectedSortable" } ...

Discover the dimensions of the viewport using jQuery

I am currently attempting to use jQuery to determine the size of the viewport, but I am not achieving the desired results. Here is my CSS code: .site { max-width: 960px; } @media only screen and (min-width : 760px) and (max-width : 1040px) /* table ...

Attempting to create a single function that can be utilized with numerous divs that share the same class in jQuery

Currently, I am working on creating a basic jquery gallery viewer. In my code, I have the following structure: <div class="photoset"> <img /> <img /> </div> <div class="photoset"> <img /> <img /> <i ...

What are some ways to employ ul and li elements to create columns that are consistently aligned, even when some columns may be empty?

In my HTML document, I have multiple sections with unordered lists of items. In a specific ul section, the list looks like this: <ul> <li>item 1</li> <li>item 2</li> <li>ck-item 2</li> <li ...

What could be causing my canvas to not display my sprite?

Currently, I am in the process of learning JavaScript and experimenting with creating games to make the learning experience more enjoyable. However, I am facing an issue while using EaselJS and CreateJS to develop these games as a beginner. I need some as ...

Lower the placement of Glyphicons

I have recently implemented a custom font on my website. Unfortunately, this font is not standard and its alignment is off. As a result, when I use this font with Twitter Bootstrap glyphicon, they do not appear in the same line. Is there a way to adjust t ...

Passing dynamic scope from Angular to a directive is a seamless process

I am working with a directive that retrieves an attribute value from an http call in the following manner: Controller app.controller("HomeController", function($scope){ $http.get("/api/source").then(function(res){ $scope.info = res.data }); }); ...

Tips for adding a gradient to your design instead of a plain solid color

I stumbled upon a snippet on CSS Tricks Attempting to replace the green color with a gradient value, but unfortunately, the value is not being applied. I have tried using both the fill property and gradient color, but neither has been successful. Here is ...

Transforming jQuery into pure Javascript code

UPDATE SUCCESS! I've cracked the code. The solution was to include jQuery by adding it in Joomla through the template/index.php file with this line under "//Add Javascript Frameworks" JHtml::_('jquery.framework');. PROGRESS I'm seekin ...

Transitioning the width in CSS without using the "auto"

Currently, I am experiencing a problem with my CSS animation that is leaping directly from the start to finish. It appears to be connected to a similar issue addressed in this thread. My inquiry is: Is there a way to create a smooth transition in width f ...

Save a specific collection of divs from a webpage as a PDF or Word document

Could the following scenario be achievable? Imagine a webpage containing a series of div elements with unique IDs or classes. Inside each div, there is a title (possibly an h1 tag) and some additional content. Is it possible to create a selection list of t ...

The insertion of the script string from the ajax response was unsuccessful

When working with PHP, I encountered an array with two parts: array('threeRowHtml'=> '<HTML output string>', 'jsCode'=> '<script output string>' ) I decided to return this array for my ajax call. ...

Unable to interact with button within a clickable div

Currently, I am encountering an issue with Chakra UI in React where the functionality of a Box component is being executed instead of a Button when clicked inside it. Despite trying to adjust the z-index, the problem persists. Here's my code snippet: ...