Allow a nested element to overlap its container instead of displacing it

As a CSS newbie writing a React app, this question may be the easiest one in the history of stackoverflow. I'm trying to make the image element overlap the header instead of pushing it down. In this basic HTML example, the cat image is stretching the header meant to be a cover. What I want is for the image to simply go over it.

This code snippet shows the CSS for the header container and main image:

.headerContainer {
  background: url(https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg);
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  width: 100%;
  min-height: 100vh;
}

.mainImage {
  width: 300px;
  height: 1500px;
}

This is what the HTML layout looks like:

<body style="margin: 0;">
  <header class="headerContainer">
    <img src="https://images.all-free-download.com/images/graphiclarge/lonely_cat_514201.jpg" class="mainImage" />
  </header>

  <section>
    <p>Lorem Ipsum content here...</p>
  </section>
  <footer>Made by a dude</footer>
</body>

Answer №1

Your cat image appears stretched because the width is set to 1500px. You can correct this by adjusting your code accordingly.

  1. Remove any extra p opening tags as recommended by @tacoshy.
  2. Ensure there is a closing tag for body.
  3. Eliminate the height property from the .mainImage class.
  4. OPTIONAL: Consider removing the min-height: 100vh; from the .headerContainer class for a better appearance.
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <style media="screen">
      .headerContainer {
        background: url(https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg);
        background-repeat: no-repeat;
        background-position: center center;
        background-size: cover;
        width: 100%;
        min-height: 100vh;
      }

      .mainImage {
        width: 300px;
      }
    </style>
  </head>
  <body>
    <header class="headerContainer">
      <img src="https://images.all-free-download.com/images/graphiclarge/lonely_cat_514201.jpg" class="mainImage" />
    </header>

    <section>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with...[truncated for brevity]
    </section>
    <footer>Created by an individual</footer>
  </body>
</html>

Answer №2

In one of my project websites, I incorporated the following class for the header design (see example here; the text appears over the image)

.header-image-text-overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);}

This is my first attempt at responding to a question, hoping this information is useful!

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

What is the best way to include a dialog div within a form tag?

I am facing an issue with a JQuery dialog on my webpage. The data entered into the dialog seems to get lost because the dialog is placed outside the form tag. Here is how it appears: https://i.stack.imgur.com/fnb07.png So far, I have attempted this soluti ...

Placing a gradient background in front of two divs

I have created two divs with gradients: .container_middle_page{ margin: auto; height: 100%; display: flex; flex-direction: column; } .container_middle_page_up{ background-image: linear-gradient(to top, #F28118,white); height: ...

displaying the JSON output from a PHP script within an HTML table

Here's the scenario: I have an HTML file that involves the following steps: Accepts a card number input from the user using an HTML form, Sends this input to a PHP file and receives a JSON-formatted response through xmlhttp.responseText, Needs to di ...

CSS margin-left causing issues in Chrome Extension

My current situation is incredibly puzzling, as I am at a loss for what is happening. I developed a Firefox add-on using jQuery and CSS to revamp a website. When attempting to transfer the add-on to Chrome (which was relatively straightforward due to the s ...

Is there a way to rearrange my divs in the mobile display?

Is there a way to make the image column stack below the text info column in mobile view? I've tried using flexbox without success, so any help would be greatly appreciated. Thank you! <div class="container"> <div class="row"> ...

I am looking to initiate the animation by triggering the keyframe upon clicking a button

Is there a way to create an animation triggered by a button click instead of loading the page? Each table data cell has its own class with a unique keyframe defining the style changes over time. @-webkit-keyframes fadeIn { from { opacity:0; ...

Modify th:hover in CSS to alter the background color

I currently have a table structured as follows: <table class="cedvel"> <caption> count: <%:Html.Encode(TempData["RowsCount"])%></caption> <thead> <tr&g ...

Ensure that each row of x images has the same height based on the height of the viewport

Currently, I am working on a website using HTML and CSS, specifically focusing on a "blog" header section. I am aiming to create a responsive grid layout featuring x images with equal heights (width adjusted according to their natural dimensions) and consi ...

How can I make the scrollbar invisible in the sticky header of a Material UI Table?

In my React project, I have implemented a Material-UI Table with a fixed header. The issue I am facing is that the scrollbar in the table header overlaps it and I want to hide it while still displaying it in the body of the table. I have attempted to modi ...

How to eliminate ampersands from a string using jQuery or JavaScript

I'm having trouble with a seemingly simple task that I can't seem to find any help for online. My CSS class names include ampersands in them (due to the system I'm using), and I need to remove these using jQuery. For example, I want to chan ...

Should one consider utilizing Ionic for creating a mobile web variant of a website?

Working for a Startup, we made the decision to create an adaptive website instead of a responsive one. This means that we serve different views for different devices in order to provide the best user experience for both mobile and desktop users. We are c ...

Causes for the display of the text within the alt attribute

Recently, I attempted to view an HTML page in text browsers and noticed that the alt attribute value of the img tag was visible. I decided to omit the alt attribute and instead utilized CSS: .headerImg:after, .headerImg::after { conte ...

What is the best way to store client-uploaded files on the client-side using Bootstrap forms and Express server code?

Essentially, the user submits a file for upload, which is then saved on the client-side (I believe this is handled by PHP), and the upload form I am utilizing is a Bootstrap HTML form. On the server side, I am writing my code with Express. I'm feeling ...

What is the best way to showcase several divs, along with their respective child elements, in an inline format?

I am struggling to get 4 div sections to display side by side by using the following CSS for the parent div: .parent { display: inline; } <div class="parent"> <div class="child"> <ul> <li>t ...

Adjust the CSS for the "a" tag's "title" attribute

I am using jquery.fancybox to create an image modal on my website. I have noticed that I can add a description using the title attribute. However, when the description is too long, the text is displayed on a single line and some of it disappears. How can ...

Utilizing CSS to incorporate percentage points

Currently, I am facing a challenge in implementing a vertical line with percentage marks. It needs to be positioned relative to the percentage bar, just like how it appears in the screenshot below: https://i.stack.imgur.com/CNWUV.png I attempted a basic ...

Setting up jsonReader for jqGrid Configuration

I am having trouble displaying data in my Jqgrid. The Json data is coming from a web server, so I am attempting to format it using Jsonreader as a function. Could someone please help me identify any mistakes? Thank you in advance. Here is the code for the ...

The jQuery find and replace feature is causing my entire content to be replaced instead of just specific paragraphs

Within this section, there are multiple paragraphs and an input field. The concept is simple: the user inputs text into the box, then hits "ENTER" to trigger a jquery function below. The process involves identifying matches between the paragraph content a ...

Expand an image to its full dimensions when it is initially loaded

Currently, I am experimenting with placing images of bubbles randomly on a webpage. To create the illusion of the bubble expanding in size from nothing to its full scale, I have been using CSS and specifically the transform:scale(); property. However, my ...

Assistance needed with CSS floating properties

Despite seeing many inquiries about float, I still can't pinpoint what's causing my issue. My objective is simple: GREETINGS FRIEND I aim for it to align just to the left of the unordered list. Referencing this solution, I attempted adding cl ...