How to do media command mathematically?

I want to reduce the margin from 35 to 15 as the screen size reduces from max to 600px

I would like margin number to be mathematically calculated as it would be cumbersome to keep adding media commands.  Is it possible to achieve it, how?

#DevMenu li
{

    padding: 0px 35px 0px 10px;
 
}

@media screen and (max-width:800px)
{
#DevMenu li
{
    padding: 0px 20px 0px 10px;
    }
}
@media screen and (max-width:600px)
{
#DevMenu li
{
    padding: 0px 10px 0px 10px;
    }
}
goodkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Your question mentions margin - but the sample code is padding?

CSS does not know about browser widths or dimensions it merely gives instructions on how to work with those
dimensions.

You can do this with javascript however what is your function you want for diminishing the margin.

For instance - what is a 700px width supposed to have as a margin.

At what point does it get to be 35px? - I am going to assume 1024 and you can amend from there

<!doctype html>
<html>
<head>
<title>Test</title>
<style type="text/css">
.something {
  margin: 35px;
}
</style>
</head>
<body>
<div class="something-container">
  Container top
  <div class="something">
    variable margine here
  </div>
  Container bottom
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  setMargin();
  $(window).on('resize', setMargin);
});
function setMargin()
{
  // working in the range 600 to 1024
  var width = Math.min(1024, Math.max(600, $(window).width()));
  value = 15 + ((width-600)/424) * 20;
  
  $('.something').css({margin: value + 'px'});
}
</script>

</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
goodkAuthor Commented:
thank you.

Could we do it without using jquery?
0
Julian HansenCommented:
Could we do it without using jquery?

You mean with just CSS (no)
You mean with plain javascript (yes)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.