How do I implement responsive typography with Bootstrap 4? - responsive-design

I'm building a responsive web app with Bootstrap 4. I want the font size of all text to be reduced on mobile devices compared to desktop, so I added the following to my base css file as per the Bootstrap documentation (https://getbootstrap.com/docs/4.0/content/typography/):
html {
font-size: 1rem;
}
#include media-breakpoint-up(sm) {
html {
font-size: 1.2rem;
}
}
#include media-breakpoint-up(md) {
html {
font-size: 1.4rem;
}
}
#include media-breakpoint-up(lg) {
html {
font-size: 1.6rem;
}
}
However the font size remains fixed. What am I doing wrong?

As of Bootstrap 4.3.1, there is now RFS (Responsive Font Sizing)! However, as explained in the docs, you must enable it using the $enable-responsive-font-sizes SASS variable.
RFS Demo: https://www.codeply.com/go/jr8RbeNf2M
Before Bootstrap 4.3.1, you'd can implement responsive text using SASS. However you need to specify the desired appropriate selector(s) for text that you want to resize...
#import "bootstrap/functions";
#import "bootstrap/variables";
#import "bootstrap/mixins";
html {
font-size: 1rem;
}
#include media-breakpoint-up(sm) {
html {
font-size: 1.1rem;
}
}
#include media-breakpoint-up(md) {
html {
font-size: 1.2rem;
}
}
#include media-breakpoint-up(lg) {
html {
font-size: 1.3rem;
}
}
#import "bootstrap";
Demo: https://www.codeply.com/go/5pZDWAvenE
This could also be done using CSS only (no SASS):
Demo: https://www.codeply.com/go/E1MVXqp21D

I think the easiest way is to use #media Queries. Suppose you want to change the font size responsively for a content with class "class-name" or even for entire html tag, just add your media queries to end of your css file or any place inside it.
Sample code:
/*
####################################################
M E D I A Q U E R I E S
####################################################
*/
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::
Bootstrap 4 breakpoints
*/
/* Small devices (landscape phones, 544px and up) */
#media (min-width: 544px) {
.class-name {font-size: 16px;}
}
/* Medium devices (tablets, 768px and up) */
#media (min-width: 768px) {
.class-name {font-size: 30px;}
}
/* Large devices (desktops, 992px and up) */
#media (min-width: 992px) {
.class-name {font-size: 40px;}
}
/* Extra large devices (large desktops, 1200px and up) */
#media (min-width: 1200px) {
.class-name {font-size: 48px;}
}
more information can be found here

This is a Sass feature.
To have access to the media-breakpoint mixins and the size variables, you need to:
add a custom.scss file
#import "node_modules/bootstrap/scss/bootstrap";
and setup a Sass compiler
https://getbootstrap.com/docs/4.0/getting-started/theming/

Not a complete answer, but a good starting point is to enable responsive font sizes in v.4.5
$enable-responsive-font-sizes: true;
#import "../../../node_modules/bootstrap/scss/bootstrap";

Here is an alternative approach with loop
#import "bootstrap/functions";
#import "bootstrap/variables";
#import "bootstrap/mixins";
$font-sizes: (
html: ( xs: 1rem, sm: 1.2rem, md: 1.3rem),
);
#each $breakpoint in map-keys($grid-breakpoints) {
#include media-breakpoint-up($breakpoint) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
#each $name, $values in $font-sizes {
#each $n, $value in $values {
#if($infix == "-#{$n}" or ($infix == "" and $n == 'xs')) {
#{$name} { font-size: $value; }
}
}
}
}
}

Related

How to add font-awesome icons from sources with SASS?

I'm creating a React/Ionic project and using SASS for styling, but I had a hard time adding the stylings to the project.
I went to the font-awsome website, created the package, added the fonts and added the file call to my routes file. But when I use the icons, they don't appear.
the fonts folder:
font-awesome.module.scss file:
#font-face {
font-family: 'cbm-icons';
src : url('./fonts/cbm-icons.eot?uwy7j0');
src : url('./fonts/cbm-icons.eot?uwy7j0#iefix') format('embedded-opentype'),
url('./fonts/cbm-icons.ttf?uwy7j0') format('truetype'),
url('./fonts/cbm-icons.woff?uwy7j0') format('woff'),
url('./fonts/cbm-icons.svg?uwy7j0#cbm-icons') format('svg');
font-weight : normal;
font-style : normal;
font-display: block;
}
[class^="cb-"],
[class*=" cb-"] {
/* use !important to prevent issues with browser extensions that change fonts */
font-family : 'cbm-icons' !important;
speak : never;
font-style : normal;
font-weight : normal;
font-variant : normal;
text-transform: none;
line-height : 1;
/* Better Font Rendering =========== */
-webkit-font-smoothing : antialiased;
-moz-osx-font-smoothing: grayscale;
}
.cb-cross:before {
content: "\ea0f";
}
.cb-checkmark:before {
content: "\ea10";
}
.cb-warning:before {
content: "\ea07";
}
Here is the import in file in my routes file:
The correct way of setting this up:
First, edit scss/variables.scss and edit the $fa-font-path variable to point to where your webfonts are.
Then, just add to your main (in your case, global) scss file:
#import "./fontawesome/scss/fontawesome.scss";
Then you can use the mixins as follows:
.twitter {
#extend %fa-icon;
#extend .fab;
&:before {
content: fa-content($fa-var-twitter);
}
I followed the wiz recommendations, but tweaked it and did it my way. I took the opportunity to make some changes to my code.
So i did like this. First, I centered all my styling in global.scss. Including the style imports from ionic and the #mixin I created from font-awesome:
#import './variables.module.scss';
#import './font-awesome.module.scss';
#import '#ionic/react/css/core.css';
#import '#ionic/react/css/normalize.css';
#import '#ionic/react/css/structure.css';
#import '#ionic/react/css/typography.css';
#import '#ionic/react/css/padding.css';
#import '#ionic/react/css/float-elements.css';
#import '#ionic/react/css/text-alignment.css';
#import '#ionic/react/css/text-transformation.css';
#import '#ionic/react/css/flex-utils.css';
#import '#ionic/react/css/display.css';
#include font-awesome;
// the rest of the global styling...
}
font-awesome.scss file settings:
#mixin font-awesome {
// file settings...
}
And in the router.tsx file I left only the call to global.scss:
import 'src/presentation/styles/global.scss'
// remaining importations...
const Router: React.FC = () => {
return (
// Routes...
)
}
export default Router

Global media queries on a react project

I'm trying to generate a general structure of styles on scss with global breakpoints as media queries on a react project, It is possible to reuse an structure to follow media queries that we declare as global.
I'm a little bit lost on this one, any ideas?
When I mean global is that we can define the breakpoints at the root of the project and we can use any reference on the components.
Thanks in advance.
There are three ways that come to mind:
You can create a variables.scss file in which you can write the value of your breakpoints:
$sm: 576px;
$md: 768px;
$lg: 992px;
$xl: 1200px;
And the use the following variables in your scss:
#media only screen and (min-width: $sm) {
.container {
.max-width: 450px;
}
}
#media only screen and (min-width: $md) {
.container {
.max-width: 650px;
}
}
#media only screen and (min-width: $lg) {
.container {
.max-width: 900px;
}
}
#media only screen and (min-width: $xl) {
.container {
.max-width: 1000px;
}
}
Or you can the mentioned variables in your mixins.scss file to create some media query mixins:
#mixin small {
#media only screen and (min-width: $sm) {
#content;
}
}
And then, use these mixins in your main scss codes:
.container {
#include small {
max-width: 450px;
}
...
}
Or if the use cases of these media queries are limited (e.g. hiding and showing elements), you can define other mixins that include all the variations:
$displays: none inline inline-block block table table-cell table-row flex inline-flex;
$sizes: (
sm: $sm,
md: $md,
lg: $lg,
lg: $xl
);
#each $display in $displays:
#each $size-key $size in $sizes {
.display-#{size-key}-#{display} {
display: $display !important;
}
}
}
A note on importing files: I personally would import all my helper scss (variables, mixins, etc.) in a file called styles/index.scss in the root of my project among with normalizing and other global rules that I want to define, and then import this file in my other scss files:
// styles/index.scss
#import './variables.scss';
#import './mixins.scss';
...
// container.scss
#import './styles/index.scss';

Not able to solve the media query issue (responsive design)

I tried the following code for making my page responsive, but still the elements are moving out. Can anyone lease have a look at it and help me out?
#media only screen and (max-width: 500px) {
.top-bar-section ul {
margin-top: 15px;
}
}
#media only screen and (min-width: 300px) and (max-width: 500px) {
.widget-area {
display: none;
}
.stat {
margin-bottom: 30px;
}
.clients-style-2 .slides li .client-logo {
margin-bottom: 5px;
}
#clients .slides li .client-logo {
margin-bottom: 5px;
}
#icategories li {
margin-bottom: 10px;
}
}
Ref url: http://7drives.in/dsq/index.html
You have a YouTube video in an iframe, and that iframe has a hard coded width of 500px, so this will be a problem on narrower viewports.
CSS Tricks has one solution to this, jQuery below. There are other solutions for responsive iframes, I like that this is a simple drop fix which doesn't require any changes to your HTML, and also that it resizes both the width and the height of your video.
Hope this helps!
// By Chris Coyier & tweaked by Mathias Bynens
$(function() {
// Find all YouTube videos
var $allVideos = $("iframe[src^='http://www.youtube.com']"),
// The element that is fluid width
$fluidEl = $("body");
// Figure out and save aspect ratio for each video
$allVideos.each(function() {
$(this)
.data('aspectRatio', this.height / this.width)
// and remove the hard coded width/height
.removeAttr('height')
.removeAttr('width');
});
// When the window is resized
// (You'll probably want to debounce this)
$(window).resize(function() {
var newWidth = $fluidEl.width();
// Resize all videos according to their own aspect ratio
$allVideos.each(function() {
var $el = $(this);
$el
.width(newWidth)
.height(newWidth * $el.data('aspectRatio'));
});
// Kick off one resize to fix all videos on page load
}).resize();
});

Using Font Awesome in ui.bootstrap.rating

How can I use Font Awesome in ui.bootstrap.rating?
I found out, that when I add state-on="'fa-star'" state-off="'fa-star-o'" to and changed class="glyphicon" to class="fa" in ui-bootstrap-tpls it works.
But I guess there is a more custom way to change the class of the icons.
Yeah as you are doing with setting state-off and state-on is their recommended manner. If you are going to have lots of the ratings on a page, I would just create a custom template and over-ride the stock template. Here is a post custom templates
I had Font Awesome and so didnt want to include Glyphicons.
uib.bootstrap Version: 1.3.3 - 2016-05-22 uses limited Glyphicons, so this is what i added to my css
.glyphicon {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.glyphicon-star:before {
content: "\f005";
}
/**
copied from
.fa-star:before {
content: "\f005";
}
*/
.glyphicon-star-empty::before {
content: "\f006";
}
.glyphicon-chevron-right:before {
content: "\f054";
}
.glyphicon-chevron-left:before {
content: "\f053";
}
.glyphicon-chevron-up:before {
content: "\f077";
}
.glyphicon-chevron-down:before {
content: "\f078";
}
i.e. added css from Font Awesome 4.6.3 to appropriate glyphicon names
Now i dont know if this code will break on version of Font Awesome

Stylus not parsing all variables in the same way

I'm trying to parse a list in Stylus (latest version), but it's having odd results.
$small = 200px
$medium = 400px
$large = 600px
$list = small $small,
medium $medium,
large $large
for ham in $list
#media screen and (min-width: ham[1])
.{ham[0]}
width ham[1]
yields
#media screen and (min-width: ham[1]) {
.small {
width: 200px;
}
}
#media screen and (min-width: ham[1]) {
.medium {
width: 400px;
}
}
#media screen and (min-width: ham[1]) {
.large {
width: 600px;
}
}
The ham[1] variable isn't getting parsed in the media query regardless of whether I wrap it in {} or not, but it's parsed elsewhere just fine. What am I missing here?
Currently, media queries do not allow for interpolation. What you can do however is use one variable. Just construct the query beforehand for now :
$small = 200px
$medium = 400px
$large = 600px
$list = small $small,
medium $medium,
large $large
for ham in $list
query = 'screen and (min-width: %s)' % ham[1]
#media query
.{ham[0]}
width ham[1]
UPDATE :
With Stylus 0.44 (or 0.45), they now do !

Resources