Is there a way to resolve the issue of the hero image appearing behind the text box without relying on media queries?

I have a hero image with a text box overlapping the hero slightly. However, when I add content below the box, it appears under the text box. While my code includes some media queries that work well, I believe there must be a way to encapsulate the hero and overlapping text box into their own section. Adding more text means adjusting all the media queries to maintain the layout, so how can I achieve this without relying on media queries?

Take a look at the code snippet below:

.shell {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

/* overlap */

.shell:before {
  content: "";
  background-image: url(https://i.ibb.co/x866XdV/test-hero.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: bottom;
  position: relative;
  height: 300px;
  width: 100%;
}

.shell-header {
  color: #fff;
  padding: 0px 20px;
}

.shell-body {
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.25);
  background-color: #fff;
  max-width: 85%;
  position: absolute;
  top: 80%;
}
img {
  width: 20%;
  height: 20%;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

body {
  margin: 0;
  font-family: sans-serif;
}

.wrapper {
  padding-top: 12rem;
}

@media only screen and (min-width: 800px) {
  .wrapper {
    padding-top: 8rem;
  }
}

@media only screen and (max-width: 360px) {
  .wrapper {
    padding-top: 11rem;
  }
}
<div class="shell">
  <div class="shell-header"></div>
  <div class="shell-body">
    <h1>Title</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat vel ducimus illo consectetur commodi ex nulla aut amet ipsum maiores itaque, iusto quam mollitia facilis consequatur tempora neque quod eligendi?</p>
  </div>
</div>
<div class="wrapper">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3f/Placeholder_view_vector.svg" alt="">
</div>

Answer №1

Using the padding property is recommended to adjust spacing within elements. Another technique is to manipulate the positioning of your elements. For example, you can set the body element to have a position: relative; and then nest the .wrapper within the .shell class and set the .wrapper to have a position: absolute;.

.shell {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* overlapping effect */

.shell:before {
  content: "";
  background-image: url(https://i.ibb.co/x866XdV/test-hero.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: bottom;
  position: relative;
  height: 300px;
  width: 100%;
}

.shell-header {
  color: #fff;
  padding: 0px 20px;
}

.shell-body {
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.25);
  background-color: #fff;
  max-width: 85%;
  position: absolute;
  top: 80%;
}

img {
  width: 20%;
  height: 20%;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

body {
  margin: 0;
  font-family: sans-serif;
  position: relative;
}

.wrapper {
  position: absolute;
  bottom: -80%;
}
<div class="shell">
  <div class="shell-header"></div>
  <div class="shell-body">
    <h1>Title</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat vel ducimus illo consectetur commodi ex nulla aut amet ipsum maiores itaque, iusto quam mollitia facilis consequatur tempora neque quod eligendi?</p>
  </div>
  <div class="wrapper">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/3f/Placeholder_view_vector.svg" alt="">
  </div>
</div>

Here's an example utilizing negative margin.

.shell {
  display: flex;
  flex-direction: column;
  align-items: center;
}


/* overlapping effect */

.shell:before {
  content: "";
  background-image: url(https://i.ibb.co/x866XdV/test-hero.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: bottom;
  position: relative;
  height: 300px;
  width: 100%;
}

.shell-header {
  color: #fff;
  padding: 0px 20px;
}

.shell-body {
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.25);
  background-color: #fff;
  max-width: 85%;
  margin-top: -6.375rem;
  position: relative;
  z-index: 1;
}

img {
  width: 20%;
  height: 20%;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

body {
  margin: 0;
  font-family: sans-serif;
  position: relative;
}

.wrapper {
  margin-top: 1em;
}
<div class="shell">
  <div class="shell-header"></div>
  <div class="shell-body">
    <h1>Title</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat vel ducimus illo consectetur commodi ex nulla aut amet ipsum maiores itaque, iusto quam mollitia facilis consequatur tempora neque quod eligendi?</p>
  </div>
</div>
<div class="wrapper">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3f/Placeholder_view_vector.svg" alt="">
</div>

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 bypass mui class?

Having trouble targeting and overriding a default class on a material ui component. Any assistance in identifying the correct target would be appreciated. Here's my style: const useStyles = makeStyles((theme: Theme) => createStyles({ r ...

The email input field is not registering the value in the HTML form

There seems to be an issue with the email field in my form. When selecting 'Headteacher' from the drop-down menu, the email input works correctly. However, when choosing 'Primary school teacher', despite entering the details as expected ...

Is anyone else seeing a mysterious gray outline surrounding the image?

When visiting my website, I encountered an issue with the main menu. Specifically, when hovering over 'Populaire boeken', the first 2 menu items display properly with an image on mouseover. However, there seems to be a pesky grey border surroundi ...

Email button designed to trigger the execution of PHP code

Can a hyperlink button in Outlook emails be used to run PHP code? For instance, I've designed a computer request form that appears as follows: New Request When you select the "new request" button, it changes to indicate completion: Completed Reque ...

What are the implications of sending a query for every individual input field alteration in a large online form?

I am creating a system for an educational institution where faculty can input scores using php and html. The layout is similar to an excel sheet, with 20 questions per student and about 60 students on each page. My dilemma is whether it's acceptable ...

What is the best way to assign a class to objects with a count greater than a

Can someone assist with editing a foreach loop to add a class to divs only if the number of divs is greater than 3, without affecting those with fewer divs? @if(!empty($property->testimonials)) @foreach($property->testimonials as $testimonial) ...

Ways to increase the font size of the text within the button for forum actions

<form action="https://www.kroger.com/account/communityrewards/" > <input type="submit" value="Go to Krogers" style="height: 50px; width: 150px;" /> </form> I'm interested in learning how to increase the font size of the form action ...

Creating a single form field with two inputs and applying custom styling in Angular Material can be achieved by following these steps

In my Angular Material project, I am creating a sample application with a form that includes a field with an input and dropdown. Currently, I have utilized flex styles and attributes as shown below: <div class="row"> <mat-label>De ...

Certain browsers are experiencing issues with the functionality of the input type "date"

I am facing an issue with my ASP.NET web forms page where I am setting the value of an input (type="date") using code-behind. Surprisingly, it displays correctly in Internet Explorer 11 and Firefox 51, but in Chrome 56 and Opera 43, it only shows the mm/dd ...

Checking the Value of the Second Child Element Using jQuery

I am struggling with a DIV structure that looks like this: <div class="metafield_table"> <div class="metafield"></div> <div class="metafield"></div> <div class="metafield"> ...

Having difficulty decoding audio streams in Flask Socket.IO due to a file received by the Python server

Thank you for taking the time to go through this post. I have been diligently working on a project that involves capturing audio input from the client side and then sending the recorded audio to a Python server for analysis. However, I have hit a roadblock ...

Currently, I am encountering a problem as I attempt to iterate through a dynamic table

I have a table containing various elements. An example row is Jack Smith with multiple rows like this: col1 col2 col3 col4 col5 col6 col7 col8 jack smith 23 Y Y error error_code error_desc The table is ...

Customizing checkbox appearance without including a label

After following a tutorial on how to style checkboxes found here, I was able to create a custom styled checkbox. However, I noticed that when I removed the label from the checkbox, it disappeared and was no longer visible. Is there a way to display the c ...

Leverage CSS to generate a visually appealing table of contents incorporating images and text blocks, perfectly aligned for an enhanced

I am in the process of designing a unique clip showcase on my website. The concept involves creating a table-like structure with a thumbnail image on the left and various information on the right, including a title, publication source, date, and descriptio ...

Issue with Element Height Rendering in Chrome

I'm having trouble adjusting the height of the Div in this theme to 156px. It seems to be inheriting a CSS property from another source which is setting it to 118px instead. Please visit the following website in Chrome and Firefox to see the differe ...

Achieving Centered Items with CSS Flexbox and Positioning

Struggling to arrange 3 elements using flexbox while keeping them centered both horizontally and vertically. Here is the desired layout: https://i.sstatic.net/Uo6WS.png This is my current attempt, but it's not working as expected. Can anyone spot t ...

When using JQuery's :first selector, it actually chooses the second element instead of the first

I'm facing an issue with a JQuery script that makes an AJAX request to the following URL https://djjohal.video/video/671/index.html#gsc.tab=0, which holds information about a video song. My goal is to extract and retrieve all the details from the HTM ...

Tips for wrapping a div element around another div at the top left corner

I am working with a particular layout. <div class="comment"> <div class="leftpart"> </div> <div class="rightpart"> </div> </div> When viewing on a desktop, I maintain the original styling where each div is in its own s ...

Attempting to extract data from an HTML table and store it in an array using JavaScript with the help of jQuery

Currently, I am faced with a challenge involving extracting values from an HTML table and storing them in a 2-dimensional array using JavaScript. jQuery seems like the ideal solution for this task. While I have managed to get the indices working properly, ...

Tips for positioning the avatar to the right along with other links?

I've encountered various methods to align text or an image to the right, but I'm having trouble aligning a basic avatar with some text. It used to work before I switched to material-ui-next, and now I can't seem to get them aligned properly. ...