Adjust background shading without influencing the contents - React / Ionic /CSS

I'm currently facing a challenge where I need to darken the background of an image without affecting its contents, much like the example below. The image is generated programmatically, so I can't directly link to it in the CSS.

This is how the desired outcome should look:

https://i.sstatic.net/Xr1HE.png

However, this is how it actually appears for me, and I need the characters to appear much brighter:

https://i.sstatic.net/3wzks.png

I've explored various solutions involving ::before or ::after pseudo-elements, but due to the inline rendering of my image, those methods won't work. Below is my code snippet:

REACT.TSX

{apartments.map(({ name, images, taskStatus }: any, index: number) => (
            <Link key={index} to={`/apartments/${index}`}>
              <div
                className="apartmentImage"
                style={{
                  backgroundImage: `url(${API_IMAGE}${images[0]})`,
                }}
              >
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h5 className="apartmentText">{name}</h5>
                </div>
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h6 className="subApartmentText">MALAGA</h6>
                </div>
              </div>
            </Link>
          ))}

CSS:

.apartmentImage {
  width: 98%;
  margin-left: 1%;
  height: 24.7vh;
  border-radius: 10px;
  margin-top: 3%;
  margin-bottom: -1%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  filter: brightness(0.8);
}

.apartmentText {
  color: white;
  font-weight: bold;
}

If you have any suggestions on how to tackle this issue, I would greatly appreciate it!

Thank you for your assistance!

Answer №1

Here are the steps you can take:

{apartments.map(({ name, images, taskStatus }: any, index: number) => (
            <Link key={index} to={`/apartments/${index}`}>
              <div
                className="apartmentImage"
                style={{
                  backgroundImage: `url(${API_IMAGE}${images[0]})`,
                }}
              >
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h5 className="apartmentText">{name}</h5>
                </div>
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h6 className="subApartmentText">MALAGA</h6>
                <div className='color-overlay'/>

                </div>
              </div>
            </Link>
          ))}

You may also need to add the following CSS rules:

.apartmentImage {
  width: 98%;
  margin-left: 1%;
  height: 24.7vh;
  border-radius: 10px;
  margin-top: 3%;
  margin-bottom: -1%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;

}

.apartmentText {
  color: white;
  font-weight: bold;
}


.apartmentText, .subApartmentText {
  position: relative;
  z-index: 1;
}

.color-overlay{
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0,0,0,.3);
  pointer-events: none;
}

I have personally tested this method and it worked, but I made some adjustments on my end to load an image. Please do let me know if you encounter any issues so that I can assist further.

Answer №2

Did you attempt implementing this using CSS?

background-image: linear-gradient( rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ),

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

I am looking to reposition the navbar span tag so that it appears beneath the icon, utilizing both HTML and CSS

I'm currently working on a navbar design with 5 items, each containing an icon and a span tag. However, I'm facing difficulties in aligning the span tag below the icon for all 5 items in the navbar. Despite my best efforts, I have been unable to ...

Adjust the height of a div according to the width using CSS in a progressive manner

I'm looking to create a fluid container that adjusts its height gradually based on the width of the window (as the div's width is set to 100%). Similar to the behavior achieved with the aspect-ratio CSS rule, I want the height to smoothly increas ...

Exploring Conditional Rendering with React Components

Attempting my first unit test and need to validate rendering in a modal or dialog based on conditions. After exploring various resources, I couldn't find a solution similar to my case. I referred to a course by Maximilian Schwarzmüller on Udemy for ...

What is the reason my function is only operating with a single ID?

I'm attempting to create color-changing "squares" that change color when clicked. The first square changes color correctly, but when I click on the others, the color change only happens on the first square. var image_tracker = 'red'; fu ...

Chrome - Display Dilemma with Redrawing/Recoloring on Show/Hide

Encountering an unusual problem in Chrome with the navbar-brand element of my website's bootstrap navigation menu. We feature a larger site logo in the header section, along with a smaller logo inside the navbar-brand element which is initially set t ...

Using React's contenteditable feature in a stateless component

I have been attempting to incorporate a contenteditable div within a stateless react component. However, I am consistently encountering the following warning: warning.js:36 Warning: A component is `contentEditable` and contains `children` managed by Reac ...

Ensure child images in a flex row are adjusted to have a height equivalent to 100% of the surrounding content

Consider this example: .row { background: #F88; margin-top: 20px; } .cont { display: block; height: 82px; width: 82px; background-color: #0AF; } .txt { background-color: #0C0; } <link href="https://stackpath.bootstrapcdn.com/bootstr ...

Can you determine the height of an image if only the width and border width are provided?

Currently delving into the world of HTML and CSS, I've successfully configured the img element's width and border width using a style element. However, one aspect that is still puzzling me is how the height is calculated in this scenario. I&apos ...

Using React JS and Redux to make an API call prior to accessing any route

Greetings, I am currently working on a single-page application using React and Redux, and I've encountered an issue. In my application, the admin has the ability to open or close the app from the admin panel. If the app is closed by the admin, users ...

Is there a way I can add opacity to a div without impacting the other elements contained in it?

When I set the opacity of a div, it seems to affect all the other elements inside that div by making them transparent too. However, I am looking for a solution where the child elements do not inherit the opacity of their parent. Any assistance on this is ...

Loading production assets from a user's local host instead of an Ubuntu server in Rails

After successfully deploying my Rails application to an Ubuntu 16.04 Droplet, I encountered a problem with the CSS when running the server locally. The website could be accessed at myappname.com through the browser as long as the rails server was active, b ...

Circular graphs displaying percentages at their center, illustrating the distribution of checked checkboxes across various categories

Looking for a JavaScript script that displays results in the form of circles with percentage values at their centers, based on the number of checkboxes checked in different categories. The circle radius should be determined by the percentage values - for e ...

What is the best way to pass parameters to child functions in a React component?

Looking to add a ripple effect in React using the react-water-wave package? Check out this repository: https://github.com/xxhomey19/react-water-wave While I can successfully call child methods without parameters, I'm running into issues when trying t ...

Tips for preventing line breaks within table cells

Looking at the image below, I need to display the Hallticket in a single line. The first one is retrieved directly from the database, while the second one is added dynamically using JavaScript. How can I show both sets of data in a single line? https://i. ...

Incorporate a map (using leafletjs or Google Maps) as a subtle backdrop

I am currently working on a one-page website and I would like to include a map as a background behind the "contact" section. The map can be set to float, draggable, or positioned at the back. I have experience using both the Google Maps API and LeafletJS, ...

What is the best way to resize dynamically according to the text content?

Currently working on a Rails3 + jQuery project that involves integrating Facebox. Encountering an issue where the title text in Facebox is too large for a single line, causing it to spread across two lines and look unattractive. I'm curious if there& ...

What are the steps to customize the color of my navigation bar?

Currently, my website has a navigation bar with links that turn white after being clicked or visited, but I am struggling to make them white initially. Here is the code snippet for my HTML: <nav> <ul> <li><a href="index.html" ...

Setting up the Bootstrap grid structure

Struggling to find the right configuration for this Bootstrap 3 setup... https://i.stack.imgur.com/ZsTdI.png I have an input field, but I'm having trouble placing the image in that position. <div class="row"> <div class="col-sm-4 col-m ...

What is the reason behind the React useState hook's inability to work in a for loop?

My curiosity is piqued by the unexpected behavior of setting state in a for loop. Below is my code utilizing hooks: const [counter, setCounter] = useState(0); const updateCounter = () => { for(let i=0; i<100; i++){ setCounter(i); } ...

Leveraging React Router v5 along with Code Splitting and Server Side Rendering for optimized Data Prefetching

For a current project, I am using react-router v3 specifically for server-side rendering with data prefetching. The most efficient way to achieve this is by maintaining a centralized route configuration in either an object or an array, and iterating throug ...