Positioning children in CSS Flexbox: one at the top and the

Is there a way to position the a element at the top and the button at the bottom, using only flex properties without absolute or fixed positioning? Currently, the button is at the bottom but the title is not at the top:

.all {
    display: flex;
    height: 300px;
}
.element1 {
    width: 50%;
    background: blue;
}
.element2 {
    background: red;
    width: 50%;
}
#details {
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: self-end;
}
#details a {
    width: 100%;
}
#details button {
    width: 100%;
    height: 50px;
}
<div class="all">
  <div class="element1"></div>
  <div class="element2">
    <div id="details">
      <a>title</a>
      <button>Add TO Cart</button>
    </div>
  </div>
</div>

Answer №1

To create a layout where children are separated using flex-direction: column; on #details and justify-content: space-between;, use the following CSS:

.all {
  display: flex;
  height: 300px;
  background: gray;
}

.element1 {
  width: 50%;
  background: blue;
}

.element2 {
  background: red;
  width: 50%;
}

#details {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

#details a {
  width: 100%;
}

#details button {
  display: block;
  width: 100%;
  height: 50px;
}
<div class="all">
  <div class="element1"></div>
  <div class="element2">
    <div id="details">
      <a>title</a>
      <button>Add TO Cart</button>
    </div>
  </div>
</div>

Answer №2

To apply the flex property to both child elements and adjust their alignment, utilize align-self. First set it to flex-start, then switch to flex-end.

.all {
    display: flex;
    height: 300px;
}
.element1 {
    width: 50%;
    background: blue;
}
.element2 {
    background: red;
    width: 50%;
}
#details {
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
}
#details a {
    width: 100%;
    display: flex;
    align-self: flex-start;
}
#details button {
    width: 100%;
    height: 50px;
    display: flex;
    align-self: flex-end;
}
<div class="all">
  <div class="element1"></div>
  <div class="element2">
    <div id="details">
      <a>title</a>
      <button>Add TO Cart</button>
    </div>
  </div>
</div>

Answer №3

Make the most of flexbox by utilizing it fully.

Instead of using percentages, consider setting flex-grow for flex items to determine their widths. When done correctly, this approach leads to cleaner code.

To position a title at the top and a button at the bottom, employ flex-direction: column along with justify-content: space-between.

.all {
    display: flex;
    height: 300px;
}
.element1 {
    flex-grow: 2;
    background: blue;
}
.element2 {
    flex-grow: 1;
    background: red;
}
#details {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
}
#details button {
    width: 100%;
    height: 50px;
}
<div class="all">
  <div class="element1">&nbsp;</div>
  <div class="element2">
    <div id="details">
      <a>title</a>
      <button>Add TO Cart</button>
    </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

`Count the number of rows needed to accommodate text line breaks within a div element`

Is there a method to determine the number of lines that text breaks into using the CSS property word-break: break-all? For example, if I have a div like this: <div>Sample text to check how many lines the text is broken into</div> And the corr ...

Using a zoom background image in an HTML document without any accompanying text

I am trying to create a zoom effect on a background image without affecting the text overlaying it. Here is the CSS code: .page1-box { width: 1200px; height:800px; overflow:hidden; } .page1 { width: 100%; height: 100%; background: ...

Displaying content on the <div> element

Looking for recommendations for a jQuery plugin or JavaScript solution that allows me to load a full "view" into a <div> when a user clicks on a link. The challenge I'm facing is that I have 8 pages, with the Homepage consisting of 3 divisions: ...

Tips for expanding the width of an image when employing position:relative

Below is the code snippet: <table border="1" cellpadding="2" cellspacing="2" height="250"> <tbody> <tr> <td> <div style="background: url('path to image'); width: 300px; height: 250px; position: ...

Difficulty with MultiHandleSliderExtender and postback in JavaScript

Attempting to implement the multihandlesliderextender (from the Ajax Toolkit) for creating a price filter option on a webshop. Upon selecting a new price, it should trigger a reload of everything including extensive behind-the-scenes code. Referenced an a ...

Error: The variable "deleted1" is not declared and cannot be used on the HTML button element's onclick

Hello, I need assistance from someone. I encountered an error message after trying to delete a row even though I have declared the button already. Error: deleted1 is not defined at HTMLButtonElement.onclick Could it be due to the script type being modul ...

Does CSS positioning change depending on the screen size?

I need help fixing the positioning of my logo on the screen. I want it to be 40px from the top and centered horizontally regardless of the screen size. Here is the code that I have: <html> <head> <style type="text/css> body{ back ...

What is the solution for the error "At-rule or selector required" ?

Can someone help me with my CSS code issues? I'm having trouble on the first and last lines. Total newbie here :) Error Messages: at-rule or selector expected (ln 1, Col 1) at-rule or selector expected (ln 51, Col 1) &bso ...

The issue arises when attempting to use Bootstrap date and time components simultaneously

I am encountering an issue with the date picker and datetimepicker when used together in my form. If I only work on time or date individually, they function properly. However, when both are included, the time is not working correctly as it displays the dat ...

Retrieve information from a SQL database table and present the output in an HTML table format

I am trying to display the result of a query to a remote ODBC SQL database in an HTML table using PHP. I have successfully established the connection with the database, but when I attempt to execute a query and display it in a table, nothing appears. I am ...

How can you automatically adjust the height of a div to be 100% of its parent while maintaining top and bottom margins

http://jsfiddle.net/YumHS/ The page showcased in the provided jsfiddle link includes a sidebar within the content div. <section class="cont"> <div class="sidebar"> <ul> <li> ...

Steps for implementing an <h1> element with the React Material UI theme

I have chosen the 'dark' theme and would like to apply it to an h1 element. How can I achieve this? The method below does not yield the desired result: <MuiThemeProvider theme={theme}> <h1>Page Title</h1> ... The following ...

Ways to efficiently display elements in a grid in real-time

Attempting to design a layout where each row consists of 3 cards using Bootstrap's Grid layout for responsiveness. The challenge lies in the fact that the card data is stored in JSON format, and the length of the JSON Object array may vary, leading t ...

Tips for capturing the dx dy SVG attribute using CSS

Seeking advice on the most effective way to access the dx dy SVG attribute in CSS. If this is not feasible, what would be the best approach in JavaScript? ...

Error: The PDFJS variable is not recognized when trying to load a PDF file

I've been experimenting with pdf.js to load PDFs into a web application in order to extract information in real-time. However, I keep encountering an error even with a simple example. I've attempted enclosing the code in $(document).ready() as a ...

Utilizing JQuery slideToggle() to Toggle Between Opening and Closing Multiple Divs

Utilizing Jquery's slideToggle() function to manage the opening and closing of my main menu has been a success. However, I am facing the challenge of making sure that when one div is opened, another one closes. Despite trying various methods, I have ...

Changes in vertical position of link icon within div based on the browser and operating system

Trying to design a straightforward 3-line mobile menu dropdown, but encountering inconsistency in appearance across different devices. The vertical positioning of the icon is varying based on the operating system and browser version: Linux -desktop Fire ...

Tips for increasing the number of inputs within a form using <script> elements

I am currently working on a form within the script tags and I would like to include additional input fields before submitting. However, the submit button seems to be malfunctioning and I suspect that there may be an issue with how I am accessing it in my c ...

Display or conceal a <div> segment based on the drop down selection made

A dropdown menu controls the visibility of certain div elements based on the selection made. While this functionality is working for one dropdown, it's not working for another even though the code is very similar. I've tried various solutions but ...

Creating a vertical scrollable container that spans the full height of the PDF document

I am currently working on embedding a PDF file into my website. I'm trying to hide the status bar, including download buttons and page numbers, through some clever techniques so that I can control them externally. My goal is to be able to redirect use ...