Aligning a div in the middle of an absolutely positioned div vertically

Currently, I am facing an issue with the layout of my website's main body content section. There is a specific element positioned absolutely towards the bottom, with some space between it and the footer.

I have tried various approaches and exhausted my ideas to vertically center another part of the body content between this bottom absolutely positioned element and the top of the screen (which, in this case, refers to the top of the body section due to the presence of a header).

The challenge arises when attempting to use the vertical-align method with table cells, as the height: 100% option does not align the content between the header and the absolutely positioned bottom element above the footer.

For reference, here is a demo link for further context:

Considering that this is a responsive website design, there could be two potential heights for the bottom absolute row. Is achieving this using CSS alone feasible, or would JavaScript intervention be necessary?

Answer №1

To vertically center elements, you can utilize the transform: translate property.

Start by giving the element a position of either absolute or relative, and then move it down 50% of its parent's height using top: 50%. Here's an example:

.vertical-align {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
}

Now, the top of the element .vertical-align will be centered within its parent. The next step is to adjust it up by 50% of its own height. This adjustment can be achieved with javascript or simply using transform: translateY(-50%).

.vertical-align {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;

  transform: translateY(-50%);
}

Check out how this works in action with this pen: http://codepen.io/anon/pen/Ckyvz

For information on browser support for 3d transforms, visit: http://caniuse.com/#feat=transforms3d

Don't forget to add vendor prefixes to the transform property depending on browser requirements.

Enjoy experimenting!

Answer №2

Implement the calc(); function in your CSS code.

.center_div{
    height:calc(100% - 200px);
}

This calculation is based on the assumption that the absolute div has a height of 200px.

Answer №3

To center an element, you can use the following method:

element{
    position: absolute;
    left:0;
    right:0;
    top:0;
    bottom:0;
    width:100px;
    height:100px;
    margin:auto;
}

This code snippet is for vertical alignment to the center.

element{
    position: absolute;
    top:0;
    bottom:0;
    height:100px;
    margin:auto 0;
}

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

Custom date formatting with jQuery table sorting

I have been using a jQuery plugin called Tablesorter to sort a table. I am facing an issue with sorting dates in the yyyy MMM dd format, especially because my date inputs are in French. Here is an example of how the dates are formatted: 2014 janv. 05 20 ...

Are there differences in alignment?

I'm looking to create a unique visual effect where text is aligned differently on each line, shifting left, right, center, or wherever else, with the alignment origin varying by just a few pixels. This would be perfect for adding some visual interest ...

What is the best way to convert a string to JSON format using Javascript?

I've been working on retrieving the results of a PHP query into a JavaScript object. However, I encountered an error message in the console saying Uncaught SyntaxError: Unexpected token { in JSON at position 66. I understand that this error occurs bec ...

Mysterious JQuery attribute

I've come across a piece of code that utilizes an unfamiliar selector that I can't seem to figure out: $("div[tag=" + someVariable + "]").css("display", "block"); From what I gather, the selector is searching for a div element with an attribute ...

Discovering the technique to dynamically load various content using jQuery in a div based on its

After discovering this code to extract information from the first div with an id of container (box4) and retrieve any new data from the URL, everything was functioning correctly. However, I encountered an issue when attempting to load box5 and rerun the co ...

How can I retrieve the URL of images listed in my CSS file using Python?

I need help extracting all image URLs used in a CSS file. Here is an example snippet from my CSS file: sitewrap { width: 890px; margin: 0 auto; background: #ffffff url(../images/bg_sitewrap.gif) repeat-y; } Can anyone advise on how to extract ...

Clear previously filtered items

I am currently working on implementing a search functionality using Javascript for my project, but I've hit a snag. After hiding certain items, I'm having trouble making them appear again. JSFiddle The code I have so far is as follows: $(' ...

Unable to insert text using selenium into a field that contains an angular 2 directive

Having spent 4 years working with Selenium, I encountered a unique challenge for the first time. I struggled to find a proper way to input text into a field because our application utilizes Angular 2, which seemed to be causing issues. The required field h ...

Is it possible to achieve the expected outcome at the end of an arrow function without using the filter method?

Here's the coding section - {data?.results ?.filter(item => { if (searchTerm === '') { return item; } else if ( item.title .toLowerCas ...

Using a single AJAX function to write on various tags

Information regarding the likes and dislikes count is retrieved from the server using an ajax function with the following queries: $q3="select count(value) as a from rank where personID='$person' and itemID='$itemID' and value>0 ...

Managing errors in React Router on the server-side

I am currently working on an isomorphic application using react-router and express. My goal is to implement custom error pages that will be displayed in case of server-side errors, rendering errors, or when a page is not found. However, I am facing difficu ...

Tips for updating a table using jquery

I was searching online for a solution to my question and came across the closest query that almost resembled mine. How to refresh a div? However, I am still struggling to find the exact answer to my query. The issue at hand is that I have the following c ...

Tips for updating the color of carousel image slider indicators (arrows) specifically for the second slide

Is there a way to customize the carousel image slider indicator arrows for only the second image slide? For the first slide, I want the indicators to be black, but for the second slide, I would like them to be white. Can this be achieved? I attempted to ...

Do we need to have the 'input type file' field as

I am currently working on a PHP form that includes mandatory fields for saving the data. For example: <td><input class="text_area required" type="text" name="new field" This form also has Javascript code with file upload functionality, which I ...

Utilize Vuex's $store.watch method to dynamically add and remove event listeners

Here's the section where I'm monitoring my store using mounted(): this.$store.watch( (state, getters) => state.windowState && state.chatState == 0, (val, oldVal) => { if (val) { console.log('state is tr ...

What could be the reason for a particular product edit page showing up completely blank?

In my ongoing project, I am developing an admin panel that allows administrators to add new products to their website. These products are then stored in a Firestore database and managed using Redux Toolkit. The added products can be viewed and edited in th ...

Preventing Multiple Form Resubmissions in PHP

I have multiple forms that require navigation from the initial form to the final form. Each form includes both 'NEXT' and 'BACK' buttons for moving forward and backward in the process. I am looking for a way to prevent the confirmation ...

Troubleshooting problem with JQuery autocomplete display

I've come across this question multiple times, but I just can't seem to crack it. I'm using a remote source to retrieve JSON data and while I can see the data in the console, I'm struggling to get it to output. What I really need is for ...

Revamping an npm package on GitHub

Currently, I am managing a project that has gained popularity among users and has received contributions from multiple individuals. The next step I want to take is to convert the entire library into TypeScript, but I am unsure of the best approach to ach ...

Implemented a solution to ensure the menubar remains fixed when zooming in and out

When zooming in and out on a webpage, the menu does not stay fixed to the header image shown in the figure below: The CSS code for this issue is as follows: #navmenu{ z-index:99999; margin-top:40px; position:absolute; width:100%; m ...