What is the best way to fix a div at the top, while anchoring the content at the bottom of the div?

Currently, I am using Pandoc to convert files to PDF and incorporating HTML/CSS for document styling, specifically for printing purposes rather than for screen display.

My objective is to replicate the "header" of a Word document. To achieve this, I have set up a div and implemented the following CSS to ensure it appears at the top of each page:

.div_header {
    position: fixed;
    top: 0;
    width: 100%;
}

Within the div_header, I aim to have three columns: an image aligned to the far left, a category centered in the middle, and two items (document title and version number) aligned to the far right. The desired layout is illustrated in this image: https://i.sstatic.net/7Oxav.png

My current progress is as follows:

<div style="border: 1px solid black" class="div_header">
  <div>
    <img
      class="left bottom"
      src="images/my_image.png"
      alt="My Image"
    />
  </div>
  <div class="center bottom">Category</div>
  <div class="right bottom">
    <div class="right-text bottom">Title</div>
    <br />
    <div class="right-text bottom">Version 0.24</div>
  </div>
</div>
.div_header {
  position: fixed;
  top: 0;
  display: flex;
  width: 100%;
  justify-content: space-between;
}
.center {
  margin: 0 auto;
  text-align: center;
}
.left {
  float: left;
}
.right {
  float: right;
}
.right-text {
  text-align: right;
}
.bottom {
  bottom: 0;
}

As a result, the output appears like this: https://i.sstatic.net/ncUlQ.png

Although the image seems to be correctly aligned at the bottom, the Category, Title, and Version are top-aligned despite setting bottom: 0. How can I ensure that all elements align at the bottom?

Answer №1

To vertically align your content items at the bottom, simply add align-items: end; along with display: flex; to the parent div.

For a live example, check out: https://codepen.io/krushna-28/pen/NWJojyX

Note the following:

  1. Avoid using float as it is considered outdated.
  2. If you use bottom: 0, ensure you also have position: absolute; instead of position: relative; for it to work correctly.

Thank you!

body {
  margin: 0;
}
.div_header {
  position: fixed;
  top: 0;
  display: flex;
  align-items: end;
  width: 100%;
  height: 400px;
  justify-content: space-between;
}
.center {
  margin: 0 auto;
  text-align: center;
}
.right-text {
  text-align: right;
}
<div style="border: 1px solid black" class="div_header">
  <div>
    <img class="left bottom" src="images/my_image.png" alt="My Image" />
  </div>
  <div class="center bottom">Category</div>
  <div class="right bottom">
    <div class="right-text bottom">Title</div>
    <div class="right-text bottom">Version 0.24</div>
  </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

Are background images covering hidden text detrimental to accessibility?

Is there a way to display images of text instead of actual text for my menu? I have implemented spans within each menu link. These spans have specific dimensions, block display properties, the text is indented to be hidden, and the background image is set ...

What are the steps to display an Angular Component on an HTML template?

Currently, I am in the process of creating a web application utilizing Angular 6. I have encountered a minor roadblock with a simple code snippet present in one of my services: method() { document.body.insertAdjacentHTML('beforeend', '&l ...

Discovering the size of content through solely the height and width of a div: Is it possible?

Can someone help me figure out how to calculate the content length based solely on the height and width of a div? For example, if a Div has a height:200px & width:200px, I would like to know that the stored content length is 298. function calculateCo ...

Troubleshooting a vertical overflow problem with Flexbox

Struggling to design a portfolio page for FreeCodeCamp using flexbox layout due to vertical overflow issues. Here is the HTML: <div class="flex-container flex-column top"> <header class="flex-container"> <h1 class="logo"><i clas ...

Issues with the Bootstrap navbar toggle functionality

Hello there! I hope you're doing well. I've been following the Bootstrap 5 tutorial on Traversy media's YouTube channel, but I'm encountering an issue with the nav toggler. The nav is not showing up for me and I can't figure out wh ...

Ways to make the submenu display underneath the main menu

Click here to view the menu It is important to note that you may need to expand the Result Box in order to see the full menu. The issue I am currently facing involves fixing a submenu that appears when hovering over the Men and Women <li> elements. ...

Toggle the visibility of a div by clicking on another div in a

I have created a unique design where a div features a background image of a face, along with a paragraph, two buttons, and an input box inside it. While I know this question has been asked before, my scenario is slightly different. I want the div with the ...

A guide to deactivating the Material UI Link API element

Previously, I relied on Material UI's Button component with a disable property that allowed the button to be disabled based on a boolean value. However, I now want to use the Material UI Link component, which resembles a text link but functions like a ...

moving a simulated element to a new location

Looking for some help with my html/CSS code that creates a unique element at the bottom of a div with an image background. The shape I've created is positioned at the bottom of the div... Below is the HTML: <div style="background-image:url(&apos ...

Troubleshooting Flexbox: Content within a Flex Item is not displaying properly within a scrollable container

I recently embarked on converting one of my websites to flexbox and encountered an issue with vertical scrolling within a flex container. It appears that Firefox displays it correctly, while Chrome and Safari do not. You can check out the code here. . ...

Rearrange the order of divs dynamically to prevent wrapping/overflow, no need for media queries

Can someone help me with this issue? I am trying to create a navigation bar with centered menus/links, a logo on the left, and call-to-action buttons and icons on the right. If the content overflows or wraps, I want the center column to move to the next l ...

Remove the source code upon accessing the inspector tool

I recently encountered a page with encrypted source code. The content is being loaded from two separate sources stored in JavaScript files. My question is: Is it feasible to completely remove the HTML source code when a user opens the inspector (F12, etc ...

After refreshing in FireFox, the "disabled" attribute of the button fails to function

On my webpage, there are 2 buttons - one enabled by default and the other disabled. When I click on an enabled button, their states switch - the enabled button becomes disabled and vice versa. This functionality works perfectly in Chrome, Edge, and IE11, b ...

Menu with dropdown navigation

Looking to create a dynamic and responsive navbar menu with items and subitems on my website. Bootstrap has proven to be challenging when adding submenus. Are there any tools, frameworks, codes or plugins that could assist me in achieving this? It's ...

What causes a span with absolute positioning to not be correctly positioned within a span with relative positioning in Firefox, unless it is displayed as inline-block or similar?

Can you explain why the code below doesn't display correctly in Firefox unless the parent span is set to display as inline-block? When inspecting the layout of the absolutely positioned span, it shows the number 184 instead of 197 on the right side. A ...

Customizing the color of cells in an HTML table within a JSON based on their status

Would you like to learn how to customize the color of cells in an HTML table based on the status of a college semester's course map? The table represents all the necessary courses for your major, with green indicating completed courses, yellow for cou ...

Arrange a cluster of CSS table cells onto a fresh line

Currently, I am exploring CSS properties to create the visual appearance of a table using two levels of DOM elements. The highest-level element wraps around its child elements, which are styled to resemble a flat list. For example: <div class="t"> ...

Switch off JavaScript beyond the parent element

Struggling with implementing an event to toggle a div using an element located outside of the parent container. I am attempting to achieve the same functionality by targeting elements beyond the parent structure utilizing a span tag. Any assistance on th ...

Building CSS columns

Similar Inquiry: Creating dual columns in HTML/CSS layout I'm exploring ways to achieve a two-column layout within a wrapping div element. Currently, I have the following CSS code snippet which centers a div inside the browser: .wrapper {width: ...

Ways to prevent my website from being accessed through the Uc Browser

Is there a way to prevent my website from functioning on UC Browser using HTML or JavaScript? ...