Results 1 to 5 of 5

Thread: positioning by center

  1. #1
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default positioning by center

    Is it possible to position an element by its center? What I mean is, instead of doing this:

    <div style="position: absolute; left: 200px;">

    is there a way of doing something like this:

    <div style="position: absolute; center: 250px;">

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If using absolute positioning as I see you are, you can use a combination of left: 50% and left margin. But, depending upon how you're envisioning it, you may need to set or at least know the width of the element.

    For instance:

    Code:
    position: absolute;
    left: 50%;
    width: 300px;
    margin-left: -150px;
    Will center the element within its offset parent element (often the body, but would otherwise be the first parent element with position set to something other than static).

    However, there must be room for the negative margin, otherwise the element you're positioning will bleed out over the left edge of the parent. In the case of this being the body or if its any other element with overflow: hidden, it (the part that bleeds to the left) would be unseen.

    If when you say:

    Code:
    <div style="position: absolute; center: 250px;">
    you mean 250px to the right of the center of the element, you could do:

    Code:
    <div style="position: absolute; left: 50%; margin-left: 250px;">
    If that pushes the element too far to the right, there may be a horizontal scrollbar or the right part or all of the element might be hidden - again depending upon the element's width, its offset parent element's width and its properties.

    In this case though, if the offset parent is the body and space is limited, and it's overflow is not hidden, you get a horizontal scrollbar. And only some other offset parent element with limited space and overflow: hidden would result in part or all of the positioned element being hidden.

    To see what I'm talking about, the best thing would be to play around with it.

    You're almost always going to have more control though if you set or at least know (like with an image) the width of the element you're doing this with.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by gib65 View Post
    Is it possible to position an element by its center? What I mean is, instead of doing this:

    <div style="position: absolute; left: 200px;">

    is there a way of doing something like this:

    <div style="position: absolute; center: 250px;">
    Hi!, no such thing in css for

    center: 250px;

    if you want to center the element,, you can use margin: auto

  4. #4
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    If when you say:

    <div style="position: absolute; center: 250px;">
    you mean 250px to the right of the center of the element, you could do:

    Code:
    <div style="position: absolute; left: 50%; margin-left: 250px;">
    Actually, when I say center: 250px; I mean place the element's center at 250px into the parent object (from the left).

    I don't have a fixed width for the element as it is dynamic and depends on its contents.

  5. #5
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think you can't do it like this way in CSS.Use <div style="position:absolute;margin-left:250px;"> Try this may be it will help you.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •