What is the size in pixels of 1 space - html

I am creating a cipher converter, where I am having trouble adiconar the figures because of the size of the spaces:
Ex: if I give 10 spaces with the keyboard, how many px or Pt have ??
By default my css looks like this:
display: block;
p - block user agent stylesheet
font-family: arial;
.word - arial
font-size: 13.3333330154419px;
.word - 10pt
height: 15px;
margin-bottom: 0px;
* - 0px
margin-left: 0px;
* - 0px
margin-right: 0px;
* - 0px
margin-top: 0px;
* - 0px
padding-bottom: 0px;
* - 0px
padding-left: 0px;
* - 0px
padding-right: 0px;
* - 0px
padding-top: 0px;
* - 0px
text-transform: uppercase;
.word - uppercase
width: 1663px;
rendered Fonts
Arial-27 glyphs
In the tests we do here, font Arial 10pt, is giving 4px each space ... is it?
function getTextWidth(text, font) {
// if given, use cached canvas for better performance
// else, create new canvas
var canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
var context = canvas.getContext("2d");
context.font = font;
var metrics = context.measureText(text);
return metrics.width;
};
/console.log(getTextWidth(' ', "normal 10pt arial"));

The width of a space, like any character, depends on the font, and it is generally not an integral number of pixels (even though rendering ultimately gets rasterized to pixels or subpixels). For example, for the Arial font, on Windows 7, the width of a space is 569 units, where “unit” is 1/2048 of the font size, to the width of a space is about 0.278 times the font size, em. For a font size of 13.3px, this gives about 3.7px. As such, it gets rounded to 4px in rasterization, but browsers and other programs may treat a sequence of spaces in different ways (and in HTML, the collapse of white space has its role, when applicable).
I have no idea of how this relates to cipher converters, whatever they are. You will probably get more useful answers by describing your original problem, rather than a theoretical question about the width of space.

The only way to know is to render the text and measure it.
Something like:
HTML:
<span id="text-to-measure">WWWWWWWWWW</span>
<div id="answer"></div>
JS:
var textNode = document.getElementById('text-to-measure');
var width = textNode.offsetWidth
document.getElementById('answer').innerText = width
http://jsfiddle.net/uxtm5yb3/
But if you are trying to figure out the size for layout purposes there may be a better way.

Its is not possible to tell you exactly , it always depend on the font's metric and way it renders.
See this example

Related

css responsive fluid html font size based on rem

I want to implement the example from css-tricks based on rems. This is what I got so far. However, the scaling does not work as intended, and the font size increases only tiny amounts. What is the mistake?
html {
font-size: 1rem;
}
#media screen and (min-width: 320px) {
html {
font-size: calc(1rem + 2 * ((100vw - 20rem) / 680));
}
}
#media screen and (min-width: 1000px) {
html {
font-size: 3rem;
}
}
I assume that 2 * ((100vw - 20rem) / 680) returns a px value. If that's true. How can I change it to return rem instead?
Edited to add some clarifications:
I want to use rem instead of px because this allows the user to overwrite the default font size in the browser.
The term 2 * ((100vw - 20rem) / 680) is between 0 and 2 (1 rem equals 16px on normal font size). This is what I want to achieve. I want to have font-size: 1rem + [0, 2]rem between 320 and 1000px viewport width. A linearly increasing rem function based on the viewport width.
Here is a link to a sandbox example.
Edit 2:
I think what I want to achieve is not possible. If the user increases the default font size by 50%, I want the scaling factor also increase by 50%: font-size: 1rem + [0, 2 * 1.5]rem.
The current problem is that the part 2 * ((100vw - 20rem) / 680)) needs to be rem based. This is not possible because there is no way in CSS to strip the unit. If I could strip the unit, I could do this: 2rem * strip-unit((100vw - 20rem) / 680))
If you want a responsive font size then you can use View Width too, no need to use calc or rem for that.
Just change this so you can try it out:
HTML {
font-size: 5vw;
}
View width goes from 0 to 100 so you know how much room you have to work with.
edit: I personally haven't found out how to scale on both axis yet but just the X-axis works good enough in most cases.
Here is a solution for fluid fonts I got from Creating a Fluid Type Scale with CSS Clamp
After some tweaking I ended up with the following Sass code to generate the clamp function.
/*
From https://www.aleksandrhovhannisyan.com/blog/fluid-type-scale-with-css-clamp/
Generates the css clamp function.
Focused on font-size, but may be used for margins and padding
Usage:
clamped(min-size-px, max-size-px, min-browser-width-px, max-browser-width-px)
font-size: clamped(26px, 36px, 600px, 1200px);
font-size: clamped(26px, 36px); //using width default values
Output:
font-size: clamp(1.63rem, 1.11vw + 1.28rem, 2.25rem);
*/
#use "sass:math";
#use "sass:map";
// Default min-mix browser width values for clamped function
$default-min-bp: 500px;
$default-max-bp: 1400px;
// Convert pixels to rems.
#function to-rems($px) {
$rems: math.div($px, 16px) * 1rem;
#return $rems;
}
//round a to number of decimal places.
#function rnd($number, $places: 0) {
$n: 1;
#if $places > 0 {
#for $i from 1 through $places {
$n: $n * 10;
}
}
#return math.div(math.round($number * $n), $n);
}
// Generate css for clamp
#function clamped($min-px, $max-px, $min-bp: $default-min-bp, $max-bp: $default-max-bp) {
$slope: math.div($max-px - $min-px, $max-bp - $min-bp);
$slope-vw: rnd($slope * 100, 2);
$intercept-rems: rnd(to-rems($min-px - $slope * $min-bp), 2);
$min-rems: rnd(to-rems($min-px), 2);
$max-rems: rnd(to-rems($max-px), 2);
#return clamp(#{$min-rems}, #{$slope-vw}vw + #{$intercept-rems}, #{$max-rems});
}

CSS - Position elements on the same place despite of device's screen size

I am developing an Ionic App and it needs to have some buttons (one above another, not inline) on the bottom-left place of the device's screen
I have the following CSS:
.button {
left = "1em";
z-index = "13";
overflow = "scroll";
position = "absolute";
width = "3em";
height = "2.5em";
textAlign = "center";
}
and then I calculate its bottom like this:
let bottom: number = 0;
this.floors.forEach(floor => {
let floorButton: HTMLElement = document.createElement("button");
floorButton.setAttribute("class", "button");
floorButton.appendChild(document.createTextNode(floor.level));
floorButton.style.bottom = bottom + "em";
bottom = bottom + 5;
});
Now my problem is simple: in a device with a bigger screen than another device, it is positioning in an upper position.
I can workarround this by calculating the height of the device's screen and dividing it x times until I get to the position I want. But this looks dirty to me (I don't know if it's the right wait tho, maybe it is).
So my question is, is there a simpler way that doing this as the one I put above instead of having to calculate the screen's height size in pixels? Can it be done directly by CSS? I've checked #media but it looks like it won't help at all. Or maybe I'm just doing it right and I'm overthinking it too much?
Thanks!
You can just use CSS for this:
.button {
display: block;
margin-top: 5px;
}
In this way doesn't matter what is the width of screen, always your buttons will be in separate line.

Efficiently assign a color to each character in HTML

I'm trying to visualize the results of some computational analysis over a large piece of text. The analysis gives me a score [0-100) for each character. I've defined some classes:
.c0 { background-color:rgb(6,50,99); }
.c1 { background-color:rgb(7,52,102); }
.c2 { background-color:rgb(8,54,105); }
.c3 { background-color:rgb(10,58,111); }
and the bulk of my document looks like this:
<span class='c76'>i</span><span class='c75'>c</span><span class='c76'>k</span><span class='c73'>l</span><span class='c70'>y</span><span class='c72'>,</span><span class='c76'> </span><span class='c76'>a</span><span class='c78'>n</span><span class='c78'>d</span><span class='c76'> </span><span class='c76'>b</span><span class='c76'>e</span><span class='c76'>
This works well visually (see blurred screenshot as an example):
but it is extremely slow to view. For large documents this drags the computer down to a standstill. Is there a more efficient way of rendering this information to a browser, perhaps one that uses JavaScript?
I would definitely consider using the HTML 5 canvas. It renders separately from the DOM, usually with some form of acceleration. You should find no slowdown when viewing, though it may still take a second or so for the initial render.
You can do something like this to draw a character with a background:
ctx.font = font;
ctx.textBaseline = 'top';
ctx.fillStyle = colour;
var width = ctx.measureText(txt).width;
ctx.fillRect(x-1, y, width+1, parseInt(font, 10));
ctx.fillStyle = '#000';
ctx.fillText(txt, x, y);
Here's a jsfiddle with a rough example to demonstrate: http://jsfiddle.net/q2cwxnbp/3/
The drawback to this method is that the text cannot be selected / copied, because it's rendered graphically.

Foundation 6.2 Large Gutter Size

According to Foundation 6.2 documentation default grid gutter size for small is 20px and for medium is 30px, but did not mention for large. What is the default gutter size for large?
Looking at _grid.scss on the Foundation GitHub page:
/// The amount of space between columns at different screen sizes. To use just one size, set the variable to a number instead of a map.
/// #type Map | Length
/// #since 6.1.0
$grid-column-gutter: (
small: 20px,
medium: 30px,
) !default;
and _column.scss:
// Gutters
#if type-of($gutter) == 'map' {
#each $breakpoint, $value in $gutter {
$padding: rem-calc($value) / 2;
#include breakpoint($breakpoint) {
padding-left: $padding;
padding-right: $padding;
}
}
}
There is no large gutter defined as the docs say. So in this case large gutters would be the same as medium since Foundation uses mobile first.
You could add a custom large gutter by doing something like this:
$grid-column-gutter: (
small: 20px,
medium: 30px,
large: 50px
) !default;

How to set min-font-size in CSS

I want to set a minimum font size to every element in my HTML page.
For example if there are elements with font-size less then 12px, then they will change to 12px.
But if there are elements with font-size grater then 12px, they will not change.
Is there any way to do it with CSS?
In CSS3 there is a simple but brilliant hack for that:
font-size:calc(12px + 1.5vw);
This is because the static part of calc() defines the minimum. Even though the dynamic part might shrink to something near 0.
As of mid-December 2019, the CSS4 min/max-function is exactly what you want:
(tread with care, this is very new, older browsers (aka IE & msEdge) don't support it just yet)
(supported as of Chromium 79 & Firefox v75)
https://developer.mozilla.org/en-US/docs/Web/CSS/min
https://developer.mozilla.org/en-US/docs/Web/CSS/max
Example:
blockquote {
font-size: max(1em, 12px);
}
That way the font-size will be 1em (if 1em > 12px), but at least 12px.
Unfortunatly this awesome CSS3 feature isn't supported by any browsers yet, but I hope this will change soon!
Edit:
This used to be part of CSS3, but was then re-scheduled for CSS4.
As per December 11th 2019, support arrived in Chrome/Chromium 79 (including on Android, and in Android WebView), and as such also in Microsoft Chredge aka Anaheim including Opera 66 and Safari 11.1 (incl. iOS)
CSS has a clamp() function that holds the value between the upper and lower bound.
The clamp() function enables the selection of the middle value in the range of values between the defined minimum and maximum values.
It simply takes three dimensions:
Minimum value.
List item
Preferred value Maximum allowed value.
try with the code below, and check the window resize, which will change the font size you see in the console. i set maximum value 150px and minimum value 100px.
$(window).resize(function(){
console.log($('#element').css('font-size'));
});
console.log($('#element').css('font-size'));
h1{
font-size: 10vw; /* Browsers that do not support "MIN () - MAX ()" and "Clamp ()" functions will take this value.*/
font-size: max(100px, min(10vw, 150px)); /* Browsers that do not support the "clamp ()" function will take this value. */
font-size: clamp(100px, 10vw, 150px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<center>
<h1 id="element">THIS IS TEXT</h1>
</center>
Looks like I'm a bit late but for others with this issue try this code
p { font-size: 3vmax; }
use whatever tag you prefer and size you prefer (replace the 3)
p { font-size: 3vmin; }
is used for a max size.
Use a media query. Example:
This is something im using the original size is 1.0vw but when it hits 1000 the letter gets too small so I scale it up
#media(max-width:600px){
body,input,textarea{
font-size:2.0vw !important;
}
}
This site I m working on is not responsive for >500px but you might need more. The pro,benefit for this solution is you keep font size scaling without having super mini letters and you can keep it js free.
.class {
font-size: clamp(minimum-size, prefered-size, maximum-size)
}
using this you could set it up so prefered and max values are 5vw but the minimum is 15px or something so it won't go over 5vw but if 5vw < 15px it will stick to 15px
CSS Solution:
.h2{
font-size: 2vw
}
#media (min-width: 700px) {
.h2{
/* Minimum font size */
font-size: 14px
}
}
#media (max-width: 1200px) {
.h2{
/* Maximum font size */
font-size: 24px
}
}
Just in case if some need scss mixin:
///
/// Viewport sized typography with minimum and maximum values
///
/// #author Eduardo Boucas (#eduardoboucas)
///
/// #param {Number} $responsive - Viewport-based size
/// #param {Number} $min - Minimum font size (px)
/// #param {Number} $max - Maximum font size (px)
/// (optional)
/// #param {Number} $fallback - Fallback for viewport-
/// based units (optional)
///
/// #example scss - 5vw font size (with 50px fallback),
/// minumum of 35px and maximum of 150px
/// #include responsive-font(5vw, 35px, 150px, 50px);
///
#mixin responsive-font($responsive, $min, $max: false, $fallback: false) {
$responsive-unitless: $responsive / ($responsive - $responsive + 1);
$dimension: if(unit($responsive) == 'vh', 'height', 'width');
$min-breakpoint: $min / $responsive-unitless * 100;
#media (max-#{$dimension}: #{$min-breakpoint}) {
font-size: $min;
}
#if $max {
$max-breakpoint: $max / $responsive-unitless * 100;
#media (min-#{$dimension}: #{$max-breakpoint}) {
font-size: $max;
}
}
#if $fallback {
font-size: $fallback;
}
font-size: $responsive;
}
A recent feature added to CSS is min() which chooses the minimum value of its parameters.
So in the following code, when the viewport is big (>1500px in this case), the font-size is capped to 30px.
However for any viewport smaller than 1500px, the other value is used: e.g. when viewport is 1000px, then the font-size will be 20px. This means it can be quite responsive.
p {
font-size: min(2vw, 30px);
}
Here's a Codepen with min() for container width and p text:
https://codepen.io/code0312/pen/dyWJLmB
But note, if you later manually set a font size greater than what you put here, that will still override this declaration.
No. While you can set a base font size on body using the font-size property, anything after that that specifies a smaller size will override the base rule for that element. In order to do what you are looking to do you will need to use Javascript.
You could iterate through the elements on the page and change the smaller fonts using something like this:
$("*").each( function () {
var $this = $(this);
if (parseInt($this.css("fontSize")) < 12) {
$this.css({ "font-size": "12px" });
}
});
Here is a Fiddle where you can see it done: http://jsfiddle.net/mifi79/LfdL8/2/
AFAIK it's not possible with plain CSS,
but you can do a pretty expensive jQuery operation like:
jsBin demo
$('*').css('fontSize', function(i, fs){
if(parseInt(fs, 10) < 12 ) return this.style.fontSize = "12px";
});
Instead of using the Global Selector * I'd suggest you (if possible) to be more specific with your selectors.
Judging by your above comment, you're OK doing this with jQuery — here goes:
// for every element in the body tag
$("*", "body").each(function() {
// parse out its computed font size, and see if it is less than 12
if ( parseInt($(this).css("font-size"), 10) < 12 )
// if so, then manually give it a CSS property of 12px
$(this).css("font-size", "12px")
});
A cleaner way to do this might be to have a "min-font" class in your CSS that sets font-size: 12px, and just add the class instead:
$("*", "body").each(function() {
if ( parseInt($(this).css("font-size"), 10) < 12 )
$(this).addClass("min-font")
});
The font-min-size and font-max-size CSS properties were removed from the CSS Fonts Module Level 4 specification (and never implemented in browsers AFAIK). And the CSS Working Group replaced the CSS examples with font-size: clamp(...) which doesn't have the greatest browser support yet so we'll have to wait for browsers to support it. See example in https://developer.mozilla.org/en-US/docs/Web/CSS/clamp#Examples.
It will work perfectly with 50px. Which will act as a static and thus as min-width.
font-size: calc(50px + 5vw);