CSS Horizontal Centering of a Fixed Div

CSS horizontal centering of a fixed div?

left: 50%;
margin-left: -400px; /* Half of the width */

How do I horizontally center a fixed div while having the width fit the content inside

Remove margin-left in code.

Set transform and left for horizontally center.

Modify width to 100%.

.tab {
overflow: hidden;
background-color: #505050;
padding: 0;
width: 100%;
border-radius: 15px;
position: fixed;
top: 0;
z-index: 10;
transform: translateX(-50%);
left: 50%;
}

Center a position:fixed element

You basically need to set top and left to 50% to center the left-top corner of the div. You also need to set the margin-top and margin-left to the negative half of the div's height and width to shift the center towards the middle of the div.

Thus, provided a <!DOCTYPE html> (standards mode), this should do:

position: fixed;
width: 500px;
height: 200px;
top: 50%;
left: 50%;
margin-top: -100px; /* Negative half of height. */
margin-left: -250px; /* Negative half of width. */

Or, if you don't care about centering vertically and old browsers such as IE6/7, then you can instead also add left: 0 and right: 0 to the element having a margin-left and margin-right of auto, so that the fixed positioned element having a fixed width knows where its left and right offsets start. In your case thus:

position: fixed;
width: 500px;
height: 200px;
margin: 5% auto; /* Will not center vertically and won't work in IE6/7. */
left: 0;
right: 0;

Again, this works only in IE8+ if you care about IE, and this centers only horizontally not vertically.

how to make div center horizontally with fixed position?

Try this

#footer{
position: fixed;
bottom: 20px;
background-color: red;
width:80%;
margin: 0 0 0 -40%;
left:50%;
}

JS Fiddle Example

The point to be noted here is, the negative margin-left of exactly half value of width and set the left 50 % of the body

Center text horizontally in fixed div

I think the problem is that you make fixed position per header. Try to move all headers to new .conatainer above .thumb-page and add position: relative to this container row. Also keep same grid structure as you have for images and of course use text-align: center to each header text.

Edid:
Working fiddle

How to horizontally center a fixed positioned element

When you position an element to fixed, it gets out of the document flow, where even margin: auto; won't work, if you want, nest an element inside that fixed positioned element and than use margin: auto; for that.

Demo

Demo 2 (Added height to the body element so that you can scroll to test)

HTML

<div class="fixed">
<div class="center"></div>
</div>

CSS

.fixed {
position: fixed;
width: 100%;
height: 40px;
background: tomato;
}

.center {
width: 300px;
margin: auto;
height: 40px;
background: blue;
}

Some will suggest you to use display: inline-block; for the child element with the parent set to text-align: center;, well if that suffice your needs, than you can go for that too...

.fixed {
position: fixed;
width: 100%;
height: 40px;
background: tomato;
text-align: center;
}

.center {
display: inline-block;
width: 300px;
height: 40px;
background: blue;
}

Demo 2

Just make sure you use text-align: left; for the child element, else it will inherit the text-align of the parent element.

How to center elements in a fixed position element

No need for inner elements, you can use pseudo elements for this. For the centering, I use one of the most common and robust centering methods:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

along with a positioned parent.

Here's what it looks like:

.note-add {
position: fixed;
background: rgb(189, 28, 175);
width: 75px;
height: 75px;
bottom: 0;
right: 50%;
border-radius: 50%;
margin-bottom: 30px;
}

.note-add::before,
.note-add::after {
content: "";
position: absolute;
background: red;
width: 30px;
height: 5px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.note-add::after {
transform: translate(-50%, -50%) rotate(90deg);
}
<div class="note-add"></div>

Center fixed div with dynamic width (CSS)

You can center a fixed or absolute positioned element setting right and left to 0, and then margin-left & margin-right to auto as if you were centering a static positioned element.

#example {
position: fixed;
/* center the element */
right: 0;
left: 0;
margin-right: auto;
margin-left: auto;
/* give it dimensions */
min-height: 10em;
width: 90%;
}

See this example working on this fiddle.



Related Topics



Leave a reply



Submit