Unable to stack nested div element

I'm currently working on recreating a snack bar box using CSS to practice the concepts I've learned in a tutorial. Within another div element, I have two child divs ("10" and "bars") that I want to stack on top of each other. However, they are appearing next to each other instead.

Below is the code snippet I am using:

/*Color Palette
lara green: 769A30;
Lara red: E63A3E;
Lara tan: F0CD92;
Lara brown: 612D22;
*/

/*GLOBAL STYLE
------------------------------*/
body {
  background: #769A30;

}

@font-face {
  font-family: 'Akzidenz';
  src: url('Akzidenz Grotesk Black.ttf') format('truetype');
}

h1, h2 {
  color: #F0CD92;
  font-family: 'Akzidenz';
}



/*HEADER
------------------------------*/
.headerSection {
  text-align: center;
  background: #612D22;
  margin: 0px;
  padding: 0px;
  height: 100px;

}

.headerDiv {
  display: inline-block;
  text-align: center;
  background: red;
  height: 100px;
  width: 100px;
  line-height: 100px;



}

#middleDiv {
  display: inline-block;
}

.middleSubDiv {
  text-align: center;
  display: inline-block;
  background: #E63A3E;
  height: 50px;
  width: 100px;
  line-height: 50px;


}

.topH1 {
  margin: 0px;
  padding: 0px;

}

.topH2 {
  margin: 0px;
  padding: 0px;
}
<!DOCTYPE html>
<html lang = "en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title> Larabar | Apple Pie</title>
  <link rel="stylesheet" href="css/styles.css">

</head>

<body>
  <div class="headerSection"><!--
--><div class="headerDiv"><!--
---><h1 class="topH1">value<h1><!--
--></div><!--
--><div id="middleDiv"><!--
---><div class ="middleSubDiv"><!--
----><h2 class="topH2">10<h2><!--
---></div><!--
---><div class ="middleSubDiv"><!--
----><h2 class="topH2">bars<h2><!--
---></div><!--
--></div><!--
--><div class="headerDiv"><!--
--><h1 class="topH1">pack<h1><!--
--></div><!--
--></div>

  <footer>
  </footer>


</body>

</html>

Removing "display: inline-block;" from the middleSubDiv class allows the two divs to stack correctly, but it affects the alignment of the neighboring divs. Can someone shed some light on why this happens? View image reference

Any assistance in understanding this issue would be greatly appreciated!

Answer №1

Make a change by removing the display: inline-block; property from the .middleSubDiv and instead add vertical-align: bottom; to the .headerDiv.

/*Custom Color Palette
lara green: 769A30;
Lara red: E63A3E;
Lara tan: F0CD92;
Lara brown: 612D22;
*/

/*GLOBAL STYLES
------------------------------*/
body {
  background: #769A30;

}

@font-face {
  font-family: 'Akzidenz';
  src: url('Akzidenz Grotesk Black.ttf') format('truetype');
}

h1, h2 {
  color: #F0CD92;
  font-family: 'Akzidenz';
}



/*HEADER SECTION
------------------------------*/
.headerSection {
  text-align: center;
  background: #612D22;
  margin: 0px;
  padding: 0px;
  height: 100px;

}

.headerDiv {
  display: inline-block;
  text-align: center;
  background: red;
  height: 100px;
  width: 100px;
  line-height: 100px;
  vertical-align: bottom;


}

#middleDiv {
  display: inline-block;
}

.middleSubDiv {
  text-align: center;
/*      display: inline-block;*/
  background: #E63A3E;
  height: 50px;
  width: 100px;
  line-height: 50px;


}

.topH1 {
  margin: 0px;
  padding: 0px;

}

.topH2 {
  margin: 0px;
  padding: 0px;
}
<!DOCTYPE html>
<html lang = "en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title> Larabar | Apple Pie</title>
  <link rel="stylesheet" href="css/styles.css">

</head>

<body>
  <div class="headerSection"><!--
--><div class="headerDiv"><!--
---><h1 class="topH1">value<h1><!--
--></div><!--
--><div id="middleDiv"><!--
---><div class ="middleSubDiv"><!--
----><h2 class="topH2">10<h2><!--
---></div><!--
---><div class ="middleSubDiv"><!--
----><h2 class="topH2">bars<h2><!--
---></div><!--
--></div><!--
--><div class="headerDiv"><!--
--><h1 class="topH1">pack<h1><!--
--></div><!--
--></div>

  <footer>
  </footer>


</body>

</html>

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

The young ones whose height is 80% that of their taller parent's

Achieving the Desired Height In order to set a div with 80% of the height of its parent element without a defined height, I am aiming for a responsive effect. The Layered Layout Issue Within my layout consisting of five layers - .header, .site, .contain ...

Steps to resolve the white-dot border problem plaguing your dropdown menu

On my website, I've designed a drop-down menu with borders set to gray on the top, left, and right sides, while the bottom border is white. This creates a smooth, continuous border that looks great in FF3 and Chrome. However, after upgrading to FF4 o ...

Creating a full-width Bootstrap layout with dual backgrounds and a two-column structure

Explaining this concept is quite difficult, which is why I haven't been able to find an answer on Google. I am currently using Bootstrap 3 and I am trying to create a layout with a full width background image, along with two transparent color backgro ...

Adjusting the size of a canvas element based on changes in the browser window dimensions

I'm currently working on developing a website layout in React that consists of: A top bar A right side bar A canvas element that occupies the remainder of the screen space These elements need to adjust to the browser window size changes while fillin ...

Add navigation dots to the slider in order to align them with the specified div element

Visit this JSFiddle link for the code <html> <link href="./style.css" rel="stylesheet" type="text/css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script&g ...

The button component is unresponsive

An app was developed with a component containing signin and signup buttons on the right side. However, there is an issue where the Sign up button is not clickable. Below is the code snippet for the component => import React from "react"; expo ...

What is the best way to format my JSON data as a table on the screen?

I need to display my data in a table format at the bottom of the screen, as shown in the screenshot below. Here is an example of the top part code: This code snippet is also used for movies. <View> <Text key={item.symbol}>{item.symbol}<Te ...

Is there a way to extend the styles applied to elements after the page has finished loading?

I have created an application with a element positioned at the top of the page. Directly below this element is another div. Upon clicking a button within the first element, HTML content from a website is loaded into the second div, bringing along all its ...

The visibility of the Bootstrap modal may vary depending on the version of Bootstrap being used

I am new to web programming and utilized various Twitter Bootstrap templates to build my website. The login page displays Bootstrap modals correctly with the following imports: <link rel="stylesheet" href="css/bootstrap.css" /> <link rel="styles ...

Adjust the color of the sidebar's list items when scrolling

How can I change the background color of left sticky sidebars li on scrolling? When scrolling through BMW, BMW's background color in the sidebar should turn green. Please refer to the code snippet provided below. The background color of the li ...

Adaptive stationary slideable navigation bar

By utilizing Bootstrap, I have successfully implemented a sidebar feature. Initially, only the Font Awesome icon is visible, but upon hovering, it smoothly slides to the left revealing more content. Challenge: Unfortunately, the sidebar lacks responsivene ...

The challenge of resizing dialog elements in Angular Material

I am currently working on developing a text dialog for my app that will be reused frequently. The text dialog consists of a header, a message displayed above the text input, the text input area, and "Ok" and "Cancel" buttons. Upon my observation, I have f ...

Unable to modify the hover color on the image or icon

After creating a circle icon with an image in the center, I wanted to make the image change colors on hover. The main focus of the icon is the circle itself. In my attempt to achieve this effect, I included the following code for the circle icon: .circle- ...

Hover over an element repeatedly to trigger an action with jQuery, as opposed to just once

Is it possible to have my p tag transition whenever I hover over it? I am looking to create a hover effect on an image that displays text in a div tag with scaling transitions. Can someone assist me with this? Instead of using jQuery or JavaScript to dyn ...

Trouble getting CSS transition to work with underline decoration

I am currently working with CSS to apply an underline effect underneath a span element: CSS: .un { text-decoration: none; transition: all .5s ease-in; } .un:hover { text-decoration: underline; } HTML: <span class="un"> Underlined Text ...

What is the most effective way to utilize CSS in order to contain a page filled with tall nested elements within the height of the viewport?

How can I restrict a page to the height of the viewport? I want to control the overflow-y on child elements, but it seems like I cannot limit the height--I can only set a max-height in pixels or a percentage. I believe I need to confine the height of certa ...

determining the 'top' and 'right' coordinates upon clicking

I encountered an issue with a table having a drop-down menu that was getting cut off when opened on the last line. After some experimentation, I came up with a solution to detach the drop-down menu from its parent element and then reposition it by setting ...

JS Code from a JS Fiddle failing to run in a web browser examination

I have been experimenting with a fantastic Expand/Collapse JavaScript function that I came across on JS Fiddle, but unfortunately, it's not working as expected when testing it in a browser. Can anyone provide some guidance on what might be causing the ...

"Troubleshooting: Spring Boot application fails to load

I recently added some bootstrap templates to my resources/templates folder. However, when I start the server and navigate to the URL where I linked the index.html page, the bootstrap styling is not loading. Surprisingly, when I use the Chrome button in Int ...

Tips on Implementing a CSS Variable in a React Component

Is there a way to use content variable with in-line styles in React? I am unsure of how to accomplish this. CSS3 .right::after, button::after { content: var(--content); display: block; position: absolute; white-space: nowrap; padding: 40px 40p ...