What is the best way to ensure the inner element has the same height as the outer element?

Struggling with arranging a flexbox 'table' in HTML to create an interesting layout at the top. Unfortunately, I can't seem to nail it down properly.

The current output: https://i.sstatic.net/Gr5cf.png

The desired result: https://i.sstatic.net/RJxyn.png

I attempted to achieve this without using inner divs and spans, opting for margin:auto. However, it ended up messing with the borders, pushing them towards the center instead of the edges. Below is the code snippet for the current output:

.flex-container {
  width: auto;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.flex-container .middle {
  flex: 1;                       
  display: flex;                 
}

.top {
  padding-top: 30px;
  border: 2px solid #05788D;
  display:flex;
}

.leftSide {
  padding-top: 30px;
  display: flex;
  flex-wrap: wrap;
  flex-basis: 50%;
  overflow: auto;
  border: 2px solid #05788D;
}

.rightSide {
  padding-top: 30px;
  display: flex;
  flex-wrap: wrap;
  flex-basis: 50%;
  overflow: auto;
  border: 2px solid #05788D;
  border-left-style: none;
}

.firstOption
{
border: 2px solid #05788D;
border-top-style: none;
border-bottom-style:none;
}

.anotherOption 
{
border: 2px solid #05788D;
border-top-style: none;
border-bottom-style:none;
border-left-style:none;
}
<div class="flex-container">

  <div class="top">
  
<div style="width:50%;">
    <span class="firstOption">One option</span>
</div>

<div style="width:50%;">
<span class="anotherOption">Another option</span>
</div>

  </div>

  <div class="middle">

    <div class="leftSide">
      left
    </div>

    <div class="rightSide">
      right
    </div>

  </div>

</div>

Answer №1

To adjust the alignment of text within a span, utilize the text-align property alongside padding to maintain the layout when styled as inline-block:

.flex-container {
  width: auto;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.flex-container .middle {
  flex: 1;
  display: flex;
}

.top {
  border: 2px solid #05788D;
  display: flex;
}
.top div {
  flex:1;

}

.leftSide {
  padding-top: 30px;
  display: flex;
  flex-wrap: wrap;
  flex-basis: 50%;
  overflow: auto;
  border: 2px solid #05788D;
}

.rightSide{
  display: flex;
  flex-wrap: wrap;
  flex-basis: 50%;
  overflow: auto;
  border: 2px solid #05788D;
  border-left-style: none;
}
.firstOption  {
 text-align:right;
}

.firstOption span,.anotherOption span{
  border: 2px solid #05788D;
  padding-top: 30px;
  display:inline-block;
}
<div class="flex-container">

  <div class="top">
    <div class="firstOption"><span>One option</span></div>
    <div class="anotherOption"><span>Another option</span></div>

  </div>

  <div class="middle">

    <div class="leftSide">
      left
    </div>

    <div class="rightSide">
      right
    </div>

  </div>

</div>

Answer №2

You have applied padding-top: 30px to the class="top". However, it would be more effective if you apply padding-top: 30px; to the inner child (which occupies 50% of the space).

Although there are better ways to achieve this, the solution provided above is the quickest fix for your issue.

Answer №3

To move the left option to the end of the div, you can utilize justify-content: flex-end. I implemented these changes in your code and you can view it in this demo.

I highly recommend checking out this comprehensive guide on Flexbox!

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

Gradually reveal each page as it loads while a spinner rotates in anticipation

I am attempting to create a fade-in effect for the entire page once it has finished loading, This is my current code: <script type="text/javascript"> $(window).bind("load", function() { $('#overlay').fadeOut(function() { ...

Is there a way to bypass cells within an html table?

Creating an online board game and looking to have the cells go around the board. Wondering if anyone knows a way to skip the cells in the middle? Attempted using   but it doesn't seem to be effective. Here is a snippet of the code: #board { ...

Mastering the art of utilizing clearInterval effectively

This reset button needs to clear/reset the timer. The issue is that when the user presses the start button more than once, it sets two timers - one from the previous start and one from the current start. I will be using clearInterval to solve this problem ...

Ways to prevent an image from expanding its width within a container

My webpage features an HTML form that takes user input and PHP code that retrieves images from a specified folder to display them in a slideshow. However, I'm facing an issue where the image width stretches and distorts the appearance of the slideshow ...

Excessive white space is appearing below the title of my card when using Bootstrap 4

As a beginner in PHP and Bootstrap, I encountered an issue with my login form. Everything was working fine until I added <link rel="stylesheet" type="text/css" href="styles.css"> to my header. I cannot simply remove these links from my header as I w ...

Error in Angular syntax

Just started learning angular and I'm facing some issues with my code. It seems there is a syntax error, but I can't seem to pinpoint it. Any help would be greatly appreciated. Thanks in advance. Here's the error message: Error: [$parse:syn ...

Top tips for effectively using AngularJS in conjunction with jQuery, Bootstrap, and other popular plugins

When incorporating AngularJS with jQuery regular plugins, what are the best practices to follow? For example, when we want to open a modal popup, we typically use this code: $("#MyModal").show("Show"); What approach should we take when working with Angul ...

How to position elements within a content block in HTML with a CSS stylesheet

Looking for guidance on how to align text and image within a block in HTML using only a .css file. Specifically, I need the text to be on the left side and the image on the right. Since I am new to CSS, I could use some direction on where to start. Any t ...

Issue with CSS Media Queries: Preventing an Element From Being Affected

As a complete beginner in HTML and CSS, I have a question that may seem silly. Recently, I created this HTML code: * { margin: 0px; padding: 0; box-sizing: border-box; } p { font-size: 14px; font-family: 'Work Sans', 'sans seri ...

Incorporate an HTML form into the primary HTML webpage using AngularJS application technique

As a newcomer to AngularJS, I am attempting to grasp how it can be integrated with an ASP.NET-MVC5 application. For my initial test, I want to launch an AngularJS app from a basic HTML page (in this case index.html) where the app contains a form template i ...

Angular and Bootstrap project with an advanced dropdown menu featuring multiple levels

Looking to create a multi-level drop-down menu using TypeScript without relying on jQuery? Bootstrap CSS framework may not have exactly what you need. https://i.sstatic.net/iruev.png Wondering how to implement a multi-level dropdown in your Angular proje ...

JavaScript is currently not executing properly. According to Firebug, the message states to "reload the page to obtain the source for: ..."

I'm currently working on creating a bookmarklet that adds some JavaScript elements to the head of a webpage. However, I've noticed that one of them is not loading properly. Is there a solution to this issue? Is it possible to reload the page wit ...

Is it possible for me to select from H1-H6 tags depending on the font size?

Back in the day, I was coding before CSS even existed and we relied solely on HTML for page aesthetics. This background info will help explain my question. Is it incorrect to use different heading tags (h1 to h6) based on font size rather than significanc ...

Is it possible to include spaces in a JavaScript alert message?

Is it possible to add spaces in an alert message? I have been trying to include spaces in my alert messages, but the alerts do not appear when there are spaces. Example where it works: https://jsfiddle.net/yczrhztg/ Example where it doesn't work: ht ...

Generating buttons automatically and transforming them into an accordion

My code consists of a function that generates buttons based on the current month, creating 28 buttons for February, 31 buttons for March, and so on. Additionally, I have implemented an accordion function. However, I am facing difficulty in integrating both ...

Change the color of the image to black and white and then back to its original state when a specific element is hovered over

Searching for a jQuery plugin to convert an image to black and white? Look no further! I've explored various options, but none quite fit the bill. What I'm really after is an effect that will revert the image back to color when a specific element ...

Changing the Position of HTML Scripts in React

I am facing an issue where I need to relocate an external script within a specific section of my page. However, I am unable to access the CSS attributes associated with this item. It seems that it is displayed as an iFrame, making it difficult to modify th ...

Inserting data into a Textbox by clicking on a Div

I'm currently working on creating a wallpaper changer for my website. Right now, I'm looking to input the URL of a wallpaper into a text box when the corresponding DIV option in a CSS menu is clicked. Below is the JQuery I am using: $("div.bg8 ...

Include chosen select option in Jquery form submission

Facing some challenges with a section of my code. Essentially, new elements are dynamically added to the page using .html() and ajax response. You can see an example of the added elements in the code. Since the elements were inserted into the page using . ...

Is it possible to utilize jQuery for loading an external PHP file?

Apologies for the simplicity of my question, but I've been struggling to pinpoint the issue. I have a basic HTML form with just one checkbox. My goal is to use jQuery to load an external PHP file onto the same page within the 'content' div i ...