/    Sign up×
Community /Pin to ProfileBookmark

How do I pur a grey border around an image with a gap?

I have an image.
I want to put a border around it.
But, I want to have a gap of X pixels between the border and the image.

How do I do this?
I can put a border around, but I can’t figure out how to put the gap in?

Thanks.

OM

to post a comment
CSS

10 Comments(s)

Copy linkTweet thisAlerts:
@WebJoelJul 21.2007 — <img src="style="width:100px; height:100px; [B]border:3px solid gray; padding:10px[/B];" />
Copy linkTweet thisAlerts:
@OM2authorJul 21.2007 — <img src="style="width:100px; height:100px; [B]border:3px solid gray; padding:10px[/B];" />[/QUOTE]
does this assume i know the exact diemnsion of the image?

i need to be able to apply the style to any image.

i tried adding the padding... didn't work.

do i need to add the padding after border?
Copy linkTweet thisAlerts:
@OM2authorJul 21.2007 — seems to work now.

thanks.

?
Copy linkTweet thisAlerts:
@FangJul 21.2007 — Order of properties is no relevant in this case&lt;img style="width:100px; height:100px; border:3px solid gray; padding:10px;"&gt;with or without height/width.
Copy linkTweet thisAlerts:
@WebJoelJul 21.2007 — re:<img src="style="width:100px; height:100px; border:3px solid gray; padding:10px;" /[/QUOTE]> I actually wrote this incorrectly: should be:<img src[B]="#"[/B] style="width:100px; height:100px; border:3px solid gray; padding:10px;" />[/QUOTE] and no, this does not parclude that this is the only width. I use Firefox and assuming you do also, using this snippette without a width & height and the absence ("#") of an image source, the image size would be a miniscule dot with a 10-px padding and a 3-px border all-around. -A 26-ish px square block of solid gray with a white dot in the middle maybe..

"border" is outside of the image, "padding" is inside the image's dimensions and part of the width/height. Therefore, the stated image being 100-px square is somewhat deceiving, as the padding adds 20-px to the total width and the total height of the image, with an additional 6-px added to each of width/height for borders...

Now, -you wanna get fancy? ? -Let make this a [I]class[/I]

CSS:.imgFRAMED {border:3px solid gray; padding:10px;}[/QUOTE]HTML:
<img [B]class=".imgFRAMED[/B]" src="#" style="width:50px; height:100px;">

<img [B]class=".imgFRAMED"[/B] src="#"style="width:75px; height:120px;">

<img [B]class=".imgFRAMED"[/B] src="#" style="width:190px; height:50px;">[/QUOTE]
Here, we have applied the 'styling' as a 'class', applied to multiple instances of images of varying widths/heights. They will ALL now have a 10-px padding all-around and a 3-px gray border 'framing' them! ?

This is the beauty of CSS, -you can re-write things and make them re-useable, more condensed. -If these images all occur only inside of a specific DIV with an ID, we can shorten the code even more:

CSS:#view {[I]foo:bar[/I]}

#view img {border:3px solid gray; padding:10px;}[/QUOTE]
html:
<div id="view">

<img src="#" style="width:50px; height:100px;">

<img src="#" style="width:75px; height:120px;">

<img src="#" style="width:190px; height:50px;">

.... etc.

</div>[/QUOTE]
Now, [I]any[/I] img occurring inside of the DIV with the id="view", will have this 10-px padding & 3-px gray border all-around. -We reduced the code a bit further.
Copy linkTweet thisAlerts:
@OM2authorJul 21.2007 — guys: thanks for the replies.

i think i had something eroneous in my code to begin with that was stopping it working.

the replies have been very educational.

some questions on the replies: what happens if i leave out width and height?

is this bad practice?

i find it very useful to leave these out: i can change images just by chaging the name - and need not worry about the width and height, which otherwise i would have to.

style="width:50px; height:100px;"

why put this inside a style?

#view {foo:bar}

what's foo:bar??

thanks.
Copy linkTweet thisAlerts:
@FangJul 22.2007 — what happens if i leave out width and height?[/QUOTE]
The images are loaded after the document. The browser uses the height/width values to calculate the size of a placeholder for the images and position of other elements in relation to the image.

If no height/width information is available no space is allotted to images, so when the images finally load, text and images will visually move to accommodate the images.

Best practice is to include height/width values.
what's foo:bar??[/QUOTE]
Generic names. http://en.wikipedia.org/wiki/Foobar
Copy linkTweet thisAlerts:
@OM2authorJul 22.2007 — thanks... i understand now. ?

one question you missed:

style="width:50px; height:100px;"

why put this inside a style?

why use style?

i wouldn't normally use style.

let me know what you think.

thanks.
Copy linkTweet thisAlerts:
@FangJul 22.2007 — Personal preference.
Copy linkTweet thisAlerts:
@WebJoelJul 22.2007 — Yes, -personal preference. For demonstration purposes, I tend to write my CSS as 'inline styles' such as <img src="#" [B]style="foo:bar;[/B]" /> whereby the [B]bold[/B] is called "inline style".

When actually writing a page for self or client, I start this way, move the 'inline style' to an 'internal STYLE' section "<STYLE>Selector {}.... </STYLE>", and lastly when everything is working correctly, I would make the STYLE be an external CSS document that is linked into the page.
×

Success!

Help @OM2 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 5.23,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...