What causes the empty space above the sidebar <div>?

I am currently exploring the use of containers with float and I am puzzled by the extra white space above the .right div box. The strange thing is, when I remove the image, the space disappears. However, once I reintroduce the image, the spacing above the red section reappears. Can anyone shed some light on why this is happening?

#container {
    width: 1000px;
    margin: 0 auto;
    background-color: lightgray;
}

.left {
    width: 700px;
    float: left;
    background-color: blue;
}

.right {
    width: 300px;
    margin: 0 0 0 700px;
    background-color: red;
}
<html>
<head>
</head>

<body>
<div id="container">

    <img src="https://picsum.photos/300/300?image=0">

    <div class="left">
        <p>Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here.</p>
    </div>

    <div class="right">
        <p>Insert dummy text here. Insert dummy text here.</p>
    </div>

</div>
</body>
</html>

Answer №1

Either remove the <p> tags or set margin to 0.

#container {
    width: 1000px;
    margin: 0 auto;
    background-color: lightgray;
}

.left {
    width: 700px;
    float: left;
    background-color: blue;
}

.right {
    width: 300px;
    margin: 0 0 0 700px;
    background-color: red;
}

p {
  margin: 0;
}
<div id="container">

        <img src="https://www.billboard.com/files/styles/768x433/public/media/Backstreet-Boys-1997-portrait-billboard-1548.jpg" height="100">
        
    <div class="left">
        <p>Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here. Insert dummy text here.</p>
    </div>

    <div class="right">

        <p>Insert dummy text here. Insert dummy text here.</p>
    </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

Position the image between two div containers in the center of the screen, ensuring it is responsive and does not overlap with any text

I've been working on trying to position an image between two divs on my webpage. So far, I've managed to place the image there but it's not responsive (only displays correctly at a width of 1920) and ends up overlapping the text in both divs ...

Tips for inserting SVG images into PDF documents

Is there a way to generate a PDF upon clicking the "generate PDF" button? The PDF should display an image containing highlighted squares corresponding to the user's selections on the webpage. I've been struggling to include the SVG in the PDF as ...

Receiving an absence of string or a null value from a text box

My goal is to test the functionality of an <input> element by entering text into a textbox and then displaying that data in the console. However, I am encountering issues with retrieving and printing the content. Additionally, I am interested in test ...

How can I remove a dynamically added <tr> element using jQuery?

I insert the <tr> into the <tbody> for (var i = 0 ;i < 12 ;i++){ $(`<tr><td>test</td></tr>`).appendTo('#songsTbody'); } within this HTML structure. <tbody id="songsTbody"> </tbody> ...

What strategies can I use to make my div content more adaptable to different screen sizes and

Currently, in my project, I have implemented a layout using the top nav menu (purple part) and the left menu (pink part) as shown in the image. However, I am facing an issue where, upon deleting some content from the view, the pink menu on the left extends ...

Side navigation in Angular is not causing the main content to shrink

In my layout, I have a container that includes two sidenavs and multiple tables in between them. When I toggle the left sidenav, instead of the expected behavior where the content shrinks to accommodate the sidenav, the tables get pushed to the right as if ...

Jquery unresponsive in AJAX form output presentation

I recently delved into the world of jquery and AJAX, and while I grasp most concepts, I'm struggling with a small code snippet. On my webpage, there is a summary of articles. Clicking on an article name triggers a popup window with detailed informati ...

The div is empty when trying to display Google Maps

When I set the height of my Google map div in pixels, it displays correctly. However, when I try to set it as a percentage, specifically 100%, the map does not show up. I have tried setting the height of all parent divs as well, based on suggestions from S ...

Encountering npm issues while attempting to publish website on GitHub

Encountering errors in the terminal while attempting to deploy a website on Github using npm I am at a loss for what steps to take next PS C:\Users\user\modern_portfolio> npm run deploy > <a href="/cdn-cgi/l/email-protection" cl ...

Exploring reviews with an innovative combination of Node.js and MongoDB

Recently diving into the world of Nodejs and mongoDB, I am excited to create two distinctive pages: one for users to update their account details such as profile picture, email, and name - all of which would be saved into the database; and another for re ...

Turn off the autofill option for passwords in Google Chrome

Is there a way to prevent the password autocomplete dropdown from appearing when clicking on the password field? In Chrome Version 61.0.3163.100, none of the solutions seem to be working. Screenshot This issue has been raised many times, but setting autoc ...

Vue.js component communication issue causing rendering problems

When it comes to the Parent component, I have this snippet of code: <todo-item v-for="(todo, index) in todos" :key="todo.id" :todo="todo" :index="index"> </todo-item> This piece simply loops through the todos array, retrieves each todo obj ...

Creating a separate CSS file for a CSS class that sets the width of a <div

I am looking to enhance a div element by incorporating a specific class from a separate CSS file. For example, my HTML file contains the following snippet: /* width 90% */ @media (min-width: 960px) { div.width90 { max-width: 90%; } } <div cl ...

Transfer data dynamically between JSP pages using parameters

In my Java application, users can log in and predict football matches. I have created an admin JSP page where I retrieve all team names from a database table to set up this week's matches. Later, after the matches finish, I set the final results to co ...

How to toggle two classes simultaneously using JQuery

Check out this code snippet: http://jsfiddle.net/celiostat/NCPv9/ Utilizing the 2 jQuery plugin allows for the following changes to be made: - The background color of the div can be set to gray. - The text color can be changed to red. The issue arises wh ...

Gradually appear/disappear div element with a delay added

Storing divs in an array and deleting them after appending is my current method. Now, I'm looking to incorporate a fade-in and fade-out effect on each div with a delay. Check out this code snippet : var arr = $(".notification"); function display ...

Tips for incorporating tabs into a Rails 3 application with the help of Sass/Haml or CoffeeScript

Currently, I am looking to implement tabbed views in a project I am developing using Rails 3. To ensure compatibility with the latest features of Rails, I have decided to utilize Sass and CoffeeScript as my primary tools. If anyone has any resources or tu ...

"Error message stating 'Unresolved variable' occurs in the code while trying to assign className={styles.nameOfClass

My current task involves adjusting the style of a button in ReactJS using the "className" property. However, I encountered an error message in WebStorm stating "Unresolved variable nameOfClass," and the desired style changes did not reflect when running we ...

Firebase web authentication is most effective upon the second attempt

I am currently working on a website that interacts with Google's firebase to read and write data. The website has both anonymous and email authentication enabled. Users can view the data anonymously, but in order to edit or write new data, they must s ...

What could be causing my navbar to not be responsive on mobile devices?

I'm in need of assistance. When viewing my website in mobile mode, the collapse hamburger menu is hiding too far to the right. This issue is making the site look unresponsive, and I've been unable to find a solution. It seems like the problem sta ...