Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8543927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:25:38+00:00 2026-06-11T12:25:38+00:00

What I need to do is add one of three CSS styles to every

  • 0

What I need to do is add one of three CSS styles to every instance of .project_content in my HTML, and the style needs to be picked at random for each instance.

The three styles are as follows:
float:left
float:right OR
margin:auto + width:x (where the width of .project_content is calculated and x is the calculated width)

Right now I have this:

<script type="text/javascript">
$(document).ready(function(){
    var floats         = ["right","left"];                
  var rand = Math.floor(Math.random()*floats.length);           
  $('.project_content').css("float", floats[rand]);
});​​​​​​​​​​​​​
</script>

All this script is doing for me is applying float:left or float:right to ALL instances of .project_content instead of choosing right or left for each instance.

How do I get my script to pick a random variable for each instance of project_content and how can I add the margin:auto + calculated width into the mix? Thanks so much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:25:39+00:00Added an answer on June 11, 2026 at 12:25 pm

    You need to get a random style for each element in the set, instead of a single random value for the entire set. That can be done using .css( propertyName, function(index, value) ), since the function gets called for each matched element:

    $('.project_content').css("float", function () {
      var rand = Math.floor(Math.random() * floats.length);
      return floats[rand];  
    })​;
    

    ​
    DEMO.

    To add margin and width into the mix, you can no longer use css() (since you need to modify one of multiple properties), but you can do it using .each():

    var styles = [ "left", "right", "margin" ];
    $('.project_content').each(function () {
      var randomStyle = styles[Math.floor(Math.random() * floats.length)];
      if (randomStyle == "left" || randomStyle == "right") {
        $(this).css('float', randomStyle);
      } else { // margin
        $(this).css('margin', 'auto')
               .css('width', your_calculated_width);
      }
    })​;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add CSS classes to each item output by an XSLT template.
I have 4 jQuery autocomplete textboxes. I need to add some style to one
For an assignment, I need to add one objects linked list to the back
i have an xml file sitemap.xml as shown below ..i need to add one
I need to add checked items from one checklistbox to another. I should not
I need to add amounts if a customer buys more than one product. how
In One of my application i need to add some image in video. so
For one of my software, I might need to have to add some objects
I've got a tr:table that I need to style using CSS. All the normal
I am using Google Chrome's developer tools to inspect CSS styles. Sometimes I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.