Struggling with CSS layout: Organizing and nesting divs

I am striving to create a layout like the one shown here: https://i.sstatic.net/LInER.jpg

Presented below is my current HTML structure:

<div id="main">
    <div id="content1">
        <span>Content1</span>
        <div id="text">
            <span>Text</span>
        </div>
        <div id="game">
            <span>Game</span>
        </div>
    </div>
    <div id="content2">
        <span>Content2</span>
        <div id="gallery">
            <span>Gallery</span>
        </div>
    </div>
</div>
<div id="footer">
    <span>Footer</span>
</div>

The central section is enclosed within the main div. Content1 represents the left side while content2 denotes the right side. Content1 is further divided into two subparts (nested divs). Despite several hours of effort, I am encountering difficulties with this seemingly simple task.. The issues I am grappling with include:

  • I aim for the website to occupy 100% of the browser width
  • Although I managed to separate content1 and content2 to align them side by side, adding borders disrupted the alignment process. I attempted solutions like overflow: hidden;, display: inline-block;, and flex display without success.
  • To center the elements in content2, would margin: auto be sufficient?
  • I also question the appropriateness of the HTML layout provided, uncertain about the degree of nesting allowed. Is nesting primarily reserved for containers or similar structures? Your assistance is greatly appreciated!

Answer №1

If you want to achieve this layout, consider using nested Flexbox

.content {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.main {
  display: flex;
  flex: 1;
}
.right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid black;
}
.left {
  display: flex;
  border: 1px solid black;
  flex-direction: column;
  flex: 1;
}
.one {
  flex: 2;
}
.two {
  flex: 1;
  border-top: 1px solid black;
}
header,
footer {
  background: lightblue;
  height: 50px;
}
<div class="content">
  <header>Header</header>
  <div class="main">
    <div class="left">
      <div class="one">One</div>
      <div class="two">Two</div>
    </div>
    <div class="right">Right</div>
  </div>
  <footer>Footer</footer>
</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

How can I convert a list of checkboxes, generated by ng-repeat, into multiple columns?

Here is the HTML code snippet: <div class="checkbox"> <label> <input type="checkbox" ng-model="selectedAll.value" ng-click="checkAll()" />Check All </label> </div> <div class="checkbox" ng-repeat="carType i ...

Increase the size of the Bootstrap select input area to exceed the dimensions of

Is there a way to ensure that the selection area of my bootstrap select is not larger than its parent container on small screens (mobile)? My issue can be seen in the image below and is recreated in the snippet provided. The black bar represents the edge ...

Tips for utilizing the if statement within ng-repeat in Angular version 1.0.8

My version of angular is 1.0.8-stable My main goal is to arrange data in rows of 3. This is the desired structure for my HTML: <div class="table-row"> <div class="item">item1</div> <div class="item">item2</div> ...

Overlap and cover additional elements within a DIV

I am looking to create a versatile function that can effortlessly overlay various elements such as selects, textfields, inputs, divs, tables, and more with a partially transparent div matching their exact height and width. I have managed to obtain the pos ...

The Jquery function for setting the active navigation tab based on the URL is currently over-selecting and choosing more options

I have a customized Jquery function that assigns the 'active' class to my navigation anchors based on the URL. While this function generally works, the issue arises when tab 1 is active - tabs 10, 11, and 12 also get marked as active. The code s ...

Struggling with aligning an entire class to the right and adding padding within a class in React.js utilizing Bootstrap. Any tips or guidance would be greatly appreciated!

I am new to react js and bootstrap and need some help. I want to make a change where the Image on the right (as seen below) is placed all the way to the right on my website. Currently, the text and image are too close together. How can I achieve this? Belo ...

Determining the class condition using AngularJS ng-class

I am working with an HTML element that contains AngularJS directives: <div class="progress"> <span ng-repeat="timeRangeObject in timeRangeObjects" style="width: {{timeRangeObject.percentage}}%" ...

Automatically adjust and separate list items for more organized spacing

Imagine you have a container that is 780px wide, with 6 items inside ranging from 10px to 130px in width. Your goal is to fill the entire 780px space with these items and evenly space them out using margin. You may have tried using a table layout, but en ...

incorporating HTML attributes without specified values in .NET

Is there a way to include the multiple attribute in a select element using .net? I attempted the following approach: If MultiSelect Then drpSelect.Attributes.Add("multiple", "true") End If However, technically speaking, the HTML should only require the ...

Form-group in a state of collapse

I am trying to figure out how to create a form-group row with multiple columns and collapse each column separately. The issue I'm facing is that the "div class="collapse" needs to be above the "div class="form group row," making it challenging to tag ...

Is it possible to visually distinguish the selected mat-grid-tile? Particularly when they are being created dynamically

On the user interface, I have a dynamic display of mat-grid-tile within a mat-grid-list. These tiles change in number and data based on backend values. When a user clicks on a mat-grid-tile, it triggers a function that receives the tile's data. My goa ...

Is the textarea functioning properly with the inclusion of mysterious character variables?

As per the documentation When using an unknown named character reference like &this, it results in an ambiguous ampersand error. This situation is confusing <textarea> &this </textarea> What am I misunderstanding here? Any re ...

When the draggable item is released near the drop zone, allow drag and drop to combine

In my latest project, I created a fun game that involves matching different shapes. The goal is to drag the correct figure next to its corresponding shadow figure for a perfect match. Currently, if you partially overlap the square with its shadow, the game ...

Clear the text in a textarea after submitting the form

I am facing an issue with a comment box (textarea) inside a dialog. After successfully saving the comment, I want to clear the content of the textarea and close the dialog box. Currently, the dialog box closes, but the content remains in the textarea. < ...

Align the content vertically in two adjacent divs

I have a logo and a menu in two separate divs side by side. The logo is on the left and the menu is on the right. I am trying to vertically align the menu in the right div with the logo in the left div. Here is the code snippet: <div id="header"&g ...

Is there a way for me to position my menu on the right side of my website?

I am currently working on a gallery website and facing an issue with the menu placement. I want to position the menu items on the right side of the images, but they keep appearing at the bottom instead. Despite adjusting the width in classes like galleryme ...

Guide to setting up Docker Nginx to work with an Express and React application on a Windows

After finding tutorials about CSS, React, and Node, I realized none of them combined all three. My goal is to learn how to deploy react websites on my domain for testing purposes. However, the learning process has been overwhelming with the cascade of new ...

Can ASP.NET display a "Server Error in '/' Application" message specifically when utilizing the <asp:RequiredFieldValidator>?

I recently encountered an issue with my code that involves displaying a message followed by a text box and a Required Field Validator: <asp:Content ID="Content1" ContentPlaceHolderID="cphMainContent" Runat="Server"> <div id="MainContent" style="w ...

"Exploring the implementation of mute event in Audio HTML5 using JavaScript within an MVC

I am searching for an event that triggers when the mute button is pressed. The volumechange event only works when the volume is adjusted. <audio id="volumeController" onvolumechange="changeVolume()"controls> <source src="~/Sounds/be ...

Adjust the width of a box-shadow using percentage values in CSS

As we work on developing our tool according to the provided design, we are facing a challenge in drawing a line within a table row using box-shadow. The requirement is to have the line cover only 30% of the row width, rather than spanning across the entire ...