The photo is too big for the designated space

While trying to master CSS, I thought I had a handle on these images but it seems like I was wrong. I have a background image for a section and I've been adjusting the position to align with buttons, but now the whole page is expanding.

I've experimented with various solutions suggested by others, such as setting a max width for the parent container and hiding overflow, but I still can't seem to fix the issue. Upon inspecting the code, I noticed that the image is bleeding into the next section.

Answer №1

position:absolute allows your image to be positioned in relation to its relative parent element. To ensure containment, make sure the parent element has a position:relative property and add overflow:hidden. This will prevent any overflow from being visible outside of the container.

I have included a placeholder image in the example provided to demonstrate that the images remain within the boundaries of the container.

header{
  max-width:100%;
  position: relative;
  overflow: hidden;
}
#hero-bg {
  position: absolute;
  background: url("http://via.placeholder.com/1000x1000") center center no-repeat;
  padding-top: 200px;
  padding-left: 60px;
  width: 100%;
  height: 100vh;
  z-index: -1;
  overflow-x:hidden;
}
<header>
      <!-- Header BG Image -->
      <div id="hero-bg"></div>
      <!-- Logo & Nav -->
      <div class="content-wrapper">
        <div id="nav-bar">
          <div id="logo"><img src="./img/logo.png" alt="" /></div>
          <div id="mobile-nav">
            <div></div>
            <div></div>
            <div></div>
          </div>
          <ul id="main-nav">
            <li>Item1</li>
            <li>Item2</li>
            <li>Item3</li>
            <li><a href="''" class="btn">LOREM</a></li>
          </ul>
        </div>

        <!-- Hero Content -->
        <div id="hero-showcase">
          <h1 class="hero-head">Lorem Ipsum</h1>
          <p class="hero-content">
            Lorem ipsum dolor sit, amet consectetur adipisicing elit. Fuga,
            dolore enim ab, fugit natus rerum eveniet delectus consectetur
            asperiores iusto accusantium modi recusandae. Incidunt autem iusto
            aut dolores reprehenderit corrupti vitae quae? Similique ratione
            alias aliquid nesciunt quidem quisquam porro!
          </p>
          <a href="#section1" class="info-btn btn">LOREM</a>
        </div>
      </div>
    </header>

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

How to alternate the display of a single li element in React?

I am looking to swap the colors of the active element in my project. Currently, both elements change style when clicked. How can I resolve this? Thanks! function Language() { const [isStyled, setIsStyled] = useState(false); const toggleStyle = ...

Optimizing Angular for search engines: step-by-step guide

Regarding Angular SEO, I have a question about setting meta tags in the constructors of .ts files. I have implemented the following code: //To set the page title this.titleServ.setTitle("PAGE TITLE") //To set the meta description this.meta.addTag ...

What is the method for reversing the colors on a specific portion of an element?

Picture this scenario: you have a square shaped div with the following styling: div { background: red; height: 5rem; width: 5rem; } How would you modify its styling so that the color of the bottom half of the red square is inverted? ...

Enhance the functionality of a directive by incorporating the ui-mask directive

Recently, I implemented a custom directive for an input field that adds a calendar icon with a datepicker to the input. I have used this directive in various sections of my application and now I am interested in incorporating ui-mask - another directive I ...

What is the best way to ensure that the drop shadow on the bootstrap carousel is not confined by the parent element?

I'm currently using bootstrap's carousel to display items, and I've noticed that the box-shadow attribute on my item blocks is being limited by the parent element that wraps it. https://i.sstatic.net/ks5lC.png Is there a way for me to ensu ...

Using colored circles in ASP Dropdownlist ListItems (without jQuery): A step-by-step guide

Objective: To create a Dropdown list that displays a green circle if someone's Availability is True, and a red circle if someone's Availability is False. Note: The task needs to be accomplished without the use of jQuery, as it is restricted in t ...

Tips for creating a layout featuring images and text placed next to each other in harmony

Is it possible to replicate this section layout using Bootstrap? Take a look at Image 1. ...

Template for developing projects using JavaScript, HTML5, and CSS3 in Visual Studio 2013

After recently downloading Visual Studio 2013 Express for Web, I am struggling to figure out how to deploy projects that only consist of JavaScript, HTML5, and CSS3. Despite searching online for JavaScript templates and even trying to download Visual Stu ...

What is the best way to hide only the rows in a table when it is clicked using JavaScript?

Is there a way to hide rows in these tables by clicking on the table head? I'm currently using bootstrap 5 so JQuery is not an option. <table class="table table-info table-bordered"> <thead id="tablea"> ...

Securing HTML pages with ASP.NET MVC authentication

Within my MVC application, there are a few static (pure html) pages that require authentication to prevent unauthorized access. Is there a method to achieve this without transferring all the code to asp files and incorporating a controller, which would t ...

What could be causing the issue of images not loading in Chrome while they are loading perfectly in Mozilla when using CSS3?

Recently, I dove into a project utilizing react with webpack, and everything was smooth sailing until I encountered the CSS hurdle. Despite successfully configuring webpack, trouble brewed when it came to styling. Specifically, setting the background image ...

Buffering ceases on the video

I am experiencing an issue with 4 videos and a preloader, which should hide once all the videos are fully buffered. <div id="preload"> <h1> Download... </h1> </div> <video preload="auto" class= ...

Adjust the vertical positioning according to the percentage of the width

Currently, I am attempting to modify the position of my navigation bar on the screen in relation to the screen width rather than the height. I previously attempted using top: 10% in CSS, but realized this is based on the screen's height instead of its ...

Changing the Div Class in Master page from a Child page is a straightforward process that involves modifying

I am trying to dynamically change the style of a div element in the master page from my child page. This is the code I am using: protected void ShowMsgText(int MsgID) { HtmlGenericControl MsgInner; MsgInner = ((HtmlGenericControl) ...

Retrieving data from several checkboxes in AngularJS

Here is my code snippet where I am attempting to retrieve values of multiple selected checkboxes, but it seems to be malfunctioning. Can you please guide me on what mistake I might be making? Just to clarify, the list is dynamic and will not be limited to ...

How can I make a dropdown menu appear when I select a checkbox?

Is it possible to have a dropdown menu with changing options appear when I click on a checkbox using HTML and JavaScript? I have experimented with some code snippets in JavaScript as a beginner, but I am unsure if they are needed here. Is there an altern ...

Activate Jquery to display the submenu when clicked and conceal any other open submenus at the same time

I'm trying to create a responsive menu with menus and submenus using JQuery. However, as a newbie to JQuery, I'm struggling to hide a previous submenu when displaying another one. Here's the CodePen link Below is the HTML code: <nav cl ...

Inquiring about the functionality of CSS hover effects

Is it possible to change the CSS of element 2 when hovering over element 1 with the mouse? For example: <h1 id="text1">Text1</h1> <h1 id="text2">Text2</h1> #text1 { color:Green; } #text1:hover -> #text2 How can I achieve ...

Troubles encountered with doctype while utilizing HtmlAgilityPack

My goal is to generate an XHTMl file from scratch using HtmlAgilityPack. Following the guidance provided in this Stack Overflow post, I attempted to add a doctype to the file: private static HtmlDocument createEmptyDoc() { HtmlDocument titlePage = new ...

What is the best way to replicate touch functionality on mobile browsers for both Android and iPhone devices?

Recently, while developing a web application, I ran into an issue on mobile browsers where the :active pseudo class wasn't functioning properly. I am currently utilizing CSS sprites and looking for guidance on how to simulate clicks for mobile browser ...