When a child element is positioned absolutely, the div will not automatically adjust its height accordingly

I am trying to create an effect where my image overhangs outside of its containing div. I have positioned the image absolute and the parent container relative, but now the parent div does not resize to contain the image. How can I achieve this?

Here is the HTML code:

      <div class=".twelve.columns" id="header">
        <div id="logoWrapper">
                <img src="https://nbson.com/sni/images/logo.png" class="ssImg">
                <div class="clr"></div>
        </div>      
      </div>  

And here is the CSS code:

.ssImg{
    width:100%;
}
.clr{
    clear:both;
}
#header{
     margin-top:0;
     background:#000;    
     position:relative;
     width:100%;
     border:1pt solid pink; 
}

You can view a sample on JSFiddle

Answer №1

Absolutely positioned elements are removed from the document flow, meaning their dimensions cannot impact their parent elements. If maintaining children as position: absolute is necessary, you can achieve this effect with JavaScript. Learn more at this link.

To achieve the desired effect without relying on JavaScript, consider using negative values for top or bottom positioning. Check out the updated JSFiddle for a practical example.

.ssImg{
width:100%;
}
.clr{
clear:both;
}
#header{
 margin-top:0;
 background:#000; 
 position:relative;a
 width:100%;
 border:1pt solid pink;
}

#logoWrapper{
width:15%;
min-width:120px;
margin-left:10px;
position:relative; /* this is new */
        bottom: -40px; /* this is new */
}
<div class="twelve columns" id="header">
    <div id="logoWrapper">
            <img src="https://nbson.com/sni/images/logo.png" class="ssImg">
            <div class="clr"></div>
    </div>      
</div>

Answer №2

What do you think of this solution?

body, html {
  height: 100%;
  padding: 20px;
}

.ssImage {
   width: 100%;
}

#mainHeader {
  background-color: #000;
  position: relative;
  width: 100%;
  height: 100%; /* Adjust the height as needed */
  border: 1pt solid pink;
}

#logoContainer {
  width: 15%;
  min-width: 120px;
  position: absolute;
  top: -15px;
  left: -25px;
}
<div id="mainHeader">
  <div id="logoContainer">
    <img src="https://example.com/logo.png" class="ssImage">
  </div>
</div>

Answer №3

Let's start with this:

If you're looking to assign multiple classes to an element, go with something like

<div class="twelve columns">
instead of
<div class=".twelve.columns">

Next, in reference to your inquiry:

Absolutely positioned elements are taken out of the document flow and consequently do not affect the parent element's dimensions.

To address this issue, be sure to explicitly specify the height and width for the element.

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

I am having trouble with the functionality of my bootstrap navbar toggler

Can you please review the code and help me identify the issue? The toggle button is not functioning properly, it should display the menu bar when clicked. <body> <!--navigation bar--> <nav class="navbar navbar-expand-sm navbar-dark bg-dan ...

Require this form to be centered flawlessly

<form class="form-horizontal" role="form" id="contactf" action="http://titan.csit.rmit.edu.au/~e54061/wp/form-tester.php" method="post"> <div class="form-group"> <label class="control-label col-sm-2 lb" for="email">Email : </ ...

How to disable a select list in HTML using vue.js when it is checked

I need assistance with a form that includes an "age" field. There are two possible scenarios: if the "unknown" box is checked, then the user should not be able to enter/select an age (the option should be disabled). If the box is unchecked, then the user s ...

The functionality of display flex is not functioning as expected outside of the CodePen

I attempted to create a responsive two-column layout on Codepen with success. The layout is quite simple, featuring a YouTube video and some text. However, when I tried to transfer this to my website, it failed to work... Here is the code – hoping someo ...

chaotic telerik editor arrangement

I have incorporated the Rad Editor into my ASP.NET page. <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <telerik:RadEditor ID="reSummery" runat="server" > </telerik:RadEditor> Despite not referencing ...

Position a modal in the vertical center with scroll functionality for handling extensive content

Looking for ways to tweak a Modal's CSS and HTML? Check out the code snippets below: div.backdrop { background-color: rgba(0, 0, 0, 0.4); height: 100%; left: 0; overflow: auto; position: fixed; top: 0; width: 100%; z-index: 2020; } ...

Error: Unable to find a matching route in Django

Currently, I'm in the process of developing a blog platform that offers users the ability to search for posts using specific keywords through a standard search bar. In order to achieve this, I have imported Q from django.db.models. Here is the view ...

Align the button to the right side inside a div container

As someone who isn't very skilled in CSS, I have a question for beginners. Below is an excerpt from an HTML document: <div _ngcontent-vyb-c4="" class="form-row"> <div _ngcontent-vyb-c4="" class="col-md-12"> <d ...

My CSS seems to be causing issues and generating errors in the console. What could be the problem?

My CSS was working fine just 5 minutes ago, but now it's not working and I'm not sure what the issue is. I checked the console and found this error: bootstrap.min.js:6 Uncaught TypeError: Cannot read property 'fn' of undefined at bo ...

The rendering of HTML DOM is being obstructed on iPhone devices running iOS 13

Currently, I'm developing a web-based URL stream music player using JavaScript. The player functions smoothly on Android devices, but I'm encountering an issue with DOM rendering being blocked on iPhone devices. Despite rearranging the JavaScript ...

Using HtmlUnit to select and navigate to a particular link based on its reference and name

Today is my first time using HtmlUnit, so I am still a beginner. I successfully navigated to IMDB and searched for the movie "Sleepers" from 1996. However, I encountered multiple results with the same name: You can view the search results here My goal i ...

Avoid automatically scrolling to the top of the page when a link is clicked

Within my ASP.NET MVC 5 application, there exists a partial view containing the following code: <a href="#" onclick="resendCode()" class="btn btn-link btn-sm">Resend</a> Additionally, on the View, the resendCode() function is present: funct ...

Why is my JQuery display none not functioning properly?

The functionality for showing an element is working perfectly, but when attempting to hide the same element, nothing seems to happen. Here is the HTML code after showing the element: $(".container").click(function() { $('.top-menu').css(&ap ...

Exploring the semantic-ui dropdown menu feature in Chrome

When I first started learning to code with semantic-ui framework, I noticed a difference in behavior in Chrome compared to other browsers. The pointing menu on the left element (such as the search box) breaks down in Chrome, whereas it displays correctly i ...

What's the best way to customize the color of the text "labels" in the Form components of a basic React JS module?

I have a React component named "Login.js" that utilizes forms and renders the following:- return ( <div className="form-container"> <Form onSubmit={onSubmit} noValidate className={loading ? 'loading' : ''}&g ...

Display two examples from the material-ui documentation

My goal is to merge two demos found in the material-ui documentation. Clipped under the app bar - Describes a page layout Fixed header - Illustrates a table with a fixed header. I am looking for a solution where the table occupies the entire available p ...

Any tips for modifying my jQuery script so that a div smoothly tracks my cursor's movement?

My jQuery code creates a span in the shape of a circle following my cursor, but I saw another website that uses transform: translate() instead of top and left properties for smoother cursor movement. Can someone help me modify the code to use transform: tr ...

PHP is unable to properly process JSON data that contains HTML tags

I'm encountering an issue with my website that communicates with a Java REST server using Jersey. Everything is functioning properly except when I try to receive a JSON object with HTML tags embedded in the content. After running a println statement ...

Why won't hover over function properly in separate divs for two items?

My goal is to show text when hovering over a logo, but the text and logo are in different divs. Despite trying various solutions like using display: none and display: block, I still can't get it to work. import styles from '../styles/Float.module ...

Change wiki text into HTML using Python for showcasing on a webpage

Despite trying various tools for 6 hours, I have been unable to find one that can properly interpret wikitext like the example below: '<center>Welcome to the world's foremost open content<br><big><big><big>'&ap ...