What is the best way to eliminate the space between <div> elements?

I am looking to arrange <div> elements in a specific layout where one is at the top, three are in the middle in a row, and one is at the bottom without any extra space. It is necessary that there remains a space between the middle <div>s and the bottom one.

https://i.sstatic.net/N5AFS.png

The height of the top and bottom elements should be 15%, while the middle ones should have 70%. Additionally, the bottom element should slightly extend out of the .container.

This setup pertains to the <body> tag.

<div id="wrap">
    <div class="container">
        <div></div><div></div><div></div><div></div><div></div>
    </div>
</div>

Below is the corresponding CSS style information.

#wrap{
        width: 90%;
        height: 500px;
        margin: 0 auto;
        border: 4px solid black;}

    .container{
        width: 93.75%;
        height: 492px;
        margin: 0 auto;
        border: 4px solid black;}

    .container div{
        /*display: block;*/
        height: 100%;}

    .container div:first-child{
        height: 15%;
        background: #003153;}

    .container div:first-child +div{
        display: inline-block;
        width: 33.33333333333333333%;
        height: 70%;
        background: #c33;}

    .container div:first-child +div +div{
        display: inline-block;
        width: 33.33333333333333333%;
        height: 70%;
        background: #2dcc70;}

    .container div:first-child +div +div +div{
        display: inline-block;
        width: 33.33333333333333333%;
        height: 70%;
        background: #e75d5d;}

    .container div:first-child +div +div +div +div{
        /*display: block;*/
        height: 15%;
        background: #003153;}

Should I make modifications using the display: property to achieve this arrangement?

Answer №1

To align elements at the top, you can use vertical-align: top in combination with display: inline-block. The default alignment is baseline.

As stated in the official documentation:

baseline: This property aligns the baseline of an element with the baseline of its parent.

#wrap{
  width: 90%;
  height: 500px;
  margin: 0 auto;
  border: 4px solid black;}

.container{
  width: 93.75%;
  height: 492px;
  margin: 0 auto;
  border: 4px solid black;}

.container div{
  /*display: block;*/
  height: 100%;}

.container div:first-child{
  height: 15%;
  background: #003153;}

.container div:first-child +div{
  display: inline-block;
  vertical-align: top;
  width: 33.33333333333333333%;
  height: 70%;
  background: #c33;
}

.container div:first-child +div +div{
  display: inline-block;
  vertical-align: top;
  width: 33.33333333333333333%;
  height: 70%;
  background: #2dcc70;}

.container div:first-child +div +div +div{
  display: inline-block;
  vertical-align: top;
  width: 33.33333333333333333%;
  height: 70%;
  background: #e75d5d;}

.container div:first-child +div +div +div +div{
  /*display: block;*/
  height: 15%;
  background: #003153;}
    
<div id="wrap">
    <div class="container">
        <div></div><div></div><div></div><div></div><div></div>
    </div>
</div>

Answer №2

Check out this functioning code snippet:

      #wrap{
        width: 90%;
        height: 500px;
        margin: 0 auto;
        border: 4px solid black;}

    .container{
        width: 93.75%;
        height: 492px;
        margin: 0 auto;
        border: 4px solid black;}

    .container div{
      vertical-align: top;
        /*display: block;*/
        height: 100%;}

    .container div:first-child{
        height: 15%;
        background: #003153;}

    .container div:first-child +div{
        display: inline-block;
        width: 33.33333333333333333%;
        height: 70%;
        background: #c33;}

    .container div:first-child +div +div{
        display: inline-block;
        width: 33.33333333333333333%;
        height: 70%;
        background: #2dcc70;}

    .container div:first-child +div +div +div{
        display: inline-block;
        width: 33.33333333333333333%;
        height: 70%;
        background: #e75d5d;}

    .container div:first-child +div +div +div +div{
        /*display: block;*/
        height: 15%;
        background: #003153;}
<div id="wrap">
    <div class="container">
        <div></div><div></div><div></div><div></div><div></div>
    </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

Making sure all items in the top row of Flexbox columns have the same height

Within my flex wrapper, I have multiple column components with various flex items inside. Each column displays content adjacent to each other. The challenge is to ensure that the top item in each column has an equal height, creating a row-like effect. I&a ...

Tips for utilizing PrependTo dynamically on every element

I am facing a requirement that involves prepending an element with a specific class to its sibling with another class. While I have managed to accomplish this for the first occurrence, it seems to fail for all other elements within the same div. Below is t ...

Ways to Toggle div Visibility for Elements with Identical Class Names on an Individual Basis

After searching for solutions on stackoverflow, I attempted to implement some answers provided by other users, but I'm still not achieving the desired outcome. In my website's about section, there are four different items. When a specific item&a ...

Gridview displaying varying border styles in CSS

I'm really struggling with this issue. I have a GridView and I want to ensure that the borders are consistent across all browsers. Right now, I'm experiencing different outcomes in IE, FF, and Chrome. Despite my best efforts with CSS (I'm re ...

Is it possible to run quirks mode in one frame while running standards mode in another?

Back in the IE6 days, I developed an old application that relies on frames instead of iframes and runs in quirks mode. Now, I'm wondering if it's possible to have one frame remain in quirks mode while another operates in standards mode when using ...

In TypeScript version 2.4.1, the fontWeight property encounters an error where a value of type 'number' cannot be assigned to the types of '"inherit", 400'

When attempting to set the fontWeight property in TypeScript, I encounter the following error: Types of property 'test' are incompatible. Type '{ fontWeight: number; }' is not assignable to type 'Partial<CSSProperties>&a ...

Avoid having the content below become pushed down by a collapsed navbar

I am utilizing Bootstrap 4.1 My current structure is as follows: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhkt ...

Using ng-bind-html within a ng-repeat loop

Currently, I am working on developing a custom autosuggest feature where a web service is queried with a search term to retrieve a list of suggestions that are then displayed. My main obstacle lies in trying to highlight the matching term within the sugges ...

Interacting with local data using Express server

I am currently in the process of developing a web application for my web art class using Node.js with npm and Express. The concept behind the site is to have the entire body colored in one solid color, but allow users to text a hexcode/CSS color to a Twili ...

Creating a dynamic carousel with adjustable images

My image carousel is giving me trouble with responsiveness, specifically the height of the images. While the width looks okay, the height is only half of what I need. Can someone offer some ideas on how to make my image height 100% responsive? I've tr ...

Ways to match a string against a numeric value

I have a span id with textContent which have their own time(hour/minutes) <div class="here"> <span class="time" >8 min</span> </div> <div class="here"> <span class="time" >22 min&l ...

it will still function properly regardless of the width being set to zero

Is anyone able to explain the strange phenomenon I am experiencing with this code snippet? In this particular example, I have set the width of selector h3 to 0. However, when viewing it in IE 9 using F12 development tools, I can see that the width is indee ...

Checkbox selection limitation feature not functioning correctly

Having trouble with my checkbox question function - I want to limit the number of checkboxes that can be checked to 3, but it's still allowing more than that. I suspect the issue lies with latestcheck.checked = false; This is my typescript function: ...

The modal appears on the screen prior to the content being shown

While attempting to render a bootstrap modal with content from a REST call, I am encountering an issue where the modal appears before the content has finished populating. The modal is triggered by a button click event. If I click the button again after wa ...

Nav bar taking up full width of page, centered to the right with content area

I am facing an issue with aligning my full-width navbar with my fixed content area. The ul keeps changing based on the browser or resolution. Check out this fiddle for reference: http://jsfiddle.net/fwyNy/ subject site CSS Code: #topribbon{ width: 10 ...

Tips for creating a radio button that appears consistent across Firefox, Chrome, and IE11

I am looking to create a set of Radio buttons that will display consistently across Chrome, Firefox, and IE 11. My current solution appears to work well in Firefox, but in Chrome, there is a blue box around the buttons, and in IE 11, it seems that the colo ...

Text overlaps when li element is nested inside another element

Seeking assistance in creating a CSS/HTML menu bar that triggers list elements on hover. Facing difficulty arranging the list in two columns. Code Sample:http://jsfiddle.net/Km922/1/ <!DOCTYPE html> <html lang="en"> <head> <meta ...

What is the best way to split an input field into distinct fields for display on the screen?

Take a look at this image: https://i.stack.imgur.com/LoVqe.png I am interested in creating a design similar to the one shown in the image, where a 4 digit one-time password (OTP) is entered by the user. Currently, I have achieved this by using 4 separate ...

`Is it challenging to convert JSON into HTML, leading to undefined results?`

I am currently attempting to extract YAHOO data by utilizing getJSON and YQL. Although the connection is successful, I can retrieve the data and log it in the console. However, I am facing difficulties when trying to display this data on the JSP page I am ...

Converting CSS to SCSS using a for loop

I am currently learning SCSS and I have successfully converted the CSS code below to SCSS, but now I am struggling with completing the rest. I have tried looking for help online, but so far no luck. Can someone please assist me with this? .par-1 { wi ...