CSS backgrounds in react - css

so i'm in the process of learning react and to do so i'm trying to convert my personal webpage to use it.. But i've hit a wall. Before I had a background image on my home page that took up 100% width and 80% height. But when I try to do that in react it's not showing my image. Here is my code.
body,h1,h2,h3,h4,h5,h6 {font-family: "Lato", sans-serif;}
body, html {
height: 100%;
color: #777;
line-height: 1.8;
margin: 0;
}
/* Create a Parallax Effect */
.bgimg-1, .bgimg-2, .bgimg-3 {
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.bgimg-1 {
background-image: url('../images/haha.jpg');
min-height: 80%;
}
#homeTest {
white-space:nowrap;
}
and on the react side i'm just doing:
import React from 'react';
import ReactDOM from 'react-dom';
import './css/w3.css';
import './css/landing.css'
class Landing extends React.Component {
render() {
return (
<div className="bgimg-1" id="home">
<div id="homeText">
<h1>David Dennis <small>Im here to create meaningful and lasting software</small>
<a href="https://www.youtube.com/channel/UCun833t1bRiYlpoRAkg0S1g?view_as=subscriber">
<button class="btn draw-border" >Subscribe on YouTube</button></a></h1>
</div>
</div>
);
}
}

Related

inline CSS styles backgroundImage in React import images not working

Can you please help me with why this is not working. I have tried every possible way and still not get why it is not showing on the screen. Only the commented out inside the div work however what i want is a backgroundImage.
import React from "react";
import styles from "./Carosel.module.css";
import legend from "../carousel-photos/legend.jpg";
import legend1 from "../carousel-photos/legend2.jpeg";
function Carousel() {
return (
<div className={styles.carousel}>
<div
className={styles.carouselInner}
styles={{
backgroundImage: `url(${legend})`,
}}
>
{/* <img src={images[0].img} /> */}
</div>
</div>
);
}
export default Carousel;
.carousel {
width: 60%;
height: 700px;
/* background-color: black; */
}
.carouselInner {
height: 100%;
width: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: flex;
}
This is how I put my folder directory looks like

background-image does not display in my react application

I am new to react and trying to make a simple component and set its background image using separate css file.
My App.js component:
import React from "react";
import "./styles.css";
export default function App() {
return (
<div className="App">
<div className="header-img">
{/* <img src={require('./header.jpg')} alt='header-img'/> */}
</div>
</div>
);
}
The CSS file:
*{
margin: 0;
padding: 0;
border: 0;
box-sizing: border-box;
}
.header-img{
width: 100%;
height: 426px;
margin-top: 50px;
background-image: url('./header.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
/* background-color: aquamarine; */
}
Here is CodeSandBox link https://codesandbox.io/s/clever-silence-cvhyt?file=/src/styles.css:0-309
Using files through pure CSS will work only on static files, so you need to put your images in the public folder.
That's because React applications that bootstrapped with create-react-app are using webpack for bundling the src folder, accessing a path like './header.jpg' through CSS won't bundle the file.
// public/header1.jpg
.header-img {
background-image: url("./header1.jpg");
}

I a using mdi-react package for Icons but when combining it with scss the "font-size" property dosen't work

I am using mdi-react package and combining it with scss to define some styles but the font-size propert is not working.
Everything else (like color property) is working.
I searched it all over the internet but can't find a solution so finally decided to write my first question on stackoverflow.
And I know I can just use size="8rem" in the icon component itself but I don't want to do that because of some reasons.
Here is my Home.js file:
import React from 'react';
import { Helmet } from 'react-helmet';
import CubeOutlineIcon from 'mdi-react/CubeOutlineIcon';
import { Link } from 'react-router-dom';
const home = () => (
<>
<Helmet><title>Quiz App - Home</title></Helmet>
<div id="home">
<section>
<div>
<CubeOutlineIcon className="cube" />
</div>
<h1>Quiz App</h1>
<div className="play-button-container">
<ul>
<li><Link to="/play/instructions">Play</Link></li>
</ul>
</div>
<div className="auth-container">
<Link to="/login">Login</Link>
<Link to="/register">Register</Link>
</div>
</section>
</div>
</>
);
export default home;
Here is my home.scss file:
#home {
background-image: url('../../assets/img/bg1.jpg');
background-size: cover;
background-position: center;
display: flex;
justify-content: space-around;
height: 100vh;
section {
background-color: rgba($color: #000000, $alpha: 0.7);
padding: $normal $md;
height: 80%;
width: 35%;
}
.cube {
font-size: 8rem; //Not working
color: $orange;
}
}
That's because the .cube is actually assigned to an SVG element and that would require you to use height and width to change it's size. Font ain't gonna work hence.
(I'm assuming that you considered it to be a font-icon or so as the color property got applied to it, but it did so cos fill is set as currentColor, which uses the current text-color.)
According to docs, to change the size of the icon you should use the size property.
Check the following link:
https://github.com/levrik/mdi-react#usage

equal sizing of column height bootstrap

I have created a React Social Card component using Bootstrap. I want all the cards to be the same size. However, the first card is rendering much larger than the other two. I have tried unsuccessfully to manipulate the CSS to fix this problem but unfortunately, I am a novice with Bootstrap. I have also tried exporting this project into Codesandbox from the Github repository I have it stored in to make it easier to help me find the solution. I am getting this error there: Error importing GitHub repository: Could not find package.json.
Anyway, does anyone know the solution to this problem? Below, I will share a screenshot to illustrate the problem as well as the code for the relevant components and CSS file. Here is the link to the Github repository: https://github.com/jevoncochran/React-Social-Card
Here is the Cards component:
import React, { Component } from "react";
import Card from "./CardUI";
import img1 from "../assets/me-telice-pelo.jpg";
import img2 from "../assets/me-pernambues.jpg";
import img3 from "../assets/me-lucas-walking-pernambues.jpg";
class Cards extends Component {
render() {
return (
<div className="container-fluid d-flex justify-content-center">
<div className="row">
<div className="col-md-4">
<Card imgsrc={img1} title="Convergeance" />
</div>
<div className="col-md-4">
<Card imgsrc={img2} title="Continuation" />
</div>
<div className="col-md-4">
<Card imgsrc={img3} title="Conspiracy" />
</div>
</div>
</div>
);
}
}
export default Cards;
Here is the CSS code:
body {
background: radial-gradient(#e5e5e5, #ffff, #e5e5e5);
}
.card {
width: 20rem;
}
.card:hover {
box-shadow: 5px 10px 20px 1px rgba(0,0,0,0.253) !important;
}
.card-body {
padding: 3rem 0 !important;
}
.card-text {
font-size: 0.9rem;
padding: 0.4rem 1.9rem;
}
.container-fluid .row {
padding: 6rem;
}
.overflow {
overflow: hidden;
}
.card-img-top {
transform: scale(1);
transition: transform 0.5s ease;
}
.card-img-top:hover {
transform: scale(1.4);
}
So, I figured out a solution. I'm not sure it's the best solution out there. But I determined that the problem was that the first image is bigger than the other two images. In my CSS, I just set the maximum height of the all images to the height of the last two (211.938px).

React component is not reading my css file

For some reason, my React Component is not reading my CSS file. This is the code in my React component.
import React, { Component } from 'react';
import './Jumbotron.css';
class Jumbotron extends Component {
render() {
return (
<div className="Jumbotron jumbotron-fluid">
<div className="container">
<h1 className="display-3">{this.props.title}</h1>
<p className="lead">{this.props.subtitle}</p>
<p className="lead">{this.props.name}</p>
</div>
</div>
)
}
}
export default Jumbotron;
Below is my file structure
The .css file has the following code.
.jumbotron {
background-image: url(../images/fog-forest-lake-113727.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: 100% auto;
color: white;
}
Short answer - The classname is lowercase in your css, it should be capital. Longer answer below which might be helpful to you.
I would recommend using `styled components'. Link is here.
For example your component could look like below
import React, { Component } from 'react';
import StyledWrapper from './styles/wrapper';
class Jumbotron extends Component {
render() {
return (
<StyledWrapper>
<div className="container">
<h1 className="display-3">{this.props.title}</h1>
<p className="lead">{this.props.subtitle}</p>
<p className="lead">{this.props.name}</p>
</div>
</StyledWrapper>
)
}
}
export default Jumbotron;
And in my wrapper.js file I would have the following
import styled from 'styled-components';
export default styled.div`
background-image: url(../images/fog-forest-lake-113727.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: 100% auto;
color: white;
`;

Resources