Creating div elements inside other divs can be achieved by using different positioning properties such as left, right, top,

I have divs named bottom, left, right, and top. I am looking to place a div called "center" inside each of them in order to achieve the following layout:

======top======
| left-  center  -right |
------bottom-----------

Although it seems straightforward, I encounter issues with my divs escaping their designated positions.

body {
  !important margin: 0 auto;
}

#container {
  margin: 0 auto;
  width: 50%;
}

#headertop {
  background-color: #0000CD;
  margin-top: 50px;
  padding-bottom: 80px;
}

#left {
  background-color: #6495ED;
  padding-bottom: 400px;
  width: 10%;
  float: left;
}

#right {
  background-color: #0000CD;
  padding-bottom: 400px;
  margin-left: 600px;
  width: 10%;
  float: right;
}

#bottom {
  clear: both;
  background-color: #6495ED;
  padding-bottom: 80px;
}
<div id="container">
  <header>
    <div id="headertop">
    </div>
  </header>
  <main>
    <div id="center">
    </div>
  </main>
  <aside>
    <div id="left">
    </div>
    <div id="right">
    </div>
  </aside>
  <footer>
    <div id="bottom">
    </div>
  </footer>
</div>

Answer №1

Here is a potential solution that utilizes flexbox:

div {
  height: 20px;
}

#top {
  background-color: pink;
}

#left {
  background-color: #0000ff;
}

#center {
  background-color: #6666ff;
}

#right {
  background-color: #9999ff;
}

#bottom {
  background-color: pink;
}

.flexbox {
  display: flex;
}

.item--flex-1 {
  flex: 1;
}
<div id="top"></div>
<div class="flexbox">
    <div id="left" class="item--flex-1"></div>
    <div id="center" class="item--flex-1"></div>
    <div id="right" class="item--flex-1"></div>
</div>
<div id="bottom"></div>

Answer №2

Streamline, simplify, streamline. It is also advisable to minimize the use of floats for layout unless absolutely necessary and have a clear understanding of how elements behave when using them.

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh; /* Allows flex-grow for .content to extend page height. */
  margin: 0;
}

.content {
  display: flex;
  flex-grow: 1;
}

main {
  flex-grow: 1;
}

/* Style exemplification */
header,
footer {
  background-color: whitesmoke;
}

aside {
  background-color: cornsilk;
}

main {
  background-color: aliceblue;
}
<header>Header</header>
<div class="content">
  <aside class="left-sidebar">
    Left Aside
  </aside>
  <main>
    Main
  </main>
  <aside class="right-sidebar">
    Right Aside
  </aside>
</div>
<footer>Footer</footer>

Answer №3

Using a grid layout would be an effective way to achieve this as it allows all elements to be siblings within the same parent container.

body {
  display: grid;
  grid-template-areas: 'header header header'
                       'left   main   right'
                       'footer footer footer';
  grid-template-rows: max-content 1fr max-content; /* Modify this to adjust column widths */
  grid-template-columns: max-content 1fr max-content;
  min-height: 100vh; /* Ensures full page height */
  margin: 0;
}

body > * {
  padding: 1em;
  outline: 1px solid red;
}

header {
  grid-area: header;
}

.left-sidebar {
  grid-area: left;
}

.right-sidebar {
  grid-area: right;
}

main {
  grid-area: main;
}

footer {
  grid-area: footer;
}
<header>Header</header>
<aside class="left-sidebar">
  Left Aside
</aside>
<main>
  Main
</main>
<aside class="right-sidebar">
  Right Aside
</aside>
<footer>Footer</footer>

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

Incorporating a complex React (Typescript) component into an HTML page: A Step-by

I used to have an old website that was originally built with Vanilia Javascript. Now, I am in the process of converting it to React and encountering some issues. I am trying to render a compound React(Typescript) component on an HTML page, but unfortunatel ...

How can I display different data values on each individual circle counter, rather than all circles showing the same data?

Hey there! I'm trying to get my circular counters to display the counter value that I specify in their class and data-percent. However, currently all four counters are only showing the data from the first counter, even though I've set different d ...

Failure to respond to dual part form by RemoveClass

Currently, I am utilizing jQuery to visually control a form. The issue arises when trying to remove classes from the first part of the form using removeClass. While this works initially, upon clicking the button to advance to the second part of the form, t ...

How can you apply a class to a different element by hovering over one element?

Is there a way to darken the rest of the page when a user hovers over the menu bar on my website? I've been playing around with jQuery but can't seem to get it right. Any suggestions? I'm looking to add a class '.darken' to #conte ...

What is the procedure for downloading a file within a brackets-shell application?

Currently, I am utilizing Brackets-shell to develop a desktop packaged application designed for both Windows and OSX. This application, which operates within the Brackets shell environment, has the capability to access dynamically generated Excel sheets an ...

Troubleshooting a Header Styling Problem in Shopify Theme Sections (Liquid)

I am encountering an issue with the header styling in a specific section of my Shopify website. The header is not displaying correctly as intended. Below is the code snippet I have implemented for the header along with its styling: Liquid Code: <hea ...

Exploring Vue 3: Choosing between using import statements in main.js and configuring additionalData in vue.config.js to load a global scss file

I am attempting to load a global SCSS file using the "vue.config.js" file. vue.config.js module.exports = { css: { loaderOptions: { sass: { additionalData: `@import "@/assets/common.scss";` } } } } If I include the style tag in my App. ...

Managing Variants in SCSS with BEM Approach: A Comprehensive Guide

Trying to solve a frustrating issue with BEM can be quite the challenge. I currently use the BEM methodology for developing UI elements in my application. For example: <div class="card"> <h2 class="card__title">S ...

What is the best approach to bring a button into focus after it has been enabled in a React.js application

I'm a beginner in react js and I'm attempting to set the focus on a button after filling out all input fields. However, despite programmatically enabling the button with autoFocus implemented, I am unable to focus on it. return ( < ...

The footer div appears distinct on various pages despite being the same

After fixing one issue with the footer div, I seem to have created another problem :) The footer is currently broken on this page: You can see that the footer extends across the entire page. Now, compare it to this page: On this page, the footer spans ...

"Enhance User Experience with Autoplay.js for Interactive Content and Sound Effects

I'm trying to get both the animation and audio to start playing automatically when the page loads. Currently, the animation pauses when clicked, but I want it to load along with the audio playback. I attempted to use var playing=true; to enable autop ...

How come my menu items are crammed together on a small screen?

My menu items are displaying properly on a large screen using CSS grid, but they overlap on a small screen. What could be causing this issue and how can I resolve it? I have included the CSS code here, and you can access the full source code (HTML and CSS ...

creating an interactive element that seamlessly integrates with a dynamic background image slideshow

Struggling to make this work correctly as a newbie in javascript. I need the background image to slide upon hover and stay active on its selected div when clicked. The html, css, and javascript I have currently work fine - when the user clicks on a div, a ...

Switching the input to LTR changes the overall direction to LTR while maintaining the RTL placeholder

The text input is currently set to be RTL, including both the placeholder and the actual input. The settings for this were done elsewhere. My question is how can I modify it so that only the input changes to LTR, while the placeholder remains RTL. For exa ...

Troubleshooting Problems with Owl Carousel Loading

Having trouble with Owl Carousel loading issue. I've set up my carousel using the Owl Carousel jQuery plugin as guided, but it's showing me an "owl-carousel loading" class added to the DOM (owl-loading). I've tried adding custom styles and J ...

Eliminating the default inline style automatically

My goal is to hide a table row until a radio button is clicked. I attempted using display:none;, but it did not work. Upon inspecting the code in my developer tools, I noticed that the specific table row has a style attribute style="display: table-row; whi ...

Send Angular to different Controller post successful Login

I'm having trouble figuring out how to route my angular app to a new controller after the user logs in. My app is pretty simple, it uses 'loginservice'... once the user logs in, it should go to /home which has a different template than the i ...

Steps for populating results in input field from td tag

Although my title might not be very clear, I will do my best to explain my question. Below is the script that I am referring to: $(this).parent("td").siblings("td:last").text(intTotal); This script currently displays the results within a <td></t ...

The Iframe contains its own scroll feature within the body

I am facing an issue with displaying an Iframe inside a modal. The challenge is that the content within the Iframe varies in height, making it difficult to set a fixed height for the Iframe itself. When I try setting the height to 100%, the result is a 150 ...

Change the Div's Position to Fixed Upon Scrolling to a Specific Location - Using jQuery

I have a task involving a right sidebar with three sliders stacked vertically. My goal is to keep the sidebar's position fixed once I scroll down to 200 pixels. Here's the snippet of my code: $(document).ready(function() { window.onscroll = ...