Attempting to consistently place an element at the bottom of my div container

I am attempting to align my

<span class="fechar_fancy">Back/span>
at the bottom of my div, regardless of the height of my content. I want this element to always be positioned at the bottom.

Therefore, I tried using position:absolute and bottom:0 on the element I want to position at the bottom, relative to its container, but it is not working as expected.

Can anyone identify why it is not working?

Here is a link to my fiddle showcasing my attempts: http://jsfiddle.net/KHU7h/1/

This is my HTML:

<div id="janela_fancybox-container">
  <div id="janela_fancybox">
    <h2>Title</h2>
    <span id="data">Date time today</span> <br /> 
    <img class="img_principal" src="../image1.jpg"/>
    <p>
    (Content)
    </p>
    <span class="fechar_fancy"><strong>Back</strong></span>
  </div>
</div>

My CSS:

#janela_fancybox
{
    text-align:center;
    width:100%;
    margin:0 auto 0 auto;
    background:#fff;
    position:relative;


}

#janela_fancybox .img_principal 
{
    width:180px;
    height:200px; 
    float:left;
    margin-right:10px;
    border:5px solid #f3f3f3;
    margin-top:15px;
}



#janela_fancybox #data
{
    width:100%;
    color:#ccc;
    font-size:13px; 
    text-align:center;
    color:#7a7a7a;
}


#janela_fancybox h2
{
    width:100%;
    color:#004B97;
    font-size:16px; 
    text-align:center;  
}

#janela_fancybox p
{
    font-size: 14px;  
    text-align:justify;
    line-height:25px;
    height:25px;    
    word-spacing:-2px;
    width:96%;
    margin-top:15px;
}

#janela_fancybox .fechar_fancy
{
    float:left; 
    text-decoration:none; 
    font-size:15px; 
    color:#000; 
    width:100%;
    text-align:center;
    cursor:pointer;
    position:absolute;
    bottom:0;
} 

Answer №1

The issue that stands out to me is the CSS rule #janela_fancybox p with height:25px, preventing #janela_fancybox from adjusting its height based on text content. I have removed this height declaration:

#janela_fancybox p {
    ...
    /*height:25px;*/
}

Furthermore, for the absolutely positioned element, adding left:0 ensures it aligns to the left and expands to 100% width, eliminating the need for float:

#janela_fancybox .fechar_fancy {
    /*float:left; */
    ...
    left:0;
}

To prevent overlap with text content, I included a padding at the bottom of the container to accommodate the "back" link:

#janela_fancybox {  
    ...
    padding-bottom:25px;    
}

Example in action on jsfiddle


In this scenario, using position:relative instead of absolute positioning may suffice post-text. However, consider the wider context of your application before making changes.

View the demonstration here.

Answer №2

The issue at hand is that the div#fancybox is not properly clearing the floated elements inside it. One solution could be to apply the clearfix class and include it in the div#fancybox. Additionally, there seems to be a specified height on the p tag which is causing the text to exceed that limit (consider removing it).

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

Problem with the overflow setting of a specific div

My website is currently hosted on a test server at medexcel.comeze.com. However, I am encountering an issue where the overflow of the top div is no longer hidden when I hover over a menu button in the top bar. This results in the bottom right corner of th ...

Interactive pop-up text box for data cells in a table

I have implemented a textarea in the td cell of each row within column 3 to store description specific to that row. The goal is for the current description in the td's textarea to be copied over to the textarea located inside #div_toggle when the user ...

Ways to trigger the onclick event from different controls

I have a videojs player and a button on my html page. The videojs player supports the functionality to pause/play the video when clicked. I want to trigger this event by clicking on my button. How can I achieve this? Here is the code that I tried, but it ...

Tips for extracting content data from a Microdata meta tag within a designated class

I am attempting to extract all the values associated with dateCreated. My current approach involves using Selenium webdriver with Python. <div class="bv-content-datetime" role="presentation"> <meta itemprop="dateCreated" content="2018-08-28" ...

Combining strings with JQuery

Looking for help with a code snippet <script> function goToDetails($i){ $(function(){ var transValue = $('#trans'+$i).html(); var mileageValue = $('#mileage'+$i).html(); var engineValue = $('#eng'+$i).html ...

Tips for stopping an image from stretching the cell in flexbox using CSS

When using Flexbox to style elements, the width of the parent should be determined by the text inside child1. The image inside child2 should grow proportionately as the parent grows. However, it is important to ensure that the parent div does not exceed it ...

Creating Transparent Rounded Backgrounds in Google Chrome Packaged Apps: Achieving the same look as Google Hangout app

Looking at the screenshot below, it is evident that the Hangout app has a fully transparent design with background shadow effect applied to it. I have tried various methods in vain, such as applying CSS styling to the "html" and "body" tags of the page, a ...

stop an html page from opening a new window

When using my HTML-based application, there are instances when it needs to open another URL within an iframe. However, a problem arises when the third-party URL within the iframe also opens a new window with the same content and URL. How can I prevent th ...

The reload button retains functionality while being present within an Angular2 form, allowing for a seamless user experience. The

After some observation, I have realized that when a button is placed inside a form but has no connection or function related to the form, such as a back or cancel button, it unexpectedly reloads the entire page. For instance, let's consider having ...

Center the text within a span element in an inline-block container

I'm currently working on a website design that features a flat aesthetic. I have a header at the top and two blocks of different colors placed side by side underneath it. Initially, I attempted to use float left and right for positioning but was recom ...

The appearance of the webkit-scrollbar is not reflecting the intended style

I have successfully created a wrapper for a styled scrollbar in React JS - import styled from '@emotion/styled'; export const ScrollbarWrapper = styled.div(() => ({ maxHeight: '65vh', overflowY: 'auto', '*::-web ...

Converting a string to regular text in JavaScript (specifically in ReactJS)

When I fetch data from an API, sometimes there are special characters involved. For example, the object returned may look like this: { question : "In which year did the British television series &quot;The Bill&quot; end?" } If I save t ...

The click() function in jQuery executing only once inside a "for" loop

This is an example of my HTML code: <!DOCTYPE html> <head> <title>Chemist</title> <link href="stylesheet.css" rel="stylesheet"> </head> <body> <h2 id="money"></h2> <table border="1px ...

Using Node.js to retrieve table data from a URL

My journey with Node JS and express is just beginning as I dive into building a website that serves static files. Through my research, I discovered the potential of using NodeJS with Express for this purpose. While I have successfully served some static HT ...

Android mobile browser lacks visibility of certain elements

please provide a brief description of the image The issue I am facing is consistent across all mobile browsers, with the exception of Firefox. It also manifests in Windows Chrome devtools mode and when the device toolbar is activated. I came across a sim ...

Exploring the benefits of using Div and semantic elements in web

After spending more than two months learning HTML, I still find myself uncertain about the most practical coding approach. Despite knowing that it's important not to overthink it, I often wonder if I should stick with simplicity or add extra elements ...

Unique content on a web page when it is loaded with HTML

Is there a way to dynamically load various content (such as <img> and <a>) into divs every time a page is loaded? I've seen websites with dynamic or rotating banners that display different content either at set intervals or when the page ...

Eliminate empty space in the table cell

I am working with a table that looks like this: <table> ... <td> <div class="btn-group btn-group-sm"> <button class="btn btn-info mini"><span class="glyphicon glyphicon-duplicate"></span></button&g ...

The production build for Angular 9 Keyvalues pipe fails to compile

After successfully running my app on the development server with ng serve, I encountered a failure when trying to build it for production. The error message that popped up was: ERROR in src/app/leaderboard/leaderboard.component.html(9,17): Argument of typ ...

Is there a way to activate a click event on a particular child element within a parent element?

When attempting to click on a specific descendant of an element, I located the ancestor using ng-class since it lacked an id. yes = document.querySelectorAll('[ng-controller = "inventoryController"]') Having found the desired ancestor, ...