Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning different font sizes on the same line of text so it looks nice?

Basically, I want to have an h1 and a p element on the same line, but the alignment is off a lot (meaning the H1 is higher than the P and looks crappy) and I've never had to do anything like this with css before!

I've thrown together a jsfiddle to accompany this, here is the code:

<h1 style="float:left;display:inline;">"Hi!</h1><p style="float:left;display:inline;">I'm James, and I <strong>LOVE</strong> building clean, fast and (most importantly) effective websites. Oh, and I also know a thing or two about computers.</p><h1 style="float:left;display:inline">"</h1>

http://jsfiddle.net/9H8xE/

Thanks!

like image 466
MrS1ck Avatar asked Jul 21 '13 23:07

MrS1ck


People also ask

How do I align text in different sizes?

One thing to remember, if you have different sizes of text that you need to align, is to align it to x-height or baseline. If there is a caps font then align them to baseline or cap-height. And remember NEVER to align horizontally different font sizes fonts on the central line.

How do I align text and image on the same line?

Entire trick is to use float: left; CSS property on the img element and all the text will automatically wrap around that image. So if you want to align text and image in the same line in HTML like this... In short that is it.

Is is possible for two fonts of the same size to look like different sizes?

That is, fonts at the same point size can have different x-heights and sometimes varying cap heights. This can be quite frustrating. Both the x-height as well as the cap height of a font affect its legibility, and will make different typefaces look larger or smaller at the same point size.


2 Answers

Some advice before answer:

  1. P tag is meant to create a new paaragraph, so if you do not need that use span tag instead.
  2. Try to avoid inline styles, use CSS selectors.

http://jsfiddle.net/9H8xE/1/

Try this and let me know if it works

HTML:

<h1 >"Hi!</h1><p>I'm James, and I <strong>LOVE</strong> building clean, fast and (most importantly) effective websites. Oh, and I also know a thing or two about computers.</p><h1>"</h1>

CSS:

p
{
    display:inline;
}
h1{
    display:inline
}
like image 124
user2580076 Avatar answered Sep 26 '22 03:09

user2580076


The technical problem with the code in the question is that float: left prevents display: inline from taking effect, setting display to block instead, so remove all the float: left declarations.

A different issue is that you have the closing quotation mark as the content of an h1 element after the p element. This is very illogical. The technical implication is that this character will appear in the font size of h1, causing uneven line spacing unless you set h1 font size to the same as p font size. You need to decide what you want: why would you want to a have a large-size closing quote after normal-size text, and if you do, how should it be rendered?

like image 40
Jukka K. Korpela Avatar answered Sep 26 '22 03:09

Jukka K. Korpela