Is there a way to position two <div> elements side by side so that they are the same width and height without causing any scrolling?

I am in the process of creating a basic HTML page with two sections, each containing content. However, the last content within the second .right div is extending to the bottom of the page, causing it to become scrollable.

I attempted to create another div with flex-direction: column, but this approach did not resolve the issue:

body {
  margin: 0;
}

.main-container {
  display: flex;
  flex-direction: column;
}

.left {
  background: #ecece9;
  width: 50%;
  height: 100vh;
}

.right {
  background: #ffffff;
  width: 50%;
  height: 100vh;
}
<div class="main-container">
  <div class="left">
    <h2>content</h2>
  </div>
  <div class="right">
    <h2>content should be on top</h2>
  </div>
</div>

Is there a way to position two <div> elements next to each other with equal width and height without causing scrolling?

Answer №1

Make sure to set flex-direction: row instead of flex-direction: column.

To streamline the code, consider creating a new class like .box that includes the properties width: 50%; height: 100vh;, which can be applied to both .left and .right.

body {
  margin: 0;
}

.main-container {
  display: flex;
  flex-direction: row;
}

.box {
  width: 50%;
  height: 100vh;
}

.left {
  background: #ecece9;
}

.right {
  background: #ffffff;
}
<div class="main-container">
  <div class="left box">
    <h2>content</h2>
  </div>
  <div class="right box">
    <h2>content should be on top</h2>
  </div>
 </div>

Answer №2

Modify the flex direction to switch from column to row

body {
  margin: 0;
}

.main-container {
  display: flex;
  flex-direction: row;
}

.left {
  background: #ecece9;
  width: 50%;
  height: 100vh;
}

.right {
  background: #ffffff;
  width: 50%;
  height: 100vh;
}
<div class="main-container">
  <div class="left">
    <h2>content</h2>
  </div>
  <div class="right">
    <h2>content should be on top</h2>
  </div>
 </div>

Answer №3

Utilize the power of CSS Grid to easily create layouts with just a few lines of code adjustments.

   .main-container {
  display: grid;
  grid-template-columns: 50% 50%
}

body {
  margin: 0;
}

.main-container {
  display: grid;
  grid-template-columns: 50% 50%
}

.left {
  background: #ecece9;
  height: 100vh;
}

.right {
  background: #ffffff;
  height: 100vh;
}
<div class="main-container">
  <div class="left">
    <h2>content</h2>
  </div>
  <div class="right">
    <h2>content should be on top</h2>
  </div>
 </div>

Answer №4

Make sure to use display flex only on larger screens and switch to block on mobile devices

body {
  margin: 0;
}

.main-container {
  display: flex;
}

.left {
  background: #ecece9;
  width: 50%;
  height: 100vh;
  padding:20px;
}

.right {
  background: #ddd;
  width: 50%;
  height: 100vh;
  padding:20px;
}

/* For mobile devices */
@media only screen and (max-width: 768px) {
  .main-container{
    display: block;
  }
  .left, .right{
    width: 100%;
  }
}
*/
<div class="main-container">
    <div class="left">
      <h2>content</h2>
    </div>
    <div class="right">
      <h2>content should be on top</h2>
    </div>
   </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

"Composing perfect sentences: The art of incorporating quotes

I am attempting to include text with quotes in a DIV, like so: "All is well that ends well" The text is generated dynamically and I'm using a JavaScript font replacement plugin (CUFON) to add quotes around the text. Sometimes, the ending quote drops ...

Three-handled slider

Just acquired a new slider component <input id="slider_price" type="text" class="span2" value="" data-slider-min="1000" data-slider-max="80000" data-slider-step="5" data-slider-value="[60000, 80000]"/> _ $('#slider_price').slider({ t ...

What is the best approach to animating a specified quantity of divs with CSS and javascript?

How neat is this code snippet: <div class="container"> <div class="box fade-in one"> look at me fade in </div> <div class="box fade-in two"> Oh hi! i can fade too! </div> <div class="box fade-in three"& ...

Transferring information from an HTML webpage to a PHP file and then redirecting to the homepage

My goal is to post the data to datahouse.php, which will then submit the data to the server and redirect to the Index page without displaying the datahouse.php file on the page. register.html <form method="POST" action="DataHouse.php"> <input ty ...

Tips for aligning an image on top of another image

Here is the code I am working with (also pasted below). My goal is to create a layout with two columns, where the first column features two images and the second column displays some text. In the first column, I specifically want the first image to have a ...

Check out the page design for section one!

I have been attempting to create a link to a specific section on a one-page website. Sometimes it works oddly, but most of the time it gets stuck at the section I clicked on and then manual scrolling does not function properly. From what I've researc ...

What is the best way to position a sidebar alongside content using flexbox?

As a newcomer to web development, I recently attempted to use flexbox for the second time while working on the layout for my website. However, I encountered an issue with trying to float aside next to content. Despite my efforts, it seems to just disappear ...

Using identical CSS for two separate layouts

Imagine having to work with an html page that cannot be altered in any way. The only tool at your disposal is CSS. The page is loaded and shown through an iframe from a different domain. This particular page serves as a payment gateway, displaying either ...

I'm attempting to make the button hover color transparent, but unfortunately, it's not having any effect

Check out the code snippet below. I'm attempting to adjust the button hover color to transparent. The div class is "button_container" while the button class is "btn". I made changes to the color initially, but it doesn't seem to be working. Any a ...

What is the best way to send a parameter from jQuery to PHP?

I am trying to create a simple button that, when clicked, will pass a value from jQuery to a PHP file for execution. The idea is that when the button is clicked, it triggers an onclick event which sends the number "1" to the jQuery code. Then, jQuery shoul ...

Code Snippet: Adjust Table Column Alignment using CSS

Apologies for the basic question, as I am new to CSS. Below is the UI that I am working with: https://i.stack.imgur.com/crAYE.png Here is the code that corresponds to the UI: <div class="row centered-form center-block"> <div cla ...

The alignment of SVG is not in sync with the aspect ratio

I've been struggling with this issue for some time now, trying to align the SVGs correctly for different width and height values. My viewBox has an aspect ratio of 16:9, and I want to scale the SVG accordingly. It's working fine for certain combi ...

What is the best way to display various dialogs based on the specific button that is clicked?

Starting a new project, I foresee the need for numerous dialog boxes and modals. I've come up with some basic code that displays the dialog when a button is clicked and hides it when the cancel button is pressed. However, I'm facing the issue o ...

Group HTML Tables According to Specific Attributes

Let's cut to the chase. My table is functioning well, printing all the necessary information without any issues. However, I'm facing a challenge when it comes to grouping the data rows under Program 1 together. Instead of having Program 1 print, ...

Don't forget to update the CSS stylesheet whenever templates are rendered in Flask

I have developed a Flask application using HTML and external (static) CSS stylesheets. One interesting feature of the application is a checkbox that uses JavaScript to toggle between two different CSS stylesheets - one for light mode and one for dark mode. ...

Implementing a tooltip popup inside the header cell of the ordering table

Apologies in advance for my lack of experience with coding more complex website features. I'm attempting to incorporate a tooltip popup into one of the header cells to provide additional information to users. While I have all the necessary component ...

Ever thought about harnessing the power of SassDoc to create detailed documentation for your sass

After experimenting with sassdoc for documenting our sass mixins and functions, I realized that there doesn't seem to be a straightforward way to generate documentation for our sass variables. Specifically, I am looking for a solution that can output ...

Div's HTML Classes are not triggering the Click Event

I'm attempting to create a custom function that will display notifications in the top right corner of my website, similar to how OS X notifications appear. Instead of relying on an external library, I am using jQuery as a resource. To house my notifi ...

Expanding the link directory

I am struggling with accessing the "file.php" in the "folder1" directory from the "site" directory. The href path I'm using is: href = "folder1/file.php" However, it redirects me to: href = "folder1/folder1/file.php" Additionally, when try ...

Manipulate the content of any webpage without using external extensions by injecting your own HTML, CSS, and JavaScript code

I am currently following a tutorial on how to draw shapes on a local webpage using Canvas. I came across this helpful article on Html5 canvas paint. Everything is going smoothly, but now I am interested in replicating the drawing functions/CSS/HTML and "i ...