Troubleshooting Flexbox alignment stretch problem

Looking for help with a layout challenge. I have a container with 2 columns: an image on the left and two <p> elements on the right. My goal is to position one <p> at the top and the other at the bottom of the container.

I attempted to use flexbox and tried using align-items: stretch, but it didn't quite achieve the desired effect. However, using flex-start and flex-end did work as expected.

Is it possible to achieve this layout using flexbox?

.container {
  display: flex;
  align-items: stretch;
}
img {
  display: block;
  width: 100%;
}
.element-box {
  height: 100%;
}
<div class="container">
  <div>
    <img src="https://placeimg.com/640/480/nature">
  </div>
  <div class="element-box">
    <p>Should be on Top</p>
    <p>Should be on Bottom</p>
  </div>
</div>

Answer №1

To create a element-box as a column in a flexbox, simply add display: flex; and justify-content: space-between;. Make sure to remove height: 100% from it.

Check out the demo example below:

.container {
  display: flex;
  align-items: stretch;
}
img {
  display: block;
  width: 100%;
}
.element-box {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
<div class="container">
  <div>
    <img src="https://placeimg.com/640/480/nature">
  </div>
  <div class="element-box">
    <p>Content on Top</p>
    <p>Content on Bottom</p>
  </div>
</div>

Answer №2

Ensure you apply flexbox to the second container as well, especially if it contains <p> elements. Here is an example of CSS that can guide you:

.element-box {
display: flex;
flex-direction: column;
justify-content: space-between;
flex-grow: 1;
}

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

Adjust the image's background color within an SVG file

Is there a way to change the background color of an image tag within an SVG? I currently have a transparent image loaded in the image tag, but I would like to add a colored shade using a color picker. <svg width="200" height="200" xmlns="http://www.w ...

Prevent unwanted padding in lists by using CSS float inside

When organizing my list element, I want to position the control buttons next to the left of the list item text. To achieve this layout, I have applied the CSS property float: left;. However, a problem arises when there are multiple lines in a list - each n ...

Utilizing JavaScript to dynamically set a CSS file from an API in AngularJS

I am currently facing an issue with integrating a CSS file returned by an API into my entire website. Since the URL of the CSS file keeps changing, hard coding the URL is not feasible. While I have successfully implemented this on 'view1', my goa ...

Mastering the Material-UI Grid in SPAs: Conquering the Negative Margin Dilemma

While attempting to build a single page application (SPA), I've encountered an issue with using the Grid component from material-ui. Normally, I rely heavily on the Grid, but in this new project, something seems amiss. The current problem is evident ...

The positioning of the <a> tag seems to be slightly skewed

My goal was to add a simple dotted underline animation to the menu bar on my website, so I crafted the following HTML: <nav role="navigation" class="navigation"> <ul class="nav-listed-links"> <li class="active"><a href="">Ho ...

What are the steps for utilizing the first-child pseudo-class?

I'm looking to target the first instance of a div with the class "aProduct", but I'm having trouble figuring out how to do so. Here is what I've attempted: <div id="kasticketProducts"> <div class="aProductHeader"></div> ...

jQuery sliding toggle effect combining multiple div sets

I have two columns, A & B. Some items in A open multiple items in B. A will toggle its corresponding B Items - BUT clicking on an A that includes an already opened B starts to mess up the toggle. I want each item in A to open its respective items in ...

Exploring the possibility of utilizing jQuery for limited incremental scrolling

Is there a way to control the scrolling on a website so that users transition through slides sequentially? Take a look at this example: No matter how much you scroll, the website transitions to the next or previous slide one at a time (unless a specific ...

How can the color of the <md-toolbar> be customized?

Here is a glimpse of how my code appears on CodePen: I am aiming to have the background color of "Sidenav Left" match that of "Menu Items", which is designated by the class .nav-theme { background-color: #34495E } I attempted to override it like this ...

Expansive menu that stretches the full height of the webpage

I'm having difficulty making my side spry menu extend the full length of the webpage. I tried using this code: $("nav").css({ "height" : $("nav").height() }); but it still isn't working as expected. I just want the grey color, like ...

click to delete submenu dropdown

I need assistance with removing or hiding the dropdown submenu when clicking on the item. We are using bootstrap for CSS. Can someone please help me? Below is the code snippet: <ul class="dropdown-menu" style="display: block; position: static;"> & ...

There appears to be a slight issue with the tailwind dark mode not fully extending when scrolling to the bottom of the page

While using dark mode in a Next.js web app with Tailwind, you may have noticed that when scrolling, if you go past the scroll container (almost as if you're bouncing off the bottom or top of the page), the dark mode doesn't extend all the way. In ...

Creating an active tab using Material UI on a particular route

I've encountered an issue with Material UI tabs and need help figuring out how to resolve it. I have a navbar with three different tabs that link to separate URLs, but two of them are quite similar. Take a look at my code snippet below: <Tabs indic ...

Seeking a jQuery Carousel with a Blizzard-inspired design

Can anyone recommend a jQuery carousel that has similar features to the one found on Blizzard's homepage? I'm looking for a carousel that is centered, does not show horizontal scroll bars even if the content extends beyond the browser width, supp ...

Issues with Javascript Date Function malfunctioning

I'm having trouble getting the text 'Oct 09, 2012' to display properly. Instead of running the function correctly, a bunch of unnecessary date text is showing up. Can anyone figure out what I'm doing wrong? Feel free to experiment with ...

How can I incorporate classes from multiple CSS files into my WordPress theme?

Currently, I am utilizing wp_enqueue_style to incorporate two CSS files within a single function. However, the issue that arises is when I try to apply a class, it seems that only classes from one of the files can be utilized and it ends up overriding anot ...

The target="_blank" attribute does not seem to function properly within an iframe

I've been tackling the WordPress media library tab recently. Within this iframe, I included a link with target="_blank". My expectation was for this link to open in a new browser tab or window, but instead, it opens within the iframe tab. Here's ...

The margin for the navbar cannot be set to "0"

I'm currently in the process of building a website, and I decided to experiment with integrating Bootstrap into it. I've utilized the navbar tags along with some custom CSS, but I'm facing an issue with removing the margin. As a result, the ...

Text positioned adjacent to image on the right side

The content is not wrapping properly on the right side of the image. It should be aligned against the image with a slight spacing in between. This is how the bar is supposed to look, and underneath there should be a spacer (line) https://i.sstatic.net/DGI ...

HTML5 CSS and Rails theme

I am currently utilizing a free HTML5 template found at this URL: Despite my efforts, I am unable to successfully implement the background images from the main.js file (bg01.jpg, bg02.jpg, bg03.jpg). How should I correctly reference these image files wit ...