What is the best way to prevent divs from overlapping each other?

Check out the fiddle:

body {
  background-color: #D3D3D3;
}

#container {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 10px;
  padding: 20px;
}

.section1 {
  display: flex;
  justify-content: center;
}

.section2 {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.column {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.show {
  padding: 20px;
  border: 2px black solid;
  width: 100%;
}

.section3 {
  display: flex;
  justify-content: center;
}
<div id="container">

  <div class="section1">
    <div class="header show"> header </div>
  </div>

  <div class="section2">
    <div class="column">
      <div class="content1 show"> content 1</div>
      <div class="content2 show"> content 2</div>
    </div>
    <div class="content3 show"> content 3</div>
  </div>

  <div class="section3">
    <div class="footer show"> footer</div>
  </div>

</div>

I attempted using overflow: auto;, overflow: hidden;, and z-index: 1; but none of them resolved the issue. I believe the problem lies with the width: 100%; in the .show divs. Changing it to width: 60%; prevents overlapping, but doesn't occupy full width. How can I prevent the .show divs from overlapping without compromising the full width?

Answer №1

To address any layout issues you may be experiencing, try implementing the box-sizing: border-box property. This ensures that your padding values are included in the total width calculation, preventing unexpected sizing discrepancies.

* {
  box-sizing: border-box;
}

For a demonstration of this solution in action, check out this proof-of-concept fix on JSFiddle. Additionally, consider utilizing a CSS reset to further standardize your styles.

As a tip, exploring CSS grid for your layouts can simplify design decisions and make the structure more intuitive to work with.

Answer №3

The issue at hand has been resolved by setting the box-sizing property to include padding in the dimensions, as suggested by others.

However, your HTML structure appears convoluted due to the use of flex. A grid layout seems more fitting for your desired design, especially for 2D layouts.

This code snippet streamlines the HTML by removing the need for separate sections and utilizes the grid-template-areas CSS property to arrange the elements accordingly.

* {
  box-sizing: border-box;
}

body {
  background-color: #D3D3D3;
}

#container {
  display: grid;
  grid-template-columns: 1fr 9fr;
  grid-template-areas: 'H H' 'C1 C3' 'C2 C3' 'F F';
  width: 100%;
  gap: 10px;
  padding: 20px;
}

.show {
  padding: 20px;
  border: 2px black solid;
  width: 100%;
}

.header {
  grid-area: H;
}

.content1 {
  grid-area: C1;
}

.content2 {
  grid-area: C2;
}

.content3 {
  grid-area: C3;
}

.footer {
  grid-area: F;
}
<div id="container">
  <div class="header show"> header </div>
  <div class="content1 show"> content 1</div>
  <div class="content2 show"> content 2</div>
  <div class="content3 show"> content 3</div>
  <div class="footer show"> footer</div>
</div>

Answer №4

For proper formatting, assign specific widths to content-1 and content-2, and then do the same for content-3:

content-1 {
  width:30vw;
  margin-right:5vw;
}
content-2 {
  width:30vw;
  margin-right:5vw;
}
content-3 {
  width:60vw;
  margin-left:5vw;

To ensure a tidy appearance, I included a 10vw margin between them. Feel free to adjust the margins and customize as needed! Hopefully, this explanation is helpful.

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

Can the Foundation 5 class label.inline be exclusively utilized for @media medium-up conditions?

I am attempting to achieve a specific layout for various media types, as shown below. On smaller screens: +----------------------+ | LABEL | +----------------------+ | TEXT INPUT | +----------------------+ On medium and larger s ...

Convert a decimal number to a suitable Alpha value for an RGBA CSS color

I'm currently working on filling a Canvas element with a linear gradient that transitions from White to a dynamic color that will be determined at runtime. In order to achieve this, I have written a function that takes a floating-point number as its ...

Redesigning the reset Zoom feature of HighCharts using a button inspired by Material UI styling

Currently, I am developing a project that incorporates HighCharts and Material UI for the user interface design components. I am wondering if there is a method to substitute the default HighChart reset Zoom button with the Material UI button component? ...

Looking for assistance with automated web testing using selenium

I am currently using Selenium to carry out testing on a web application, but I have encountered an issue. One of the nodes that needs to be clicked has a value that constantly changes upon startup. For example: click css=#f0012 > ins.jstree-icon At s ...

Trouble with my dropright Bootstrap menu getting cropped

I am currently working on creating a scrollable sidebar that includes a bootstrap dropright function. The goal is for the menu to appear on the right-hand side when a user clicks on an item in the sidebar. I have successfully implemented the scrollable f ...

troubles with downloading using the <a> tag

I attempted to create a personalized file name for user downloads, but I am having trouble using ${} in the process. My goal is: let foo = "name" let fileName = `${name}footer.html` <a download={filename} className="btn btn-info">Download</a> ...

Enhance your video with Bootstrap 4 by overlaying text and buttons

I am in search of a solution that resembles the following design, but it needs to be styled using Bootstrap 4. Here is my current code snippet: <section class="banner embed-responsive-item"> <video class="hidden-sm-down" autoplay="" loop=""& ...

I am faced with the puzzling situation where the value of my input type slider is displayed below the slider

I am currently working with HTML code that allows users to select a percentage using an input type range. Here is the code snippet I am using: <div class="container-fluid"> <div class="row"> <div class="c ...

I am having trouble getting my console.log function to work properly on my HTML page

As a beginner in JavaScript, I am facing an issue with my console.log not working at all. When I type a console.log message, nothing shows up on my HTML page. I have tried to debug it, but being a newbie, I lack the necessary knowledge. All I can do is see ...

What is the correct way to declare variables in a CSS custom fragment shader?

Whenever I attempt to define a custom variable in my CSS fragment shader, the shader inexplicably ceases to function. Despite being confident in the syntax, I can't figure out why this issue persists. Below is the excerpt of my HTML code and CSS: #h ...

Overlapping borders in Bootstrap 4 design

Working on my project with Bootstrap 4 and encountered a coding issue. Here is the code snippet: .form-info-tab { border: 1px solid #ccc; border-collapse: separate; border-spacing: 0px; padding:5px; text-align:center; } <link ...

What could be causing the black spaces to appear after my text in HTML?

I recently tried to implement text on an image following a tutorial I found at https://css-tricks.com/text-blocks-over-image/. Here is the code snippet I used: .image{ position: relative; } h2{ position:absolute; top: 200px; left: 200px; wi ...

Tips for toggling the visibility of the 'more' button based on the number of lines in a paragraph

While writing code using HTML, CSS, JavaScript, and Bootstrap, I encountered an issue with the "show more" link. The problem arises when there is only one line in the paragraph; I would like the "show more" link to be hidden and not displayed on the screen ...

Stylesheet specific to Internet Explorer not loading

My Rails application (link) is experiencing display bugs in Internet Explorer. I am trying to fix these bugs by making changes in the app/views/layouts/application.html.haml file where I have included: /[if IE] ...

AngularJS encountered an unhandled syntax error

My current approach involves utilizing the code below to display data fetched from Parse API into a table using AngularJS and Bootstrap. However, the JavaScript section where I have defined the controller doesn't seem to be running as expected. Below ...

On a smaller screen size, the event listeners I add dynamically are successfully attached using the exact same HTML, however, they fail to attach on a larger screen size

I recently created a small website where I dynamically add code to HTML divs. Once the data is set, I attach click and mouse enter/leave events using the div's ID. The site consists of plain HTML, javascript, and CSS. However, I noticed that when I v ...

Can someone explain the significance of the statement "We strongly advise using custom validation styles because browsers' default styles are not announced to screen readers"?

The Bootstrap 4 website explains the importance of using custom validation styles for form validation, as native browser defaults may not be easily announced to screen readers. What does this mean? Should users opt for native browser form validation o ...

Using jQuery UI to create sortable lists that are connected and have hidden overflow features

How can I hide overflow from two fixed-height divs containing sortable lists connected to each other without affecting their connection? For example, if overflow is set to hidden, the list item doesn't display when dragged outside of the div. I' ...

When attempting to transfer data from an Ajax HTML form to a Flask template in Python upon clicking a button, encountered difficulties

I am a beginner with Flask and HTML. I need some help as I am struggling to retrieve parameter values from the HTML form (index1.html). Here is my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

.NET Reporting and Report Design Tool for Enhanced Data Visualization

I have various locations equipped with sensors that monitor air quality parameters such as CO2, CO, O3, PM10, and more. This data is collected every five minutes. Currently, I am in need of a tool where users can select a specific station from either a ma ...