Learn to customize your own blog with confidence

Code it Pretty is an archive of tutorials from 2012-2015. Tutorial details may be outdated. More Info

Showing posts with label web design. Show all posts
Showing posts with label web design. Show all posts

You Can't Float Center with CSS

If you're new to web development or just starting to play around with CSS, what I'm about to say may surprise or disappoint you:

There's no way to float center in CSS. You can float left. You can float right. But you can't float center.

Yes, I'm sure.

you can't float center

Of course, this doesn't mean you can't center with CSS. Today I'll cover two basic centering methods to get you started, and I'll also point you to some resources for learning more advanced centering techniques.

Why Can't CSS Float Center?

I know, it's weird. It seems like something you should be able to do, doesn't it?

Think of it this way, though: imagine you're in a pool, floating on a raft. You paddle yourself to the very center of the pool, perfectly equidistant from the left and right sides, and you float there... for a moment. Unless you're tethered or you start paddling to keep yourself in the center, you're going to drift away from it.

Your HTML element is not relaxing in a pool, but when you tell it to float, you need to tell it which way. Left and right are real directions it can float in. "Center" isn't.

So, How Do I Center With CSS?

That depends on what you're centering, but there are two good go-to options.

Before I get to those, let me tell you one quick important thing: when you're centering an element, it centers within its container. So, for example, if you use a centering technique on a photo in your blog's sidebar, it will be centered within the sidebar. It won't jump out of its sidebar and land in the center of your page unless you do something really wacky. The same goes for anything centered within a blog post — it will be centered from the left and right edges of the post column, not necessarily at the very center of your page.

Centering Text

If you're centering text, text-align: center; will do the trick. Here's an example of the CSS styling of a simple, centered heading:

h1 { text-align: center; }

That will give you a headline centered between the left and right edges of its container.

Centering Other Elements

If you're centering something else, margin: 0 auto; will get your element centered most of the time. (Quick note: your element must have a declared width for this to work.)

The margin: 0 auto; rule is shorthand for 0 top and bottom margin, and automatic left and right margins. Automatic left and right margins work together to push the element into the center of its container.

The margin: 0 auto; setting doesn't work perfectly in every centering situation, but it works in a whole lot of them. I'll point you to some more information on advanced centering in the Further Reading at the bottom of this post.

What About Using <center> in HTML?

Short answer: No. Bad. Don't.

Long answer: The <center> tag is deprecated as of HTML 4, and using it creates a few different issues. HTML centered elements can display differently in different browsers, and using the <center> tag can increase page load time. Also, heavy use of <center> will complicate your site redesigns — removing hundreds of <center> tags takes a lot longer than changing one style rule in a stylesheet.

The <center> tag was officially deprecated many, many years ago, but it is still recognized by most browsers through their backward-compatibility features. So yeah, if you <center> something, it'll be centered. However, in the interest of future-proofing your blog — and sparing yourself (or your developer) the agony of sifting through old <center> tags — you should use modern CSS centering methods instead.

Further Reading:

Now that I've taken you through the basics of CSS centering, let me show you a selection of resources to help you step up your CSS centering game!

"Centering in CSS: A Complete Guide", from CSS-Tricks.com. How to center everything. The tutorial has a helpful decision-tree format.

Also from CSS-Tricks.com, "Faking Float Center". A clever CSS trick indeed, you can use this to center an image between two columns of text. Very cool.

Design Shack's "How to Center Anything with CSS"; lots of practical centering advice.

For more information on why the <center> tag was deprecated, and what to use instead of it, I recommend this Sitepoint forum thread. You'll need to skim through a bit of nerdy grouching, but there are thick slabs of good info in there.

Floating frangipani photo by Flickr user Rolfe Kolbe, used under Creative Commons license.

The Font Combinator - Find Beautiful Web Fonts

The Font Combinator, a web typography tool

I love Google Web Fonts. They're free, they're stylish, and they're easy to use on any website. There's one thing lacking from the web fonts interface though — it's not easy to preview how different fonts look together. That's where developer Chip Cullen's web app, The Font Combinator, comes in!

The Font Combinator is so easy to use it barely needs a tutorial, but I want to give you a rundown of its features so you can use it to its fullest right away.

To start using the Font Combinator, go to www.font-combinator.com.

the font combinator website

At the top of the page you'll find a sample article with a headline, subhead, and two paragraphs. At the bottom of the page you'll find simple controls for changing the font, the font size, the line height, the color of the text, and the color of the background.

The Font Combinator includes all of the Google Web Fonts, plus the traditional "web safe" system fonts like Arial, Helvetica, and Times New Roman. So handy for setting fallback fonts! The combinator automatically updates when a new font is added to the Google fonts collection, so it's always up to date.

Here's an example of the Font Combinator in action: I've selected three fonts for this demo. The headline is Syncopate, the subhead is Rammetto One, and the body text is in good ol' Georgia. I've also played around with the colors using the Combinator's built-in color picker.

font combinator in use demonstrating different fonts

Did you notice that I changed what the text says, too? Yet another cool feature! You can delete the default headings and paragraphs and replace them with your own.

You can hide portions of the demo text if you're not going to use all of the elements in your design. So, for example, if you're not planning to use a subhead and just want to test a headline and a paragraph font, you can easily hide the subhead by clicking the "hide" button. If you want it to come back, click the "show" button.

Here's another useful detail: if you resize your browser window, the combinator resizes along with it. This way you can test how your fonts will look in different sections of your site or in different viewports, and adjust your sizing (or font choices) accordingly.

I've spent hours playing with this addictive design tool, and if you're a font fan like me I bet you will, too! Many thanks to Chip Cullen for his hard work on this awesome app.

Installing Google Web Fonts

After you've chosen the perfect font, you're ready to install it! Check out How to Install Google Web Fonts on Your Blog for installation instructions.

Fonts in the title card: Alegreya SC and Pinyon Script, selected with the Font Combinator, of course!
Embellishments: Creative Market