How to position text in the center of a video using CSS

My attempt to center the name of my blog on top of a video background was not successful, even though I tried positioning it absolutely with 50% from the top. Can someone assist me in vertically and horizontally centering the text over the video?

Here is the code: https://codepen.io/anon/pen/QgBZqd

HTML

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>SINEMACERA</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="styles.css">
<link href="https://fonts.googleapis.com/css?family=Anton|Cabin" rel="stylesheet">
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">SINEMACERA</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Anasayfa</a></li>
          </ul>

          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Hakkımda</a></li>
            <li><a href="#">İletişim</a></li>
          </ul>
        </div>
     </div>
</nav>

<div id="COVERPHOTO">
    <div id="COVER">
      <h1 id="COVERTEXT">TEXT ON VIDEO</h1>
      <video autoplay loop id="video-background" muted plays-inline>
        <source src="http://thenewcode.com/assets/videos/polina.mp4" type="video/mp4">
      </video>
    </div>
</div>

<div class="container">
    <div class="row">
        <div id="MAJORCONTENT">
            <div class="col-lg-12">
                <h1>Lorem Ipsum Üzerine</h1>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                <span class="readmore">
                    <p>... (content truncated) ...</p>
                </span>
                <button type="button" class="btn btn-primary wide-button">Devamını oku!</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="sinemacera.js"></script>
</body>
</html>

CSS

 /* CSS code goes here */ 

JS

$(".btn-primary").click(function() {
    var thisBTN = $(this);
    var RM = $(this).prev();

    RM.slideToggle(1000, function() {
        thisBTN.toggleClass("btn-info");
        
        if(thisBTN.text()==="Küçült") {
            thisBTN.text("Devamını oku");
        } else {
            thisBTN.text("Küçült")
        }
    });
});

Answer №1

Make the necessary updates to the CSS with the following code:

#COVERTEXT {
    margin: 0;
    font-family: Anton;
    font-size: 10vw;
    text-shadow: 0px 4px 3px rgba(0, 0, 0, 0.7), 0px 8px 13px rgba(0, 0, 0, 0.3), 0px 18px 23px rgba(0, 0, 0,0.3);
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    margin: auto;
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transform: -moz-translateY(-50%);
}

Answer №2

To center the text over the video in relation to the parent element #COVER, apply the following CSS properties on #COVERTEXT:

position: absolute; top: 50%; transform: translateY(-50%); left: 0; right: 0;

$(".btn-primary").click(function() {

var thisBTN = $(this);
var RM = $(this).prev();

RM.slideToggle(1000, function() {
thisBTN.toggleClass("btn-info")
if(thisBTN.text()==="Küçült") {
thisBTN.text("Devamını oku");
}
else {
thisBTN.text("Küçült")
}
});
});
/* Your CSS code goes here */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

<!--NAVBAR STARTS-->

<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">  
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">SINEMACERA</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        
        <li class="active"><a href="#">Anasayfa</a></li>
        
        
      </ul>
      
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Hakkımda</a></li>
        <li><a href="#">İletişim</a></li>
        
      </ul>
    </div><!-- /.navbar-collapse -->
    </div>
</nav>

<!--NAVBAR ENDS-->

<div id="COVERPHOTO">

    <div id="COVER">
      <h1 id="COVERTEXT">TEXT ON VIDEO</h1>
      <video autoplay loop id="video-background" muted plays-inline>
        <source src="http://thenewcode.com/assets/videos/polina.mp4" type="video/mp4">
      </video>  
      </div>
  </div>

<div class="container">
<div class="row">
<div id="MAJORCONTENT">

/* Your HTML content goes here */

</div>
</div>
</div>
</body>
</html>

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

After submitting the form, I must only deactivate the checkbox that has been selected

Here is the HTML code I am working with: I have multiple checkboxes designed as buttons to select a room, but it's quite annoying when they all get selected at once. $("form").submit(function() { $('.seat input:checkbox').prop("disable ...

One way to eliminate a prefix from a downloaded file path is by trimming the URL. For example, if you have a URL like "http://localhost

As my web app runs on port number, I am looking to download some files in a specific section. However, the download file path is being prefixed with "". <a href={file_path} download={file_name}> <Button variant={"link"}> <b>Dow ...

Encountering a Node Js post handling error with the message "Cannot GET /url

This is my ejs file titled Post_handling.ejs: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>POST-Handling Page</title> </head> <body& ...

Is it time to swap out those divs for tables in your email template?

I have set up my template like this and I am questioning whether I should replace every div with a table. Let's start with the first div in the example. Do I really need to create a new table element, followed by tr and td tags just for a banner? I u ...

"By setting the HTML input box to read-only, the JavaScript button has the

Check out this js fiddle demonstration: http://jsfiddle.net/YD6PL/110/ Here is the HTML code snippet: <input type="text" value="a" readonly> <input type="text"> <input type="text"> <div> <button class="buttons">c</button ...

Missing ng-required fields not displaying the has-error validation in AngularJS forms

While editing any part of their address, the user should see a red invalid border around each field to indicate that the full form is required. However, for some reason I can't seem to get the 'Address' field to display this border. The set ...

Creating an adaptable image with CSS or Material UI

Is it possible to create a responsive image that shifts from the top right position to the bottom as the size decreases? I've been using Material UI but haven't found any guidance on how to achieve this. .my-photo{ height: 300px; positio ...

Simple Way to Modify Color of Active Page Link - HTML, CSS, and JavaScript

I found a helpful example on how to change link colors for the current page here. Here is the script I added: <script> // current page highlight $(document).ready(function() { $("[href]").each(function() { if (this.href == window.l ...

Do not proceed with the form submission if the fields are left blank

I am facing a challenge with organizing two sets of data, "heat" and "cold", obtained from an external provider. The data is messy and I have trimmed down the code to focus on the main issue at hand. Both "heat" and "cold" have properties that users need t ...

What is the best way to increase the font size without resizing the parent input element?

Is there a way to increase the font size in this text box without enlarging the box itself? Any suggestions or advice would be appreciated! . Below is my CSS code for reference (the class of the form is 'box'). .box input[type = 'text' ...

Adding a slight 1px right margin between an HTML table and its enclosing div in Bootstrap 3

Help! I'm trying to troubleshoot some HTML issues and can't figure out where this pesky 1px gap between my table and the wrapping div is coming from... Just for reference, I am using Bootstrap. I suspect that it could be a glitch in the Bootst ...

Can a font size be adjusted dynamically based on the width of its containing class?

I have encountered this issue before, but the previous solution did not work for me. I am now seeking a viable alternative. Currently, I am developing a website and have an event announcement block that needs to display the event date spanning the entire ...

Unable to apply CSS styles to a form input using Ajax

Alright, so I've got this Ajax form that successfully fetches the data and displays it in #register_msg. However, I'm also trying to apply some styles to the input forms. Here's my Ajax code, along with the form. But for some reason, the st ...

Generating HTML widgets dynamically with jQuery: A step-by-step guide

Looking for a way to display an interactive widget on your page while allowing users to generate multiple instances and interact with them simultaneously? Imagine having a widget like this: <div id="my_widget"> <script type="text/javascript" ...

What methods can be used to authenticate the user's input?

I am facing an issue with my program where it breaks if there is a space behind the last number entered. I want to prevent the function from breaking when a space is entered. I tried using $.trim but couldn't get it to work. I also attempted using an ...

Unable to display a horizontal scroll bar

I'm having some trouble with implementing a horizontal scroll feature and can't seem to make it work. If you want to take a look, here is the fiddle Please provide me with assistance in identifying what mistakes I may be making edit Here is th ...

Looking for a solution to fix the VueJS calculator that only works once?

My calculator project using VueJS, HTML and CSS is almost complete. However, I'm facing an issue where it only works once. For example, if I input 6x3, it correctly gives me 18. But if I then clear the result and try to input a new calculation like 3 ...

"Utilizing CSS to Format Academic Publications in the Style of APA Guidelines

Is there a way to automatically format a list of academic papers for an updated page using CSS rules? I want to style published articles similar to the following example: https://i.stack.imgur.com/jO56V.png I don't want to manually enter &nbsp;& ...

Modify the surrounding container of the chosen selector

I have a complex website with multiple containers. My objective is to apply a color to a container with the class "colorme" when another container, located far away from it, has the class "active". Here is how the code is structured: .tooltip:has(tool ...

Create a sleek Bootstrap 4 webpage featuring a fixed footer and navigation bar. However, the challenge lies in ensuring that the content fills the

Currently, I am in the process of creating a template page that includes a navbar and a footer with the intention of adding additional columns within the main div (container-fluid) at a later stage. However, I have encountered two issues that I cannot seem ...