I've selected a white background color, but I'm curious how the opacity of the parent element will impact it

The inner div's background color is set to white with the parent div's opacity at 0.6. However, the inner div doesn't appear exactly white. The issue disappears when the parent div's opacity is changed to 1.0. Why is that happening? http://jsbin.com/zekacunefi/edit?html,output

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<title>Document</title>
<script src="js/jquery.js"></script>
<style>
#msg_container {
position: fixed;
z-index: 3800;
background-color: #000000;
opacity: 0.6;
left: 0;
top: 0;
right: 0;
bottom: 0;
}

#modalDialog {
display: inline-block;
z-index: 3801;
background-color: white;
position: absolute;
border: 1px solid rgb(103, 103, 103);
box-shadow: 0 4px 17px 0px rgba(0,0,0,0.4);
top: 50%;
left: 50%;
}

body {
background-color: blue;
}
</style>
</head>
<body>

<script>
MessageBox("abc\ndef\ng\nhdasfasdfsdsdfsd");
function MessageBox(str) {
var boxHtml = "<div id='msg_container'><div id='modalDialog'></div></div>";
$("body").append(boxHtml);
var md = $("#modalDialog");
var contentArray = str.split("\n");
var newArray = contentArray.map(function(ele, idx) {
return ele + "<br>";
});
md.html("<p>" + newArray.join("") + "</p>");
var w = md.width(),
    h = md.height();
md.css({
marginTop: -1 * h / 2 + "px",
marginLeft: -1 * w / 2 + "px"
});
$("#msg_container").appendTo($("body"));
}
</script>
</body>
</html>

Answer №1

Setting the "opacity" property not only affects the background of a div but also the opacity of its entire content, as seen on this W3C Wiki page: http://www.w3.org/wiki/CSS/Properties/opacity

In my opinion, it would be best to remove the opacity setting from both #msg_container and #modalDialog and instead use rgba() to specify the background color of #msg_container.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<title>Document</title>
<script src="//code.jquery.com/jquery-1.11.3.min.js></script>
<style>
#msg_container {
position: fixed;
z-index: 3800;
background-color: rgba(0,0,0,0.6);
left: 0;
top: 0;
right: 0;
bottom: 0;
}

#modalDialog {
display: inline-block;
z-index: 3801;
background-color: white;
position: absolute;
border: 1px solid rgb(103, 103, 103);
box-shadow: 0 4px 17px 0px rgba(0,0,0,0.4);
top: 50%;
left: 50%;
}

body {
background-color: blue;
}
</style>
</head>
<body>

<script>
MessageBox("abc\ndef\ng\nhdasfasdfsdsdfsd");
function MessageBox(str) {
var boxHtml = "<div id='msg_container'><div id='modalDialog'></div></div>";
$("body").append(boxHtml);
var md = $("#modalDialog");
var contentArray = str.split("\n");
var newArray = contentArray.map(function(ele, idx) {
return ele + "<br>";
});
md.html("<p>" + newArray.join("") + "</p>");
var w = md.width(),
    h = md.height();
md.css({
marginTop: -1 * h / 2 + "px",
marginLeft: -1 * w / 2 + "px"
});
$("#msg_container").appendTo($("body"));
}
</script>
</body>
</html>

Answer №2

The reason for this issue is that the opacity on the parent element is also affecting the inner div, and the blue color is from the body background. To fix this, you can instead use the rgba() function for the parent div.

Give this a try:

#msg_container {
    position: fixed;
    background-color: rgba(0, 0, 0, 0.6);
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
}

Answer №3

Adjusting the opacity of the main element will also affect all its descendants. As a result, the blue background will be visible not only through the parent container but also through its child elements.

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

Having issues with integrating a basic Bootstrap carousel in Angular 8

I am currently using Angular 8 in conjunction with Bootstrap version 4.5 and a Font Awesome kit. The specific component I am working on is called font-project. However, I have encountered an issue when trying to slide images within the carousel. Although t ...

Discover how to apply unique styles to specific HTML elements using their index values in jQuery

In the process of creating a party game similar to Tambola or Housie, I am faced with the challenge of automatically detecting and highlighting the numbers called out on each ticket in the game. For instance, if number 86 is announced, I aim to visually di ...

What is causing this test using promises to fail?

I recently entered the enchanting realm of Promises, feeling as though I had discovered a hidden treasure. However, despite their seeming simplicity, promises can be quite perplexing. Can someone please explain why the test below is failing? var Promise ...

Experiencing difficulties replicating two auto-scrolling divs

I have implemented a script to automatically slide two different divs. Here is the code I am using: The HTML: <div id="gallery"> <div id="slider" style="width: 6000px; left: -500px;"> <div><aside class="widget widget_testimoni ...

Unable to change font using jquery - issue persisting

I am struggling to modify the font of a specific text in the following code, as it keeps displaying a page error. function adjust_font_style(div_id) { $("#testsave").html(<strong onclick=\"javascript:save_font_changes("+div_id+",font,'Ar ...

The implementation of triggering a jQuery function when a link with a dynamic id is clicked

Greetings, I have the following HTML code: <table class="table table-bordered"> <thead> <tr> <th>Docket No. #</th> <th>Del Date</th> <th>Box</th> ...

The appearance of the website varies across different web browsers

My current project involves creating a portfolio showcasing my work, but I've encountered an issue: When you visit my website and click on the "About" link, the text at the bottom of the tab is displayed differently in Chrome compared to IE and Firef ...

React has identified a modification in the sequence of Hooks invoked, however, I am unable to locate any Hooks being invoked conditionally

I am currently utilizing: ReactJS 16.14.0 In my React functional component, I am relying on data stored in context for accurate rendering. Certain data requires additional processing before display, and some data needs to be fetched. However, I am encoun ...

Enhancing DataTable Performance with Django Filters

I have implemented a feature where users can apply filters to customize the data displayed in a Table. When a user interacts with these filters, an asynchronous Ajax call is triggered: $.ajax({ url: '/fund_monitor/fund_directory&a ...

What could be causing the text-end to fail in Bootstrap 5?

Why can't I align the text to the right? Feeling frustrated as a beginner. <div class="top-bar"> <div class="container"> <div class="col-12 text-end"> <p><a href="tel:06 ...

Guide to incorporating jQuery as $ in a Vue.js 3 / Vite project

Looking to integrate jQuery into a Vue.js 3 project with Vite (https://github.com/vitejs/vite). Added jQuery to the package.json dependencies: "dependencies": { "@types/jquery": "^3.5.0", "jquery": "^ ...

Struggling with implementing the group by feature in AngularJS?

Currently, I am trying to group a select-window by 2 different object arrays - subcategories and rootcategories. Each subcategory has a relation id that links to the rootcategory's id. My goal is to have the dropdown window group the subcategories bas ...

AngularJS: Customize form elements based on model type

I need to work with an Angular model that contains ConfigValues. This is essentially a Dictionary object passed from C# which looks something like this: { Name: "Config Name", Value "True", Type: 0 // boolean } Some of these values are boolean, ...

EJS failing to render HTML within script tags

Here is some code that I'm working with: <% // accessing content from a cdn api cloudinary.api.resources( { type: 'upload', prefix: '' }, (error, result) => { const assets = result.r ...

Issue with ASP.NET ConfirmButtonExtender Missing Background in Internet Explorer

Recently, I noticed an issue with the ConfirmButtonExtender on a Cancel button. It was working perfectly fine in Firefox with a semi-transparent background and a simple modal popup. But when I tested it on IE, it showed a generic JavaScript-style alert box ...

Creating an Innovative Grid Design with Varying Column Widths for Each Row

Seeking advice on achieving a specific layout using HTML. I have attempted to use HTML tables but struggled with creating rows with varying columns. Does anyone have recommendations for grid tools that may be more helpful? Thank you ...

What is the best way to ensure that two col-lg-6 elements stay horizontally aligned on mobile screens?

What is the best way to keep two col-lg-6 columns aligned horizontally on mobile devices using Bootstrap? ...

Struggling to create a CSS dropdown menu where the dropdown items refuse to align to the left

I am currently working on creating a dropdown menu to reveal hidden elements on mobile devices such as smartphones. However, I am facing an issue where the child elements of the dropdown menu are appearing to the right of the parent element instead of the ...

implementing ajax with json data type

I am in the process of converting my AJAX script, which previously used text as its datatype, into JSON format. The datatype:"text" was functioning correctly, so I attempted to modify it to this updated script: Within the PHP code: header("Content-type: ...

Incorporating JavaScript into ASP.NET web controls

I am currently working on an ASP.NET webforms application and I've encountered an issue with injecting a script into the page from a web control. The script is located in the same folder as the control: Here is a screenshot showcasing the issue My a ...