The concept of transparency in CSS being utilized on a nested div element

Seeking clarity on the opacity property in CSS. My dilemma involves a header with a title, tinted with an opacity of .3 (in addition to a transition effect - irrelevant to the inquiry). Referencing the gif below:

https://i.sstatic.net/LXwPv.gif

I appreciate the translucency offered by the header, allowing visibility of the background image. However, I desire the title itself to display as solid white, at full opacity. Due to the parent div's opacity affecting the header, the text appears faded like the rest of the element. Is there a technique to achieve this? Essentially, is it possible to "override" the opacity of a parent element?

html,
body {
  margin: 0px;
  padding: 0px;
  height: 100vh;
}

#header {
  position: absolute;
  width: 100vw;
  height: 0vh;
  background-color: black;
  opacity: 0;
  z-index: 6;
  transition: height 1s ease, opacity 1s ease;
}

#hoverable {
  position: absolute;
  height: 10vh;
  width: 100%;
  z-index: 7;
}

#hoverable:hover #header {
  opacity: .3;
  height: 10vh;
}

#title {
  margin-left: 10vw;
  line-height: 10vh;
  float: left;
}
<div id="hoverable">
  <div id="header">
    <div id="title">
      <h1>TITLE</h1>
    </div>
  </div>
</div>

Answer №1

To achieve transparency, you can utilize background-color: rgba(0,0,0,0); or

background-color: rgba(0,0,0,.3);

RGBA refers to red green blue alpha.

html,
body {
  margin: 0px;
  padding: 0px;
  height: 100vh;
}

#header {
  position: absolute;
  width: 100vw;
  height: 0vh;
  background-color: rgba(0,0,0,0);
  z-index: 6;
  transition: height 1s ease, background 1s ease;
}

#hoverable {
  position: absolute;
  height: 10vh;
  width: 100%;
  z-index: 7;
}

#hoverable:hover #header {
  background-color: rgba(0, 0,0,.3);
  height: 10vh;
}

#title {
  margin-left: 10vw;
  line-height: 10vh;
  float: left;
}
<div id="hoverable">
  <div id="header">
    <div id="title">
      <h1>TITLE</h1>
    </div>
  </div>
</div>

Answer №2

It seems like you are using IDs for your div elements. Have you considered using classes instead?

In CSS, styles are supposed to cascade, meaning that styles declared later in a stylesheet should override styles declared earlier. However, due to more specific selectors like IDs, this doesn't always happen as expected.

For instance, consider the following HTML:

<div id="element" class="element">
<!-- content goes here... -->
</div>

And corresponding CSS:

#element {
background: blue;
}

body div.element {
background: green;
}

In this scenario, even though the body div.element selector comes after the #element ID selector, the background of the element will be blue, not green. This is because the ID selector is more specific and overrides the natural cascading order.

Learn more about the difference between classes and IDs in CSS here.

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 for the best approach to utilize AngularJS in order to successfully submit a form to my c# backend

In the process of utilizing REST ASP.NET, I am currently in need of extracting certain values from my frontend input form so that I can search for corresponding data in my backend system coded in C#. Despite trying out the suggestions provided in this he ...

CSS tables are not properly recognizing the class values within the table

This is the CSS I am working with: table { margin: 10px 0 20px 0; width: 100%; text-align: center; } table th { color: #38A4FC; padding-top: 5px; background-color: #f9f9f9; width: 200px; height: 30px; border: 1px solid #d7d7d7; border-bottom: none; } ...

Tips for adjusting the font size in table cells based on the content within them

In my HTML table, I have numbers incrementing from 0 in each cell, arranged from left to right and top to bottom. I have set the width and height of the cells using CSS (with dimensions of 40px width and 25px height). As the table grows larger, the numbe ...

suggesting options comparable to addthis or sharethis

Could you please check out ? There is a sharing box in the bottom right corner that resembles ShareThis. However, as far as I know, ShareThis does not have options for embedding or submitting content. Does anyone happen to know which plugin is being used ...

Basic Jquery CSS style requests

Can you help me troubleshoot this issue? var hover = $('<img />').attr('src', hovers[i]).css('position', 'absolute') I'm having trouble getting th ...

What is the best way to maintain the formatting of a textarea in the database?

My HTML form includes a text area and a functionality to save the data into a database upon submission. The issue arises when the saved data is retrieved from the database, as the original formatting of the text gets altered. For instance: "This is a text ...

New to CSS: Applying a background image on top of a gradient without any transparency on the image

Appreciate you taking the time to read this I'm facing an issue where I want to have a background image overlaid on a gradient, but every solution I've tried ends up making the background image transparent, causing a discrepancy in colors. I att ...

What is the best way to grab the initial section of a website's text while using a WKWebView?

When working with a WKWebView, retrieving the webpage's title is simple using webView.title. However, obtaining the first paragraph of the webpage's content poses a challenge with no straightforward solution. Any tips or suggestions on how to ac ...

Is there a way for me to choose the nearest input value when clicking on a href

Is there a way to efficiently select the nearest input value when clicking on a link? I attempted to do so like this, but it returns undefined: $(".change").click(function(e){ e.preventDefault(); var qty = $(this).closest('input').val(); ...

Getting data from a custom radio select in Django view

Yesterday, I dedicated the entire day to customizing a star-rating system that looks like radio buttons. Despite not being very skilled in front-end development, I managed to create what I consider a css masterpiece. However, it appears that my approach to ...

Is there a way to align elements to the left and right within a <div> container?

Having trouble styling my website at . I can't seem to get the images to align with names underneath and bios on the right. My VS Code keeps flagging Float properties as errors, and I need some CSS help from someone more experienced. function conde ...

Is there a way to minimize the spacing between the list item numbers and their respective content?

Is there a way to reduce the spacing between the ordinals and the content? For example, I would like to change from: 1. foo to: 1. foo If so, how can I achieve this? <ol> <li>...content</li> </ol> ...

unable to view bootstrap navigation bar

I can't seem to get my Bootstrap application page in rails with postgresql and turbolinks disabled to work properly. I've tried everything but still can't figure out what's wrong. <!DOCTYPE html> <html> <head> <ti ...

Tips for allowing an HTML form to submit only if the numbers in the fields total to a specific sum

One of my forms is responsible for inserting data into a database. Within this form, there are multiple input fields, with 4 specifically designated for numerical values: <form action="page.php" method="post"> <input type="text" name="Text1"> ...

When using a Jquery $.each loop with DOM, it may not function properly on every element

I am attempting to create an HTML editor with additional markup capabilities in this demonstration, I am utilizing a jQuery $.each loop however, this loop only functions on the final element Here is the code snippet I am using: // Custom Svg Icon ...

What is the best way to relocate an image or link using CSS?

I've been attempting to adjust the position of this image using CSS, but no matter how many times I try, it just refuses to budge. Can someone please help me troubleshoot what might be causing this issue? #yahoo1 { position: absolute; top: 100p ...

Use Jquery to swap out text without the need for a separate update button

Looking to replace "Gastos de envío: " with "Shipping costs" on the English page. I currently have this jQuery code (which is working), but it only replaces the text the first time you visit the page. If you update the shipping costs, the jquery does not ...

When the form is submitted, the value is not refreshed if the button is exited

I need to have a form where the save button is positioned outside of the form elements. However, when I move it elsewhere, the form does not refresh. <form ng-submit="save()" action="" name="addInv" novalidate> <div class="col-md-10 ...

Adding or changing class in Angular 2 based on specific conditions

Currently, my focus is on building a web application using Angular and the Semantic-UI framework. In order to add a single class to an element based on a specific condition, I have successfully implemented the following syntax: [class.className]='co ...

Use CSS to configure the mouse wheel for horizontal scrolling

Looking to create a horizontal page layout, but when I scroll with the mouse wheel the content only moves vertically. Is there a way to enable horizontal scrolling using the mouse wheel? Does CSS have a property for this? For instance, is there something ...