Adjust the size of a div using CSS when it is set to display as a

My website header consists of a container with three divs. The header container is 100px high. The first div floats to the left with a width of 150px, while the second div floats to the right also with a width of 150px. The third div contains another div inside it and adjusts its size to fill the remaining space.

I am trying to achieve vertical centering for the third div. However, when I apply display: table-cell and vertical-align: middle, the div shrinks to fit the text. Adjusting the size requires using fixed dimensions.

<div id="#headingcontainer">
<div class="leftimg">Left</div>
<div class="rightimg">Right</div>
<div class="heading">Content to be centered horizontally and vertically</div>
</div>

#headingcontainer
{
border: none;
border-bottom: 2px solid #8c8cd4;
width: 100%;
height: 100px;
text-align: center;
background-color: #000;
margin-bottom: 10px;
margin-left: auto;
margin-right: auto;
}

div.heading
{
display: table-cell;
vertical-align: middle;
height: 100px;
text-align: center;
width: auto;
}


div.leftimg
{
width: 150px;
float: left;
}

div.rightimg
{
width: 150px;
float: right;
}

I am seeking suggestions on how to center the middle div without specifying the exact width?

If I remove the display: table-cell property from the heading class, the div is no longer centered vertically but remains aligned horizontally.

Answer №1

Perhaps this is the solution you're seeking... I made some adjustments to the CSS by adding padding to div.header, eliminating the table-cell, and setting the margin to auto instead of width auto. Take a look and see if it meets your expectations. You may need to fine-tune the top padding based on spacing preferences, but I find this to be the simplest approach.

#headingcontainer
{
border: none;
border-bottom: 2px solid #8c8cd4;
width: 100%;
height: 100px;
text-align: center;
background-color: #000;
margin-bottom: 10px;
margin-left: auto;
margin-right: auto;
}

div.heading
{
height: 100px;
text-align: center;
margin: auto;
padding-top:40px;
}


div.leftimg
{
width: 150px;
float: left;
}

div.rightimg
{
width: 150px;
float: right;
}

<div id="headingcontainer">
<div class="leftimg">Left</div>
<div class="rightimg">Right</div>
<div class="heading">Content to be centered horizontally and vertically</div>
</div>

Answer №2

After much trial and error, I have finally discovered a solution that suits my needs.

Initially, I made a minor adjustment to the HTML code (adding two extra divs within the heading):

<div id="#headingcontainer">
    <div class="leftimg">Left</div>
    <div class="rightimg">Right</div>
    <div class="heading"><div><div>Content to be centered horizontally and vertically<div></div></div>
</div>

Subsequently, I modified the CSS as follows:

#headingcontainer
{
    border: none;
    border-bottom: 2px solid #8c8cd4;
    background-color: #000;
    margin-bottom: 10px;
    width: 100%;
    height: 100px;
}

div.heading
{
    display: table;
    border-collapse: collapse;
    width: 100%;
    height: 100px;
    position: absolute;
}

div.heading div
{   
    display: table-row;
}

div.heading div div
{   
    display: table-cell;
    text-align: center; 
    vertical-align: middle;
}

These adjustments now allow the text within the final div to be perfectly centered both vertically and horizontally. This valuable insight was obtained from another thread on Stack Overflow which I stumbled upon during my extensive search - question number 818725.

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

Web page designed using HTML and CSS styling

Looking to create a webpage similar to this design: Click here for reference image. Is there any pre-existing theme that mimics this style, or should I start from scratch to achieve a similar look? ...

Position a Font Awesome icon and a paragraph side by side

I'm having trouble getting the green "v" mark icon to align with the paragraph text on the same line. The "v" always appears above the paragraph, and this issue arose when I used flex or grid for spacing between the two elements. I've tried using ...

Steps to utilize a PHP include for displaying HTML content corresponding to the URL file name

I am currently working on a PHP script that utilizes PHP include to insert HTML content based on the filename from the URL. For instance, with the use of PHP include: - if the filename in the URL is dog.html, it will display dog within the HTML document. ...

Setting !important to every property's value at once

One interesting approach I am using is applying !important to all of the css properties' values like this: .someclass{ color: #f00 !important; background-color: #ff0 !important; margin: 0 !important; padding: 0 !important; width: 100% ! ...

Want to ensure a span is perfectly centered both vertically and horizontally within a div using minimal CSS code?

I am currently working with LESS (CSS) and facing an issue with aligning a span element (which contains button text) inside a div container (button). The span is horizontally centered but vertically aligned to the top. I also want the span to automatically ...

Combining photos seamlessly and bringing them to life through animation upon window loading

My main goal is to seamlessly fit the images together, but I'm struggling to achieve this. I tried using masonry, but unfortunately it didn't work for me. All I want is to tightly pack the divs together. For instance, in my fiddle example, I woul ...

Photo collection showcasing images in separate lines, created with a single div element per row

Currently, I am studying Scss and experimenting with creating an Image gallery using SASS. I am attempting to utilize only one div tag for the entire image gallery as a row, avoiding the need for a new div for each row. Would it be feasible to achieve th ...

Is there a "+" symbol positioned at the center of a div with a border-radius of 50%?

I need help centering a + symbol inside a div. It's displaying differently on iOS compared to Chrome, with the icon appearing lower in the div on iOS. Any suggestions for fixing this issue? Fiddle .addplus { border-radius: 50%; background-c ...

What importance does the arrangement of link elements in the <head> section hold?

According to Code Academy's Make a Website: CSS Styling: <head> <link href="font.css" rel="stylesheet"> <link href="main.css" rel="stylesheet"> </head> "In the head element, there are two link ...

Assign a new class to the anchor tag that has a distinct ID named "link"

I want to add the CSS class "active" to the anchor tag that contains the ID currently active in the URL. Here's what I've tried, but I'm unable to get window.location.hash to function properly. $( "a[href=' + window.location.hash + &ap ...

Reducing HTML content to 20 words efficiently through the use of JS and VueJS

Currently in the process of developing a news feed using VueJS and have encountered an issue with rendering the content. Unfortunately, the API I am working with does not allow for easy customization to fit my specific needs. This API provides all the cont ...

Begin a fresh page within a separate window, proceed to print the contents, and subsequently close the window

I am facing some difficulties with this code. I am attempting to use the "onClick" function to change the image once it is clicked, open a new page in a new window, print the newly opened window, and then close it. The issue I am encountering is that while ...

Is the <a data-method='something' href ... requesting a POST method?

Yes, it does. Has this behavior been documented anywhere? Situation: I made an error with my RoR Helper and created Links with data-method='GET'. Everything still functioned correctly, but I received a "Resend Data Warning" when refreshing the ...

Tips for resizing the width automatically within a container?

I am facing an issue with a container that contains 3 DIVS: left sidebar, main content, and a right sidebar. I have made the main content responsive by setting the width to width:calc(100% - 400px); (where 400px is the combined width of the sidebars). Howe ...

Animating the maximum height causes a delay in the performance of other elements

Attempting to adjust the maximum height of a ul. When the max-height property changes (via toggling a class), the height shifts immediately, while the items below the ul maintain the animated height as expected. var expander = $('.skill-expand&apos ...

Tips for making Slider display the next or previous slide when span is clicked

I have implemented a mix of bootstrap 3 and manual coding for my project. While the image thumbnails function correctly when clicked, the span arrows do not navigate to the next or previous images as intended. Below is the code snippet: var maximages ...

Ways to Personalize Navbar in the Bulma Framework

How can I make the <router link> element in my navbar component full width? I'm also looking for keywords related to achieving this layout. Here is an image of the issue: https://i.sstatic.net/2f2b0BcM.png I want a layout that spans the full ...

What are the best ways to engage with a div element using keyboard shortcuts?

Is it possible to enable keyboard shortcuts for interacting with div elements? I am working on a project tailored for seniors who may have difficulty using a mouse. Is there a way to utilize keyboard shortcuts to click on divs and access their contents? H ...

Achieving a centered SearchBar with a single button on the NativeBase header

I am trying to center the search bar with the menu button on the right side of the header using Nativebase. The code for this is provided below: <Header> <Body> <Button style={styles.searchButton} onPress={() => ...

Removing a jQuery class while simultaneously adding it to different elements

I am currently working on a webpage where I have a row of 6 images. The first image, when the page loads, undergoes transformations using CSS3 due to a specific class applied to it. When the user hovers over the other images in the line, the same class is ...