Overcoming floats and absolute

OK, I put together a limited demo showing how the lower image can be coded to be responsive from desktop width through narrow device widths.

This is ONLY about the bottom image and text. It does not consider the menu column because I’m not keen on the way the menu column is structured on the page.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>template</title>
<!--
https://www.sitepoint.com/community/t/overcoming-floats-and-absolute/255162
safeasfcuk
-->
    <style type="text/css">
* {outline:1px dashed blue;}  /* TEST Outline.  To Be Deleted */

.outer {
    max-width:960px;
    margin:0 auto;
}

.postTop {
    overflow:hidden;
}
.postLeft {
    float:left;
    width:30%;
    min-width:200px;
    max-width:300px;
    padding:1.5%;
}
.postBox {
    padding-top:100%;
    background-image:url("http://www.wns.com/Portals/0/Images/HeaderBanner/desktop/1933/85/India_HD.jpg");
    background-size:cover;
    background-position:0 0;
}
.postRight p {
    line-height:1.4;
    padding: 1% 2%;
}
date a {
    background: transparent url(_images/posted.png) no-repeat top left;
    text-decoration: none;
}
.readmore {
    float:right;
}
.postBottom {
    overflow:hidden;
    padding: 1% 2%;
}

@media screen and (max-width:400px) {
    .postLeft {
        float:none;
        width:auto;
        max-width:none;
    }
    .postBox {
        padding-top:53%;
    }
}
    </style>
</head>
<body>

<div class="outer">

    <section class="post">
        <div class="postTop">
            <div class="postLeft">
                <div class="postBox"></div>
            </div>
            <div class="postRight">
                <h2>How to travel with no money</h2>
                <p>Nam dapibus nisl vitae elit fringilla rutrum. Aenean sollicitudin, erat a elementum rutrum, neque sem pretium metus, quis mollis nisl nunc et massa. Vestibulum sed metus in lorem tristique ullamcorper id vitae erat. Nulla mollis sapien sollicitudin lacinia lacinia. Vivamus facilisis dolor et massa placerat, at vestibulum nisl egestas. Quis mollis nisl nunc et massa. Vestibulum sed metus in lorem tristique ullamcorper id vitae erat. Nulla mollis sapien sollicitudin lacinia lacinia. Vivamus facilisis dolor et massa placerat, at vestibulum nisl egestas.</p>
            </div>
        </div>
        <div class="postBottom">
            <date><a href="#">Posted 20 Feb</a></date>
            <div class="readmore"><a href="#">Read More</a></div>
        </div>
    </section>

</div>

</body>
</html>

That’s the sort of thing I would do with server-side scripting which removes any client-side dependency (that could be drifting OT).
Thought there is css for this. The new object-fit property can allow you cover on foreground images.

Though it is yet to be supported by Microsoft’s browsers. :disappointed:
http://caniuse.com/#feat=object-fit

There is a semantic consideration between foreground and background images, whether the image is actual content (foreground), or merely decorative (background).
Agreed the img in the ul does not seem appropriate, maybe if it were a gallery of many images/thumbs it could be a list.

1 Like

You can easily reproduce background-size:cover for foreground images with a little extra css.

Of course as with background-size:cover you may lose the focal point of the image if the image aspect ratios differ wildly.

2 Likes

Thank you for taking the time to answer. I did mean landscape sorry got that mixed up.

I was wondering how it woud affect the flow of the document if I used background images?

I was thinking if you could offer a valid way of achiving this without JS then i would scrap the JS all together.

I will definatly only use one version of the site that then is managed via @media instead of mulitple layouts.

After discovering the display:table earlier in the post I am tempted to redesign the site using this instead of floating the nav and the content and then clearing the footer.

I will def loose the ul from the images

:slight_smile:

1 Like

This seems to work perfectly! Could you take the time to explain the code a little?

See posts #17 and #18.

Is it possible to centre the images with this? :thinking:

Yea I was thinking that It would prob be best to create a square crop of the image on time of upload however I have no knowledge of server side scripting that would create this, thus why I resulted in “trying” to find a front end solution.

Looking at the object-fit property you just sent me got me doing a little googling for an IE fit and I came across this not sure how well this would work but looks handy.

@PaulOB posted one. The only issue I see with that is if the subject is central in a wide image.

Im starting to think that the best way would be server side scripting that takes a square crop at the time of uploading that is x2 in size that is then scaled down either as a background-image or IMG that is restricted in its height/width.

I have decided I’m going to scrap the float from the layout above and build the layout using display:table with two columns for the nav and content. Im guesing this is more of a modern way to layout a site these days?

That is another option, to physically crop the images, either manually yourself (if possible) or via scripting. Though that was not what I was referring to by server-side scripting. I was thinking of querying the image size to get the aspect, then applying a class accordingly, basically what you attempted with js, but server-side.

yes you can use large negative numbers on the positions and then use margin:auto which automatically centres horizontally and vertically for absolute elements.

.object-fit img {
	position:absolute;
	top:-99em;
	left:-99em;
	right:-99em;
	bottom:-99em;
	margin:auto;
	width:auto;
	height:auto;
	min-width:100%;
	min-height:100%;
}

Results do again tend to be based on the original size used and letterbox or very narrow images will always be awkward to make fit automatically. Even cropping in a paint package requires the author to make a choice as where to crop so this can’t be automated even with js as js won’t know what the focal part of the image is.

2 Likes

Yes but that’s exactly the same as you get for background-size:cover or object-fit.

That makes no difference to the method I have showed because you won’t know where to crop. If your images is a letterbox style image with blue sky covering most of the image your script won’t know that it has to crop the focal part of the image which may be in the right hand corner. Therefore I see no benefit in a script than the css methods already shown.

1 Like

I will be happy to explain the code that I posted that uses the background image and the background-size property, but I will take the lazy cat approach for now and wait until you’ve had a chance to evaluate the foreground image choice using the object-fit property and decided which method you prefer to use. Personally, I feet that a background image is appropriate or I would not have suggested it, but this is a great opportunity to learn about other choices so you can make a more informed decision.

2 Likes

Certainly some human intervention in art direction is ideal in deciding where to crop. But it may not be possible in some cases, like with user uploaded images.

With object-fit:cover the image is centred, with backgrounds you may specify the position.
In most cases I would probably prefer centring, though it may not be suitable in all cases. I’m thinking of a case where I have used object-fit, there the images are portraits, so the composition lends itself to the centre position. In that case I just let the IE/Edge users live with it not being square, but that did require an @supports query to enable/disable the fixed aspect, otherwise it squishes them.

4 Likes

The code addition I mentioned will cater for that and will ensure that the center part of the image is in the center of the element.:slight_smile:

There is a jquery polyfill for object fit also but I tend to avoid those types of things.

Yes, generally the centre of an image is the focus and the best bet at a decent picture.

4 Likes

safeasfcuk, are you still around and are you still interested in how the code works that I posted? If so, I’ll put up a couple of simple, tutorial-like examples.

Let me know.

Certainly sorry not been online but def be interested in learning more.

Just a reminder, as I mentioned earlier, this technique uses a background image. If you have decided that a foreground image is more appropriate for your page, then the techniques presented by SamA74 and PaulOB in earler posts would be the way to go.

This first example is practically a repeat of the example that I posted earlier in that it shows a blue dashed outline around all boxes. The outline can deleted, commented out, or moved to specific containers as desired.

The .postLeft image box is floated to the left. It’s parent container, .postTop, has been assigned {overflow:hidden} to contain the float. There are other float containment methods available, such as the fabled “clearifix”.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>template</title>
<!--
https://www.sitepoint.com/community/t/overcoming-floats-and-absolute/255162
safeasfcuk
"http://www.wns.com/Portals/0/Images/HeaderBanner/desktop/1933/85/India_HD.jpg"
-->
    <style type="text/css">
* {outline:1px dashed blue;} 

.outer {
    max-width:1040px;
    margin:0 auto;
}

html, p, a, h1, h2 {
    padding:0;
    margin:0;
}
h2 {
    color:#3c3b3f;
    font-family:CaviarDreams;
    font-size:1.7em;
    font-weight:500;
}
.post h2 {
    padding:.5em 1.5%;
}

.postTop {
    overflow:hidden;
}
.postLeft {
    float:left;
    width:30%;
    min-width:200px;
    max-width:300px;
    padding:1.5%;
}
.postBox {
    padding-top:100%;
    background-image:url("India_HD.jpg");
    background-size:cover;
    background-position:0 0;
}
.postRight {}
.postRight p {
    line-height:1.4;
    padding:.5em 2%;
}
.postBottom a {
    background:transparent url(_images/posted.png) no-repeat top left;
    text-decoration:none;
}
.readmore {
    float:right;
}
.postBottom {
    overflow:hidden;
    padding:1% 2%;
}

@media screen and (max-width:400px) {
    .postLeft {
        float:none;
        width:auto;
        max-width:none;
    }
    .postBox {
        padding-top:53%;
    }
}
    </style>
</head>
<body>

<div class="outer">

    <section class="post">
        <div class="postTop">
            <div class="postLeft">
                <div class="postBox"></div>
            </div>
            <div class="postRight">
                <h2>How to travel with no money</h2>
                <p>Nam dapibus nisl vitae elit fringilla rutrum. Aenean sollicitudin, erat a elementum rutrum, neque sem pretium metus, quis mollis nisl nunc et massa. Vestibulum sed metus in lorem tristique ullamcorper id vitae erat. Nulla mollis sapien sollicitudin lacinia lacinia. Vivamus facilisis dolor et massa placerat, at vestibulum nisl egestas. Quis mollis nisl nunc et massa. Vestibulum sed metus in lorem tristique ullamcorper id vitae erat. Nulla mollis sapien sollicitudin lacinia lacinia. Vivamus facilisis dolor et massa placerat, at vestibulum nisl egestas.</p>
            </div>
        </div>
        <div class="postBottom">
            <a href="#">Posted 20 Feb</a>
            <div class="readmore"><a href="#">Read More</a></div>
        </div>
    </section>

</div>

</body>
</html>

 

Example #2 demonstrates two methods of handling the small image.

The upper demo shows the image becoming wide at 450px (the media query width) and approximating the expected aspect ratio, then scaling down as the window becomes narrower.

The lower demo shows the image becoming wide at 450px at which time the height becomes fixed and the edges of the image become cropped to fit the narrowing image box. The code difference is found in the media query for .imageBox and .imageBox2.

The more significant gem in this last demo is the way the text is handled to add space between the text and the borders around the .postLeft box to the left. This technique applies the space to the left of the text even if the text continues past the .postLeft box and wraps against the left wall of .postTop. This is one of the few valid occasions where {position:relative} can be used to move an element. Note that margin-right is a required part of the technique.

p,h2 {
    position:relative;
    left:1.5%;
    margin-right:3%;
}

Working page (should be entitled “Choices”) :slight_smile:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
	<title>Resize background images</title>
<!--
https://www.sitepoint.com/community/t/overcoming-floats-and-absolute/255162
safeasfcuk
"http://www.wns.com/Portals/0/Images/HeaderBanner/desktop/1933/85/India_HD.jpg"
NOTES: .imageBox and .imageBox2 within the media query are the only code differences in these examples.
    There is no HTML <date> element.  You may be thinking of the new <time> element in HTML5.
    https://www.sitepoint.com/html5-time-element-guide/
-->
    <style>
html,p,a,h1,h2 {
    padding:0;
    margin:0;
}
body {
    background:#ededea;
    line-height:1.5em;
    font-size:100%;
    font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
    padding:30px;
}
.content {
    max-width:1040px;
    margin:0 auto 2em;
}

/*Standard posts */
.post {
    border:1px solid #d0cccc;
    background-color:#f2f2f2; 
}
.postTop {
    overflow:hidden;
}
.postLeft {
    float:left;
    width:35%;
    min-width:200px;
    max-width:300px;
    border-right:1px solid #d0cccc;
    background-color:#f2f2f2;
    position:relative;
    z-index:1;
    padding:1.5%;
}

[class^="imageBox"] {
    padding-top:100%;
    background-image:url("India_HD.jpg");
    background-size:cover;
    background-position:0 50%;
}

h2 {
    color:#3c3b3f;
    font-family:CaviarDreams;
    font-size:1.75em;
    font-weight:500;
}
.post h2 {
    border-bottom:1px solid #d0cccc;  /* */
    padding:2% 0;
    margin-right:1.25%;
}

.postRight p {
    padding:.5em 0;
}
.postBottom {
    border-top:1px solid #d0cccc;  /* */
    overflow:hidden;
    padding:.5em 4% .75em;
}
.readmore {
    float:right;
}
p,h2 {
    position:relative;
    left:1.5%;
    margin-right:3%;
}
@media screen and (max-width:450px) {
    .postLeft {
        float:none;
        width:auto;
        max-width:none;
    }
    .imageBox {
        padding-top:55%;  /* estimated */
    }
    .imageBox2 {
        background-position:35% 50%;
        padding-top:200px;
    }
}

    </style>
</head>
<body>

<div class="content">
    <section class="post">
        <div class="postTop">
            <div class="postLeft">
                <div class="imageBox"></div>
            </div>
            <div class="postRight">
                 <div>
                     <h2>Title</h2>
                </div>
                <p>Image scales down and keeps its aspect ratio at widths below 450px.</p>
            </div>
        </div>
        <div class="postBottom">
            <a href="#">Posted 20 Feb</a>
            <div class="readmore"><a href="#">Read More</a></div>
        </div>
    </section>
</div>

<div class="content">
    <section class="post">
        <div class="postTop">
            <div class="postLeft">
                <div class="imageBox2"></div>
            </div>
            <div class="postRight">
                 <div>
                     <h2>Title</h2>
                </div>
                <p>Below 450px width, height of image becomes fixed, image box narrows at narrow widths and image edges are cropped; building is approx centered.</p>
            </div>
        </div>
        <div class="postBottom">
            <a href="#">Posted 20 Feb</a>
            <div class="readmore"><a href="#">Read More</a></div>
        </div>
    </section>
</div>

</body>
</html>

If you have questions or see a mistake (yes, it happens), please let me know.

Cheers

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.