Center component vertically and horizontally in 'Antd' library not working - css

I have a weird issue where I can't center a Row vertically in my screen.
The Card I have is perfectally centered horizontally but not vertically. How can I do that ?
I used the Row component which has align='middle' to align vertically but it isn't working
Here is my implementation:
<Row align='middle' justify='center'>
<Col span={18}>
<Card>
{/* <img className='login-logo' src={logo} alt='logo' /> */}
<Text size='30px' type='BOLD'>
QIFF DASHBOARD
</Text>
<Form name='login' onFinish={formik.handleSubmit}>
<Form.Item>
<Input
id='email'
name='email'
label='Email'
placeholder='Email'
type='email'
autoComplete='new-email'
onBlur={formik.handleBlur}
onChange={formik.handleChange}
value={formik.values.email}
/>
{formik.errors.email ? <Text>{formik.errors.email}</Text> : null}
</Form.Item>
<Form.Item>
<Input
id='password'
name='password'
placeholder='Password'
type='password'
autoComplete='new-password'
onBlur={formik.handleBlur}
onChange={formik.handleChange}
value={formik.values.password}
/>
{formik.errors.password ? <Text>{formik.errors.password}</Text> : null}
</Form.Item>
<div className='submit-button'>
<Button
htmlType='submit'
onClick={formik.handleSubmit}
style={{ width: '100%', backgroundColor: '#f85940', color: 'white' }}
>
Sign in
</Button>
</div>
</Form>
</Card>
</Col>
</Row>

<Row align='middle' justify='center'></Row>
Presumably, the Row tag does not set the attribute display:flex
So justify='center' is invalid
The following solutions can be used:
1、Child element (Need to be centered vertically)
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
2、 <Row style="display:flex"></Row>

Related

How to get text span to be on top of flex-direction rows

I am creating a form and have the following so far:
I need the inputs Name to be on top and then Start Date and End Date to be in the rows below which is fine. However I want the text for "Start Date" and "End Date" to be on top of the input fields rather than within the row. I am not sure how to achieve this and have tried using flexDirection as follows:
<span>Name </span>
<Input
type="text"
onChange={(e) => setName(e.target.value)}
value={name}
/>
<div style={{ flexDirection: "row", display: "flex" }}>
<span
style={{
flexDirection: "column",
display: "flex",
}}
>
Start Date
</span>
<Input
style={{ flexDirection: "row", width: "45%", flexWrap: "wrap" }}
type="text"
onChange={(e) => setStartDate(e.target.value)}
value={startDate}
/>
<span>End Date</span>
<Input
style={{ flexDirection: "row", width: "45%", flexWrap: "wrap" }}
type="text"
onChange={(e) => setEndDate(e.target.value)}
value={endDate}
/>
</div>
One solution is wrap each span and input into a div and give each span display: block
It would be better to use classes instead of using inline style.
<span>Name </span>
<Input
type="text"
onChange={(e) => setName(e.target.value)}
value={name}
/>
<div style={{ flexDirection: "row", display: "flex" }}>
<div>
<span
style={{ display: 'block' }}
>
Start Date
</span>
<Input
style={{ flexDirection: "row", width: "45%", flexWrap: "wrap" }}
type="text"
onChange={(e) => setStartDate(e.target.value)}
value={startDate}
/>
</div>
<div>
<span style={{ display: 'block' }}>End Date</span>
<Input
style={{ flexDirection: "row", width: "45%", flexWrap: "wrap" }}
type="text"
onChange={(e) => setEndDate(e.target.value)}
value={endDate}
/>
</div>
</div>

React-bootstrap how to horizontally center an element only taking account of it's parent

Hello, I am using react-bootstrap library. I want to center the search bar and button on the navbar. But while centering, it's also taking into account the sibling element (logo) and leaning towards the right of the page. I don't want to solve this by applying brute force minus margin-left if possible. I'm confused with these justify-content stuff. Thanks in advance.
<Navbar style={{ backgroundColor: "#D3D3D3" }}>
<Nav className="d-flex justify-content-start">
<NavbarBrand href="/">
<img src={logo} alt="pokedex-logo" style={{ width: 250, marginLeft: 20 }} />
</NavbarBrand>
</Nav>
<Nav className="d-flex flex-grow-1 justify-content-center">
<Form className="d-inline-flex">
<FormControl
type="search"
placeholder="Type pokemon name"
value={search}
onChange={e => setSearch(e.target.value)}
/>
<Button
style={{ backgroundColor: "#ffe031", color: "#396bba" }}
onClick={() => history.push(`/pokemon/${search}`)}
>
<strong>Search</strong>
</Button>
</Form>
</Nav>
</Navbar>
Try it like this:
<Navbar style={{ backgroundColor: "#D3D3D3", display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'start' }}>
<NavbarBrand href="/">
<img src={logo} alt="pokedex-logo" style={{ width: 250 }} />
</NavbarBrand>
<Form className="" style={{ margin: 'auto' }}>
<FormControl type="search" placeholder="Type pokemon name" value={search} onChange={e => setSearch(e.target.value)}/>
<Button style={{ backgroundColor: "#ffe031", color: "#396bba" }} onClick={() => history.push(`/pokemon/${search}`)}>
<strong>Search</strong>
</Button>
</Form>
</Navbar>
Here, the Navbar has display flex and the flex direction is row so the logo along with the 2 search elements (text field and search button) will be shown in a row. Then alignItems:'center' centers these 3 elements vertically in the Navbar and the justifyContent: 'start' moves the 3 elements to the start of the Navbar horizontally.
After this, to make the 2 elements of search come in the center, margin:'auto' is used in the Form.
And here's another way: (if the above code doesn't correctly align the search elements in center)
<Navbar style={{ position: 'relative', display: 'flex', backgroundColor: "#D3D3D3" }}>
<NavbarBrand href="/" style={{float: 'left'}}>
<img src={logo} alt="pokedex-logo" style={{ width: 250 }} />
</NavbarBrand>
<Form style={{ float: 'none', position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)' }}>
<FormControl type="search" placeholder="Type pokemon name" value={search} onChange={e => setSearch(e.target.value)} />
<Button style={{ backgroundColor: "#ffe031", color: "#396bba" }} onClick={() => history.push(`/pokemon/${search}`)}>
<strong>Search</strong>
</Button>
</Form>
</Navbar>
In this code, the CSS properties of position and float are used. float: 'left' moves the logo to the left and the search elements have float: 'none'. The search elements use the position property to align to the center of the Navbar which has position: 'relative'.

Can't set height of textarea in reactstrap card

I'm using reactstrap in my project I have a simple card and I want to place a text area inside it using the following code:
<Card>
<CardBody>
<Form>
<FormGroup>
<Input
type="textarea"
defaultValue="Hello world"
/>
</FormGroup>
</Form>
</CardBody>
</Card>
If I then try to set the height it just doesn't work. I have tried using style:
<Card>
<CardBody>
<Form>
<FormGroup>
<Input
type="textarea"
defaultValue={tweet.full_text}
style={{ height: 220 }}
/>
</FormGroup>
</Form>
</CardBody>
</Card>
And also tried using a custom css class:
.text-area-custom {
height: 220px;
}
<Card>
<CardBody>
<Form>
<FormGroup>
<Input
className="text-area-custom"
type="textarea"
defaultValue={tweet.full_text}
/>
</FormGroup>
</Form>
</CardBody>
</Card>
Neither of these methods work. Any idea what I am doing wrong here?
Try and set the rows attribute
<Card>
<CardBody>
<Form>
<FormGroup>
<Input
type="textarea"
defaultValue={tweet.full_text}
rows="5"
/>
</FormGroup>
</Form>
</CardBody>
</Card>

React : Vertically centre Bootstrap Container component on screen

I have a login form using the Container component from Bootstrap. How do I vertically align the entire container in the center of the screen?
function Auth() {
return (
<Container >
<Row className="justify-content-center">
<Col sm={6}>
<Card >
<Card.Body>
<Form>
<Form.Group controlId="formBasicEmail">
<Form.Label>Username</Form.Label>
<Form.Control type="text" placeholder="Username" />
</Form.Group>
<Form.Group controlId="formBasicPassword">
<Form.Label>Password</Form.Label>
<Form.Control type="password" placeholder="Password" />
</Form.Group>
<Form.Group controlId="formBasicCheckbox">
<Form.Check type="checkbox" label="Check me out" />
</Form.Group>
<Button variant="primary" type="submit">
Login
</Button>
</Form>
</Card.Body>
</Card>
</Col>
</Row>
</Container>
);
}

How to center SearchBar with one button on header NativeBase

Trying to center the searchbar with menu button in right on header using nativebase. Code has been given below:
<Header>
<Body>
<Button style={styles.searchButton} onPress={() => this.onSearchPress()} >
<Text>Search Service</Text>
</Button>
</Body>
<Right>
<Icon name='menu' style={styles.drawerIcon} onPress={()=> navigation.openDrawer()} />
</Right>
</Header>
How to position it center with full width?
<Header>
<Left/>
<Body>
<Button style={styles.searchButton} onPress={() => this.onSearchPress()} >
<Text>Search Service</Text>
</Button>
</Body>
<Right>
<Icon name='menu' style={styles.drawerIcon} onPress={()=> navigation.openDrawer()} />
</Right>
</Header>

Resources