1

I'm trying to set an image to stretch across the entire landing page in React.js. I've tried using div tags and having corresponding class names in the css to set this attribute, but am only getting a small section of the page to be the image rather than the whole page:

background only showing in part of a section of the page

I'd like for the image to cover the entire page with no white spaces.

This is my .css

    .start-bg {
  margin: 0;
  padding: 0;
  background: url("./images/movierama.jpeg") no-repeat center fixed;
  background-size: cover;
  min-width: 100%;
  min-height: 100%;
}

This is my React.jsx file:

      <div className="start-bg">
    <h1 className="landing">
      Welcome to Movierama!
      <p>Explore our movie collection today!</p>
    </h1>

    <Link to="/">
      <button
        className="btn btn-primary"
      >
        Enter
      </button>
    </Link>
  </div>

Bonus: I'm trying to center the button too, but it's not working with the css tags I'm applying. Help appreciated.

Thanks!!

2
  • Try changing min-height: 100% to min-height: 100vh. That should get it to be at least the height of the viewport, but it looks like you also have content or a margin keeping it away from the top of the viewport, which might be a separate issue. Another option would be to set a class on body when your component mounts using the DOM api. Commented Jul 25, 2020 at 18:55
  • Alternatively, you could apply min-height: 100% to html, body to ensure that the document always fills the viewport. Commented Jul 25, 2020 at 19:10

1 Answer 1

1
  • first try to reset all the default behavior of the browsers like default padding, margins for every element like this:
*,
*::before,
*::after {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
  • then add full height uding vh rather than % like this:
.start-bg {
  background: url("./images/movierama.jpeg") no-repeat center fixed;
  background-size: cover;
  height: 100vh;
  object-fit: cover;
}
Sign up to request clarification or add additional context in comments.

3 Comments

It worked!!! That was super helpful!!! Thank you sooo much, Abdelmonaem Shahat!! :)
I am happy to help :)
Thank you, Abdelmonaem!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.