How to Center a Video in HTML | Codabase

How to Center a Video in HTML

5 Min Read

Learning how to center a video in HTML can enhance the visual appeal of your webpage. In this article, we will explore various methods to achieve this, including CSS and HTML attributes.

Table of Contents

Using CSS

The most recommended approach to center a video in HTML is by using CSS. You can create a separate CSS file and link it to your HTML document or include the CSS code within the <style> tags inside the <head> section of your HTML file. By creating a .video-container class and applying the text-align: center; style to it, you can wrap the <video> element within a <div> with the video-container class. This is demonstrated in the following example:


<!DOCTYPE html>
<html>
<head>
<style>
  .video-container {
    text-align: center;
  }
</style>
</head>
<body>
  <div class="video-container">
    <video width="320" height="240" controls>
      <source src="movie.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>
</body>
</html>

Using Flexbox

Another method for centering a video in HTML is by using the CSS Flexbox layout. Flexbox provides an easy and efficient way to align and distribute elements within a container. The following example demonstrates centering a video using Flexbox:


<!DOCTYPE html>
<html>
<head>
<style>
  .video-container {
    display: flex;
    justify-content: center;
  }
</style>
</head>
<body>
  <div class="video-container">
    <video width="320" height="240" controls>
      <source src="movie.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>
</body>
</html>

In this example, we apply the display: flex; and justify-content: center; styles to the .video-container class to horizontally center the video within its container. This approach provides a flexible and responsive layout, making it suitable for various screen sizes and devices.

Using Grid

Another powerful layout method in CSS is the Grid layout. It allows you to create complex and responsive designs with ease. You can center a video in HTML using the CSS Grid layout by defining a grid container and placing the video in the center of the grid. Here’s an example:


<!DOCTYPE html>
<html>
<head>
<style>
  .video-container {
    display: grid;
    justify-items: center;
  }
</style>
</head>
<body>
  <div class="video-container">
    <video width="320" height="240" controls>
      <source src="movie.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>
</body>
</html>

In this example, we set the display: grid; and justify-items: center; styles on the .video-container class to horizontally center the video within the container.

Using Inline CSS

While not recommended for large-scale projects, inline CSS can be a quick way to center a video in HTML. However, it can lead to cluttered and hard-to-maintain code. Here’s an example of using inline CSS to center a video:


<!DOCTYPE html>
<html>
<head></head>
<body>
  <div style="text-align: center;">
    <video width="320" height="240" controls>
      <source src="movie.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>
</body>
</html>

This example demonstrates the use of inline CSS by adding the text-align: center; style directly to the <div> element’s style attribute.

Regardless of the method you choose to center a video in HTML, it’s essential to keep your code clean, maintainable, and responsive. For more in-depth knowledge on related topics, check out the following articles:

Additionally, don’t forget to check out other resources and stay updated on the latest web development trends:

By understanding different techniques to center a video in HTML, you can create a visually appealing and responsive web page that caters to various devices and screen sizes. Experiment with the methods mentioned above and choose the one that best fits your project’s needs.

Before you go, don’t forget to sign up for our newsletter to stay updated on the latest web development tips, tricks, and tutorials:

    Share this Article
    Leave a comment