Shivam Sahil
Shivam Sahil

Reputation: 4921

How to keep components auto resizable while creating templates in React JS?

So I am trying to put a background image and then center the text to it, but when I change the screen size the text misaligns and moves to the top. Here's my code:

import React from "react";
import UniversalValues from "../constants.js";
export default function Body() {
  return (
    <div>
      <div className="Container">
        <img
          className="ResizeImage"
          src="https://images.unsplash.com/photo-1533139143976-30918502365b?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max"
          alt="Background not loading! X_X"
        />
        <div className="TextField">
          <h1 className="BGH1">WE DO IT TOGETHER</h1>
          <h1 className="BGH1">SO LET'S CREATE</h1>
        </div>
      </div>
    </div>
  );
}

Here is my css file:

.App {
  font-family: sans-serif;
  text-align: center;
}
.BGH1 {
  letter-spacing: 0.06em;
  font-family: "Cinzel", serif;
  font-size: 5rem;
  position: relative;
}
.BrandPoint {
  font-size: 3rem;
  font-family: "Cinzel", serif;
  padding-left: 3rem;
}
.Container {
  position: relative;
}

.dropbtn {
  background: transparent;
  color: white;
  padding-bottom: 10px;
  font-size: 1.2rem;
  font-weight: bold;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #33393f;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: white;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {
  transition: transform 0.5s ease;
  transform: scale(0.9);
  color: #3eccb5;
  border: 1px solid;
  border-radius: 0.2rem;
  border-color: #e0dede;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown:hover .dropbtn {
  transition: transform 0.5s ease;
  transform: scale(0.9);
  color: #3eccb5;
  border: 1px solid;
  border-radius: 0.2rem;
  border-color: #e0dede;
}

.HeaderElem {
  color: white;
  padding-bottom: 10px;
  font-size: 1.2rem;
}
.HeaderElem:hover {
  transition: transform 0.5s ease;
  transform: scale(0.9);
  color: #3eccb5;
  border: 1px solid;
  border-radius: 0.2rem;
  border-color: #e0dede;
}
.HeaderSeperator {
  padding-left: 2rem;
}
.HeadUp {
  position: fixed;
  top: 0;
  left: 0;
}

.ResizeImage {
  height: auto;
  width: 100%;
  margin: 0;
  padding: 0;
  opacity: 0.99;
}

.TextField {
  position: absolute;
  bottom: 40%;
  left: 35%;
  color: white;
  padding-left: 20px;
  padding-right: 20px;
}

What I am getting vs What I wanted: enter image description here

enter image description here

I want to pack this whole thing together and so it could just effectively change its shape according to the screen size. I have used Bootstrap but I am new to React and designing my entire website on codesandbox.io. Do let me know the best way to do so.

Thanks in advance.

Upvotes: 0

Views: 232

Answers (1)

Gegenwind
Gegenwind

Reputation: 1428

I think your problem is purely HTML/CSS and not so much React. Thanks for flexbox centering content got a lot easier today.

Change your HTML like this:

<div>
  <div class="Container">
    <div class="imageContainer"></div>
    <div class="TextField">
      <h1 class="BGH1">WE DO IT TOGETHER</h1>
      <h1 class="BGH1">SO LET'S CREATE</h1>
    </div>
  </div>
</div>

And your CSS like this:

.Container {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color:blue;
  position: relative;
  color: white;
  text-align: center;
  height: 300px;
}

.imageContainer {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0.2;
  background-image: url("https://images.unsplash.com/photo-1533139143976-30918502365b?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max");
  background-position: center center;
}

Here is a fiddle test it: https://jsfiddle.net/gtqna9c1/7/

How it works

Instead of trying to center changing text by absolute positioning it is a lot easier to create conditions where the text is always centered, no matter what. The background image is now a simple background of a div that is covering your whole box.

The fiddle is simplified and coloured for clarity. Feel free to add paddings etc. as you wish.

Upvotes: 1

Related Questions