Achieve center alignment of a div within another div by utilizing the display property

Here is my code snippet:

#innerLabels,
#innerFields {
  display: inline-block;
  width: 200px;
  height: 200px;
  border-style: solid;
  border-width: 1px;
  vertical-align: top;
}
.innerLabel {
  display: table;
  border-style: solid;
  border-width: 1px;
  height: 100px;
  width: 80%;
}
.innerLabel div {
  display: table-cell;
  vertical-align: middle;
  border-style: solid;
  border-width: 1px;
}
#outterFields {
  background-color: red;
  width: 60%;
  min-width: 300px;
  height: 300px;
}
#outterFields div {
  display: inline-block;
}
<div id="outterFields">
  <div id="innerLabels">
    <div class="innerLabel">
      <div>hello</div>
    </div>
  </div>
</div>

I am trying to center the inner most div vertically, but it's not working as expected. I have reviewed solutions for centering elements, but can't seem to find the issue...https://i.sstatic.net/0xhNi.png

The goal is to vertically center the text "hello" without affecting the horizontal alignment. All other divs are correctly positioned next to each other. I only need to adjust the positioning of the "hello" div vertically.

Answer №1

You are simply overwriting your inner div with

#outterFields div {
    display: inline-block;
}

Simply remove it or if you intended to target a direct child, do:

#outterFields > div {
    display: inline-block;
}

#innerLabels,
#innerFields {
  display: inline-block;
  width: 200px;
  height: 200px;
  border-style: solid;
  border-width: 1px;
  vertical-align: top;
}
.innerLabel {
  display: table;
  border-style: solid;
  border-width: 1px;
  height: 100px;
  width: 80%;
}
.innerLabel div {
  display: table-cell;
  vertical-align: middle;
  border-style: solid;
  border-width: 1px;
}
#outterFields {
  background-color: red;
  width: 60%;
  min-width: 300px;
  height: 300px;
}
#outterFields div {
 /* display: inline-block; */
}
<div id="outterFields">
  <div id="innerLabels">
    <div class="innerLabel">
      <div>hello</div>
    </div>
  </div>
</div>

Answer №2

Your outer field's display inline block is causing conflicts with other display elements. Allow me to propose a more effective solution. Instead of using tables, I have implemented flexbox for a cleaner and more efficient layout.

#outerFields {
   background-color: blue;
   width: 70%;
   min-width: 320px;
   height: 350px;
}
#innerLabels, #innerFields {
   display: flex;
   align-items: center;
   width: 220px;
   height: 220px;
   border: 1px solid #333;
}
.innerLabel {
   display: flex;
   align-items: center;
   border: 1px solid #333;
   height: 130px;
   width: 85%;
}
.innerLabel div {
   display: table-cell; 
   vertical-align: middle;
   border: 1px solid #333;
}
<div id="outerFields">
     <div id="innerLabels">
        <div class="innerLabel"><div>world</div></div>
     </div>
</div>

Answer №3

To successfully center a div using the display: table-cell property, certain conditions must be met:

<div id="x">
    <div id="y">
        <div id="z">Hello</div>
    </div>
</div>

The CSS code to achieve this is as follows:

body, html {
  height: 100%;
}

#x {
  display: table;
  width: 100%;
  height: 100%;
}

#y {
  display: table-cell;
  vertical-align: middle;
}

#z {
  margin: auto;
  text-align: center;
}
  1. Make sure the html and body elements cover the entire document area if you want the x div to take up its full height.
  2. When using display: table, the element's width no longer expands automatically, so setting width: 100% ensures it fills the available space.
  3. To extend the element's height beyond its content, specify height: 100%.
  4. Remember that for display: table-cell to work, there must be an ancestor with display: table - hence the nested structure in the HTML.
  5. The vertical-align property centers the content vertically within the table-cell element.
  6. Using margin: auto ensures proper alignment even if the content does not fill the parent's width completely.

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

As I continue to fill the child DIV element with more text, the shrink wrap is compromised and eventually breaks

Snippet of HTML code: <div id="container"> <div id="first"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean lacinia velit ut magna ultricies. </div> <div id="second"> Blandit </div ...

My goal is to develop a table that is both able to be resized and easily repositioned

I've been working on a project to develop an interactive table that can be manipulated, resized, and repositioned within a canvas. The code snippet below shows my attempt at creating this table on the canvas: var canvas = document.getElementById("dra ...

CKeditor does not accept special characters or diacritics in keywords

Recently, I came across a helpful code snippet for CKeditor that counts and ranks the most used words in a textarea. This feature is invaluable for generating SEO-keywords suggestions while writing articles. However, there is an issue with non-English char ...

No translation or compiling of SCSS/SASS to CSS available

As a beginner Sass user, I am working on mastering the SMACSS hierarchy and incorporating it into my project using Brackets. However, I have encountered an issue with mapping the main.scss file to the main.css file even though it compiles successfully via ...

Set the element's text color to match the placeholder color specific to each browser

Can CSS be used to dynamically set the font color of a custom element to match the default input placeholder color of the current browser/OS? Thank you. ...

Having trouble with Github pages' responsiveness on certain devices

The responsive view in the browser appears to be working correctly. Everything is functioning fine. However, when I switch to my Android phone, the alignment is off. I've tested it on multiple devices and the results are consistent. What could be cau ...

Scouring through the text to find a specific word and bring it to the forefront

I need help creating a PHP script that can analyze a text input, which could be an extensive essay, and search for a specific word entered by the user. The script should display the entire text with the searched word highlighted whenever it is found. < ...

Using HTML to position multiple lines of text alongside an image

I have multiple lines of content and hyperlinks. Additionally, there is an image included. I am looking to have the text aligned on the left with the image on the far right, positioned next to the text. While I attempted to use flexblocks to center everyt ...

Loop through multiple levels of elements using jQuery

I have a snippet of HTML that resembles the following structure: <div class="block"> <div id="item1"></div> <div id="item2"></div> </div> <div class="block"> <div id="item3"></div> </di ...

Uploader and viewer tool for HTML5 documents

My website is currently built using PHP and allows users to add and view documents. The current upload process is basic, requiring users to manually input information and then view the document with minimal formatting on a webpage. I am looking to upgrade ...

Mobile display exhibiting glitches in animation performance

I have implemented an animation in the provided code snippet. const logo = document.querySelector('.logo'); const buttons = document.querySelectorAll('.loadclass'); const html = document.querySelector('html') const cornerme ...

Display the homepage if a user accesses a page intended for an ajax request

Currently, I am utilizing jQuery to create a straightforward website. The main page is called 'index.html' and it has the capability to load different content (such as 'info1.html' or 'info2.html') through jQuery ajax requests ...

Struggling to fix the excess white space appearing underneath my website

I am new to HTML/CSS and I'm currently working on adding a timeline to my website. However, I've encountered a strange issue with the #timeline class where there is an odd block of space below it whenever I try to adjust the height. So far, I ha ...

Numerous column pictures that occupy the entire browser screen

I am looking to create a grid of clickable images that expand to cover the width of the browser window without any space on either side. Ideally, I would like each image to be 180x180px in size, but if it's easier they can resize based on the browser ...

How can I integrate material-ui with react-jsonschema-form?

Currently, I am working on a form utilizing react-jsonschema-form. However, I would like to further enhance the appearance of my application (including the form) by incorporating Material-UI. I am encountering difficulties integrating both frameworks toget ...

What is the reason behind an inline element being able to have an explicit width when floating?

When trying to set the width for an inline element <span> within a table-row containing multiple span elements, I encountered difficulty. However, after adding float: left, I was finally able to adjust the width. What is the reason behind the initia ...

What's the best way to correct a word that is positioned at the bottom of a banner image?

https://i.sstatic.net/3gSoi.pngI am a newcomer to all of this. I have a banner image and I want to position a word at the bottom center of the banner. I've tried using padding, position, and text-align, but it's not responsive - when I widen the ...

Delete the HREF attribute after accessing the LINK

I am looking to achieve a specific effect... <div id="content"><a href="#MyForm">some text</a></div> After Clicking/Accessing the Link once, I want "some text" to appear as ordinary text... like <div id="content"><a>s ...

Learn how to create a horizontally scrollable ToggleButton using MUI when the ToggleButtonGroup exceeds the screen width

Looking to enhance the responsiveness of my web design project, I aim to create a horizontally scrollable ToggleButton using Mui. The goal is to have the buttons adjust when the screen width becomes too narrow to display all three buttons at once, allowing ...

Painting with color blends

I need help designing a color selector in the shape of a triangle. I've come across resources like the color wheel (), but I'm unsure how to alter it to resemble a triangle instead of a square. ...