Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Randomly placing divs/images with CSS

Featured Replies

http://edgar.ldn.kualo.net/~lmpmeuk/web/busybees/products-and-prices/

 

On the above page I have created some polaroid-style photos and even added a rotational effect in CSS to tilt them.

Im having trouble getting to grips with placing these where I want on the page.

 

Is there a way I can randomly position them in CSS but that can still be scalable in future?

Alternatively, how would you position them (don't worry about the scalable/responsive side for now)?

 

 

I have tried each of the following classes but cannot get them to look right:

 

 

.right {
    margin: 20px 20px 0 0;
    width: 180px; /*was 160px*/
    padding: 15px 20px 20px;
    float: right;
    color: #333;    
}

 

 

.right-indent1 {
        ;
}
 

Ideally I want to make them a certain amount of pixels off the paragraph text but not all in a vertical line - I want to add character by having 1 image maybe 20px right, another underneath and 15px right etc to give a random placed look.

 

What would be the best way to achieve this?

 

  • Author

.right-indent1 {

Margin-left:100px;

}

 

This would be in another div which is floated right.

There's no totally way of generating random numbers in CSS. you should use Javascript to loop through the images then generate a random number for each and set a CSS margin or positional attribute using those numbers.

In theory you could use php to generate the offset for each image and apply it to css properties then save the data directly into the css file overwriting the previous version before the meta data tags that load the style sheets. Or simply apply the style to each image using the php variables within the output page. Either way you could have the layout change for each time the page was loaded as long as you tell the browser not to cache that style sheet.

 

I say theory because I have not tested it, but it should work. Others will be able to tell you if that can be done using JS as well.

Edited by Weedy101

  • Author

Thanks for your help.

 

I don't know if I explained It correctly or if maybe it is quite difficult to do what I want to do.

 

I'm just trying to place an image/div to the right of the paragraph text. More than one image though.

 

I did want some indented more than others so it didn't look like a vertical line of images (if that's makes sense?) but even if each image was in the same position vertically that is fine.

 

I think maybe I am getting some divs wrong - maybe a container with 2 divs alongside (no collapse) - what code though?

 

Any ideas or are the answers correct for what I want to do - Im just confused by them.

 

I hope someone can explain this for me.

 

Thanks in advance.

Edited by ~Luke

Based on what I think you mean, here's a real rough example (http://jsfiddle.net/sgp79).

 

I would avoid using absolute positioning if you can. It's much easier to work with margins and padding on a responsive site, especially when you get to smaller viewpoints. In this case, you would probably see the polaroid images going over the text ect, and it's not easy to control.

 

You also need to consider browser support. I've used CSS transforms and box-sizing. Box-sizing will work in IE8+ and the transforms will degrade to simply having no rotation when it's not supported.

 

In terms of generating numbers for each image, if you use a CMS, each entry/post should have its own ID. You can use this to output a class like so <div class="polaroid img-<?php the_ID(); ?>"></div>. This example should work in Wordpress, but I don't use it so I'm not 100% on that. Hopefully, you get the idea though.

  • Author

Hi Jack, that is exactly what I am after.

 

However, I have edited my markup and slightly altered my CSS to match what is there in that example (as it is exactly that that I want) but the polaroid still doesn't sit to the right of the paragraph text.

 

Any idea's?

 

My markup for each paragraph and polaroid is:

 

 

 

            <section class="column-content">
                <div class="column twelve">
                        <h2>Products & Prices</h2>
                        <h3>Here is a list of some of my products and prices:</h3>
<p>Cupcakes with frosting – £1.30 each (£1.50 with individual handmade toppers)
Giant Cupcake with frosting – £40
Plain Iced Loaf Cakes – £6
Plain Iced Mini Cakes (4.5 Inch Double Layer) – £10
8 inch Round Frosted Cake – £16 Single Layer or £20 Double Layer
10 inch Round Frosted Cake – £18 Single Layer or £22 Double Layer
12 inch Round Frosted Cake – £20 Single Layer or £24 Double Layer
10 inch Iced Bundt Cake – £18
12 inch Iced Bundt Cake – £20</p>
<h3>Cake Flavours:</h3>
<p>Vanilla, Vanilla and Raspberry, Chocolate, Chocolate Orange, Chocolate and Peanut Butter, Chocolate Chip, Mint Chocolate, Mocha, Carrot, Lemon, Neapolitan, Gingerbread, Tiramisu, Black Forest, Banana Split
Also available – Novelty cakes, Decorated cakes and Wedding cakes (quoted individually as each cake is unique)</p>
                
                </div><!--end column twelve-->
                
                <div class="polaroid l-tilt">
                            <img src="../img/products/cake.jpg" alt="">
                            <p class="caption">Caption text here</p>
                </div>


            </section>
            
            <section id="column-content">
                <div class="column twelve">




<p>Mini Pies – £1.30 each
Small Pies (5 inches) – £6 each
Large Pies (10 inches) – £12 each
</p>
    
<h3>Pie Flavours:</h3>
<p>
Lemon Cream and Raspberry Tart, Apple Pie, Cherry Pie, Cherry Bakewell Tart, Rhubarb and Custard Crumble
 Pie, Plum Crumble Pie, Peanut Butter Chocolate Cups, Chocolate and Raspberry Tart, Chocolate Pudding Pie, Pecan Pie, Banoffee Pie.</p>


                 </div><!--end column twelve-->
                
                <div class="polaroid l-tilt">
                            <img src="../img/products/cake.jpg" alt="">
                            <p class="caption">Caption text here</p>
                </div>


            </section> 
 

 

 

With CSS:

 

 

 

.twelve {
    vertical-align: top;
    width: 660px; /*660px*/
    padding: 0 20px 0;
}


.column {
    float: left;
    margin-right: 20px;
}




.polaroid {
    float: right;
    width: 150px;
    padding: 10px 10px 20px 10px;
    border: 1px solid #BFBFBF;
    background-color: white;
    -webkit-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    -moz-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
}


.twelve, .polaroid {
    display: inline-block;
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    box-sizing: border-box;
}




.column-content {
    max-width: 700px;
    
}
 

 

 

  • Author

Ah, it seems that changing the

.column-content {
    max-width: 700px;
    
}

to

.column-content {
    max-width: 900px;
    
}

 

Made everything better :unknw:

 

I can easily alter and forget widths - this can't be good practise. Hmm.

That's probably down to a mixture of incorrect widths and additional padding. If you calculate everything correctly, it should work.

  • Author

Hi Jack, that is exactly what I am after.

 

However, I have edited my markup and slightly altered my CSS to match what is there in that example (as it is exactly that that I want) but the polaroid still doesn't sit to the right of the paragraph text.

 

Any idea's?

 

My markup for each paragraph and polaroid is:

 

 

            <section class="column-content">
                <div class="column twelve">
                        <h2>Products & Prices</h2>
                        <h3>Here is a list of some of my products and prices:</h3>
<p>Cupcakes with frosting – £1.30 each (£1.50 with individual handmade toppers)
Giant Cupcake with frosting – £40
Plain Iced Loaf Cakes – £6
Plain Iced Mini Cakes (4.5 Inch Double Layer) – £10
8 inch Round Frosted Cake – £16 Single Layer or £20 Double Layer
10 inch Round Frosted Cake – £18 Single Layer or £22 Double Layer
12 inch Round Frosted Cake – £20 Single Layer or £24 Double Layer
10 inch Iced Bundt Cake – £18
12 inch Iced Bundt Cake – £20</p>
<h3>Cake Flavours:</h3>
<p>Vanilla, Vanilla and Raspberry, Chocolate, Chocolate Orange, Chocolate and Peanut Butter, Chocolate Chip, Mint Chocolate%2

If you update the live example, I can take a look.

 

Whenever I'm in this situation, I always put an outline or border on the elements to work out what's going wrong. If something is too wide, you will see it instantly.

 

You could do something like:

* {
outline: 1px solid blue;
}
  • Author

Thanks for the great tip. I have re-uploaded the page with the blue border to outline everything.

 

It is getting there.

 

I want the polaroid images to be slightly lowered so that they are inline with the red subheadings.

 

Is there an easy way to do this - What would be the correct way?

I've had to adjust and remove some margins. On your example, remove the float: right; on the polaroid.

 

You're going to have to do it per image, which isn't ideal if the website is CMS based. It may be better to loop through two different styles (odd, even), rather than make each one unique.

 

http://jsfiddle.net/sgp79/1/

  • Author

I've had to adjust and remove some margins. On your example, remove the float: right; on the polaroid.

 

You're going to have to do it per image, which isn't ideal if the website is CMS based. It may be better to loop through two different styles (odd, even), rather than make each one unique.

 

http://jsfiddle.net/sgp79/1/

 

Thanks for your help,

 

I removed the float element.

 

I did append (and duplicate) -one -two and -three to the polaroid classes so that I could add the margin element an add different numbers to vary the positioning however it stretched the polaroid an looked odd - presumably because this margin box is the caption border.

 

Any idea's?

  • Author

Thanks, I have re-uploaded.

 

Just by adding the margin element (as below):

.polaroid-one {
    margin: 20px 0 0 0;
    width: 150px;
    padding: 10px 10px 20px 10px;
    border: 1px solid #BFBFBF;
    background-color: white;
    -webkit-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    -moz-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
}


.polaroid-two {
    margin: 10px 0 0 0;
    width: 150px;
    padding: 10px 10px 20px 10px;
    border: 1px solid #BFBFBF;
    background-color: white;
    -webkit-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    -moz-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
}


.polaroid-three {
    margin: 20px 0 0 0;
    width: 150px;
    padding: 10px 10px 20px 10px;
    border: 1px solid #BFBFBF;
    background-color: white;
    -webkit-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    -moz-box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
    box-shadow: 2px 2px 3px rgba(135, 139, 144, 0.4);
}

 

The polaroid box goes crazy and ends up to the left of the screen.

 

PS The site isn't CMS driven. I wish I could theme for CMS but that will be for another day :)

Edited by ~Luke

  • Remove float: left; from .column

  • Add display:inline-block; to .twelve and .polaroid-one, .polaroid-two, .polaroid-three
  • Change the width on .column-content to width: 100%;
  • Author

That's brilliant - thank you. It is now displaying perfectly.

 

The only thing is on the other pages the <aside> sidebar I did was no longer floated to the right in line with the column on the left - so to fix this I just added a separate class of .float-left with the element float:left; and then loaded the .float-left with the .column on the other pages that needed it.

 

On the page we had been working on I took out the <aside> bar to have those images in place.

 

Im glad this has been sorted. Thank you so much.

Another way to do that would be to add a class on the container or body. Then you can toggle styles and position on different templates.

<body class="home">
<body class="gallery">

.home aside {
float: left;
}

.gallery aside {
float: right;
}

Glad you got it working anyway :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.