I'm looking for a smart way to control fonts sizes depending on browser resolution - as reusable ass possible.
So i have done the following:
<section>
<h1>Contact</h1>
</section>
And in LESS file i've defined:
.h1 {
font-size: 90px;
}
section h1 {
.h1;
}
So, as you see i have .h1 class defined and i can reuse it.
But later i saw that my idea is now working (media queries don't work:)
#media only screen and (max-width: 991px) {
.h1 {
font-size: 20px;
}
}
.h1 class has 90px always and media queries didn't change font size to 20px.
My question: is this possible to tweak to work ?
This way i can have small amount of reusable classes and change their size in just one place instead of many places.
Thanks.
PS: I've benn using REM's for long time but it's not perfect for me.
I use a fluid-typography mixin, as show in this guide. This page links to a codepen showing a less mixin.
Essentially you pass through the font size values you want to scale between and also the screen sizes that the scaling to occur between. Beneath is some code directly from the codepen link:
.fluid-type(#property, #min-vw, #max-vw, #min-size, #max-size) {
#{property}: #min-size;
#media screen and (min-width: #min-vw) {
#{property}: calc( #min-size ~" + " unit(#max-size - #min-size) ~" * ((100vw - " #min-vw ~") /" unit(#max-vw - #min-vw) ~")" );
}
#media screen and (min-width: #max-vw) {
#{property}: #max-size;
}
}
the way I use it:
h1{
.fluid-type(font-size, #mobileSize, #desktopSize, 36px, 72px);
}
credit to Mike
Related
So far, my rule of thumb is to use viewport units for simple webpages, because it gives the website a lot of flexibility; but when the webpage is complex, use media queries.
There are some intermediate cases where I use both, just because it is easier. The next link is an example I built: https://codepen.io/santimirandarp/pen/jjboKN the css file looks like this:
body{
background-color:lightblue;
text-align:center;}
main{
font-size:calc(10px + 0.5vw);
margin: auto;
width: 80vw;
}
#title{
color:magenta;
}
span{
color:hsl(110,100%,55%);
font-size:calc(20px + 3vw) ;
}
#description{}
p{font-size:calc(13px + 0.5vw) ;
text-align:justify;
line-height:calc(20px + 0.5vw);
margin-bottom:2vw;
}
#linend{
color:blue;
text-align:center;
font-family:Garamond;
font-size:1.5em;
background-color:yellow;
}
#media only screen and (min-width: 1000px) { #media only screen and (min-width: 1000px) {
main{width:45%;}
p {font-size:100%;
}
p strong{font-size:100%;
color:brown;}
}
Question
Is this approach correct? When to use viewport units vs media queries?
Can you explain the right approach?
As of MDN:
Media queries are useful when you want to modify your site or app depending on a device's general type (such as print vs. screen)
In comparison, the purpose of the viewport is to adapt on the screens dimensions, like width and height.
That in mind, there is no 'use viewport or media queries'. With media queries you can differentiate media types - not only their dimensions. For example, when you have media print, you might want to remove colors. Additionally (not alternatively) you can use viewport units to specify the dimensions.
Since you are using both media queries and viewport units, my answer would be: yes, your approach is right. Though, I don't think there is a strict right or wrong here.
I am creating a chart that shows keyboard commands overlaid on top of an image of a keyboard. I would like the text to shrink to the size of the container if it is too long to fit inside a key, but remain the default size otherwise. How would I accomplish this in HTML? The solutions I've seen seem to always alter the text size, versus only when the container is too small. Thanks.
Here's an example of what the result might look like:
You can use something like word-wrap or overflow-wrap or adding a variable with css(i.e: a font-size that fits your needs) and only change it when a media queries (that you established) returns true;
Media Queries explanation:
Let's say I established this variable
:root {
--font-size: 16px;
/* this is your regular font size that you will use in your entire document */
}
So in a given screen size like so:
#media screen and (device-width: 320px) and (orientation: portrait) {
:root {
--font-size: 12px;
}
}
You just change the value of the variable and it will change where ever you used that specific variable
If you don't know how this works you can use this css variables like this
.someClass {
font-size: var(--font-size); */ and that's it :) */
/* You can also make calculations like this */
font-size: calc(var(--font-size) - 40%);
}
Reference:
overflow-wrap: https://css-tricks.com/almanac/properties/o/overflow-wrap/
word-wrap: https://css-tricks.com/almanac/properties/w/word-break/
css variables: https://www.madebymike.com.au/writing/using-css-variables/
media queries: https://www.uxpin.com/studio/blog/media-queries-responsive-web-design/
I am trying to make the font on my app larger for small devices by overriding bootstrap media queries.
I am trying to do it like this, by setting the font size for h1's using bootstrap media queries
#media (min-width: #screen-sm-min) {
h1{font-size: 5rem}
}
only I get this Rails error message.
Invalid CSS after "...ia (min-width: ": expected expression (e.g. 1px, bold), was "#screen-sm-min) {"
I see in the below answer they are overriding by explicitly stating the width, which doesn't throw the error anymore for me, but I'm not sure why I can't use bootstraps variables. Do I have to set the value of #screen-sm-min somewhere in order to use it?
Rails 4 Bootstrap 3.3, media queries causing error
Looks like this number explains what was going on on the bootstrap migrating to bootstrap 4 website:
https://v4-alpha.getbootstrap.com/migration/
All #screen- variables have been removed in v4.0.0. Use the media-breakpoint-up(), media-breakpoint-down(), or media-breakpoint-only() Sass mixins or the $grid-breakpoints Sass map instead.
If you're using sass you can do something like this for small devices:
#include media-breakpoint-up(xs) {
strong {
font-size: 5rem;
}
}
But I ended up ultimately setting the values:
#media (min-width: 576px) {
strong {
font-size: 4rem;
}
}
The media query should say:
#media (min-width: $screen-sm-min) {
h1 {
font-size: 5rem;
}
}
variables in css start with a dollar sign. Assuming the variable is actually called screen-sm-min, you should be good. You also forgot the semi-colon after font-size: 5rem just FYI
I want to use two different background-image:url() values based on screen size. This is fairly simple to do if the urls are known ahead of time:
#media (max-width: 400px) {
.element {
background-image:url("/Images/1.png");
}
}
#media (min-width: 401px) {
.element {
background-image:url("/Images/2.png");
}
}
However, I do not know my image urls ahead of time - they are based on information from the user. I can set a single background-image:url() dynamically in the code behind like so:
string backgroundStyle = "background-image: url(\""+loginImagePath+"\"); ";
sBodyWrapper.Attributes.Add("style", backgroundStyle);
But I'm not sure how I might go about setting two different background images which alternatively show based on screen width. I could set the two background images on different elements and hide one of those elements, but I really would like this background image to be on a single body wrapping element.
Is there any way to set values inside of media queries? Can conditional css be applied on the element's style attribute?
You could add the conditional CSS via a style tag appended to the document:
var rules = '<style>
#media(max-width: 400px){
background-image:url("Images/1.png");
}
#media(min-width: 401px){
background-image:url("Images/2.png");
}
</style>';
maybe this question is too weird or even is off-topic, sorry for that but I have this doubt:
Exists a way to show a HTML page in the same size for all screens when the user types control + p (print page)?
For example my laptop have this screen resolution: 1600x900 and when I type control + p the HTML page look perfect (all in 1 page)!
Problem appears when I use a bigger(page appear in 2 or more pages) or smaller screen(page appear in 1 page but with a lot of blank spaces).
At the moment I tried with #page margins but not work at all, just in some cases, however here is the code:
<style>
#page :left {
}
#page :right {
margin-top: 0.2cm;
margin-bottom: 0.2cm;
}
body {
font-size: 9.5px;
}
</style>
PD. I'm using JavaScript, jQuery, HTML5, CSS3, Bootstrap.
Control + P means print, right?
Then yes, you should use the media query
#media print{
}
This a good explanation: https://www.smashingmagazine.com/2011/11/how-to-set-up-a-print-style-sheet/