Encountering white-space at top of react-app - reactjs

enter image description here
Image
encountering white space at top of the react-app
Check the image
There's weird whitespace occurring at the top of the react-app. My developed app not even a big react-app but there something occupying the space there and creating a white space there. I have deleted the old project and did this new one thinking it'll remove my error but the same result. I'm using a routing package to route through my app react-route-dom. I tried all the possible ways but couldn't find out the error or the line of code that is triggering the white-space at the top of the react-app. Please anyone help I have to submit my project!.
App.js
import React from 'react'
import './App.css';
function App() {
return (
<div className="App">
<h1>Hello</h1>
</div>
);
}export default App
App.css
.App {
text-align: center;
background: red;
height: 30%;
}
Index.js
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom'
const app = <BrowserRouter><App/></BrowserRouter>
ReactDOM.render(
<React.StrictMode>
{app}
</React.StrictMode>,
document.getElementById('root') );
reportWebVitals()
**index.css**
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background: #333;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}

Add this to your index.css :
*{
margin: 0;
padding: 0;
box-sizing: border-box; // not neccesary
}
This happens because chrome by default adds some padding to your page

user agent stylesheet is doing that. For instance Chrome styles h1 by default like so:
h1 {
display: block;
font-size: 2em;
margin-block-start: 0.67em;
margin-block-end: 0.67em;
margin-inline-start: 0px;
margin-inline-end: 0px;
font-weight: bold;
}
This will create those margins you're complaining about. All you need to do is explicitly style your h1 in your CSS file. Something like this: h1{margin: 0px}. Do the same for the body.
Also note that If <!DOCTYPE> is missing in your HTML content oftentimes the browser gives preference to the "user agent stylesheet" over your custom stylesheet.

In my case, the space provoked the css property place-items: center from the index.css file.

Related

How Do you reset the CSS in React if you're doing styled components?

So I'm creating a footer with styled components and we don't use classes like in normal css.
If I want to replicate the same CSS trick where we do this code below
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
How would I replicate this in a styled component? Is that even the correct way to do it?
Or would I need to create an App.css file and simply add it to my project?
Here's the typical code for a styled component in react
import styled from 'styled-components/macro';
export const Container = styled.div`
padding: 80px 60px;
background: black;
font-family: 'Nunito Sans', sans-serif;
#media (max-width: 1000px) {
padding: 70px 30px;
}
`;
Is it possible to add that * { box-sizing} code into my styled component or what is the proper way to implement that into my project?
import { createGlobalStyle } from 'styled-components'
const GlobalStyle = createGlobalStyle`
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
`
// in your app root
<React.Fragment>
<GlobalStyle />
<Navigation /> {/* example of other top-level stuff */}
</React.Fragment>

How to use font-weight and font-family in styled components?

When I am trying to use font-weight and font-family when creating a styled component, nothing happens. Is there some specific way to set font-weight and font-family, when using styled components?
Any help is appreciated. Thanks in advance.
export const H3 = styled.h3`
font-size: ${fontsize.H3FONTSIZE};
font-weight: 700;
font-family: "museo", Helvetica Neue, Helvetica, sans-serif;
`;
What you have works perfectly fine.
Complete example
import React, { Component } from 'react';
import { render } from 'react-dom';
import styled from 'styled-components';
const fontsize = {
H3FONTSIZE: '400px',
}
export const H3 = styled.h3`
font-size: ${fontsize.H3FONTSIZE};
font-weight: 10000;
font-family: "museo", Helvetica Neue, Helvetica, sans-serif;
`;
class App extends Component {
render() {
return (<H3>Hello</H3>)
}
}
render(
<App />,
document.getElementById('root')
)
Demo: https://stackblitz.com/edit/styled-components-rbd4xo?file=index.js

Trying to import component but getting error

I am creating a Tetris game and a created a component (StyledDisplay), which I an trying to import but I am getting an error for my Display.Js page that says the following:
Attempted import error: 'StyledDisplay' is not exported from
'./styles/StyledDisplay'.
I know it's a very dumb question, but can someone look at how I am importing and exporting it? I was fairly confident I was exporting the file and importing it the correct way, but a new set of eyes would not hurt.
This is how I have my Display.js folder, which is where I am importing:
import React from 'react';
import { StyledDisplay } from './styles/StyledDisplay';
const Display = ({ gameOver, text }) =>(
<StyledDisplay gameOver={gameOver}>{text}</StyledDisplay>
)
export default Display;
This is how I have my Displayed.Js folder, which is where I created the component:
import styled from 'styled-components';
export const StyledDisplayed = styled.div`
box-sizzing: border-box;
display: flex;
align-items: center;
margin: 0 0 20px 0;
padding: 20px;
border: 4px; solid #333;
min-height: 30px;
width: 100%;
border-radius: 20px;
color: ${props => (props.gameOver ? 'red' : '#999')};
background: #000;
font-family: Pixel, Arial, Helvetica, sans-seriff;
font-size: 0.8rem;
`;
This is how I have my files set up in Visual Studio Code:
Your export is named StyledDisplayed not StyledDisplay.
You also appear to have some additional typos like box-sizzing and sans-seriff.
Change export const StyledDisplayed to export const StyledDisplay.
P.S. You have misspellings in your CSS that might cause weird visuals.

I have installed #fortawesome/react-fontawesome via Yarn. How do I use the font in SCSS?

I'm trying to take the carousel from this: https://www.creative-tim.com/product/material-kit-react?partner=104080
and add it to my own react project. However, the slick-prev and slick-next buttons aren't displaying properly. I took a look at the SCSS that came with the kit and it looks like this:
.slick-prev {
left: 0;
&::before {
content: "\f053";
font-weight: 600;
font-family: Font Awesome\ 5 Free;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
line-height: 1;
color: white;
font-size: 30px;
width: 100%;
}
}
.slick-next {
right: 0;
&::before {
content: "\f054";
font-weight: 600;
font-family: Font Awesome\ 5 Free;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
line-height: 1;
color: #fff;
font-size: 30px;
width: 100%;
}
}
and I see that they have content that is displayed with the "Font Awesome 5" family, so I've been trying to install that in my project. I used the command yarn add #fortawesome/react-fontawesome, most of the things I found dealt with adding Icon components to React though, when I want to be able to use the font in CSS. What are the next steps to import this package as a font?
For my React Component, it looks like this:
import React from 'react'
import Carousel from 'react-slick'
import "../styles/scss/carousel.scss";
const SectionCarousel = () => {
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true
};
const image1 = 'https://demos.creative-tim.com/material-kit-react/static/media/bg.e5241971.jpg'
const image2 = 'https://demos.creative-tim.com/material-kit-react/static/media/bg2.84378183.jpg'
const image3 = 'https://demos.creative-tim.com/material-kit-react/static/media/bg3.e76de24b.jpg'
return (
<Carousel {...settings}>
<img src={image1} alt="First slide" className="slick-image" />
<img src={image2} alt="Second slide" className="slick-image" />
<img src={image3} alt="Third slide" className="slick-image" />
</Carousel>
)
}
export default SectionCarousel
When using the Slick Carousel, you have to import the needed CSS. Like this. SLICK
!-- Add the slick-theme.css if you want default styling -->
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/kenwheeler/slick#1.9.0/slick/slick.css"/>
<!-- Add the slick-theme.css if you want default styling -->
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/kenwheeler/slick#1.9.0/slick/slick-theme.css"/>
The font-family: Font Awesome\ 5 Free; in the slick btn css is used, because it's not with react-fontawesome. Btw. there is also a react port of slick carousel HERE
To use Font Awesome in react, you have to install a couple of things. The details are HERE.
yarn add #fortawesome/fontawesome-svg-core
yarn add #fortawesome/free-solid-svg-icons
yarn add #fortawesome/react-fontawesome
Then use it like this
import ReactDOM from 'react-dom'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
const element = <FontAwesomeIcon icon={faCoffee} />
ReactDOM.render(element, document.body)

Run on android with react-native-web

I'm new to React and React Native, I want to build a cross platform app which would use native components on mobile platforms.
I installed and initialized a react-native-app and install react-native-web and I wonder how to start my boilerplate code on android emulator, the documentation doesn't tell how.
Use these commands to get Demo Code
npm install -g create-react-native-app
create-react-native-app AwesomeProject
cd AwesomeProject
npm start or react-native start
Once you Get familiar to basics. I recommend you boilerplate code you must check out .
1. https://github.com/cubixlabs/ReactCube
2. https://github.com/GeekyAnts/NativeBase
3. https://github.com/shoutem/ui
Web View Component index.js
// #flow
import React from "react";
import PropTypes from "prop-types";
import {
WebView as WebViewRn
} from "react-native";
export default class WebView extends React.PureComponent {
static propTypes = {
source: PropTypes.object
};
static defaultProps = {
source: {
html: `
<html>
<header>
<style>
html, body {
height: 100%;
}
html {
display: table;
margin: auto;
}
body {
text-align: center;
display: table-cell;
vertical-align: middle;
}
</style>
</header>
<body>
<p>No valid HTML provided</p>
<p>source props is missing</p>
</body>
</html>
`
}
};
render() {
const {
source,
...rest
} = this.props;
return <WebViewRn source = {
source
} { ...rest
}
/>;
}
}
Component : Styles.js
// #flow
import {
StyleSheet
} from 'react-native';
import {
Colors
} from '../../theme';
export default StyleSheet.create({
container: {
flex: 1,
backgroundColor: Colors.primary.backgroundColor
}
});
Container Class index.js
// #flow
/*
Disable selection style
style="-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;"
Word wrap and break word style
style="word-wrap: break-word;"
*/
import {
connect
} from "react-redux";
import React, {
Component
} from "react";
import {
WebView
} from "../../components";
class Terms extends Component {
render() {
return <WebView source = {
{
html
}
}
/>;
}
}
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
margin: 0 auto;
}
h2 {
color: #000;
margin-bottom: 15px;
font-size: 30px;
font-weight: 700;
font-family: 'Roboto', sans-serif;
line-height: 1.2;
margin-top: 25px;
}
p {
font-family: 'Roboto', sans-serif;
text-align: left;
line-height: 26px;
font-weight: 300;
font-style: normal;
color: #000;
font-size: 18px;
}
</style>
</head>
<body>
<div class="container" style="-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;">
<h2>Disclaimer</h2>
<p>Always make sure to double check recipe and component ingredients (and where they are produced) if you have any health
related issues to avoid particular foods. Please make sure to consult a doctor and always double check labels. Please
notify us of any errors on the recipes itself. We strive to make information as acurate as possible for convenience, however
we disclaim any warranty on it.</p>
<p>Always consult a licensed physician or licesned nutritionalist for all medical, dietary and allergen issues. All information
on our site “as is” has no warranty on it of any kind. Either expressed or implied. </p>
<p>Rich thomas LLC and its partners disclaim all warranties expressed or implied including without limitation those of merchanitbility
or fitness for a particular purpose and non fringement or arsiing from a course of dealing, usage, or trade practice.
Rich Thomas LLC shall not be liable for any indirect, special, consequential, or incidental damages arising from any information
present.</p>
</div>
</body>
</html>`;
const mapStateToProps = () => ({});
const actions = {};
export default connect(mapStateToProps, actions)(Terms);

Resources