I'm trying to make a font in a div responsive to the browser window. So far, it has worked perfectly, but the parent div has a max-width
of 525px
. Resizing the browser further will not make the font stop resizing. This has made me wonder if there is such a thing as min-font-size
or max-font-size
, and if such a thing does not exist, if there is a way to achieve something similar.
I thought that using percentages at font-size
would work, but the bit of text won't scale accordingly to the parent div. Here's what I have:
The CSS for the parent div:
.textField{
background-color:rgba(88, 88, 88, 0.33);
width:40%;
height:450px;
min-width:200px;
max-width:525px;
z-index:2;
}
The CSS for the piece of text in question:
.subText{
position:relative;
top:-55px;
left:15px;
font-family:"news_gothic";
font-size:1.3vw;
font-size-adjust:auto;
width:90%;
color:white;
z-index:1;
}
I have searched for quite a while on the internet, but to no avail.
It works well with CSS.
I went through the same issues and fixed it as follow.
Use a fixed "px" size for maximum size at a specific width and above. Then for different smaller widths, use a relative "vw" as a percentage of the screen.
The result below is that it adjusts itself at screens below 960px but keep a fixed size above. Just a reminder, not to forget to add in the html doc in header:
Example in CSS:
I hope it'll help!
You can use Sass to control min and max font sizes. Here is a brilliant solution by Eduardo Boucas.
https://eduardoboucas.com/blog/2015/06/18/viewport-sized-typography-with-minimum-and-maximum-sizes.html
You can do it by using a formula and including the viewport width.
This sets the minimum font size at 7px and amplifies it by .5vw depending on the viewport width.
Good luck!
Rucksack is brilliant, but you don't necessarily have to resort to build tools like Gulp or Grunt etc.
I made a demo using CSS Custom Properties (CSS Variables) to easily control the min and max font sizes.
Like so:
I am coming a bit late here, I don't get that much credit for it, I am just doing a mix of the answers below because I was forced to do that for a project.
So to answer the question : There is no such thing as this CSS property. I don't know why, but I think it's because they are afraid of a misused of this property, but I don't find any use case where it can be a serious problem.
Whatever, what are the solutions ?
Two tools will allow us to do that : media queries ans vw property
1) There is a "fool" solution consisting in making a media query for every step we eant in our css, changing font from a fixed amount to another fixed amount. It works, but it is very boring to do, and you don't have a smooth linear aspect.
2) As AlmostPitt explained, there is a brillant solution for the minima :
Minimum here would be 7px in addition to 0.5% of the view width. That is already really cool and working in most of cases. It does not require any media query, you just have to spend some time finding the right parameters.
As you noticed it is a linear function, basic maths learn you that two points already find you the parameters. Then just fix the font-size in px you want for very large screens and for mobile version, then calculate if you want to do a scientific method. Thought, it is absolutely not necessary and you can just go by trying.
3) Let's suppose you have a very boring client (like me) who absolutely wants a title to be one line and no more. If you used AlmostPitt solution, then you are in trouble because your font will keep growing, and if you have a fixed width container (like bootstrap stoping at 1140px or something in large windows). Here I suggest you to use also a media query. In fact you can just find the amout of px size maximum you can handle in your container before the aspect become unwanted (pxMax). This will be your maximum. Then you just have to find the exact screen width you must stop (wMax). (I let you inverse a linear function on your own).
After that just do
Then it is perfectly linear and your font-size stop growing ! Notice that you don't need to put your previous css property (calc...) in a media query under wMax because media query are considered as more imnportant and it will overwrite the previous property.
I don't think it is useful to make a snippet for this, as you would have trouble to make it to whole screen and it is not rocket science afterall.
Hope this could help others, and don't forget to thank AlmostPitt for his solution.
No, there is no CSS property for minimum or maximum font size. Browsers often have a setting for minimum font size, but that’s under the control of the user, not an author.
You can use
@media
queries to make some CSS settings depending on things like screen or window width. In such settings, you can e.g. set the font size to a specific small value if the window is very narrow.