Centering in CSS is a pain in the ass. There seems to be a gazillion ways to do it, depending on a variety of factors. This consolidates them and gives you the code you need for each situation.
This is a code generator for your alignment needs.
CSS generator for centering text/div depending on their size or the container size.
Home Page: http://howtocenterincss.com/
@oliverzheng filed this issue from https://twitter.com/oliverzheng/status/593167841999724545
@oliverzheng filed this issue from https://twitter.com/oliverzheng/status/593166712645660672
Uncaught Error: Invariant Violation No content
Under "width / known" and "height / known", if a user clicks the "em" or "%" choices (which are styled to look like radio buttons, but whose markup is deceptively composed of divs), the "px" option is always selected. A second click is required for "em" or "%".
This requires extra attention (and extra interaction) from users.
Thank you.
@oliverzheng filed this issue from https://twitter.com/oliverzheng/status/593168797458046976
A map of options -> method is probably fine. Browser tests seem exhaustive for this.
The auto margin method to centre horizontally can only be used on divs of known width. This website incorrectly recommends it for divs of unknown width. The generated code does not work.
@oliverzheng filed this issue from https://twitter.com/oliverzheng/status/602970443448848384
@oliverzheng filed this issue from https://twitter.com/oliverzheng/status/593185481958952960
Cool site, coming from InDesign etc I was always bemused by how difficult it was to do "simple" layout things in CSS. I didn't test on IE (no Windows box) but Chrome and Firefox produced the output I was expecting, but Safari didn't ...
I don't see any mention of Safari on the site, so perhaps you don't intend to support it? It appears that Safari doesn't support flex
. Crappy gif video showing what I mean:
#app-loading {
background: white;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items:center;
font-size: 2em;
}
When I input unknown width and height for both container and content I get this output which doesn't center content vertically:
<div style="display:table-cell;vertical-align:middle;">
<div></div>
</div>
You need to have a wrapping element with display table like this: http://codepen.io/blmstr/pen/OVLarb
The first time I used the site, I was a bit confused because I couldn't see the code appear below.
Maybe do:
var codeElement = document.getElementById('generated-code')
codeElement.scrollIntoView()
or
document.getElementById('generated-code').scrollIntoView()
Because the page is composed mostly of divs rather than inputs, keyboard navigation (by presses of the tab, arrows, and spacebar keys) is not possible.
This slows user interaction, and it would seem to be against ARIA standards.
Thank you.
display: flex
no longer needs to be relied on to center items. Learn more
When centering a 50% box both horizontal and vertical:
But margin-top percentages are calculated from the width of the document, not the height. The vertical alignment does not work.
Instead, just change the top value:
top = (containerheight-contentheight)/2
And do the same width:
@oliverzheng filed this issue from https://twitter.com/oliverzheng/status/593168063647744000
howtofuckingcenterincss.com
howthefucktocenterincss.com
fuckingcenteringincss.com
centeringinfuckingcss.com
howtocentershit.com
centershitincss.com
I just tried your nice site, unfortunately the generate code button does not do anything. No code generated, no error message, nothing.
While trying to center text vertically when height of the parent is 100%, this code is generated:
line-height: 100%;
Unfortunately that sets the line height to 100% of the font size and not the parent. I propose the following change:
line-height: 100vh;
Which works in most modern browsers.
http://caniuse.com/#search=vh
Except IE didn't support the table display types until IE8.
It shouldn't be unknown if the outer div wants to be as tall as the inner div.
@oliverzheng filed this issue from https://twitter.com/oliverzheng/status/593202896671363072
This would be AWESOME as a Visual Studio Code extension. Perhaps it could even scan the markup in your current file somehow and automatically set the parameters from your current markup?
I might like to specify the width/height of my div
s i.e. rows, in fractions.
Is display: grid;
compatible with the flexbox rules proposed by the generator?
Is that a good solution?
Would you mind if I fork it and create a portuguese version? ๐
I would like to suggest having a clear all or a clear button on each section. Possibly could be a nice feature. I will fork it if you'd like for me to implement that feature to the site. It would be nice to have something for the user to clear their input since there is a lot of input. Also have you considered doing it in AngularJS to implement a live generation as the user inputs things. No issues with your site though just ideas. :D ๐
To the question:
Content
What do you want to center?
The option "Text" has description Just text, or an inline-level block of text and images.
Change inline-level
to inline-level and inline-block level
or something like that to make it more clear.
http://fb.me/react-0.13.1.min.js Failed to load resource: the server responded with a status of 403 (Forbidden)
bundle.min.js:1 Uncaught TypeError: Cannot read property 'Component' of undefined
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.