Prevent black margins in video aspect ratio of 1:1 using CSS

I am working with a square video (scale 1:1) and I need to position it in the center of the screen.

My main issue is that when using the <video> tag, two black areas are inserted on both sides. Is there a way to prevent this from happening?

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

<div style="width: 400px; height: 400px; background-color: red;">
        <video>
        <source src="~/Content/video/FreeBirdConstruction.mp4" type="video/mp4">
    </video>
</div>

The containing div is set to be a square (400px x 400px), so why doesn't the video fill it? I have also attempted to use VideoJS but haven't been able to solve the problem.

Another question arises. Whenever I try to center the div on the screen, like this:

position: absolute; /*it can be fixed too*/
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
overflow: auto;

The div initially appears in the center of the screen, but then disappears instantly.

Could someone please assist me in finding a solution to this issue? Thank you for your help.

Answer №1

If you want to make sure the video stays square, I recommend setting the height and width directly within the video tag like this:

<div style="background-color: blue;">
    <video width="400" height="400">
        <source src="~/Content/video/FreeBirdConstruction.mp4" type="video/mp4">
    </video>
</div>

Setting a specific width and height for your div may be causing some confusion in centering it. Try simplifying your CSS by using the following code to ensure your div is centered:

div{
    display:inline-block; /*keeps the div the size of its contents*/
    margin:auto; /*centers the div on the screen*/
}

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

Ensure that the Div element is able to wrap lines in the same way that the Span element

Is there a method to ensure that a div element wraps the lines similar to how a span does? JSFiddle for testing In this example, I utilized the span tag to create an appearance as if the text was highlighted by a yellow highlighter pen. Everything functi ...

Why is the dropdown list value returning as null upon clicking the submit button?

When I click on the submit button after selecting a country from the dropdown menu, the country ID value is passed to the controller. Pressing the F10 key triggers the country bind method, resetting the country value to Null. Below is a screenshot: <di ...

Arranging a flex item below another flex item within a flexbox container

Is there a way to use flexbox to ensure that the 2.5 element is placed below the 2 element instead of beside it on the same row within the flex container? Given the HTML provided, how can I achieve this layout using flexbox? I attempted to accomplish thi ...

React - issue with modal due to invariant violation

In my application, I have implemented a modal with a dropdown menu containing various Categories. Upon selecting a Category, a new dropdown menu appears which displays Classifications. If a Classification is selected, another component is rendered on the p ...

Validation and Summaries in MVC 2: Ensuring Data Accuracy and Cl

Starting on MVC 2, I have a question about how validation summaries function. I originally thought they would be similar to the ASP.NET web forms validation summary control. For simple validation tasks, which of these .js files are necessary? <script s ...

What is the method for incorporating multiple classes into an li element?

Here is the code I am working with: function createCardList(classname, cardName) { console.log(classname); var $li = $('<li class=' + classname + '>'); var $input = $('<input >', { type: 'radio&ap ...

Organizing information in local storage using an array and converting it to a string

After storing user inputs (Worker's name, Car Vin, Start time and End time) in the local storage, you want to sort the employees' names in alphabetical order. The question is where should the code be placed and how should it be written? // Car C ...

Required inputs do not disrupt the form's action flow

Here is the HTML code that I am working with: function document_save_changes(){ if (is_key_dirty == true){ var elm = document.getElementById('set_doc_button'); key_change_warning(elm, 'D'); return; } if (document_save_warning('A ...

Exploring ways to enhance Bootstrap Navigation by adding extra link options. Seeking a resolution using jQuery

Are you looking for a solution to handle site navigation with a larger number of links? When more links are added, they display in multiple lines which might not be the desired layout. You can view an example of this issue in the attached image: See here a ...

Using an object as a parameter in the Include Template Tag in Django

I've run into an issue while developing a website in Django. I'm using the 'include' template tag and trying to pass an object into it. However, it seems to be passing the dunder str method of the object instead. {% include 'functi ...

Having trouble retrieving a Controller parameter from a form in ASP.NET MVC?

Currently encountering an issue with a controller that is structured as follows: public class AccountsController:Controller { public ActionResult List(int? page, int? pageSize, string keywords) {...} } The approach we are using to send data to this c ...

Displaying elements side by side on larger screens and centered on smaller screens

Is there a way to arrange elements next to each other on wider screens, aligning them left and right, while also centering one above the other on narrower screens? Preferably without using media queries: ...

Guide on using JavaScript to extract and display a random text from a datalist upon clicking with the onclick event

Is there a way for a JavaScript function to select a random sentence from a datalist within the same file and display it upon clicking a button with an "onclick" event? I'm new to JavaScript and seeking the simplest solution. Can anyone provide an exa ...

Tips for concealing overflowing CSS content

How can I prevent CSS content from overflowing? I am trying to display a price tag in the CSS element, but it is protruding out of my card. .content{ content:''; position: absolute; left: 309px; ...

Utilizing the DirectusImage tag for a stylish background effect

I'm new to Directus and I need help styling a card with a background image from a Directus Image. Can anyone guide me on how to achieve this? Here's what I have tried: <Card style={{backgroundImage:"url({someColletion.image.id})" &g ...

Is the form always being processed by onsubmit and confirm?

Apologies if this question has been asked before. I am currently working on a form where I ask the user to confirm an action, but it's not functioning as expected. The form proceeds regardless of whether the user clicks ok, cancel, or even closes the ...

Reveal and Conceal, the ever-changing show

As I work on my blog, I want to make the layout more compact by having a link that reveals comments and entry forms when clicked. I've seen this feature on other sites as "Comments (5)", but I'm unsure how to implement it myself. Below is a snip ...

Generating an Array using Jquery

I am working with two functions. The first function takes the input from users in a text box, appends it to a div, and then displays it on the HTML page. The second function is supposed to change the styling of the word entered by the user, alternating the ...

Issue with Bootstrap causing individual scroll panels for each column to be unresponsive

Hello, I'm currently using Bootstrap to build a website from scratch. This is my first time working with Bootstrap, so I might be making some errors along the way. My current challenge involves creating individual scroll panels for each of the 3 cont ...

Web browser displaying vertical scroll bars centered on the page

I'm new to HTML and have been experimenting with this CSS file to center my form. I've managed to get it working, but the issue now is that it's causing scroll bars to appear on the web browser. How can I resolve this without them? @import ...