0

I'm looking for a way to set different width and height separately based on viewport width and height. Is it possible in css or js?

7
  • 1
    It is certainly possible in js, but it depends on what you need exactly to determine if it can be done in CSS. Can you be more specific about your problem? Please show us code samples. Commented Dec 20, 2014 at 16:50
  • I'm trying to achieve a text which will stay in the same proportions to div in which the text is. Not working example here: jsfiddle.net/s3wabLkL/1 Commented Dec 20, 2014 at 16:52
  • 1
    Have a look at stackoverflow.com/questions/16056591/… Commented Dec 20, 2014 at 16:53
  • Looks like the link that @Musa supplied could be the answer for you. Commented Dec 20, 2014 at 16:55
  • @Musa font-size by viewport units will not keep the proportions. It's scaling only by width or only by height. I've been looking for solution for 4 hours. Commented Dec 20, 2014 at 16:56

1 Answer 1

1

For a JS solution, you can use the FlowType.js library.

It will allow you to quickly and easily control the text elements on your page relative to the size of their containing div.

Your example could be setup like this:

$(document).ready(function(){

    // customize the options you pass into the
    // flowtype function in order to get the effects you want
    $('.text_block').flowtype({
     minimum   : 500,
     maximum   : 1200,
     minFont   : 12,
     maxFont   : 40,
     fontRatio : 30
    });
    
});

This example uses jQuery to ensure that all assignments via flowtype are only assigned after the page has loaded.

To activate flowtype, you just need to put this call somewhere before your closing <body> tag:

<script> $('.text_block').flowtype(); </script>

See the flowtype.js website for more information on how to install flowtype, and how to configure it.

Here is a jsfiddle showing your example with the flowtype library assignments.

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

You can customize use the flowtype configurations to get the effect you are looking for.
Glad to hear it! Happy coding! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.