I can't align my components horizontally using React and CSS - css

I'm having some problems lining up some React Bootstrap cards, the only way I could align them horizontally was by putting them both inside a div, but I need a component for each card, if I ran the code that way, it would end up going wrong
Basically, every time I add a card, it's aligned vertically, not horizontally.
HostingCard.tsx
import { Card, Button } from "react-bootstrap";
import "./App.css";
export default function HostingCard() {
return (
<div className="main">
<Card style={{ width: "18rem" }}>
<Card.Img variant="top" src="https://picsum.photos/200" />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the
bulk of the card's content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</div>
);
}
App.tsx
import React from "react";
import "./App.css";
import Header from "./Header";
import HostingCard from "./HostingCard";
function App() {
return (
<div className="App">
<Header />
<h1 style={{ padding: "1rem" }}>Hospedagens</h1>
<HostingCard />
<HostingCard />
</div>
);
}
App.css
.main {
display: flex;
flex-direction: row;
margin: 0.1rem;
padding: 1rem;
}

flexDirection: row applies to the children inside the row. You're creating a column of rows where each row contains only one item in each.
For the row to work as a row it needs to be a container for the cards, something like this:
function App() {
return (
<div className="App">
<Header />
<h1 style={{ padding: "1rem" }}>Hospedagens</h1>
<div style={{ flexDirection: 'row', /* ...more styles */ }}>
<HostingCard />
<HostingCard />
</div>
</div>
);
}
Since you're already using React Bootstrap, you can browse its Layout docs and choose something suitable that they already built instead of styling your own div.
Horizontal Stack looks like what you want:
function App() {
return (
<div className="App">
<Header />
<h1 style={{ padding: "1rem" }}>Hospedagens</h1>
<Stack direction="horizontal" gap={4}>
<HostingCard />
<HostingCard />
</Stack>
</div>
);
}
Look at its implementation and you'll see it's ultimately a div container with flexDirection: row plus a bunch of additional preset options and styles.

Related

Can't set background under Layout components React and Tailwind CSS

I am trying to set the background image under my components. But it always is on top, so my elements are under it.
Or in the second variant, all works fine, except that I can't apply blur-xl only to the background. it applies to background and Layout elements.
bg-repeat-y needs me to copy down my image, am I right?
import moons from './../assets/moon.svg'
const Background = ({ children }) => {
return (
<div className='bg-white '>
<div className='min-h-screen min-w-screen bg-repeat-y' style={{ backgroundImage: `url(${moons})` }}>
<div className="div"></div>
</div>
{children}
</div>
);
}
export default Background;
This Background element wraps other elements in Laoyout in react-router-dom
export const AppLayout = () => (
<div>
<Background>
<NewNavbar />
<Outlet />
<Footer />
</Background>
</div>
);
Help me, please!
try this, the blur will be applied between your components & background image
export const AppLayout = () => (
<Background>
<div class="backdrop-blur-xl h-full">
<NewNavbar />
<Outlet />
<footer />
</div>
</Background>
);
if you have any queries, refer to this link
https://play.tailwindcss.com/yX9aAMagRj
For the first variant/approach that you mentioned, you can set the z-index of the background to negative using the class -z-10 . Then your background will not come over your other elements.

Can't get Bootstrap Scrollspy working on React

Hi I'm trying to get scrollspy working on React using Bootstrap but I seem to struggle to make it work.
I have tried several ways to do this but I really have no idea how to make this work and it'll be much appreciate if you can help me with this...
This is the css
body {
margin: 0;
font-family: "Roboto", sans-serif !important;
scroll-behavior: smooth;
overflow-x: hidden;
position: relative;
height: 100%;
}
This is body in index.html
body
data-bs-spy="scroll"
data-bs-target="#navbar-menu"
data-bs-offset="0"
class="scrollspy-example"
tabindex="0"
This is in Navbar component and inside Menu component I'm assigning the hrefs
<Navbar
id="navbar-menu"
className="py-2 py-md-0 bg-white"
expand="lg"
sticky="top"
>
<Container>
<NavbarBrand classes="" />
<Navbar.Toggle aria-controls="basic-navbar-nav" className="border-0" />
<Navbar.Collapse id="basic-navbar-nav" className="justify-content-end">
<Menus classes="top-nav px-3 mx-2 px-md-2 py-md-4 mx-md-1 mb-3 mb-md-0" />
</Navbar.Collapse>
</Container>
</Navbar>
Inside Menu
<Nav className="flex-wrap flex-lg-nowrap me-3">
{navMenu.map((menu, index) => (
<Nav.Link
href={menu.href}
key={menu.href}
>
{menu.name}
</Nav.Link>
))}
</Nav>
I have Landing component that hold everything and inside each wrapper there's id name as same as the nav menu
const Landing = () => {
return (
<>
<NavbarMenu />
<HeroWrapper />
<AboutWrapper />
<EcosystemWrapper />
<TokenomicsWrapper />
<TeamWrapper />
<FooterNav />
</>
);
};
export default Landing;

Ant Design React : Vertical align div inside a col

I'm using Ant Design for a React project. It seems quite good and easy to use, except for one thing : vertical alignment. I'm trying to vertical align those 2 blocks of text on the right inside their grey parent div but cant make it work.
If I use foundation 6 it works perfectly but with ant cant figure it out what's wrong. So here is my code :
<Row className="keynumbers" align="middle">
<Col span={12}>
<Graph tickets={nature} />
</Col>
<Col span={12} align='middle'>
<Space
direction="vertical"
size="middle"
style={{
display: 'flex',
}}
>
<TotalTickets total={total} />
<TimeSolving
tickets={resolution}
/>
</Space>
</Col>
</Row>
The components TimeSolving and TotalTickets are basic div :
TotalTickets
<div class="block" >
<h3>{volumeTickets}</h3>
<p>nombre de tickets</p>
</div>
TimeSolving
<div class="block">
<h3>{prop.tickets}</h3>
<p>temps moyen de résolution</p>
</div>
and the css for .block
.block{
background: #f8f8f8;
height: 140px;
}
I just want to vertical align those two blocks of text.
Did I miss something ?
Thanks a lot
If you are adding css for your block class, you can use flexbox to vertically centre its contents
.block {
background: #f8f8f8;
height: 140px;
display: flex;
flex-direction: column;
justify-content: center;
}
Edit:
You could achieve similar with Ant's Space primitive, which is really just a Flexbox primitive.
const TimeSolving = () => (
<Space
direction="vertical"
size={0}
style={{
height: 140,
justifyContent: "center"
}}
>
<h3>10</h3>
<p>temps moyen de résolution</p>
</Space>
);
Maybe you need to set <Col />'s span as {24}? Because Ant Design uses 24 grid.
<Row className="keynumbers" align="middle">
<Col span={24}>
<Graph tickets={nature} />
</Col>
<Col span={24} align='middle'>
<Space direction="vertical" size="middle" style={{ display: 'flex' }}>
<TotalTickets total={total} />
<TimeSolving tickets={resolution} />
</Space>
</Col>
</Row>

How to make the content of these cards spaced and justified at left side to make the cards even

These are my cards and this is it's CSS code
card: {
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
border: 'solid 1px #f0eff2',
paddingRight: '1rem',
},
and this is my react js code for the card content
<div className={classes.card}>
<Checkbox />
<div className={classes.title}>{title}</div>
<div>{description.substring(0, 70)} ...</div>
<div>{time}</div>
<div>
{favourite ? (
<BookmarkIcon className={classes.gold} />
) : (
<BookmarkTwoToneIcon />
)}
</div>
<div>
<IconButton>
<MoreIcon />
</IconButton>
</div>
</div>
I want the content spaced and justified at left side so that the cards looks the same.

How to align the contents of card in React-Bootstrap based on the content's height/length?

I want to align the content of the cards such that when the title of the content is too long, the other details such as the Created on, By and View would be on the same line. I've tried using a fixed height with min-height for the title row but I don't think it's a good solution as when the title is shorter, there will be a lot of white/empty space.
I also tried using flex-grow: 1 on the title but it won't fill up the empty spaces as well.
<Col>
<Card
className='text-center'
style={{
height: '100%',
padding: '15px',
borderRadius: '20px',
boxShadow: '0px 5px 15px 0px rgb(0 0 0 / 20%)'
}}
>
<Row style={{marginBottom: '5px'}}>
<Col md={6} style={{textAlign: 'left'}}>
<div
className='btn btn-success'}
style={{padding: '5px 25px', borderRadius: '50px', cursor: 'default', textTransform: 'capitalize'}}
>
{bulletin.liveStatus}
</div>
</Col>
<Col md={6} style={{textAlign: 'right'}}>
<div className='ellipsis-menu' ref={ref}>
<FaIcons.FaEllipsisH onClick={showDropDown} style={{cursor: 'pointer'}} />
</Col>
</Row>
<div className='view-bulletin-link'>
<Card.Img variant='top' src={imageCoverUrl} style={{objectFit: 'cover', height: '15rem', width: '100%', display: 'block'}}/>
<Card.Body>
<Card.Title style={{fontSize: '1rem'}}><strong>{bulletin.title}</strong></Card.Title>
<Card.Text>Created on {moment(bulletin.createdDate).format("DD/MM/YYYY")} <br />By {bulletin.createdBy}</Card.Text>
<Card.Text>{bulletin.viewCount > 1 ? <>{bulletin.viewCount} Views</> : <>{bulletin.viewCount} View</>}</Card.Text>
</Card.Body>
</div>
</Card>
</Col>
You can achieve this with flex.
Card content should have
flex-direction: col;
align-items: space-around
and wrap the top and bottom aligned elements in different div/spans.
You could group the created and view count together and make it bottom position.
See example below
<div style="postion:absolute; bottom: 0">
<Card.Text>Created on {moment(bulletin.createdDate).format("DD/MM/YYYY")} <br />By {bulletin.createdBy}</Card.Text>
<Card.Text>{bulletin.viewCount > 1 ? <>{bulletin.viewCount} Views</> : <>{bulletin.viewCount} View</>}</Card.Text>
</div>

Resources