Steps for designing a complete background picture

Seeking to enhance my CSS skills, I've been faced with a challenging task recently.

The objective is to create a full-screen background image with centered text overlay. However, the image appears larger than the screen itself.

This is my current setup:

My App.js

import React, { Component } from 'react';
import background from './images/background.jpg';
import './App.css';

export default class App extends Component {
  render() {
    return (
      <div>
        <div className="App-header">
          <img src={background} className="App-background-header"/>
          <div className="App-header-text">
            <p>This text should be center on the image above.</p>
          </div>
        </div>
        <div>
          <p>This is a sample text after the image...</p>
        </div>
      </div>
    );
  }
}

My App.css

.App-header {
  height: 100%;
}

.App-background-header {
  height: 100%;

  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

.App-header-text {
  color: #444444;
  text-align: center;
}

I aim for something like this:

Current result: https://i.stack.imgur.com/EKZ6W.jpg

-- EDIT: --

I managed to resolve the issue but unsure if it's considered best practice.

Here is the updated code:

App.js:

import React, { Component } from 'react';
import './App.css';

export default class App extends Component {
  render() {
    return (
      <div>
        <div className="App-header">
          <div className="App-header-text">
            <p>This text should be center on the image above.</p>
          </div>
        </div>
        <div className="App-divider"></div>
        <div>
          Just a random text here...
        </div>
      </div>
    );
  }
}

App.css:

.App-header {
  height: 100%;
  width: 100%;

  position: absolute;

  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url('./images/background.jpg');
}

.App-header-text {
  color: #aaaaaa;
  top: 50%;
  position: relative;
  text-align: center;
  font-size: 30px;
}

.App-divider {
  height: 100vh;
  width: 100%;
}

The presence of "App-divider" may not be ideal, but it serves its purpose effectively.

Answer №1

If you want to display your image as an img element in HTML, use the following code:

<img src={background} className="App-background-header"/>
. This means that the styling from CSS will not be applied to the image.

To ensure proper styling, update your CSS by replacing .App-background-header with:

.App-background-header img {
  position: relative;
  width: 100%;
  height: auto;
} 

.App-header-text {
  position: absolute;
  top: 50%;
  left: 50%;
  with: 200px;
  height: 200px;
  margin-left: -100px;
  margin-top: -100px;
  text-align: center;
}

Alternatively, you can set your image as a background using the following CSS:

.App-background-header img {
  width: 100%;
  height: auto;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url("your_path_to_image.jpg");
} 

Remember to remove this line from your app.js file:

<img src={background} className="App-background-header"/>

Answer №2

Using an image in your design requires it to blend seamlessly with the style of your App-header. To achieve this, apply the CSS property background-image: src(imagePath)

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

Create a class for the grandparent element

Is there a way to dynamically add a class to a dropdown menu item when a specific child element is clicked? Here's the HTML structure I have: <ul id="FirstLevel"> <li><a href="#">FirstLevel</a></li> <li>< ...

Deactivating one div's class upon clicking on another div

Below is the HTML code snippet: <div class="container"> <ul class="navbar"> <li class="nb-link"><a>Home</a></li> <li class="dropdown"> <a>CBSE</a> <ul class="dropdown-menu"&g ...

Troubleshooting problem with ion-input in Ionic 3 regarding the keyboard issue

Issue with Keyboard Overlaying Button In my ionic3 app, I am facing an issue where the keyboard overlaps the "Registrarse" button when it is open, as shown in the image. The button is positioned at the bottom of the ion-content using CSS rules such as pos ...

I'm having trouble getting this text to align properly next to the image. Plus, I can't seem to achieve a horizontal screen alignment either

There seems to be an answer available, but I'm clearly not understanding it, so I've come here seeking further explanation and assistance. CSS can be quite complex, right? Here's a screenshot of my current project: https://i.stack.imgur.com/ ...

What is the best way to place tfoot at the top of the table

Below is the structure of my table: https://i.stack.imgur.com/QmlVn.png In the table, there are search input fields at the bottom of each column. I would like to move them to the top, right after the <thead> </thead>. The basic HTML code for ...

Navigating a collection of objects after a button is clicked

My current library project involves looping through an array of objects to display them on a grid based on input values. Here is the code snippet for my loop: const addBook = (ev) => { ev.preventDefault(); let myLibrary = []; let bookIn ...

How can I display a Skeleton when pressing pagination buttons and then turn it off after 3 seconds?

Here is a snippet of my code featuring the use of a Skeleton as a placeholder for CardMedia: {loading ? ( <Skeleton variant="rectangular" width={308} height={420} animation="wave" /> ) : ( <CardMedia s ...

Parent component experiencing delays while updating React state

As I work on my react application, I've always believed that state updates in react happen quickly. However, I've come across a problem recently. In one of the pages of my application, there are multiple elements present. One particular element ...

I'm curious, what height would be most suitable for a first impression?

I'm in the process of building a website and I want to ensure that all elements are visible within the div without requiring users to scroll. However, I know that screen resolutions vary between computers. What can I do to address this issue? Is ther ...

The responsiveness of Slick Slider's breakpoints is malfunctioning

After implementing a slider using slick slider, I encountered an issue with the width when testing it online and in a normal HTML file. If anyone could assist me in resolving this issue, I would greatly appreciate it. Please inspect the code for both scen ...

Organizing list items into vertical columns

Could you help me with UL/LI syntax and how to create a wrapping list? For example, I would like to display: 1 2 3 4 5 6 As: 1 4 2 5 3 6 Currently, I have a header, content, and footer all set with specified heights. I want the list to wrap when ...

fullcalendar adjusting color while being moved

Currently, I have implemented a fullcalendar feature that displays entries for multiple users with different colored calendars. However, there seems to be an issue when dragging an entry to another date - the color reverts back to default. Below is an exa ...

Using CSS to align the position of a div with the last word position of an h4 element

Trying to figure out how to position a div at the end of an h4 text element has been challenging. When the h4 text is on a single line, it's easy to place the div correctly. However, things get complicated when the text spans multiple lines. In that c ...

What is the best way to enlarge an element when scrolling downwards within the element?

I am looking to create a div that dynamically adjusts its height based on the user's scrolling behavior. The goal is for the div to expand to the very top as the user scrolls downward, and stop when it reaches 70% of the container/parent element. Is t ...

Here is a guide on how to ensure that the div elements automatically fill the available space

Looking at this html page: Check out the jsFidlle Demo here I am aiming to have the boxes fill up the space effortlessly, each with equal width but varying heights. This is similar to the layout in Google Keep notes ...

Customizing columns in React Material-tableIncorporating unique columns

As a MERN newbie, I'm seeking assistance in creating a Stocks app. I've successfully fetched data using axios and presented it in a table using Material-Table. import React, {useState, useEffect} from 'react' import MaterialTab ...

Discovering the window.scrollTop, ScrollY, or any other distance value while utilizing CSS scroll snap can be achieved by following these

I am currently utilizing css scroll snap for smooth scrolling through sections that are 100vh in height. The functionality of the scroll snap is quite impressive. However, I need to find a way to determine the exact distance the user has scrolled down the ...

Maintaining nth-child(odd) following filtering using jQuery

I am using a jQuery script to filter a table with multiple entries on this site. The filtering works well, but the issue arises when it comes to maintaining the correct tr:nth-child(odd) color that I have specified in my CSS. After filtering, the original ...

What causes Google fonts to fail on Heroku but succeed when used locally?

I am currently using express and heroku to host a prototype of a landing page. While the webpage functions properly when running locally, I encounter font loading issues when accessing it on heroku most of the time... The CSS files are located under /pub ...

When viewed through the Firefox browser on an Android device, the text on the webpage suddenly enlarg

I currently have the following content displayed on my website: <div style="font-size: 11px; line-height: 17px; text-align: left;"> <p>lorem ipsum etc etc</p> </div> Strangely, despite setting the font size to 11px, the te ...