What is the best way to display a background image within my div element?

I'm trying to create my first child inside the div.missions, but it's not displaying. The only thing showing is the background color of the second child. Can someone assist me with this issue?

Let me share my code with you:

Here's my HTML:


<div class="mission">
   <div> <h2> Our Goal </h2> </div>
   <div> <p> At Summit, we aim to make mathematics understandable for everyone. It's a challenging subject for many, and we want to help people solve problems, whether they're simple or complex. Our mission is to spread knowledge to all. </p> </div>
 </div>

And here's a snippet of my CSS:

.mission{
    margin-top: 50px;
    display: grid;
    grid-template-columns: 40% 60%;
}

.mission div:nth-child(1){
    background-image: url("MountainImage3.jpg");
    background-size: contain; 
}

.mission div:nth-child(2){
    background-color: #48cae4;
    padding-top: 100px; padding-bottom: 100px;
}

Any help would be greatly appreciated. Thanks!

Answer №1

Make sure to input the correct filepath for your image - your code will only work with a valid image filepath:

.mission {
  margin-top: 50px;
  display: grid;
  grid-template-columns: 40% 60%;
}

.mission div:nth-child(1) {
  background-image: url("https://example.com/image.jpg");
  background-size: contain;
  background-position: center;/* centered image */
  background-repeat: no-repeat;/* no image repetition */
}

.mission div:nth-child(2) {
  background-color: #48cae4;
  padding-top: 100px;
  padding-bottom: 100px;
}
<div class="mission">
  <div>
    <h2> Our Mission </h2>
  </div>
  <div>
    <p> We aim to educate people globally on the subject of mathematics. Mathematics can be challenging for many individuals, and we strive to empower everyone to solve problems, no matter the difficulty. Through Summit, we seek to disseminate knowledge to all.
      </p>
  </div>
</div>

Answer №2

Is it true that in CSS arrays also start at 0? If so, then it would look like this:

.mission div:nth-child(0) {
    background-image: url("MountainImage3.jpg");
    background-size: contain; 
}

.mission div:nth-child(1) {
    background-color: #48cae4;
    padding-top: 100px; padding-bottom: 100px;
}

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

Rotate the horizontal scroll of an inner div within an outer div using CSS

I am looking to create an outer div with horizontal scroll and inner div with vertical scroll where the inner div moves along with the outer div. <body> <div id="wrapper"> <div id="outer">content outer <div id="inner"&g ...

Creating an email in Gmail with a pre-filled HTML body

Currently, I am developing a Django web application and seeking a way to enable my app to send emails via Gmail. I have explored creating a hyperlink that opens the compose window in Gmail, as well as prepopulating fields such as "to," "cc," "bcc," and bod ...

Full-screen and auto-cropping capabilities are featured in the Bootstrap 4 carousel design

Is there a way to make full-width images in the bootstrap 4 carousel cropped based on position: center, background: cover to prevent scrolling issues? I attempted to follow advice from this webpage but encountered stretching and scroll-bar problems with d ...

Is there a hierarchy to be followed within the <head> element?

I have been developing websites using HTML for nearly 3 years now, but since I had to teach myself, there are still a few things I am unsure about. One question that has recently become important is whether it is possible to create groups within the <h ...

Is there a way to change my cursor to a pointer finger when hovering over my box?

<script type="text/javascript"> function draw() { var canvas = document.getElementById('Background'); if (canvas.getContext) { var ctx = canvas.getContext('2d'); ctx.lineWidth = 0.4 ctx.strokeRect(15, 135, 240, 40) Is there a w ...

What is preventing the mat-slide-toggle from being moved inside the form tags?

I've got a functioning slide toggle that works perfectly, except when I try to move it next to the form, it stops working. When placed within the form tag, the toggle fails to change on click and remains in a false state. I've checked out other ...

Expanding Material Ui menu to occupy the entire width of the page

I'm encountering an issue with a menu I created where I can't seem to adjust its height and width properly. It seems to be taking up the full width of the page. import React, { Component } from "react"; import Menu from "@material-ui/core/Menu"; ...

Adjustable Bootstrap Progress Bar - Modify element width on the fly

I have encountered an issue with my progress bars on the webpage. The static HTML version works perfectly fine, but the dynamically created one using jQuery seems to be instantly at 100% without any delay in progression. To illustrate the problem better, ...

Some elements in the DOM appear to not be rendering even though they are present in the HTML source code

As a newcomer to web development, I have stumbled upon a perplexing issue. In my usage of d3.js (specifically the script found here), I have been attempting to incorporate additional features. I modified my JavaScript code to include a simple <p> ele ...

Issue with Bootstrap 4 Navbar collapsing and not expanding again

Help needed! My navigation bar collapses when the window is resized, but clicking on the hamburger icon does not open it back up. I have included my code below. Can someone please provide guidance on how to expand the collapsed navbar? <html lang=&quo ...

Exploring the capabilities of rowGroup within DataTables

Currently, in the process of completing a project, I am retrieving data from a REST API to populate my DataTable. To avoid displaying duplicate items, I am interested in creating subrows in the DataTable with a drop-down menu based on an item in the "Deliv ...

Tips for extracting data from cells within an HTML table using JavaScript

I am looking to extract the values from a cell of an HTML table in order to store them in a MySQL table using PHP. I prefer to utilize JavaScript instead of jQuery. I have assigned a unique ID to each TR based on the ID field in the MySQL table. Additiona ...

When the label is clicked, the radio button does not have a selected value

I am experiencing some strange behavior with a radio button, as shown in the attached GIF. Whenever I click on the label, the selected value disappears and the radio buttons become unchecked. I have been unable to identify the cause of this issue. I am ...

Clicking on the button will result in the addition of new

Having some trouble with jQuery as I try to dynamically add and remove HTML elements (on click of +) while also making sure each element has a unique name and ID like "name_1", "name_2"... Unfortunately, things aren't quite going as planned. Below i ...

Columns in Bootstrap4 housing elements positioned absolutely

Recently, I've been developing a game that involves using absolute positioning for certain elements. This is necessary for creating moving animations where elements need to slide around and overlap to achieve a specific effect. As I focus on optimizi ...

Verify whether the current location is within the circle

I am conducting a test to determine if my current location falls within a given radius of 300m. If it does, return true; otherwise return false. Below is the code I am currently using: <body> <button onclick="getLocation()"> ...

How to use jQuery to display only the following div with a matching class

I'm encountering an issue while attempting to perform a series of steps using jQuery. Unfortunately, I am unable to achieve the desired outcome. There are 3 steps, each with a class of .wiki-step- followed by a number. Here is my JavaScript code: fu ...

Execute an AJAX post request to the identical PHP page (utilizing the Jquery Form Plugin)

I am in the process of developing a new web interface using JavaTMP, an AJAX-based admin template. After spending time understanding its functionality, I have created a page with a form to allow users to create projects within my software. Typically, creat ...

The modification of HTML styles

How can I change the style (width, color etc) of all 8 functions like this? function my(){document.getElementById("question1").innerHTML="THIS QUESTION"+ "<br>" +"<button onclick=answer1() id=ques1 >first answer</button>" +"<button ...

Converting Variable Values to Element IDs in JavaScript: A Helpful Guide

Is it possible to declare a value in variable A and then access that value as an id in variable B using the method getElementById()? var A = 10; var B = document.getElementById(A); console.log(B); ...