Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9093335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:04:25+00:00 2026-06-16T23:04:25+00:00

Not sure what I am getting wrong here, but let’s say I have two

  • 0

Not sure what I am getting wrong here, but let’s say I have two divs, and an h1 element (or P1) for that matter that looks like this:

<div class="wrapper">
 <div class="top">
    <h1>Content header</h1>
 </div>
</div>

I want my element to appear in the ‘center middle’ of the inner div, that is it’s immediate parent. To achieve this, I give it a margin-top:50% & a margin-left:50% with the understanding that this would render it exactly towards the center middle of the div. But while it does get it to the middle, it doesn’t quite get it to the center. Infact it seems to position itself relative to the outer div, the one with class wrapper.

I have recreated this using jsfiddle here:

http://jsfiddle.net/KLRsN/

Am I specifying the selectors wrong or is my positioning in itself incorrect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:04:27+00:00Added an answer on June 16, 2026 at 11:04 pm

    -the above ans isnt completely correct as the text will still not be completely centered vertically.

    .wrapper{
      margin:5px;
      max-height:250px;
      min-height:250px;/*not required only height:250px will do*/
      border:1px solid green;
    }
    
    .content
    {
      margin:5px;
      border:1px solid black;
      height:100px;/*you have to give the parent element a height and a width within which you wish to center*/
      width:100px;
      position:relative;/*giving it a position relative so that anything inside it will be positioned absolutely relative to this container*/
      text-align:center;/*aligning the h1 to the center*/
    }
    .content h1{
    border:1px solid black;
     position:absolute;
    top:50%;
      left:50%;
      line-height:50px;
      width:50px;
        margin-left:-25px;/*half the width of the h1 so that it exactly centers*/
      margin-top:-25px;/*half the height of the h1 so that it exactly centers*/
    }
    

    explanation:

    -ever element in html is in the form of a rectangular box so applying margin-top:50% is aligning the top of that box to 50% of the parent element and not the text inside the box.
    -that is the reason the text is not exactly aligned to the center.

    -also it is essential to provide the parent element(within which you wish to center the h1) a width and height.

    The correct way to do what you are looking for would be by using absolute and relative positioning.

    -give the .container a position value of relative and the h1 a value of absolute

    -by giving the h1 a width and height we then apply a negative left margin equal to half the width and a negative top margin equal to half the height so that the text is exactly centered.

    • also for more on positioning – check out the following link
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure what I'm doing wrong but I have two services one is WCF
not sure what I'm missing here, but i keep getting the error. SQLSTATE[HY093]: Invalid
I'm not sure if I'm doing something wrong here or not, but I need
I am not sure what I am doing wrong here. I'm not getting an
Not sure what I am doing wrong here. I am hoping that it is
I keep getting this RuntimeError which I'm not sure how to fix. Here's what
I'm not sure if I'm missing something really obvious, but I keep getting a
Not sure how to work this but I found a way that works although
Not sure why this is occurring: Basically, I have an array that contains the
Not sure why I am getting just one row with wrong count! Following is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.