Hi All,
I've started to really get into blogging (
my blog) and am currently using images from
flickrCC in my posts. Currently I am always having to select the "small" size images, which don't take up as much space as I'd like but "medium" size images are too tall.
Instead of cropping every image as I put it in, is there a way I could create a box with the medium size image as a background and then shorten the box to crop the picture? If so, could someone show me the best way to do this using CSS.
Also, is there a way I could include the CSS code in the default page style and just add in the image's URL with minimal code while posting?
Comments
First make an A tag that links to the full-size image. Then inside of that A tag, you put an IMG tag that shows the thumbnail image. Here is a small demo.
If you don't like the border, toss in a border="0", or set the border to none using css, like this.
If you want to get really fancy, you can throw in some JavaScript to make a Lightbox like this.
HTML
<div id="foobar"></div>
CSS
div#foobar {
background-image: url( 'images/theimage.jpg');
width: 100px;
height: 100px;
}
Using this method you will only see the top and left pixels of the image. However, this is the wrong way to do things. You really should crop the images in an image editor. You will only be able to crop pixels off of the right or bottom of an image. Also, you will have to finagle the CSS a bit to make sure that some browsers aren't stupid about it. That might mean some floating, some clearing, some relative positioning, who knows.
Well I think I'll just stick to cropping images manually. Problem is that cropping an image means editing it which is against some variants of the CCL.
All clear, have fun.
Now, as for the solution to your question, it depends upon your needs, I'll try to indicate where choice is possible.
This is the html.
<div id="foobar" style="*¹">
<span>[Insert your alternative text here, example: "Image of a cat."]</span>
</div>
Of course you can name the div whatever you want, use something useful, like 'cropped-image' or something.
And here we have the css.
#foobar {
width:[Put your desired standard-width of the cropped image here];*²
height:[Put your desired standard-height of the cropped image here];*²
margin:0; // You can change margin and padding according to taste if I'm correct.
padding:0;
}
#foobar span{
visibility:hidden;
}
*¹: Now here you put the good stuff. Since we're only going to use the background style definition in this style tag for now, let's put it in.
<div id="foobar" style="background:;"></div>
Now we need values in there for it to actually work, first a background image:<div id="foobar" style="background: url([link to image]);"></div>
We don't want it to repeat itself, so for safety we'll throw no-repeat in there:<div id="foobar" style="background: url([link to image]) no-repeat;"></div>
And now the fun stuff, to offset the image as to not just show the top left, but the 'interesting' part of the image (say, crop the goatse image to... yes, that), we will need one, or two values. The first value will offset the image on the horizontal plane, and the second will offset it on the vertical plane. So, to 'move' the visible area to the bottom right of the image you'd use the values: cropped-image width minus full-image width px, and cropped-image height - full-image height px. This will result in two negative numbers. Now we'll have ended up with something like this:<div id="foobar" style="background: url('cat.png') no-repeat -50px -100px;"></div>
*²: If you wish to use different dimensions for a cropped image, just put add the new width and height to the style tag in the div. You will get something like this:
<div id="foobar" style="background: url(\'cat.png\') no-repeat -50px -100px; width: 150px; height: 250px;">
<span>Image of a cat.</span>
</div>
Tell me if that helped you, or if anything is unclear.
What does ";*²" at the end of the height and width styles do? (I know what ";" does).
Problems: I get the width but no height and the words "a picture of a cat" are still visible.
Also, can I scale the images as well?
#foobar span {
statement correctly?I'll see if I can think of a way to scale the images. But first copy the first stuff correctly.
Also, as Scott said, Bob made it horribly unreadable. Most likely on purpose.
Also, is there a way of putting the "no-repeat" in the CSS stylesheet file? I just want to have as little in the posts as possible to make changes easier.
This is what we had:
Here's the css:
// For the first 'image' we had this css.
And then the html:#foobar {
width:100px;
height:100px;
margin:0;
padding:0;
}
#foobar span{
visibility:hidden;
}
// For the scaled and cropped image we need this
#football {
width: 100px;
height: 100px;
margin: 0;
paddign: 0;
overflow: hidden;
}
#football img {
position: relative;
}
<!-- The first image -->
Just remove the linecuts and make it one sentence again in your code. It's known that I hate horizontal scrolling, more so if it's due to text.<div id="foobar" style="background: url('http://farm1.static.flickr.com/25/46121827_33deecf1ec.jpg')
[linecut against board wrap]no-repeat -200px -50px;">
<span>Image of a cat.</span>
</div>
<!-- And the second image -->
<div id="football">
<img src="http://farm1.static.flickr.com/25/46121827_33deecf1ec.jpg" style="width: 400;
[linecut]left: -150px; top: -40px;" alt="Image of a cat." title="Image of a cat." />
</div>
Hope that helps. Don't know how well either is supported in older browsers or old versions.
If I just set the image width will it change the height in proportion?
And yes, if you give a width, then: Here some examples, the image is scaled down to 80%. That is 400px wide and 265.6px tall.
Width only:
Height only:
Width and height:
If you use Firefox you can right click each of those, select properties and see that each is scaled down to 400 by 266 pixels.