Unable to center div horizontally, position fixed involved

HerrimanCoder

I have a div that I want to center horizontally inside my browser window without changing the vertical position. Here's my div:

<div id="divErrorMessage" class="ErrorClientSide">
    <div class="ui-icon ui-icon-info" style="float: left; margin-right: 6px;"></div>
    <div id="divErrorMessageText" style="margin-left: 6px;margin-top: 2px;">{ERROR MESSAGE HERE}</div>
    <img src="./Images/Icons/tinyx.png" style="position: absolute; top: 4px; right:4px; cursor:pointer" onclick="closeErrorMessage();" />
</div>

And here's the class:

.ErrorClientSide {
  display: none;
  position: fixed;
  top: 4px;
  left: 370px;
  padding: 10px 25px 10px 25px;
  border-radius: 7px;
}

Note: I removed the color styles and such from the class above for brevity.

Try it out here. Instructions: Leave all boxes blank and just click the Next >> button. You should see a pink error message div appear at the top that says "Error: All fields are required.", but the horizontal positioning is way off.

Problem: I cannot center divErrorMessage horizontally in my browser window. I'm pretty sure that position:fixed is the problem, but if I remove that, my div disappears and I can't find it. left:370px is clearly a problem as well. If I remove that, the div aligns at the left of the browser and I can't find any way to center it.

Here are my only positioning requirements for this div:

  1. It needs to be positioned vertically a little below the brown header at the top.
  2. It needs to be horizontally centered in the browser window.

If CSS can't do this easily I would be happy for a jquery solution as well, but I'm guessing this should be easy with CSS.

Important note: The error message div and much of the content is inside an iFrame.

Hashem Qolami

If the absolutely positioned error box doesn't have a fixed width, you could achieve the horizontal alignment by a combination of left: 50%; and transform: translateX(-50%);.

For instance:

.ErrorClientSide {
  position: fixed;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
}

Vendor prefixes omitted due to brevity.


It's worth noting that translate transform is supported in IE9+. For further reading you could refer to the topic below:

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Center a position:fixed element

分類Dev

Center a position:fixed element

分類Dev

Center a position:fixed element

分類Dev

Horizontally center MatDialog with 100% width and fixed maxWidth

分類Dev

Center fixed div with dynamic width (CSS)

分類Dev

How do I horizontally center a span element inside a div

分類Dev

How do you easily horizontally center a <div> using CSS?

分類Dev

How to vertically and horizontally center two images inside a div on a responsive page

分類Dev

div position fixed re-size behaviour

分類Dev

Fixed Position <div> Extends Beyond Bottom of Page

分類Dev

div :: after with position:fixed breaks site

分類Dev

Position: fixed; makes the div element that it is applied to disappear

分類Dev

window.print() position DIV center of the page

分類Dev

How do I center this horizontal list within a fixed div?

分類Dev

How to center an iframe horizontally?

分類Dev

Angular Flex Layout refuses to center container horizontally, applied directives to div, router outlet, everything, yet it still wont center

分類Dev

Center the Text of QTextEdit horizontally and vertically

分類Dev

how to align center to page of which child div has some fixed width

分類Dev

Position fixed 100 of parent

分類Dev

Fixed position is not working

分類Dev

Fixed position not working for header

分類Dev

divの幅をposition:fixedで親に調整する方法は?

分類Dev

div(position:fixed)を水平方向にスクロールさせる

分類Dev

Text in Center Position Vertically

分類Dev

How to make a heading with image and text horizontally center?

分類Dev

Center an image horizontally with CSS, on top of a background video

分類Dev

「Position:fixed」divの幅を親div(flexbox item)と同じに設定します

分類Dev

Centering text vertically and horizontally in a div

分類Dev

position fixed not working on sticky menu

Related 関連記事

  1. 1

    Center a position:fixed element

  2. 2

    Center a position:fixed element

  3. 3

    Center a position:fixed element

  4. 4

    Horizontally center MatDialog with 100% width and fixed maxWidth

  5. 5

    Center fixed div with dynamic width (CSS)

  6. 6

    How do I horizontally center a span element inside a div

  7. 7

    How do you easily horizontally center a <div> using CSS?

  8. 8

    How to vertically and horizontally center two images inside a div on a responsive page

  9. 9

    div position fixed re-size behaviour

  10. 10

    Fixed Position <div> Extends Beyond Bottom of Page

  11. 11

    div :: after with position:fixed breaks site

  12. 12

    Position: fixed; makes the div element that it is applied to disappear

  13. 13

    window.print() position DIV center of the page

  14. 14

    How do I center this horizontal list within a fixed div?

  15. 15

    How to center an iframe horizontally?

  16. 16

    Angular Flex Layout refuses to center container horizontally, applied directives to div, router outlet, everything, yet it still wont center

  17. 17

    Center the Text of QTextEdit horizontally and vertically

  18. 18

    how to align center to page of which child div has some fixed width

  19. 19

    Position fixed 100 of parent

  20. 20

    Fixed position is not working

  21. 21

    Fixed position not working for header

  22. 22

    divの幅をposition:fixedで親に調整する方法は?

  23. 23

    div(position:fixed)を水平方向にスクロールさせる

  24. 24

    Text in Center Position Vertically

  25. 25

    How to make a heading with image and text horizontally center?

  26. 26

    Center an image horizontally with CSS, on top of a background video

  27. 27

    「Position:fixed」divの幅を親div(flexbox item)と同じに設定します

  28. 28

    Centering text vertically and horizontally in a div

  29. 29

    position fixed not working on sticky menu

ホットタグ

アーカイブ