Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

HTML & CSS Can someone help me find the error in my code?

tae20

New Coder
My content won't rest inside my background image properly. I need it to be centered with one another. It's just not happening.

(expectation)
Screenshot 2024-12-15 172146.webp

(mine)
Screenshot 2024-12-15 172057.webp

HTML:
<html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Background Video</title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <section class="hero">
            <video autoplay loop muted plays-inline >
                <source src="assets/4763824-uhd_3840_2160_24fps.mp4" />
            </video>
        </section>
        <section class="content">
            <h1>Journey</h1>
            <a href=" ">Explore</a>
        </section>
    </body>
</html>

CSS:
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: sans-serif;
}

.hero {
    width: 100%;
    height: 100vh;
    background-image: linear-gradient(rgba(12, 3, 51, 0.3), rgba(12, 3, 51, 0.3));
    position: relative;
    padding: 0 5%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.content {
    text-align: center;
 
}

.content h1 {
    font-size: 160px;
    color: #fff;
    font-weight: 600;
    transition: 0.5s;
}
.content h1:hover {
    -webkit-text-stroke: 2px white;
    color: transparent;
}

.content a {
   text-decoration: none;
   display: inline-block;
   color: #fff;
   font-size: 24px;
   border: 2px solid white;
   padding: 14px 50px;
   margin-top: 20px;
   transition: 0.5s;
}

.content a:hover {
  background: #fff;
  color: #000;
}

.back-video {
    position: absolute;
    right: 0;
    bottom: 0;
    z-index: -1;
}

@media (min-aspect-ratio: 16/9) {
   .back-video {
    width: 100%;
    height: auto;
   }
}

@media (max-aspect-ratio: 16/9) {
    .back-video {
     width: auto;
     height: 100%;
    }
 }

I tried changing the position and display of the hero class but it did nothing.
 
Last edited:
Hey there! It looks like your main issue is that the text and button aren’t actually sitting on top of your video background; instead, they’re placed in a whole separate section (section class="content"). Since your .hero section is the one with the video background (and the flex centering styles), the easiest fix is to move your heading and button inside that same .hero container.

Here’s how you might restructure your HTML:

Code:
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Background Video</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <section class="hero">
      <video autoplay loop muted playsinline class="back-video">
        <source src="assets/4763824-uhd_3840_2160_24fps.mp4" />
      </video>
      <!-- Move your content here so it overlays the video -->
      <div class="content">
        <h1>Journey</h1>
        <a href="#">Explore</a>
      </div>
    </section>
  </body>
</html>

Then, in your CSS, make sure the video is behind everything else and the content is centered:

Code:
.hero {
  position: relative;
  width: 100%;
  height: 100vh;
  background-image: linear-gradient(rgba(12, 3, 51, 0.3), rgba(12, 3, 51, 0.3));
  display: flex;
  justify-content: center; /* centers horizontally */
  align-items: center;    /* centers vertically */
  overflow: hidden;       /* hides any overflow from the video */
}


.back-video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;  /* ensures the video covers entire section */
  z-index: -1;        /* sends the video behind the content */
}


.content {
  text-align: center;
  z-index: 1;         /* ensures the text is above the video */
}


.content h1 {
  font-size: 160px;
  color: #fff;
  font-weight: 600;
  transition: 0.5s;
}


.content h1:hover {
  -webkit-text-stroke: 2px white;
  color: transparent;
}


.content a {
  text-decoration: none;
  display: inline-block;
  color: #fff;
  font-size: 24px;
  border: 2px solid white;
  padding: 14px 50px;
  margin-top: 20px;
  transition: 0.5s;
}


.content a:hover {
  background: #fff;
  color: #000;
}
With these tweaks, your heading and button should be nicely centered over the video background, matching your expectation screenshot. Hope that helps!
 
Back
Top Bottom