CSS transitioning can be tricky when trying to fade an image or video with a solid background alongside an HTML element that shares the same background color

When attempting to apply a fade in/out effect to an image or video with a solid background color and using the same transition for the opacity of an html element with a matching background color, a noticeable "square" appears around the image/video.

Why do they appear differently when using the same settings?

Below is an excerpt from my code:

.bluebg {
  position: relative;
  padding: 30px;
  background: gray;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 100px;
}

.bluebg:after {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  content: '';
  background: #325795;
  opacity: 0;
  visibility: hidden;
}

.bluebg video {
  position: absolute;
  z-index: 1;
  opacity: 0;
  visibility: hidden;
}

.bluebg video,
.bluebg:after {
  transition: all 2s;
}

.bluebg:hover video,
.bluebg:hover:after {
  opacity: 1;
  visibility: visible;
}
<div class="bluebg">Hover me
  <video playsinline autoplay muted loop width="300" src="https://lionmusicals.dk/xaskepot/wp-content/uploads/glitterlogo-blue.webm"></video>
</div>

You can view the full code sample here: https://codepen.io/WeCU/pen/zYJJNQV

Answer №1

This issue likely occurs due to the video container overlapping the :after element, creating a dual transition effect. One way to address this is by applying the transition only to the encompassing div since the video is contained within it.

Furthermore, the background color of the div does not perfectly align with the video's background. A shade like #3a5791 may be a closer match (although not an exact one).

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

Fixed top navigation bar that remains sticky while keeping the logo at a consistent size

I stumbled upon this code snippet: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <nav class="navbar navbar-default navbar-fixed-top"> <div class="container"> <div class=" ...

The submission of the Jquery form is not successful

I am struggling with a form on my page. I want to disable the submit button and display a custom message when submitted, then use jQuery to actually submit the form. <form> <input type="text" name="run"/> <input type=&quo ...

What is the best way to create a line break in a flex div container to ensure that overflowing items wrap onto the next line using

Using material-ui popper to display a list of avatars. Trying to arrange the avatars horizontally within the popper. <Popper style={{ display: 'flex', maxWidth: '200px', }}> <div style={{ marginRight: '20px' }}&g ...

Dynamic transition when selecting a navigation bar link in Bootstrap

Check out my website here: On my webpage, I have 3 navigation links which are: Who We Are What We Do Contact Us I have set up the "a href" tag to link to each section of the page when clicked. My query is, how can I create a sliding effect when clicki ...

Using media queries in CSS to create responsive designs

<link type="text/css" rel="stylesheet" media="only screen and (max-device-width: 480px)" href="/assets/css/phone.css" /> <link type="text/css" rel="stylesheet" media="only screen and (min-device-width: 768px)" href="/assets/css/tablet.css" /&g ...

Using AJAX to send data with a POST request in Django may not function properly

Let me preface by saying I have searched for solutions online, but none of them seem to address my specific issue (mainly because they use outdated methods like Jason). I am currently working on a Django project and trying to implement ajax for a particul ...

Unique float structure within a division of divs

Can divs be floated within another div like this? Here is the code: <div class="event_month"> <div class="event1">1</div> <div class="event2">2</div> <div class="event3">3</div> <div class="event4">4</di ...

Exploring the semantic-ui dropdown menu feature in Chrome

When I first started learning to code with semantic-ui framework, I noticed a difference in behavior in Chrome compared to other browsers. The pointing menu on the left element (such as the search box) breaks down in Chrome, whereas it displays correctly i ...

problem with html tags

I've been attempting to customize an input field using inline styles, but I'm running into issues. Here's what I tried: <div class="row vh-md-100"> <div class="col-md-7 my-md-auto text-center text-md-left"> ...

Challenge with collapsing a button within a Bootstrap panel

Check out this snippet of code: <div class="panel panel-default"> <div class="panel-heading clearfix" role="tab" id="heading-details" data-toggle="collapse" data-target="#details" data-parent="#panel-group" href="#details"> <h4 c ...

Obtaining the source code from a different domain website with the help of jQuery

Is there a way to extract part of the source code from a YouTube page without using server-side programming? I've tried cross-domain AJAX techniques like Yahoo YQL and JsonP. While Yahoo YQL allows me to grab part of the source code, I'm facing ...

Creating dropdown options within a DataGrid in Form.IO: A step-by-step guide

I'm currently working with the Form.IO JS library to develop a new form. Within this form, I need to include a DataGrid containing 11 components. To ensure all components fit inline, I have applied the CSS rule overflow: auto (overflow-x: auto; overfl ...

"Exploring the process of implementing a fixed method POST in Angular 5

When developing an application for Portal, I encountered an issue where a newly created role is not displayed without refreshing the browser. How can I ensure that the added element is directly displayed in the table without needing to refresh the browser? ...

- increase clarity and readability- separate sections for better

I'm working on a code snippet to showcase 4 buttons: #buttons { text-align: center; padding-top: 4%; } #buttons img { display: inline-block; list-style: none; padding: 10px; } #buttons p { color: #fff; font-family: ; font-weight: ...

An alternative to CSS clearfix for children with fixed positioning

I understand that using clearfix can help with the zero-height container issue for floated elements. However, I am curious if there is a similar technique to clearfix for fixed elements? I am currently stuck trying to implement the clearfix method. .n ...

Steps to display a modal within an inactive tab:

I have been using ngx-bootstrap tabset, although I believe this issue could apply to all bootstrap tabs as well. <tabset> <tab class='active'> <angular-modal-component> </angular-modal-component> </tab> <tab> & ...

Steps to display the datatable footer on the printed page

I am having trouble understanding the solutions provided for my table query. The current table setup is as follows: <table class="table table-bordered make_datatable"> <thead> <tr> <th>SL No</th> ...

Having trouble setting margin-bottom to 0 in react and antd - why won't it reset?

While practicing with React, I added <link rel="stylesheet" href="%PUBLIC_URL%/css/reset.css" /> to the head of index.html in order to reset the styles. However, I noticed that <h1> still had a margin-bottom:0.5em that I c ...

Loading web pages using ajax and handling relative paths

My method involves using ajax to load HTML files when links on my page are clicked. The process is simplified as follows: links.click(function () { var href = $(this).attr("href"); history.pushState(null, null, href); $.get($(this).attr("href" ...

How can I insert PHP code within the style attribute of a div tag?

I tried implementing this code, but unfortunately it's not functioning properly. My goal is to take user-defined width and height inputs in PHP and use them to generate a shape. echo "<div style='width:<?php $sirina?>;height: <?php $v ...