/    Sign up×
Community /Pin to ProfileBookmark

Centre a graphig using CSS

I want a div to centre graphics on my page. I have tried

.centre-image
{
display: block;
margin-left: auto;
margin-right: auto;
padding-top: 1em;
padding-bottom: 1em;
}

without success. What should I use?

to post a comment
CSS

12 Comments(s)

Copy linkTweet thisAlerts:
@tirnaApr 25.2010 — divs are block level elements and so by default will be set 100% of the page width.

Give the div containing the images an appropriate width (< 100%) and the the left and right auto margins will then centre that div on the page.
Copy linkTweet thisAlerts:
@rickduleyauthorMay 10.2010 — I have been unable to gain success with any combination of

<i>
</i>.centre-image
{
/*display: block;*/
width: 80%;
margin-left: auto;
margin-right: auto;
padding-top: 1em;
padding-bottom: 1em;
}


regardless of percentage width and/or presence of the block declaration .

The plot, as someone once said, thickens ?
Copy linkTweet thisAlerts:
@tirnaMay 10.2010 — That css works for me so there must be something unusual with your html markup

The div width needs to be the same as the image width.

If you can, post your html markup as well so we can see what is going on.
Copy linkTweet thisAlerts:
@rickduleyauthorMay 10.2010 — Hi Tirna

Setting [I]width [/I]to the width of the image worked, but that means that the [I]div [/I]must have internal knowledge of the image. That means you have to have a new div for each image you use. ?

I have attached the whole box and dice.

Cheers

[upl-file uuid=3219d6f2-e70c-4bdd-90e6-ee296c314c6d size=70kB]CentredImage.zip[/upl-file]
Copy linkTweet thisAlerts:
@tirnaMay 10.2010 — Hi Tirna

That means you have to have a new div for each image you use. ?
[/quote]


Not necessarily.

You can get the image's width in pixels using

[CODE]document.getElementById("myPic").width[/CODE] where myPic is the id of the <img>

or if using preloaded images

[CODE]
var myPic = new Image();
myPic.src = 'pic1.jpg';
alert(myPic.width); //myPic.width is the image's width
[/CODE]


You can then set the image's container div to the image's width using javascript.
Copy linkTweet thisAlerts:
@rickduleyauthorMay 10.2010 — Hi Tirna


You can then set the image's container div to the image's width using javascript.
[/quote]


I try to avoid scripting languages. There are too many of them and most of the functionality I need in a webpage is already available through HTML/CSS. This situation should actually improve with HTML-5, but I'll reserve judgement until 5 is ratified and I start to use it. Call me a Luddite if you will, but I cannot see the sense in using technology for the sake of using technology. My engineering background tells me that the simpler solution is usually the most reliable.

However, it would appear that there is no generic means to centre a graphic using CSS and that the validatable solution remains:

<i>
</i>&lt;p style="text-align: center;"&gt;
blah blah blah
&lt;/p&gt;


Do you agree?
Copy linkTweet thisAlerts:
@tirnaMay 11.2010 — 

Do you agree?[/quote]



Try it and see if it does what you want.

Regarding My engineering background tells me that the simpler solution is usually the most reliable.
[/QUOTE]
"simple" is a relative term. For me, the simplest solutuon is to write 2-3 lines of js code and I would get the effect you require. But what is simple for me might not be simple for others and vice-versa.
Copy linkTweet thisAlerts:
@criterion9May 11.2010 — See:

.centre-image

{

/*display: block;*/

width: 80%;

margin-left: auto;

margin-right: auto;

padding-top: 1em;

padding-bottom: 1em;

[B]text-align:center;[/B]

}
[/quote]
Copy linkTweet thisAlerts:
@KorMay 11.2010 — See:
<i>
</i>.centre-image
{
/*display: block;*/
width: 80%;
margin-left: auto;
margin-right: auto;
padding-top: 1em;
padding-bottom: 1em;
[B]text-align:center;[/B]
}

[/QUOTE]

Nope. That will center only text and inline elements, not the block elements which might be inside. Could be:
<i>
</i>.center-image *{
display:block;
margin-left:auto;
margin-right:auto;
}
Copy linkTweet thisAlerts:
@criterion9May 11.2010 — Nope. That will center only text and inline elements, not the block elements which might be inside. Could be:
<i>
</i>.center-image *{
display:block;
margin-left:auto;
margin-right:auto;
}
[/QUOTE]


Images aren't block-level elements though...
Copy linkTweet thisAlerts:
@KorMay 11.2010 — Images aren't block-level elements though...[/QUOTE]
It depends on the browser, though... ? And you'd better make them to be so (block), and, most of the time, it is better to envelop them inside a DIV, to prevent an IE silly bug: an extra pixel below the image... ?. Even better might be using DIVs with a CSS class and a background-image. easier to handle, though ?
Copy linkTweet thisAlerts:
@rickduleyauthorMay 11.2010 — Hi all

Criterion9's solution (adding the line [I]"text-align: center;"[/I] to the div declaration) produced the desired result. Aarrgghhh!:mad: How simple is that! Still, it's always something obvious that you miss.

My thanks to Tirna and everyone who responded to my RFI.
×

Success!

Help @rickduley spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...