The wonders of CSS flexbox and the power of justify-content

I am working on a layout where I need three flex items (rows) in a flex container, and my requirement is to justify them as space-between... The first row will consist of cloud tags, the second will have a price, and the third will contain a Read more link.

However, there may be cases where only the last row (Read more link) needs to be displayed for specific items.

In such scenarios, to maintain consistency, I want the Read more link to always be positioned at the bottom of the container; but using space-between alone does not achieve this desired layout...

Is there a way to set a fallback justify-content property to end when there is only one child item present?

.container {
  background-color: #aaa;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: flex-end;
  height: 200px;
  margin-bottom: 20px;
}

.tags {
  display: flex;
}

.tags span {
  background-color: #f0f;
  padding: 10px;
  margin: 0 0 0 10px;
}

.price {
  display: flex;
  background-color: #ff0;
  padding: 10px;
  font-size: 150%
}

.read-more {
  display: flex;
  background-color: #0ff;
  padding: 10px;
}
<div class="container">
  <div class="tags">
    <span>tag 1</span><span>tag2</span><span>tag 3</span>
  </div>
  <div class="price">
    $100
  </div>
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>

<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>

Answer №1

To rearrange the order of flex-items in your HTML code, you have the option to flip them and apply flex-direction: column-reverse; on the container. This will position the "read more" element as the initial flex-item, with the reversed direction at the container's bottom:

.container {
  background-color: #aaa;
  display: flex;
  flex-direction: column-reverse;
  justify-content: space-between;
  align-items: flex-end;
  height: 200px;
  margin-bottom: 20px;
}

.tags {
  display: flex;
}

.tags span {
  background-color: #f0f;
  padding: 10px;
  margin: 0 0 0 10px;
}

.price {
  display: flex;
  background-color: #ff0;
  padding: 10px;
  font-size: 150%
}

.read-more {
  display: flex;
  background-color: #0ff;
  padding: 10px;
}
<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
  <div class="price">
    $100
  </div>
  <div class="tags">
    <span>tag 1</span><span>tag2</span><span>tag 3</span>
  </div>
</div>

<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>

Answer №2

To position the container in the bottom right corner with the attributes

position: absolute; bottom: 0; right: 0;
, you can assign the container a position: relative; property, along with the .read-more class and the :only-child pseudo-class. This will ensure that when the read more link is the only child in the container, it will be placed at the desired location.

Using justify-content: end !important; does not achieve the same positioning effect as adding the specified attributes to the read more link.

Here's an example:

.container {
  background-color: #aaa;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: flex-end;
  height: 200px;
  margin-bottom: 20px;
  position:relative;
}

.tags {
  display: flex;
}

.tags span {
  background-color: #f0f;
  padding: 10px;
  margin: 0 0 0 10px;
}

.price {
  display: flex;
  background-color: #ff0;
  padding: 10px;
  font-size: 150%;
}

.read-more {
  display: flex;
  background-color: #0ff;
  padding: 10px;
}

.read-more:only-child{
    position:absolute;
    bottom:0;
    right:0;
}
<div class="container">
  <div class="tags">
    <span>tag 1</span><span>tag2</span><span>tag 3</span>
  </div>
  <div class="price">
    $100
  </div>
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>

<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </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

What is the best way to ensure that a React app is always displayed in full screen

I am facing an issue while developing an app with React and Material UI. I am trying to display the app in full-page mode, but unable to achieve it successfully. Currently, it appears like this: Here is my code snippet from App.js: import 'typeface- ...

Difficulty in making Materialize.css column match the height of the entire row

Utilizing React.js, Materialize.css, and certain MaterialUI components in collaboration to build a website. Developed a header (React component, basic div with title and logout button) for the site using a Materialize.css "grid". Issue: The react compone ...

Arranging shapes for varying levels of magnification

Seeking assistance with properly aligning two forms. I have tried using a positioning approach, but the layout gets disrupted when the browser's Zoom level is adjusted. The second button ends up shifting slightly either upwards or downwards. Here is t ...

Responsive Div that Resizes Proportionally within Parent Container

Having a div element that adjusts its height based on width to maintain aspect ratio, I aim to place this div within another while maximizing available space vertically and horizontally without any cropping. It seems that object-fit: contain, typically use ...

Arranging React Grid Items with Stylish Overlapping Layout

Is there a way to create a react-grid-layout with 100 grid points width, while ensuring that the grid items do not overlap? (Reducing the number of columns can prevent overlap, but sacrifices the 100-point width resolution for grid placement) import Butto ...

Fade one element on top of another using Framer Motion

Looking to smoothly transition between fading out one div and fading in another using Framer Motion, but facing issues with immediate rendering causing objects to jump around. Example code snippet: const [short, setShort] = useState(false); return ( ...

Exploring the World of Html

I'm struggling with an HTML problem related to a web programming class I'm taking. The assignment involves creating a video game using HTML and JavaScript, where an image moves randomly on the screen and the player must click on it as many times ...

What is the process for incorporating a new task into my HTML/CSS/JavaScript to-do list application when the Enter key is pressed?

Currently, I am working on developing a to-do list application using HTML, CSS, and JavaScript. The application includes a text input field for users to enter their tasks, along with an "Add Task" button. However, I want to enhance the user experience by a ...

Using jQuery to create a div that animates when scrolling

Recently, I came across this interesting code snippet: $(document).ready(function(){ $(window).scroll(function(){ if($("#1").offset().top < $(window).scrollTop()); $("#1").animate({"color":"#efbe5c","font-size":"52pt", "top":"0", "position":"fix ...

What is the process for fetching the chosen outcome from a subsequent webpage using HTML?

How can I display selected cities on a different webpage after clicking a button? Currently, I can only get the results on the same page. For example, if a user selects NYC and Delhi, those cities should be displayed on another HTML page. ...

Tips for creating a script that compiles all SCSS files into CSS within a Vue 3 project

Within my project, there exists a file named index.scss located in the src/assets/styles directory. Adjacent to this file are multiple folders housing SCSS files that are ultimately imported into index.scss. My objective is to devise a script within the pa ...

Issues with the presentation of HTML DIV elements

I am puzzled by a situation where my HTML layout to present financial data does not seem to update properly in certain browsers. Despite using jQuery and inspecting it with Firebug, the visual display of the parent divs remains unaltered. This issue become ...

What are some methods for utilizing the "name" attribute within React components?

About My Coding Environment Utilizing TypeScript and ReactJS The Issue with Using name as an Attribute Encountering the following error: Type '{ name: string; "data-id": string; "data-type": string; }' is not assignable to ...

Applying styled numbering to elements using nth-child selector in

I have a div with multiple p tags inside. My goal is to assign them numbers using CSS. The number of p tags may vary. By using the :nth-child() selector and the ::before pseudo-element, I can achieve something like this: div p:first-child::before { ...

I'm having trouble getting my accordion menu to work properly. Whenever I click on the question title, the answer doesn't seem to show up or collapse. What could be causing this issue?

I am currently working on a project to create an accordion menu where the answers are hidden and only revealed upon clicking the question. However, when I test it out, nothing happens when I click on the question. Here is my code: .accordion-Section ...

Encountering an error message stating that the "@font-face declaration does not adhere to the fontspring bulletproof syntax" while attempting to integrate a custom font

I've been struggling to incorporate a unique font into my website, but I keep encountering the same error every time. Despite my efforts, I haven't found much guidance on how to rectify this issue. Below is the code I'm using: @font-face ...

The grid area may not properly adjust based on the width size of the browser

When adjusting the width of your browser, you may notice occasional white space in the bottom right corner. https://i.stack.imgur.com/UFV6R.png https://i.stack.imgur.com/VfhMN.png Is there a way to remove this extra margin? In other words, how can I ens ...

The slideshow feature on W3 Schools does not start automatically when the page loads

After following the W3Schools tutorial to create a slideshow, I found that the animations are working correctly. However, only three dots appear on the screen and I have to manually click on one of them to view the pictures. var slideIndex = 0; sh ...

How come I am unable to connect my CSS to my EJS files?

Having difficulty linking my css files to my ejs files. Using a standard node.js/express setup, I'm attempting to connect my main.css file from the public/css directory to my index.ejs file in views. The html and routing are functioning correctly. Ple ...

Ensuring the positioning of input fields and buttons are in perfect alignment

I've been struggling to align two buttons next to an input field, but every time I try, I end up messing everything up. I managed to align an input field with a single button, but adding a second button is proving to be quite difficult. Here is ...