What is the best way to line up two flexbox items?

Having an issue with flexbox alignment, specifically trying to position two elements - one at the top of the container and the other in the center. Most examples I found involved three elements, so I devised my own solution.

#main {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: space-between;
  height: 100px;
  background-color: #dfdfdf;
}

#box1 {
  display: flex;
  justify-content: flex-end;
  background-color: #ff0000;
}

#box2 {
  display: flex;
  background-color: #00ff00;
}

#dummy {
  display: flex;
  opacity: 0;
}
<div id="main">
  <div id="box1">box1</div>
  <div id="box2">box2</div>
  <div id="dummy">dummy</div>
</div>

...and I tried the same approach for a horizontal layout.

#main {
  display: flex;
  flex: 1;
  flex-direction: row;
  justify-content: space-between;
  height: 100px;
  background-color: #dfdfdf;
}

#box1 {
  display: flex;
  justify-content: flex-end;
  background-color: #ff0000;
  width: 200px;
}

#box2 {
  display: flex;
  background-color: #00ff00;
  width: 100px;
}

#dummy {
  display: flex;
  opacity: 0;
  width: 200px;
}
<div id="main">
  <div id="box1">box1</div>
  <div id="box2">box2</div>
  <div id="dummy">dummy</div>
</div>

However, this solution requires an unnecessary dummy element. Looking for a better alternative.

Any suggestions on how to improve this?

Answer №1

A 'dummy' div is not necessary in this case. In my opinion, you should maintain display flex in the container but change justify-content from space-between to center.

Next, apply position absolute to the first child element and place it at the top of the container. Don't forget to add relative positioning to the container as well.

Check out the revised code below:

#main {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  height: 100px;
  background-color: #dfdfdf;
  position: relative;
}

#box1 {
  display: flex;
  justify-content: flex-end;
  background-color: #ff0000;
  position: absolute;
  top: 0;
  width: 100%;
}

#box2 {
  display: flex;
  background-color: #00ff00;
}

Test it out on Fiddle: https://jsfiddle.net/95Lwcbuk/1/

Answer №2

To exclusively utilize flex-box, you can encase each of your boxes in another .container element, configure them to use flex as well, with the first one set to justify-content: flex-start, and the last one set to justify-content: flex-end.

Take a look at this example:

#main {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  height: 100px;
  background-color: #dfdfdf;
}
.container {
  flex-basis:50%;
  display:flex;
  flex-direction:column;
}
.container:first-child {
  justify-content: flex-start;
}
.container:last-child {
  justify-content: flex-end;
}
#box1 {
  background-color: #ff0000;
}
#box2 {
  background-color: #00ff00;
}
<div id="main">
  <div class="container">
    <div id="box1">box1</div>
  </div>
  <div class="container">          
    <div id="box2">box2</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

Adjust the width of the flex columns?

In one of my columns, I have a list that I'm splitting using CSS to wrap the data in the next column after a specific height. However, in the demo, you'll notice there's a lot of white space between the first and second columns. I'm uns ...

Styling Text on Buttons (Using CSS)

I need to modify my button, which contains lengthy text. When the mouse pointer hovers over the button, I would like the long text to wrap into 2 or 3 lines so that more of it is visible. Additionally, I want the remaining text to be displayed with an el ...

Using data fetched from PHP in HTML via jQuery post: A step-by-step guide

I have created a function in jQuery that uses $.post to send data to a PHP file. The query in the PHP file is working fine and returning the data back successfully. JavaScript code: function send_agenda_data(cidade_data){ var data = {'cidade_dat ...

Having issues with the alignment on Bootstrap 4, any suggestions on how to fix it?

I'm struggling with the layout of a web page using Bootstrap 4, and for some reason, I can't figure out why it's not displaying correctly. I've been staring at my code for about 15 minutes now, but I just can't seem to identify the ...

Why is the oninput function in JavaScript not functioning properly?

After following a practical video tutorial step by step, I tried to implement the code provided (in HTML and JS) but nothing seems to be working! The code snippet from the tutorial includes: var $count = document.getElementById("count"), $textarea = ...

Discovering the way to retrieve background height following a window resize using jQuery

Is there a way to obtain the background height once the window has been resized? div { background-image: url(/images/somebackground.jpg); background-size: 100% 90%; background-repeat: no-repeat; width: 70%; background-size: contain; ...

The jQuery toggle function is malfunctioning when applied to the rows of a table

I have been experimenting with toggling table rows using jquery. Initially, the state is hidden This feature is functioning properly $('.table tr.items.' + name).each(function() { $(this).show(); }); Furthermore, this snippet of code is wor ...

Display text upon hovering over a button using jQuery

I'm currently attempting to implement a feature where a line of text is displayed beneath a row of buttons and the content of the text changes based on which button is hovered over. I've encountered some challenges while using the .hover() and .s ...

Increase the size of a centered image within a table

Lately, I've felt like my mind is starting to unravel. Here's the issue at hand: I've been attempting to resize an image within a table cell so that it spans the full width of the cell. Strangely enough, this seems to be harder than anticip ...

Creating a filter for a regular expression is a simple task that can be accomplished

In my Angular 7 project, I have a regular expression configured in the config.json file. I am looking for a common filter solution to match data with this regular expression and display it using data binding. For example: If the regular expression is: [0 ...

Table Header Stays Put Without Shrinking or Expanding with Window Adjustment

I have a sticky table header that stays at the top when scrolling down on my web page. To achieve this effect, I followed the solution provided on css-tricks.com/persistent-headers/. However, I encountered an issue where the sticky table header does not ...

What is the best way to position buttons on the same line, with one on the left and

How can I adjust the layout of my previous and next buttons so they appear on the same line? Currently, my buttons are displayed like this: https://i.sstatic.net/6zQxs.png <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3 ...

Tips for resolving the issue with "Text Animation"

Could someone please help with fixing this CSS animation? I want to align the text animation to the paragraph properly. I'm not confident if I am approaching this in the correct way, so if there is a simpler solution out there, I would appreciate lea ...

Display the value when the update is set

Is there a better way to echo a value in an input field if 'update' is set? Here is the code I have been using: <input type="text" class="form-control" name="InputTitle" id="InputTitle" placeholder="Enter title" <?php if (isset($_GET[&apos ...

Tips for effectively locating code within a web browser's code base

Struggling as a new programmer in a new job with a fresh codebase, I am finding it difficult to connect the code to the webpage I am currently on. What methods can I use to locate the relevant HTML in the codebase that is being rendered in the web browser? ...

Tips for arranging two svg elements next to each other within a larger div

Hey everyone, I've been working on some HTML and JavaScript but I'm stuck on a basic issue. I have a simple code with a div containing two inner divs, each with an SVG element equal to the size of the parent div. My goal is to have these two inne ...

Ways to verify the application of CSS hyphens using Chrome's developer tools

I cannot seem to get automatic hyphens to work via CSS. I have applied the following CSS: body { -moz-hyphens: auto; -ms-hyphens: auto; -o-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } Despite adding this CSS, my text is not being hyph ...

How can I display the secondary menu in Wordpress on specific pages only?

I recently set up a secondary menu in my WordPress theme by adding it to the function.php file. However, now both the primary and secondary menus are showing on every page, giving me double menu bars on the top of my home page and about us page. I only wan ...

Troubleshooting: Issue with detecting keypress using jQuery

Currently, I have a jQuery script that checks password strength and changes an image source based on complexity. The functionality works smoothly within jsFiddle (set to jQuery 1.91), but when implemented on my webpage, the event seems to not be triggered. ...

Adjust the size of the text and the textarea at the same time

I am trying to resize text simultaneously while resizing the textarea in my code. I am using jQuery for this functionality. However, I believe there is an issue with the click function being separated. Any advice on how to fix this would be greatly appreci ...