What is the best way to create divs with no gaps in between them?

I am trying to create a div structure that resembles the one shown below, with one div on top and two divs under it. I would prefer not to use tables, ensuring there is no space between the divs, and if possible, having collapsed borders.

 _________
|         |
|_________|
|    |    |
|____|____| 

However, my current attempt does not seem to be working as intended.

<div></div>
<br/>
<div></div><div></div>

If anyone has any insights or suggestions on how to achieve this layout, I would greatly appreciate it!

Thank you.

Answer №1

Take a look at this DEMO.

div is considered a block level element, which means nothing can sit on either side of it by default. However, you can manipulate this behavior by using floating or changing the display attribute. For example, using display: inline-block can achieve the desired effect, but may not be fully supported in older browsers. Here's an alternative method using float.

This is how you would structure the HTML:

<div class="container">
    <div class="top">top</div>
    <div class="left">left</div>
    <div class="right">right</div>
</div>

And here is the corresponding CSS:

.container {
    width: 150px;
}
.top {
 border: solid 1px blue;
}
.left {
 border: solid 1px red;   
    width: 73px;
    float:left;
}
.right {
 border: solid 1px green; 
    width: 73px;
    float:right;
}

For further information on display, refer to the Documentation for display.
If you're interested in learning more about float, check out the Documentation for float.

Answer №2

Website Layout

<div id="wrapper">
    <div class="header"></div>
    <div class="content"></div>
    <div class="sidebar"></div>
</div>

CSS Styling

div
{
    margin: 0;
}

#wrapper
{
    font-size: 0;
}

.header
{
    border: 1px solid blue;
    height: 50px;
    width: 202px;
}

.sidebar
{
    border: 1px solid red;
    display:inline-block;
    height: 50px;
    width: 100px;
}

Fiddle Demo: http://jsfiddle.net/NgakP/1/

Answer №3

Maybe something along these lines?

FIDDLE

HTML

<div class="main-container"></div>
<div class="left-section"></div>
<div class="right-section"></div>

CSS

.main-container{
    background-color:red;
    width:200px;
    height:100px;    
}
.left-section{
     background-color:yellow;
    width:100px;
    height:100px; 
    position:relative;
    float:left;

}
.right-section{
     background-color:blue;
    width:100px;
    height:100px;
    position:relative;
    float:left;

}

UPDATE: Regarding the borders-collapse concern, it won't be a problem in this scenario since we're working with div elements that do not have borders unless specified manually.

Answer №4

To achieve the desired layout, CSS is necessary. Assign a style class to your div and utilize the float:right or float:left properties to position the bottom divs accordingly. Another option is using block styling to stack them one on top of the other.

Answer №5

<div style="width:100px;height:50px;"></div>
<br />
<div style="width:100px">
    <div style="width:50px;height:50px;float:left;display:inline-block;"></div>
    <div style="width:50px;height:50px;float:right;"></div>
</div>

Answer №6

Make sure to show the bottom two elements as inline-blocks. The first div will automatically create a break.

Take a look at this link for reference: http://jsfiddle.net/2MBWT/2/

Remember that borders also occupy space.

div {
    border: 1px solid black;
    max-width: 300px;
}

.column {
    display:inline-block;
    width: 149px;
}

Answer №7

One reason for this behavior is that divs are set to have a default display type of "block", causing them to start on a new line each time. Fortunately, there are various CSS solutions available to address this issue. My preferred solution would involve using the "float" property.

Here's an example in HTML:

<div id="container">
   <div class="fullWidth"></div>
   <div class="halfWidthContainer">
       <div class="halfWidth green"></div>
       <div class="halfWidth blue"></div>
   </div>
</div>

And here's the corresponding CSS code:

#container{
   width: 300px;
   height: 200px;
   border: 2px solid red;
}
.fullWidth{
   width: 300px;
   height: 100px;
   background: #aaa;
}
.halfWidth{
   width: 150px;
   height: 100px;
   float: left;
}
.green{
   background: green;
}
.blue{
   background:blue;
}

Remember, the classes ".green" and ".red" are merely examples for illustration purposes. Also, consider not setting fixed heights for your divs to allow for dynamic content fitting.

If you utilize padding or margins, be aware that they impact the overall width of elements. For instance, adding a 10px padding would increase the total width by 20px (10px padding on both sides), so adjust the element's width accordingly for proper alignment within the container.

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

What is the best way to style a select element to achieve this appearance?

https://i.sstatic.net/4yGqf.png Currently working on converting a .psd file to html/css and encountering difficulty with a unique looking select element. Struggling to find a suitable example online for reference. Wondering if this design element is indee ...

The mask effect is not only darkening the background image but also affecting the color of my header content

Why is the mask I applied making not only my background image darker but also my h1 and button? <!-- Header - set the background image for the header in the line below--> <header class="bg-image d-flex justify-content-center al ...

Internet Explorer is failing to display images

Hey there! I'm having an issue with my product listing where the images are not showing up in Internet Explorer, even though it works fine in all other browsers. Here is the code snippet for the image container: Image container: <div class="image ...

Footer div is being covered by the page

I am currently working on a website built with "WordPress", and I have implemented a mobile footer plugin that is designed to only appear on mobile devices. However, I am encountering an issue where the page content overlaps the footer on phones. I attemp ...

Incorporating a Layered Div onto a Presentation Slider

Over at , we have a slider featured on the homepage. I am attempting to place a div ABOVE the image but BELOW the text. Essentially, my goal is to add a semi-transparent white overlay to enhance the image on the slider while keeping the text visible for b ...

Watching for changes to an object's value in an AngularJS service triggered by a controller from a separate module (Extended Edition)

Referring to this discussion on Stack Overflow: AngularJS trigger and watch object value change in service from controller The original question was about watching for changes in a service from a controller. I am interested in extending this concept to ...

Utilizing JavaScript to adjust the width of an HTML element

Struggling to manipulate the position and size of a div tag using a JavaScript function, particularly confused about how to retrieve the current width of the div. Here is the function in question function socialExt() { document.getElementById("Left") ...

The line breaks in elements occur when the combined widths add up to 100%

Is there a way to display an input and a button inline, with the input taking up 70% of the row and the button taking up 30%? I tried setting the CSS for the input to 70% and the button to 30%, but they keep breaking onto separate lines. How can I solve ...

Creating a radial gradient texture using CSS

I'm encountering an issue with my radial gradient and texture combination. Here is the code snippet I am using: background-image: url('../img/texture.png'); /* fallback */ background: -moz-radial-gradient(rgba(253,253,253,0.1) 0%, rgba(2 ...

What are the steps to reveal the second element once the first one has vanished?

Is there a way to delay the appearance of the second square until after the first square has disappeared? For example, I want the first square to appear after 3 seconds and then disappear, followed by the second square becoming visible after 11 seconds. ...

Tips for postponing an action until the webpage is fully loaded

Currently, I am using Selenium in R to perform a specific task. The script I have written enables the program to search for pizza restaurants near a designated geographical coordinate on Google Maps. The script continues to scroll until all restaurant inf ...

When the screen shrinks, the navbar toggler disappears and blends in with the background

My toggle button is designed to work only for xs screens, where all the navigation links go to the toggle. However, when I click on the toggle button, it initially displays with a white background and then suddenly collapses to the size of the navbar, caus ...

Element in SwipeableDrawer of Material UI is repositioned on mobile devices while scrolling

Utilizing Material UI's SwipeableDrawer component, I have successfully created a bottom drawer with a fixed action button container. The container is positioned using position: fixed; bottom: 0;. While this setup works effectively on desktop browsers, ...

Icons that are clickable in ionic

I have successfully created a list card with 2 icons in each row. However, I am facing a challenge in making these icons clickable without disrupting the layout of the list. I attempted to add an ng-click to the icon element, but it did not work as expecte ...

CSS Text-Align failing to align elements

As a newcomer to CSS, I am seeking assistance with aligning items on a webpage. Essentially, I am trying to achieve an alignment that resembles the following: Item 1 ...

Creating multiple versions of a website based on the user's location can be achieved by implementing geotargeting techniques

It may be a bit challenging to convey this idea clearly. I am interested in creating distinct home pages based on the source from which visitors arrive. For instance, if they come from FaceBook, I envision a tailored home page for FaceBook users. If they ...

The functionality of display flex is not functioning as expected outside of the CodePen

I attempted to create a responsive two-column layout on Codepen with success. The layout is quite simple, featuring a YouTube video and some text. However, when I tried to transfer this to my website, it failed to work... Here is the code – hoping someo ...

Tips for creating scrolling fixed elements beyond the full body width

I'm currently working on a final assignment for my class, which involves creating a basic webpage to learn about using local storage. However, I am encountering some challenges at an early stage of the process since I am still quite new to this. I hav ...

Issues with the functionality of customisable list items in a list

I am attempting to customize the appearance of specific list items within a ul element. One li element should be displayed in red font, while another li element should have a hyperlink styled with a different color and an underline rollover effect. The thi ...

Uploading an image using ajax with multer

When using Multer to upload an image file to my Node server, I keep getting 'undefined' when trying to use ajax to send the image. Ajax : image = $("#input-file-img").val() const data = new FormData(); data.appe ...