how can I make align-items work in reactJS? - css

I have 4 boxes which I'm using them as component , when I'm applying justify-content:center it is centering my items in terms of with , but when I'm trying to use align-items:center , it's not working , and the more confusing thing that I made body color teal and the whole page turned teal but in developer tools it shows that I only have small amount of height, so why the whole page turned to teal ?
import React from 'react';
import "./App.css"
function Myfunc(props){
return(
<div className="holder">
<h1>{props.text}</h1>
</div>
);
}
export default Myfunc;
import React from "react";
import Myfunc from "./CSSexample";
function App(){
return(
<div className="container">
<Myfunc text=""/>
<Myfunc text=""/>
<Myfunc text= ""/>
<Myfunc text=""/>
</div>
);
}
export default App;
*,
*::before,
*::after{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.holder{
background-color: crimson;
width: 10rem;
height: 10rem;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid white;
}
body{
display: flex;
justify-content: center;
align-items: center;
background-color: teal;
}
.container{
display: flex;
justify-content: center;
align-items: center;
color: white;
}

Try setting the width/height of container
.container {
display: flex;
justify-content: center;
align-items: center;
color: white;
height: 100vh;
width: 100vw
}

Related

CSS Issue on React project

I'm starting to play with CSS and I'm struggling to center my "tasks". To debug I used a colored border and it's centered, but the elements aren't.
.App {
display: block;
text-align: center;
align-items: center;
}
.done {
color: red;
}
.task {
text-align: center;
align-items: center;
display: flex;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
Here is where the CSS is targetting:
import React, { Component } from "react";
class Task extends Component {
render() {
return (
<div className={`task ${this.getClass()}`}>
<div>{this.props.task.text}</div>
<button className="task-element" onClick={() => this.props.onDone(this.props.task.id)}>
<span className="material-symbols-outlined">check_box</span>
</button>
<button className="task-element" onClick={() => this.props.onDelete(this.props.task.id)}>
<span className="material-symbols-outlined">
delete
</span>
</button>
</div>
);
}
getClass() {
return this.props.task.isDone ? 'done' : 'notDone';
}
}
export default Task;
Here is the Output
Tryed to center elements and can't.
You need to use the justify-content property to center the elements inside the flexbox
Try this
.task {
align-items: center;
justify-content: center;
display: flex;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
You would need to justifyContent to centen, as your flex main axis is horizontal, and you want to center the element horizontally. Justify content manage content along main Axis, Here you can give a CSS a try, and let me know whether it works
.task {
text-align: center;
align-items: center;
justify-content: center; /*This will align the content to center in horizontal direction which is main axis if flex direction is row*/
display: flex;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
If you can provide whole code in a codesandbox, I can help more on that
Following are good articles on Flex box
An Interguide to Flex box by Josh W Comeau
Css Trick article on Flex box

How to make scss code more efficient in this scenario

I have two buttons with classnames of "app__footer-card-email" and "app__footer-card-mobile".
They are the same buttons css wise except they have different background colours.
What are some efficient methods of reducing repetition in scss in this scenario? I am even looking for multiple options so i can apply the principle to other instances of scss aswell. Thanks!
HTML
const Footer = () => {
return (
<>
<div className="app__footer">
<h3 className="head-text">Want to reach out?</h3>
<h3 className="head-text">Lets have a chat over some coffee.</h3>
<div className="app__footer-cards">
<div className="app__footer-card-email">
<img src={images.email} alt="email"/>
edwin.huang9#gmail.com
</div>
<div className="app__footer-card-mobile">
<img src={images.mobile} alt="mobile"/>
0415560320
</div>
</div>
</div>
</>
)
}
SCSS
.app__footer-card-mobile {
width: 360px;
display: flex;
justify-content: flex-start;
align-items: flex-start;
margin: 2rem;
border: radius 0%;
background-color: #d0e2fe;
opacity: 0.7;
border-radius: 10px;
padding: 10px;
flex-direction: row;
align-items: center;
}
.app__footer-card-email {
width: 360px;
display: flex;
justify-content: flex-start;
align-items: flex-start;
margin: 2rem;
border: radius 0%;
background-color: #ffc9d0;
opacity: 0.7;
border-radius: 10px;
padding: 10px;
flex-direction: row;
align-items: center;
}
Like the others have said, split out the shared properties into their own class, then change based on your variations (mobile, email, etc.).
You're already using SASS, so you can use nesting here to keep everything together nicely.
I've renamed the classes so you can see one way this could be done with BEM naming, but you can adjust to whatever suits best for you.
You should however use appropriate semantic elements.
.footer-card {
background-color: var(--background-color, #f2f2f2);
width: 360px;
display: flex;
justify-content: flex-start;
align-items: flex-start;
margin: 2rem;
border: radius 0%;
opacity: 0.7;
border-radius: 10px;
padding: 10px;
flex-direction: row;
align-items: center;
&--mobile {
--background-color: #d0e2fe;
}
&--email {
--background-color: #ffc9d0;
}
}
const Footer = () => {
return (
<>
<footer className="footer">
<h3 className="footer__heading">Want to reach out?</h3>
<p className="footer__body">Lets have a chat over some coffee.</p>
<div className="footer__cards">
<div className="footer__card footer__card--email">
<img src={images.email} alt="email"/>
hi#example.com
</div>
<div className="footer__card footer__card--mobile">
<img src={images.mobile} alt="mobile"/>
55555555
</div>
</div>
</footer>
</>
)
}
Buttons and cards are something you'll find yourself repeating often, so it can also be smart to make yourself a mixin or function to do this more handily.
You can do this with CSS by moving the shared CSS properties into a common class, which is most efficient because there will be no unnecessarily duplicated CSS across classes.
.app__footer-card-mobile {
background-color: #d0e2fe;
}
.app__footer-card-email {
background-color: #ffc9d0;
}
.app__footer-card {
width: 360px;
display: flex;
justify-content: flex-start;
align-items: flex-start;
margin: 2rem;
opacity: 0.7;
border-radius: 10px;
padding: 10px;
flex-direction: row;
align-items: center;
}
<h3>Want to reach out?</h3>
<h4>Lets have a chat over some coffee.</h4>
<div class="app__footer-card app__footer-card-email">
<img src="" alt="email" />
edwin.huang9#gmail.com
</div>
<div class="app__footer-card app__footer-card-mobile">
<img src="" alt="mobile" />
0415560320
</div>
You can create a general class name with the similarities of your two buttons classes and then extend the general class in your custom styles; Like below:
.btn{
width: 360px;
display: flex;
justify-content: flex-start;
align-items: flex-start;
margin: 2rem;
border: radius 0%;
opacity: 0.7;
border-radius: 10px;
padding: 10px;
flex-direction: row;
align-items: center;
}
.app__footer-card-mobile {
#extend .btn;
background-color: #d0e2fe;
}
.app__footer-card-email {
#extend .btn;
background-color: #ffc9d0;
}

I want to have 3 div in a row in my page, i tried flex,grid,inline, but i am unable to do so

[I created a card component which has wrapper div to wrap cards in it, the cards are display fine 1 in a row, however I want to display 3 in a row and unable to do it. I have tried display flex to show 1 in a row.
Cards component
import React from "react";
import "../index.css";
const Card = (props) => {
return (
<>
<div className="cards">
<div className="card">
<img src={props.imgSrc} alt="Dark" className="card-img" />
<div className="card-info">
<span className="card-category">{props.cat}</span>
<h3 className="card-title">{props.title}</h3>
<a href={props.link}>
<button>watch now</button>
</a>
</div>
</div>
</div>
</>
);
};
export { Card };
Index
import react from "react";
import ReactDOM from "react-dom";
import { Whole } from "./Whole";
import { Cards } from "./learningReact/Cards";
ReactDOM.render(
<>
<Cards />
</>,
document.getElementById("root")
);
css
cards {
display: flex;
justify-content: center;
align-content: center;
margin: 100px auto;
}
.card {
width: 250px;
height: 300px;
border: none;
background: rgb(248, 239, 248);
border-radius: 10px;
}
.card img {
width: 250px;
height: 200px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
.card:hover {
transform: scale(1.1);
}
you should use flex-basis:33%; for .card div tag and flex-wrap:no-wrap; & justify-content: space-between; for .cards div tag:
.cards {
display: flex;
justify-content: space-between;
align-content: center;
margin: 100px auto;
flex-wrap:nowrap;
}
.card {
flex-basis:33%;
height: 300px;
border: none;
background: rgb(248, 239, 248);
}
<style>
.card-data {
display: flex;
flex-wrap: nowrap;
background-color: #000;
}
.card-data div {
background-color: #f1f1f1;
width: 33%;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
<div class="card-data">
<div>Hello..</div>
<div>Test...</div>
<div>Content..</div>
</div>
Replace the justify-content: center with justify-content: space-between inside .cards block and it will be displayed in a single row.
output here

Syntax Highlighting for CSS in a Style Tag in a TSX Component

In my React app, I like to write CSS style within a style tag of a given TSX component. Is there a way to achieve VS Code syntax highlighting for CSS within this context?
Currently, the entire file reflects TSX syntax highlighting, which means the CSS string fragment is all a single color.
I am not using styled-components.
Below is an example of the text I'd like highlighted within the style tag content:
export default function HomeStyle() {
return (
<style>{`
.container {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
`}</style>
);
}
If you use a tagged template literal named css you should get CSS Intellisense & syntax highlighting. eg:
const css = String.raw;
export default function HomeStyle() {
return (
<style>{css`
.container {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
`}</style>
);
}
If you use a tagged template literal named html you should get HTML Intellisense and syntax highlighting. eg:
const html = String.raw
const template = html`
<div class="container"></div>
`
In these examples I’m using String.raw as a passthrough. Frameworks may provide their own html and css functions to use for this purpose that add framework specific features.

Placing a div below another div using Flexbox

I have markup like so:
HTML:
<ListElementRoot>
<ListElementText>
{children}
</ListElementText>
<ListElementDescription>
{description}
</ListElementDescription>
</ListElementRoot>
//platform.web.js
export const ListElementRoot = div(style.root)
export const ListElementIcon = div(style.icon)
export const ListElementText = div(style.text)
export const ListElementDescription = div(style.description)
//CSS
.root {
display: flex;
width: 100%;
height: 56px;
align-items: center;
border-top: 1px solid var(--color-gray2);
padding: 0;
}
.icon {
position: absolute;
width: 28px;
height: 28px;
align-items: center;
padding-left: 18px;
}
.text {
color: #000;
font-size: calc(var(--font-size-body) / 10)rem;
padding-left: 64px;
}
.description {
color: #000;
font-size: calc(var(--font-size-body) / 12.3)rem;
padding-left: 64px;
}
I'm new to flexbox.
How can I make element just under ?
I'm trying to find a solution but it is heavily twisted in flexbox for me (for now). Any ideas?
EDIT:
Without flex-direction: column;
With flex-direction: column;
You need to have a flex-direction property set to column for your flex container (.root).
Here you can find a jsfiddle example.
EDIT:
Change align-items: center to be align-items: flex-start in order to have the elements align to the left

Resources