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
Easiest Way to Extract the Urls from an HTML Page Using Sed or Awk Only
Highlight the Navigation Menu for the Current Page
How to Set a Stroke-Width:1 on Only Certain Sides of Svg Shapes
White Space Showing Up on Right Side of Page When Background Image Should Extend Full Length of Page
How to Make a Transparent Canvas in HTML5
How to Do Awesome Refreshless Page Changes Like Github Do
Making a Div That Covers the Entire Page
How to Make Rooftext Effect and Valley Text Effect in HTML5 (Or Fabric.Js)
How to Notify the HTML Container That the Swf Has Finished, Using Swfobject
How to Embed Latex in a Webpage
CSS Select Elements with Partial Id
Html5 Video Not Working on iPad
<Video> Plays in Other Browsers, But Not Safari
Is This HTML Structure Valid? Ul > Div > { Li, Li } , Div > { Li, Li } , Div > { Li, Li }
Is HTML Considered a Programming Language
How to Link HTML Pages in Same or Different Folders
Why Is a Self-Closing Iframe Tag Preventing Further Dom Elements to Be Displayed