What is the proper way to eliminate the top margin from a div that has the display table-cell property?

There are two div elements with display: table-cell property:

<div>
    This is a table cell
</div>
<div>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
</div>

CSS

div {
    display: table-cell;
    background-color: blue;
    padding: 0 20px;
}

div:nth-child(1) {
    background-color: red;
    padding: 0 20px;
}

http://jsfiddle.net/aa61mLbn/

However, the first div's content seems to have an extra space at the top due to the use of display-table. Although there is no computed margin when inspecting the element, there is visible space between the content and the top of the div in the jsfiddle. I am looking for a way to eliminate that space.

Thank you in advance!

Answer №1

To align the content at the top of your div, simply add a vertical-align property with the value "top"

div {
    display: table-cell;
    background-color: #7cbaf4;
    padding: 0 20px;
    vertical-align:top;
}

div {
    display: table-cell;
    background-color: #7cbaf4;
    padding: 0 20px;
    vertical-align:top;
}

div:nth-child(1) {
    background-color: #FF6347;
}
<div>
    This is table cell
</div>
<div>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
    <h1>Some content</h1>
</div>

Answer №2

attempt:

section {
    display: inline-block;  // achieving the same outcome with improved efficiency
    background-color: green;
    padding: 0 15px;
}

section:nth-child(1) {
    background-color: orange;
    padding: 0 15px;
}

Answer №3

To make the text move upwards, simply include vertical-align:top; in the div style:

div {
    display: table-cell;
    background-color: blue;
    padding: 0 20px;
    vertical-align:top;
}

By adding this property, the text will be forced to align at the top of the container. You can view an example here: http://jsfiddle.net/aa61mLbn/2/

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

Place a single div above two divs in a flexbox container

I am struggling with aligning the fourth div, which has a class of "details", above the second and third div in a flex box layout. Despite my efforts, I have not been successful in achieving this alignment. Can someone please assist me? .flex-container ...

Utilizing HTML to emphasize a section of text

Hey there! I have a question related to an image with unicodes. The letter featured in the image was written using unicode characters పా. I'm trying to highlight the white portion of the image, but simply replacing it with ా isn&apos ...

Activate the preview mode by transmitting information between two controllers

I'm trying to set up a preview mode in one controller to show an image in another controller using an angular service. However, I'm struggling with the final step of getting the URL from the passed parameter into the ng-src in SideMenuCtrl dynami ...

Has Apache initiated a forced logout process?

Running a LAMP server, I have implemented Apache basic authentication to log in users accessing the server homepage. I am currently seeking a way to enforce user logout, but my attempts with mod_session & mod_auth_form have not been successful. The page ...

Adjust the checkmark color of MUI Checkbox

When using Material UI's MUI library for React, I encountered an issue with the checkbox checkmark color. By default, the checkmark takes on the background color of the container element, which is great for light backgrounds but not ideal for dark one ...

WordPress site experiencing issues with sub-menus disappearing following recent upgrade

I'm currently investigating a problem on a WordPress website where sub-menus are not showing up after upgrading to WP 3.9.1. The website, which can be found here, is using the Zeus theme (v. 1.1.0) and it seems that the behavior of the sub-menus is co ...

Tips for retrieving the value of a textarea in the jQuery LineControl Editor

I recently integrated a jquery plugin into my project to develop a wysiwyg text editor. I set up a textarea element to contain the text editor: <textarea class="editor" rows="3" name="textEditor" id="textEditor&quo ...

What information is transferred when the submit button on a form is clicked?

Currently, I am utilizing node.js along with the jade templating engine. Within my jade file, I have a form structured like this: form.form-signin(style="padding-left:10px", action='/update', method='post') table.table.table-hove ...

The alignment of margins appears as intended in Opera, Chrome, and IE, but unfortunately it does not

I have exhausted all possible options, but I am still struggling to get this page to appear correctly in Firefox. When you click inside the square, a menu with images should display on the right side. It is properly aligned in every browser except for Fire ...

Modifying Row Color in Bootstrap 3: A Step-by-Step Guide

I'm trying to customize the background color of alternating rows in a Bootstrap 3 grid. I attempted to use CSS and add it to the class within the div, but unfortunately, the color isn't changing as expected. Below is the CSS code I used: .row-b ...

Disappearing a menu list while zooming in on a page: The art of vanishing navigation

[QUESTION] Is there a way to make the Menu List disappear when zooming in on a webpage? For example: <-- Normal Page [No Zoom] --> [Logo] Profile Gallery Guestbook <-- Zoom In 120% --> [Logo] Profile Guestbook <-- Zoom In 150% --> ...

How to target the attribute value of an image element that has a specific parent class using jQuery

I'm working with a list that looks like this: <ul> <li><div class="thumb"><img src="image-1.jpg"></div></li> <li><div class="thumb"><img src="image-2.jpg"></div></li> <l ...

Adjust the field's color depending on the outcome displayed within

I'm trying to implement a feature where the field value changes to green when "OK" is selected and red when "NOK" is chosen. I have written the following JavaScript code but it's not working as expected - the colors change before clicking submit, ...

I'm not sure why, but for some strange reason, Markdown images refuse to display when surrounded by

Recently, I've encountered a strange issue where my markdown image tag doesn't display the image when wrapped in a <div> or a <section>. Instead, it just outputs markdown code. Take a look at this screenshot for reference. <sectio ...

Having trouble with custom padding for b-sidebar in VueJS?

I am struggling with adding padding to the sidebar using bootstrap-vue. When I attempt to add padding in the browser devtools, it works perfectly fine. However, when trying to implement the same code, it doesn't reflect on the actual webpage. I tried ...

Steps to remove a row from a table in a ReactJS component

I'm struggling with implementing a delete operation for table rows and encountering errors. Any assistance in resolving this issue would be greatly appreciated. Since I am unsure how to set an auto-incrementing id, I used Date.now(). Now my goal is t ...

Tips for aligning text to the right within a div using the "justify" text-align property

I want the final sentence of my content to be aligned to the right side while the rest remains justified. I attempted using a <span> within a <p> tag, but it did not work as expected: span.activity-conclusion { font:22px 'Open Sans ...

I possess a certain input and am seeking a new and distinct output

I am looking to insert a word into an <input> and see an altered output. For example, Input = Michael Output = From Michael Jordan function modifyOutput() { var inputWord = document.getElementById("inputField").value; var outputText = "print ...

If I create an App with Phonegap, HTML5, and Websql, what happens to the app's data when the user restarts their mobile device?

My application utilizes HTML5's WebSQL to store temporary notes and bookmarks, which will later be synchronized with the server. I am curious whether data stored in WebSQL will be lost upon mobile restart or cache clear, similar to how browsers on ph ...

My webpage layout doesn't quite accommodate my content, so I need to adjust it to be more zoom

Why does the page container load bigger than the html/body container in mobile mode, as shown in the photo? https://i.sstatic.net/fD1N4.png Here is my CSS: html { margin-top: 48px; width: 100%; } body { margin: 0; width: 100%; background: ...