How to have 4 sizable images HTML Wordpress - html

On Apple.com, their homepage, underneath the slides, you see 4 clickable posts/images and when you scale the browser window(or use a phone or tablet), they scale according.
Is their a plugin for Wordpress that mimics this type of behavior or HTML code?
Where you can have 4 images to scale to the UI, simple HTML code?
I know there are plugins that do this with latest posts but its not what I want. Thank you in advance.
Example image

They are using the background-size attribute. What that does is ensure the image is contained within the parent element and scaled up or down to ensure that it's at 100% width and height but still within the parent container.
It accepts a couple of properties. Here is the table of browser support.
background-size: contain;
Contains the image within the parent element and does not overflow.
background-size: cover;
Ensures the background image covers the full width and height of the element it is applied to. It won't skew the aspect ratio, it will simply overflow the element to the right size to maintain the aspect ratio.
Also, just to note, you can use pixels and media queries to explicitly set the image width/height ratio. This would look like this and goes in the order of width then height and if you use one value instead of two, that will also work.
background-size: 250px 150px;
Note that the background image is contained to the element it is applied to regardless. So even though it technically overflows when using cover, it doesn't visually spill over the element so it doesn't look messy.

Related

Controlling the size of a background image

With JavaScript, the element is assigned to var a. If I simply assign its style background to an image (most images) as such: a.style = 'background:url(image.jpg)';, the image's width and length will adjust perfectly to fit that of the div element's by default.
One particular issue though: I've snapped a quick pic with my phone (.jpg) and downloaded it on my pc and assigned its url value to the div element's background. It however does not adjust its width and height to that of the div element's.
Why is this? and how do I correct this issue?
Thanks in advance.
To control the size of the background-image, you can use the background-size property.
To ensure the image stays within the constraints of the div, you would use "contain"
background-size: contain;
https://developer.mozilla.org/en-US/docs/Web/CSS/background-size

Difference between background-size:cover and background-size:contain

Visually I can appreciate the difference, but in which situations should I prefer one over the other?
Is there any point using them at all or can they be replaced by percentages?
Currently I don't seem to be able to go beyond a trial-error approach when using these properties, which does my head in.
Also I can only find pretty vague explanations and especially I find the W3C doc quite baffling.
Values have the following meanings:
‘contain’
Scale the image, while preserving its intrinsic aspect ratio
(if any), to the largest size such that both its width and its height
can fit inside the background positioning area.
‘cover’
Scale the image, while preserving its intrinsic aspect ratio (if any), to the
smallest size such that both its width and its height can completely
cover the background positioning area.
I'm probably being a bit thick, but can anyone give me a plain English explanation with relative examples?
Please use this fiddle. Thanks.
CSS
body{
width:500px;
height:500px;
background:url(http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg);
background-size:contain;
background-repeat:no-repeat;
}
Note
The accepted answer is the one I currently find the most concise and complete.
Thanks everybody for their help.
You can consider looking at the pseudocodes that govern the output. The values allotted to the image's size depend directly on the aspect ratios of container wrt aspect ratio of the background image.
Note: Aspect ratio = width / height
Contain
if (aspect ratio of container > aspect ratio of image)
image-height = container-height
image-width = aspect-ratio-preserved width
else
image-width = container width
image-height = aspect-ratio-preserved height
Cover
if (aspect ratio of container > aspect ratio of image)
image-width = container width
image-height = aspect-ratio-preserved height
else
image-height = container height
image-width = aspect-ratio-preserved width
You see the relation? In both cover and contain, aspect ratio is preserved. But the if - else conditions reverse in both the cases.
This is what makes cover to cover full page, without any white portion visible. When aspect ratio of container is greater, scaling image so that its width becomes equal to container width. Now, height will be greater, as aspect ratio is smaller. Thus it covers the whole page without any white portion.
Q. Can they be replaced by percentages?
No, not simply by percentages. You'll need conditioning.
Q. In which situations should I prefer one over the other?
When you are creating a website, you wouldn't want any white portion in the fixed background. So use cover.
contain on the other can be used when you are using a repeating background (e.g. when you have a pattern image with very high aspect ratio wrt veiwport/container you can use contain and set background-repeat to repeat-y). But a more appropriate use for contain would be for a fixed height/width element.
Although the question assumes the reader already understands how the contain and cover values for background-size work, here's a plain-English paraphrasing of what the spec says, which can serve as a quick primer:
background-size: contain ensures that the entire background image will fit the background area, keeping its original aspect ratio. If the background area is smaller than the image, the image will shrink so that it can fit the background area. If the background area is either taller or wider than the image, then any parts of the area not occupied by the main image will either be filled by repetitions of the image, or letterboxes/whitespace if background-repeat is set to no-repeat.
background-size: cover makes the background image as large as possible such that it will fill the entire background area leaving no gaps. The difference between cover and 100% 100% is that the aspect ratio of the image is preserved, so no unnatural stretching of the image occurs.
Note that neither of these two keyword values can be expressed using any combination of lengths, percentages, or auto keywords.
So when do you use one over the other? Personally, I think cover has more practical uses than contain, so I will go with that first.1
background-size: cover
One common use case of background-size: cover is in a full-screen layout where the background image is rich in detail, such as a photo, and you want to feature this image prominently, albeit as a background as opposed to the main content.
You want just enough of the image to be able to completely cover the browser viewport or screen, regardless of the aspect ratio of the viewport, or whether the image or the viewport is in portrait or landscape. You're not concerned if any parts of the image are cropped out as a result of this, as long as the image fills up the entire background area and maintains its original aspect ratio.
Here's an example of a layout where the content is housed in a semitransparent white background, which hovers over a full-screen background. When you increase the height of the preview pane, notice that the image automatically scales up to ensure that it still covers the entire preview area.
html {
height: 100%;
background-image: url(http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg);
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
body {
width: 80%;
min-height: 100%;
background-color: rgba(255, 255, 255, 0.5);
margin: 5em auto;
padding: 1em;
}
If you use background-size: contain instead, what happens is that the background image shrinks in order for the entire image to fit in the preview pane. This leaves ugly white letterboxes around the image depending on the aspect ratio of the preview pane, which ruins the effect.
background-size: contain
So why would one use background-size: contain if it leaves ugly blank spaces around the image? One use case that immediately comes to mind is if the designer doesn't care about the blank spaces, so long as the entire image fits within the background area.
That may sound contrived, but consider that not every image looks bad with empty space around it. This is where the example of using a logo instead of a photo actually demonstrates this best, even though you probably won't find yourself using a logo as a background image.
A logo is typically an irregular shape sitting on either a blank or completely transparent background. This leaves a canvas that can be filled by a solid color or a different background. Using background-size: contain will ensure that the entire image fits the background without any parts of it being cropped out, but the image still looks right at home on the canvas.
But it doesn't necessarily have to apply to an irregularly-shaped image. It can apply to rectangular images as well. As long as you require that no cropping of the background image occurs, whitespace can either be seen as a reasonable tradeoff, or not a big deal at all. Remember fixed-width layouts? Think of background-size: contain as essentially that, but for background images and in both portrait and landscape orientations: if you can ensure that the content will always fit the boundaries of the background image at all times, then whitespace becomes a non-issue altogether.
Although background-size: contain will work whether or not the image is set to repeat, I can't think of any good use cases involving repeating backgrounds.
1 Note that if you're using a gradient as a background, both contain and cover have no effect because gradients do not have any intrinsic dimensions. In both cases, the gradient will stretch to cover the container, as though you had specified 100% 100%.
background-size:cover will cover the entire div with the image. This could be useful for showing thumbnail images of a main image where the entire image being displayed isn't that important, but you still want to conform to a size for all images. (for example, a blog post excerpt)
background-size:contain will show the entire image within the div. This can be useful if you specifically want to display the entirety of the images, but within a set container div size. (For example, a collection of company logos)
Both keep the image at the same aspect ratio
http://cdn.onextrapixel.com/wp-content/uploads/2012/02/cover-contain.jpg
background-size:contain;
When using contain you may still see white-spacing, due to the way that it sizes and contains itself within the element.
background-size:cover;
will completely cover said element, you will not see any white-spacing
source:
http://www.css3.info/preview/background-size/
see example H
edit: use background-size:contain if:
You want it so that your image is always displayed in the viewport. Please note that: while you can see the full image, it will leave white spacing either on the top or bottom of the image whenever the aspect ratio of the browser and window are not the same.
use background-size:cover if:
You want a background-image, but you don't want the negative effect of the white-spacing which contain does have, please note that: when using background-size:cover; you may experience that it will cut off some of the image.
source: http://alistapart.com/article/supersize-that-background-please
Contain:- Scale the image to the largest size such that both its width and its height can fit inside the content area.
Exmaple:
Cover:-Scale the background image to be as large as possible so that the background area is completely covered by the background image. Some parts of the background image may not be in view within the background positioning area.
Example:
We had a huge conversation about cover vs contain just want to share this thoughts:
landscape image on landscape screen - best to use cover
portrait image on landscape screen - best to use contain
portrait image on landscape screen - best to use contain
portrait image on landscape screen - best to use contain
Illustration:
if(iDonPutSomeCode) const result = iCantPasteLinkToCodePen
https://codepen.io/Kinosura/pen/EGZbdy?editors=1100

background image width and height to fit all media - unable to make it work

I had created a fluid layout (to build a RWD website) and in my design there are some images which needs to be shown as background image, as there will be text shown above the image (over lapping it).
Now in 'fluidLayouts.css' file, it shows:
img, object, embed, video { max-width: 100%;}
I have edited to:
img, object, embed, video
{max-width: 100%;
height:auto;}
Here i have added "height:auto;" but nothing happens to the background image, i don't see it fully, nor it re-sizes to any media which are mobile, tab or desktop.
How do i set a height for an background image for it to be scaled automatically to any media? Am using background image to the website design overall.
You are mixing two things here: img, object, embed and video are block elements that behave like they are supposed to (being block elements).
background-image is just a CSS property applied to an element (with some certain width and height that is calculated by yout CSS layout) but background-image itself does not and can not influence the size of an element.
Here's the fiddle for you to see how different settings behave: jsFiddle
You have two options.
First one is to use regular img tag and put the content above it using relative positioning.
Second one is to use background-image and either just apply background-size: cover - this will not however keep original proportions of an image, or and background-size: cover plus width declaration (whatever you like, fixed pixels, 100%, doesn't matter) and use a padding-bottom hack to extend this div to desired aspect ratio. So if your image is 500/300 than your padding-bottom needs to be (300/500)*100 = 60%.

Scaling images in HTML with a minimum width/height in HTML/CSS only

I have to display a bunch of images in a page. Images are of different size, some very wide and some very thin. I want to put them all in a container of fixed width and fixed height.
The logic of placing the images should be like this:
Say if image is smaller than the container, scale it up to the maximum size such that the aspect ratio is maintained, and put it at the center of container.
If image is bigger, scale it down while maintaining the aspect ratio.
Some examples:
Say our container is 150x150, and we have an image sized 100x50. In this case the image should be scaled up to 150x75.
If we have an image sized 100x300, the image should be scaled down to 50x150.
While this can be easily done with javascript, I'd like to avoid that if possible.
I'm wondering if there's any way to accomplish this with CSS alone. I can live with a CSS3 only solution, or even with webkit/firefox specific directives, as long as it works on latest versions of Chrome, Firefox and Safari (I will use a fallback for IE if there's no choice).
Edit: I know about max-height and max-width of course. The issue is that if I set both max-height and max-width to 150, images won't be scaled up if needed.
Don't use an <img>. Instead, use a background-image style:
background: transparent url('path/to/image.png') no-repeat scroll center center;
background-size: contain;
The background-size does the magic, scaling the image up or down so that it snugly fits inside the container.
Check out this fiddle http://jsfiddle.net/demchak_alex/FazvX/3/
Though this only works if you can apply classes to individual images
EDIT:
if you can use background images, check out this fiddle http://jsfiddle.net/demchak_alex/FazvX/4/
The "image only with added classes" works on the top, and the using background images works on the bottom.

How can I position an image in a box such that it fits exaclty in width or heigth, whichever is smaller?

I want to load some photos from a server and display each of them in an own box such that the box is filled and the image centered (not stretched), if it is to big. Can I achieve this for example with CSS without knowing the size of each image? Maybe with max-width or so?
Here is an example of what I want:
You could use the CSS3 background-size property.
Specifically, you would use either background-size:contain or background-size:cover.
From the spec:
Values have the following meanings:
‘contain’
Scale the image, while preserving its intrinsic aspect ratio
(if any), to the largest size such that both its width and its height
can fit inside the background positioning area.
‘cover’
Scale the image, while preserving its intrinsic aspect ratio
(if any), to the smallest size such that both its width and its height
can completely cover the background positioning area.
Source: http://www.w3.org/TR/css3-background/#the-background-size
Which one you used would depend on the aspect ratio of the original images you are using.
Scroll down on this resource to see some examples: http://www.css3.info/preview/background-size/
The quickest thing that you can do is to put the image as a background image that is centered:
style="background: url(images/42.png) 50% 50% no-repeat"
Images smaller than the box will be centered in the box. Images that are larger will experience cropping.
The downside is, there is no scaling.
For scaling, you would have to know the dimensions, employ some math to calculate a scaling amount that will preserve the aspect ratio and use an actual element that is inside a cropping container that uses "overflow: hidden".
Here what you do. If for instance the image is inside a DIV with an ID called "boxer" You'll now create a CSS that will automatically re-size every image that's inside the DIV with the ID "boxer" The CSS will look like this
#boxer img {
Width: 600px
Height: 600px;
}
The above CSS will automatically re-size whatever image you put inside to the specifications in the CSS. This will fit the box with the ID "boxer" precisely if the dimensions corresponds to that of the CSS. You could just do 100% for both the width and the height, that way it fits the box.