React components positioning - css

I'm trying to position the react components and having hard time figuring out with CSS flexbox.
I have 3 react components as below
function App() {
return (
<>
<div className = "header">
<h1>Connect cruAMS and Security Center</h1>
</div>
<br></br>
<div className="App">
<Cruams />
<SecurityCenter />
<UserContainer />
</div>
</>
);
}
Currently it's location is like below.
Cruams SecurityCenter UserContainer
What I want is
Cruams SecurityCenter
UserContainer
How can I do this using CSS flexbox? Or is there a different way?
Thanks in advance!

If you don't want to make your css too complicated, a simple top margin can help you here:
.App {
display: flex;
justify-content: center;
}
.cruams {
display: inline-block;
}
.securityCenter{
display: inline-block;
margin-top: 20px;
}
.userContainer {
display: inline-block;
}
<div className = "header">
<h1>Connect cruAMS and Security Center</h1>
</div>
<br></br>
<div class="App">
<div class="cruams">cruams</div>
<div class="securityCenter">securityCenter</div>
<div class="userContainer">userContainer</div>
</div>

Without using any extra divs:
<div className="container">
<Cruams className="half-width" />
<SecurityCenter className="half-width"/>
<UserContainer className="full-width" />
</div>
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.half-width {
flex-basis: 50%
}
.full-width {
flex-basis: 100%
}

Related

How do I move one item under another in Flexbox? I am trying to get the price under the annual plan

I am trying to get the price under the annual plan.
.paymentsection {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
gap: 2rem;
}
<div class="paymentsection">
<img src="/images/icon-music.svg" alt="Music" class="icon-img" />
<div class="plan">Annual Plan</div>
<div class="price">$59.99/year</div>
</div>
Change
final image
The .plan and .price needs to be inside a div and then if display flex is applied to the paymentsection, you get your desired result.
<div class="paymentsection">
<img src="/images/icon-music.svg" alt="Music" class="icon-img" />
<div class="plan-container">
<div class="plan">Annual Plan</div>
<div class="price">$59.99/year</div>
</div>
Change
</div>
.paymentsection {
display: flex;
flex-direction: row;
}
.plan-container{
flex-grow:1;
}
You need different flex boxes for implementation your image. Each flex box should have different direction. first you need to wrap your .plan div and .price div with another div. This help you to separate pricing component:
<div class="paymentsection">
<img src="/images/icon-music.svg" alt="Music" class="icon-img" />
<div class="plan-wrapper">
<div class="plan">Annual Plan</div>
<div class="price">$59.99/year</div>
</div>
</div>
Change
next you need to set flex direction to your new div element. this is your desire style:
.paymentsection {
display: flex;
}
.plan-wrapper{
display: flex;
flex-direction: column; /* default is row */
}
this changes should fix your issue.
To have the pricing aligned below the text, you'd have to nest the div further and specify the styles of the nested div to be oriented vertically, i.e, flex-direction: column.
You could do something like this:
<div class="paymentsection">
<img src="/images/icon-music.svg" alt="Music" class="icon-img" />
<div class="pricing">
<div class="plan">Annual Plan</div>
<div class="price">$59.99/year</div>
</div>
</div>
Change
Styles for pricing class:
.pricing{
display:flex;
flex-direction: column;
}

Issue with Angular flex

I have got a basic flexbox grid. If I move the ngFor loop into a component (<comp-ngfor-testGrid></comp-ngfor-testGrid>) the flex is broken.
The <comp-ngfor-testGrid> on the DOM causes the issue. I would like to remove it and keep only the content of the component <comp-ngfor-testGrid>.
:host {} is not the solution because the <comp-ngfor-testGrid> still exist...
.grid {
display: flex;
justify-content: space-between;
width: 100%;
flex-wrap: wrap;
div {
width: calc(50% - 10px);
margin-bottom: 15px;
}
}
testGrid = [4,5,6];
<div class="grid">
<div> 1 </div>
<div> 2 </div>
<div> 3 </div>
<div *ngFor="let item of testGrid ">
{{item}}
</div>
<comp-ngfor-testGrid></comp-ngfor-testGrid>
</div>

How can I align my items two items per row using flexbox?

I want to align my flex items (two items per row)
This image show what I want :
https://ibb.co/rFLXP5P
and this is what I did (my result for now) :
https://ibb.co/yB3Sftv
I want them two items in the center per row , as the two first rows ( search case and the button case).
here's my code.
JSX code :
render() {
return <div className="employees-container">
<div className='employee-search'>
<label for="name">Search Employees</label>
<input placeholder='Name...' onChange={(e) => this.setState({ name: e.target.value })} id="name"/>
</div>
<Link to="/add/employee">Add Employee</Link>
{this.state.employees
.filter(x => new RegExp(this.state.name, 'i').test(x.name))
.map(x=><EmployeeCard data={x}></EmployeeCard>)
}
</div>;
}
Sass code :
.employees-container{
display: flex;
display: -ms-flexbox;
display: -webkit-flex;
flex-wrap: wrap;
-ms-flex-wrap: wrap;
justify-content: center;
}
>*{
height: 70px;
margin:5px;
width:300px!important;
}
}
Suppose this is your render structure
HTML
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
and CSS
.item {
width: 100%
}
.container {
display: flex;
flex-wrap: wrap;
}
.container>div {
flex: 0 50%;
/*demo*/
border: red solid;
box-sizing:border-box
}
No match these properties whit your own code may be that will help you

How to fix buttons above footer using flex

I am using Materializecss to create React app. I have already applied flex inside my app class. And now I want to apply flex inside main tag where buttons get fixed at bottom of main tag & above the footer.
I have tried {margin-top: auto;} , justify-content: flex-end which didn't help. The buttons always print after content class. I can set the height of the content class, but small devices render view badly and it's not fixing my problem.
JSX code:
<div className="app">
<header> <header>
<main>
<div className="box">
<div className="content"> Long text less than 100 words <div>
<div className="buttons"> <button> Button-1 </button> <button> Button-2 </button>
<div>
<main>
<footer><footer>
<div>
My css
app {
display: flex;
min-height: 100vh;
flex-direction: column;
}
main {
flex: 1 0 auto;
}
I want to stick my button above the footer. My content class has 100 words then the button should stick above the footer not rendered after the content class.
I would appreciate the help.
You haven't applied display:flex etc to the main element. If you do that the margin-top:auto will work.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
::before,
::after {
box-sizing: inherit;
}
.app {
display: flex;
min-height: 100vh;
flex-direction: column;
background: ;
}
main {
flex: 1;
display: flex;
flex-direction: column;
}
.buttons {
margin-top: auto;
}
<div class="app">
<header>header </header>
<main>
<div class="box">box</div>
<div class="content"> Long text less than 100 words </div>
<div class="buttons">
<button> Button-1 </button>
<button> Button-2 </button>
</div>
</main>
<footer>footer</footer>
</div>

Bootstrap 3 container with justify-content: space-between not working properly

I am trying to apply flex to bootstrap 3 container and found strange behaviour of child elements Here is the code.
<div class="container">
<div class="child">One</div>
<div class="child">Two</div>
</div>
.container {
display: flex;
justify-content: space-between;
}
(see example on jsfiddle JSFiddle)
I cannot find out what's the problem with the space-between.
Thank you for the help.
Combining Bootstrap 3 with Flexbox is not recommended.
That said, Bootstraps container class also uses both the pseudo elements, hence they will participate in the flex container being flex items, and here one can see them when giving them a small width/height and background color.
And as you can see, the justify-content: space-between; work as it is supposed to.
.container {
display: flex;
justify-content: space-between;
}
.container::before, .container::after {
width: 30px;
height: 30px;
background: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="child">One</div>
<div class="child">Two</div>
</div>
One workaround would be a wrapper
.container .flex {
display: flex;
justify-content: space-between;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="flex">
<div class="child">One</div>
<div class="child">Two</div>
</div>
</div>
I think the issue is that you're using a classname from bootstrap .container you're attempting to change the style. Just use a id instead of the classname container.
#main {
display: flex;
justify-content: space-between;
}
<div id="main">
<div class="child">One</div>
<div class="child">Two</div>
</div>

Resources