Design a banner within the main image

I'm a CSS newbie looking to enhance my simple responsive hero grid by adding a banner with white text and 0.5 opacity at the center-right.

https://i.sstatic.net/qoQic.jpg

Check out my CodePen example here.

.hero-section {
  display: grid;
  grid-template-columns: 1fr; // stretch to the full frame
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  align-content: center;
  justify-content: center;
  .banner-image-div {
    grid-area: 1 / 1 / 2 / 2;
  } // image
  .banner-overlay-div {
    grid-area: 1 / 1 / 2 / 2;
  } // gradient or other overlay
  .banner-text-div {
    grid-area: 1 / 1 / 2 / 2;
  }
}

.hero {
  display: grid;
  min-width: 350px;
  width: 100%;
  height: 968px;
  object-fit: cover;
}

.hero-rectangle {
  width: 100px;
  height: 250px;
  opacity: 0.5;
  background-color: #fffff;
  position: absolute;
}
<section class="hero-section">
  <div class="row" style="padding: 0 15px; margin: 0px auto; background-color: #fff">
    <div class="col-sm-12">
      <div class="banner-image-div">
        <img src="https://source.unsplash.com/random" alt="" class="hero" />
        <div class="hero-rectangle">
          <p>Text here </p>
        </div>
      </div>
    </div>
  </div>
</section>

I've been struggling to position it above the hero image. Any tips on how I can achieve that? Thanks!

Answer №1

Adjust the positioning properties for the hero-rectangle element to properly position it. Additionally, I have included:

.banner-image-div {
  position: relative;
}

To make the image container positioned absolutely for bubbling.

MDN: The element is taken out of the regular document flow, leaving no space in the page layout for it. It is placed relative to its closest ancestor with a defined position (element with position: absolute;). If no such ancestor exists, it is positioned relative to the initial containing block. The final position is determined by values of top, right, bottom, and left.

.hero-section {
  display: grid;
  grid-template-columns: 1fr; // stretch to the full frame
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  align-content: center;
  justify-content: center;
  position: relative;
  .banner-image-div {
    grid-area: 1 / 1 / 2 / 2;
  } // image
  .banner-overlay-div {
    grid-area: 1 / 1 / 2 / 2;
  } // gradient or other overlay
  .banner-text-div {
    grid-area: 1 / 1 / 2 / 2;
  }
}

.hero {
  display: grid;
  min-width: 350px;
  width: 100%;
  height: 968px;
  object-fit: cover;
}

.banner-image-div {
  position: relative;
}

.hero-rectangle {
  width: 100px;
  height: 250px;
  opacity: 0.5;
  background-color: #eee;
  position: absolute;
  right: 25%;
  top: 35%;
}
<section class="hero-section">
  <div class="row" style="padding: 0 15px; margin: 0px auto; background-color: #fff">
    <div class="col-sm-12">
      <div class="banner-image-div">
        <img src="https://source.unsplash.com/random" alt="" class="hero" />
        <div class="hero-rectangle">
          <p>Text here </p>
        </div>
      </div>
    </div>
  </div>
</section>

Answer №2

When dealing with absolute elements, keep in mind they are positioned relative to their closest relative ancestor. To properly position your absolute element, follow these steps:

  1. Ensure the parent (.banner-image-div) has a position: relative property.
  2. Add the top and left properties to the style of your .hero-rectangle.

.parent {
  width: 200px;
  height: 200px;
  background-color: tomato;
}

.child {
  width: 80px;
  height: 80px;
  background-color: white;
  position: absolute;
}

.parent.well-defined {
  position: relative;
}

.child.well-defined {
  top: 50px;
  left: 50px;
}
<div class="parent">
  <div class="child">
    I'm not positioned well :(
  </div>
</div>

<br/><br/>

<div class="parent well-defined">
  <div class="child well-defined">
    I'm well positioned :)
  </div>
</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

CSS Dropdown Menu Malfunctioning

I recently created a navigation bar using some online tutorials, and everything seems to be working fine except for the drop-down menu. I've been struggling with it for hours and can't seem to find a solution. Any help would be greatly appreciate ...

Having difficulty scrolling down in a section with 100% height on iOS devices

Currently facing an issue with a website I am creating for my wedding invitation. The top section is set to have a 100% height and requires scrolling to view the rest of the content. While it functions perfectly on FireFox / Chrome on my computer, there s ...

Tips on reducing the height and improving responsiveness in Bootstrap 4

I've been struggling to adjust the image height to take up 75% of the screen. I attempted using height: 75%; and class="h-75", but it doesn't seem to be effective. Currently, I have class="h-800" with h-800 { height: 800px; }, ...

Looks like there's an issue: $("div#created") does not have a function called scrollintoview in jQuery

UPDATE: Encountered a new error: Error Jquery not defined on line 208 The issue seems to be with line 208 in this code snippet: https://github.com/litera/jquery-scrollintoview/blob/master/jquery.scrollintoview.js I am puzzled as to why I am facing this e ...

Having trouble getting Bootstrap CSS to load in an .ejs file?

I am new to ejs and Node.js. I have a Node.js file. Whenever the localhost receives a get request, I redirect to the index.ejs file. The data is displayed in the browser, but the CSS is not loaded. Can someone help me identify the issue in this code? I hav ...

Buttons containing two lines of text appear bigger compared to those with only one line of text, but it is essential for all buttons to be consistent in size with the single line buttons

I am facing an issue with the buttons on my website, as they seem to resize based on the amount of text present. When there are 2 lines of text, the buttons appear larger compared to those with just one line. How can I rectify this situation? Although thi ...

Is there a way to detect when a CSS background image has finished loading? Does an event trigger upon completion?

I am facing an issue with my sidebar widget where there is an image background in place. On top of this, I have a search input form but I don't want the input to display until the image has fully loaded. Is there a way to add a load event handler to ...

The Carousel is covering up my Mega-Menu and it is not visible on top

I'm facing an issue where my mega menu is hidden behind the carousel, and I need it to show on top. You can see how it looks before adding the carousel and after adding the carousel from Bootstrap. Here are the links to the Mega Menu CSS and Mega menu ...

Animating links with multi-line effects on :before.orEnhancing

As per the given query, I have a code snippet Is there any way to apply this effect to multiple lines of text instead of just one line? Currently, the effect only appears on one of two text lines (as shown in the example). :root { --00a3a3: #00a3a3; ...

ngx hierarchical dropdown

Is it possible that the 'disabled' attribute is not a recognized property of the 'ngx-dropdown-treeview' component? The ngxDisabledOnSelector property seems to be malfunctioning in my specific case. This is my code snippet: <ngx-dr ...

Adjusting an HTML table to fit within a designated space

I am currently in the process of constructing an HTML table that will showcase data retrieved from a MySQL database. This table will include functionalities to update or delete rows within the database. Here is a glimpse of my code: <table> <tr& ...

The webpage fails to automatically scroll to the bottom

I'm having an issue with a div element that I need to make scrollable. The menu is contained within this div (covering the full screen) you can see it here. When hovering over an element with children, the menu expands and extends beyond the screen. ...

Tips for maintaining the alignment of four buttons in a single row as the size of the screen changes

I'm creating a simple browser game as part of my web development learning process. I have a set of divs that represent a warrior, and at the head of the "div table" I have a group of 4 buttons arranged horizontally. However, when the screen size is re ...

Placing a table within a div causes the div to expand in width beyond 100%

A situation has arisen where a table with a large number of columns (30 columns) is being affected by the white-space:nowrap style set on each th tag, causing the parent div to stretch unnaturally. How can this issue be resolved and allow for a horizonta ...

Adaptable CSS column layout

I'm in the process of setting up a two-column layout. The main content column houses the blog card, while the secondary column I'm attempting to position at the top right corner. Check out this link for the blog card. This is the Bootstrap layo ...

In the past, I've crafted buttons and other elements using Photoshop, and subsequently saved them for web use. However, I'm now contemplating whether it's more advantageous to employ CSS in

In the past, I have utilized Photoshop to create visually appealing buttons and footer bars for my websites. However, I'm now pondering if it's more beneficial to utilize CSS for button creation. I've noticed that manually coding them can re ...

Trouble centering a list within a parent div using CSS

Even though this issue seems straightforward and many others have asked similar questions on the site, I find myself stuck after reading through several solutions and attempting various fixes. Why is this happening? My problem involves centering a navigat ...

Issue with fixed header in Vuetify v-data-table not functional

While working with the Vuetify v-data-table component, I have enabled the fixed-header property. However, I have noticed that the table header is scrolling along with the table body. I am using the latest version of Chrome. Does anyone know how to addres ...

Problem with CSS: In Chrome, text fields have 3px additional margin-right

https://i.sstatic.net/I8mzi.jpg I am facing an issue with the margins of my text fields. Even though I have set a margin-right of 5px for each field, Google Chrome seems to be adding an additional 3-4px automatically. This problem is evident when I dynami ...

Utilizing Jquery for draggable/droppable functionality in combination with custom overflow styles

Encountering a challenge with a drag and drop system that utilizes CSS overflow properties to enable vertical scrolling of a list of values. The issue arises when dragging an item between selected and unselected areas, causing the dragged item to disappear ...