Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Image positioning in CSS

Hello, I have just taken the first module in the website basics and am now attempting to build my own site.

I am trying to put a holding page up on the domain I've just bought with a simple image centrally aligned with a coming soon link. I am have trouble aligning my image though.

I've tried using the grid.sss, in-line position and in the style.css.

I am a COMPLETE beginner so know this is a very basic question but if anyone could point me int he right direction as none of the online forums are making sense.

Thank you very much in advance.

Becky

9 Answers

James Barnett
James Barnett
39,199 Points

Becky Pate - Your CSS is working fine, in CSS it's text-align: center; it uses the American spelling center instead of the British spelling of centre I noted you were using.

Hey James, I guess changed the spelling and it worked! Thanks for your help!

James Barnett
James Barnett
39,199 Points

@Becky Pate - Here's a quick primer on how to type code on the forum.

Check out codepen it's was invented getting help issues with CSS on forums.

Don't know how to use it? No problem! :smile:

Here's a quick guide on how to use codepen

James Barnett
James Barnett
39,199 Points

Becky Pate -

Do you guys run any meetups for TeamTreehouse members?

There are some member run meetups going in various cities. In London in particular Treehouse sponsors the Untangle the Web meetup.

Great I've signed up! Thanks for the link to codepen too :)

Jon Wood
Jon Wood
9,884 Points

I don't know too much about CSS, but maybe this StackOverflow link will help?

And I'm sure more people will chime in that have much more experience in it than me.

James Barnett
James Barnett
39,199 Points

Jonathan Wood - In general here on the forum if you don't have specific experience with the video/code challenge they are having issues with it is best to move on to another question that is more in your area. It generally results in less confusing for the newbies.

Thanks and rock on with your forum'ing :guitar:

Andy Wickes
Andy Wickes
5,093 Points

Hi Becky,

Have you got a link we can look at so we can try and diganose what solution you need?

Hey, I don't have a link as it's just on my local.

My html looks like this though:

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Rebecca Pate</title> <link rel="stylesheet" href="normalize.css" type="text/css" media="screen"> <link rel="stylesheet" href="grid.css" type="text/css" media="screen"> <link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection"> </head> <body> <div class="container clearfix"> <h1>Rebecca Jane Pate</h1> <div class="box"> <img src="Rebecca.jpg" alt="Rebecca Pate" class="img"> </div>

<p>coming soon</p>

</div> </body>

</html>

and my style.css like this:

h1{ color: #0066FF; text-align: centre; }

h2 { color: #9966FF; text-align: centre; }

body { font-family: 'Raleway', sans-serif; color: #0066FF; background: #FFFFED; text-align: centre;

}

box { text-align: centre; }

I am wondering whether it's something to do with the grid.css. As I said I am complete beginner so really appreciate your help.

Do you guys run any meetups for TeamTreehouse members?

Becky

oh that didn't work! How is best to share with you?

Andy Wickes
Andy Wickes
5,093 Points

Hiya,

Looks like the image part of the HTML is missing here - can you copy and paste all the HTML, followed by all the CSS into the forum and I'll take a look!

No apologies needed about being a beginner! We're all on here because we're learning something!

Not sure about meetups as I've not looked into that side of things - I'm sure there will be some sort of social side of things though!

Cheers

Jon Wood
Jon Wood
9,884 Points

No worries. I would suggest having a GitHub account (I really like what they're doing) and you can definitely give links out to your code that way.

There are a few sites that let you share CSS/HTML/JavaScript such as CodePen (It seems the Treehouse instructors use this quite a bit), Plunkr, and JSFiddle. I'm sure there are quite a few more out there, as well.

James Barnett
James Barnett
39,199 Points

Github repos is a terrible solution for sharing one off bits of code, however Github gists is pretty good for backend code that codepen doesn't support.

So that's what I tried to do above (copy and paste the html) but here's my second attempt:

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Rebecca Pate</title> <link rel="stylesheet" href="normalize.css" type="text/css" media="screen"> <link rel="stylesheet" href="grid.css" type="text/css" media="screen"> <link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection"> </head> <body> <div class="container clearfix"> <h1>Rebecca Jane Pate</h1> <div class="box"> <img src="Rebecca.jpg" alt="Rebecca Pate" class="img"> </div>

<p>coming soon</p>

</div>

and the CSS

h1{ color: #0066FF; text-align: centre; }

h2 { color: #9966FF; text-align: centre; }

body { font-family: 'Raleway', sans-serif; color: #0066FF; background: #FFFFED; text-align: centre;

}

box { text-align: centre; }

oh dear. I can't even seem to copy and paste! I created a github account but not sure how to use it...

Jon Wood
Jon Wood
9,884 Points

Awesome. Actually, I believe the Git courses on here also use GitHub and the help on the site is very useful. We're all here to help, if needed, as well!

Andy Wickes
Andy Wickes
5,093 Points

Hi Becky,

Thanks - from the HTML and the CSS you pasted in, I'm not seeing any reference to an image? And I believe your problem is that you are trying to align an image?

I would expect to see something in your HTML along the lines of:

<img src="file.jpg">

Or in your CSS

.header {background-image: (url='file.jpg'); }

Is there something missing from the HTML and CSS you pasted?

If I was doing it I'd probably add this in:

<div align="center"> <img src="IMAGE NAME HERE" alt="my image"> </div>

This ought to align the image centrally within the DIV.

Cheers

Andy Wickes
Andy Wickes
5,093 Points

Ahhh, I think the forum is stripping out HTML - just seen that my answer above is incomplete!

What I was going to suggest you do is either:

<div align="center">
<img src="image goes here" alt="alt text goes here">
</div>
Andy Wickes
Andy Wickes
5,093 Points

Or alternatively you could do this:

HTML

<img class="center" src="image goes here" alt="alt text goes here">

CSS

img.center {
display: block;
margin-left: auto;
margin-right: auto 

} ... <IMG class="displayed" src="..." alt="...">

Hi Andy, thank you so much for your help. I tried both of these options and they both worked :D

Out of interest, which is more commonly used? Is it better practice to use the div or the class?

Thanks again, really appreciate it :)

Andy Wickes
Andy Wickes
5,093 Points

Hi Becky,

You're welcome! The CSS solution is the more commonly used solution as it follows the 'web standards' doctrine of keeping markup (HTML) and styling (CSS) separate.

So the HTML calls the image, and the CSS does the alignment bit - the first solution achieves the same result , but by adding the align='center' bit into the HTML we are essentially combining both markup and styling in the HTML document, which isn't ideal!

Cheers

Andy

OK, got it, thanks. Super helpful :)