how to give button a button right alignment in bootstrap? - css

I'm trying to align two buttons at the right corner of a row but no matter what method I use it shows the same result every time here's the code for your reference:
import React from "react";
import bootstrap from "bootstrap";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
const AdminPanel = () => {
const users = [{ name: "Talha" }, { name: "Ali" }];
return (
<Container className="pt-5">
<Row className="d-flex" style={{ height: "70vh" }}>
<Col className="shadow-lg bg-white rounded mx-5">
{users.map((user) => (
<Row
className="shadow my-3 mx-2 py-2 px-2 rounded font-weight-bold"
style={{ height: "7vh" }}
>
{user.name}
<div className="btn-group">
<a href="#">
<button className="btn btn-primary">Edit</button>
</a>
<a href="#">
<button className="btn btn-danger">Delete</button>
</a>
</div>
</Row>
))}
</Col>
<Col className="shadow-lg bg-white rounded mx-5">
<h1>
list
</h1>
</Col>
</Row>
</Container>
);
};
export default AdminPanel;
the result I'm getting is this:
Any help would be appreciated!!!!!

Add class: ml-auto (= margin-left:auto !important) into:
<div className="btn-group ml-auto">
As this div is in a div.row that has the property display:flex; applied, it should push it to the right automaticly

Add
className="float-right"
to the div wrapping the buttons

You can add each block of child Element of your Row in a Col Component and add the justify-content-between class to the Row Component like this
<Col className="shadow-lg bg-white rounded mx-5">
{users.map((user) => (
<Row
className="justify-content-between shadow my-3 mx-2 py-2 px-2 rounded font-weight-bold"
style={{ height: "7vh" }}
>
<Col>
{user.name}
</Col>
<Col>
<div className="btn-group">
<a href="#">
<button className="btn btn-primary">Edit</button>
</a>
<a href="#">
<button className="btn btn-danger">Delete</button>
</a>
</div>
</Col>
</Row>
))}
</Col>

Related

Why is z-index not working even after providing the position and the index

I wanted to place an absolute element behind a relative element. I have mentioned the z-index and also the position for the respective elements but its still not working.
This is what i had tried. I am using tailwind css.
const Searchbar: React.FC<SearchbarProps> = ({}) => {
const [toggle, setToggle] = useState(false);
const [dropdown, setDropdownToggle] = useState(false);
return (
<form className={`search flex items-center basis-9/12 pl-5 z-20 relative ${toggle ? 'dark': ''}`} >
{/* ICON */}
<svg className="ico-svg" viewBox="0 0 20 20" width="20">
<path d="M20,8c0-4.4-3.6-8-8-8C7.6,0,4,3.6,4,8c0,1.9,0.6,3.6,1.7,4.9L0,18.6L1.4,20l7.1-7.1l-0.2-0.2 C6.9,11.6,6,9.9,6,8c0-3.3,2.7-6,6-6c3.3,0,6,2.7,6,6c0,3.3-2.7,6-6,6c-0.7,0-1.3-0.1-1.9-0.3L10,13.7l-1.5,1.5l0.4,0.2 c1,0.4,2.1,0.7,3.2,0.7C16.4,16,20,12.4,20,8z"></path>
</svg>
{/* INPUT */}
<input className='basis-9/12 pl-3 h-[48px] text-sm font-normal self-stretch'
onClick={() => setToggle(!toggle)}
type="text" name="search" id="search" placeholder='Search websites, elements, courses...'/>
<div className="dropdown flex items-center justify-end basis-3/12">
{/* TEXT */}
<p className='uppercase mr-9 font-light text-xs'>in</p>
{/* DROPDOWN */}
<button className={`flex items-center h-[48px] justify-between basis-6/12 px-5 ${dropdown ? 'active': ''}`} onClick={(e) => {
e.preventDefault();
setDropdownToggle(!dropdown)
}
}>
<p className="capitalize text-sm font-medium">inspiration</p>
<svg className="dropdown__arrow" viewBox="0 0 20 20" width="12">
<path d="M1.6,4.1c-0.4,0-0.9,0.2-1.2,0.5c-0.7,0.7-0.7,1.7,0,2.4l8.4,8.4c0.7,0.7,1.7,0.7,2.4,0L19.5,7 c0.7-0.7,0.7-1.7,0-2.4c-0.7-0.7-1.7-0.7-2.4,0L10,11.8L2.8,4.6C2.5,4.3,2.1,4.1,1.6,4.1z"></path>
</svg>
</button>
{/* DROPDOWN LIST */}
<div className={`dropdown__list capitalize px-5 pt-2 pb-5 text-sm font-normal ${dropdown ? 'active' : ''}`}>
<ul>
<li className='pt-3'>Inspiration</li>
<li className='pt-3 pl-3'>websites</li>
<li className='pt-3 pl-3'>elements</li>
<li className='pt-3 pl-3'>collections</li>
<li className='pt-3'>blog</li>
<li className='pt-3'>courses</li>
<li className='pt-3'>directory</li>
</ul>
</div>
</div>
{/* SUGGESTIONS */}
<div className="search__suggestions z-10 absolute">
</div>
</form>
);
}
This is how its behaving:
Actual behaviour
Instead of this:
Expected Behaviour
As you can see i have a form tag that is relative places and i also have a div with the class "search__suggestions" that is placed absolutely. I have also provided the appropriate index to the tags, but for some reason its not working. Anyone knows why its behaving this way?

How to align react Boostrap cards

I have cards with different images and texts that make them bigger/smaller, I couldn't find any React-Bootstrap specific answer, but every css and normal Bootstrap alternative that i tried didn't work. The only thing that put a patch in the problem is setting a max height in the cards, but still the cards that have a larger image or text have their image higher or lower and is really bad for rows that have some medium sized images and one larger one. For example:
I tried adapting the top three answers of this answer: Link and it didn't work, for example using d-flex align-items-stretch
import React from "react";
import Product from "./product";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
const Flexbox = (props) => {
return (
<Container>
<Row>
<Col className="d-flex align-items-stretch">
<Product product={props.productList[0]}></Product>
</Col>
<Col className="d-flex align-items-stretch">
<Product product={props.productList[1]}></Product>
</Col>
<Col className="d-flex align-items-stretch">
<Product product={props.productList[2]}></Product>
</Col>
</Row>
</Container>
);
};
export default Flexbox;
The result is every card having their own size.
Using h-100 nothing changes, container code:
import React from "react";
import Product from "./product";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
const Flexbox = (props) => {
return (
<Container fluid className="content-row">
<Row>
<Col>
<Product product={props.productList[0]}></Product>
</Col>
<Col>
<Product product={props.productList[1]}></Product>
</Col>
<Col>
<Product product={props.productList[2]}></Product>
</Col>
</Row>
</Container>
);
};
export default Flexbox;
Card code:
import React from "react";
import "../App.css";
import "../stylesheets/product.css";
import Rating from "./rating";
import Card from "react-bootstrap/Card";
const Product = (props) => {
console.log(props.product);
return (
<div>
<Card className="card rounded shadow-sm border-0 h-100">
<Card.Body className="card-body p-4">
<img src={props.product.img} class="img-fluid d-block mx-auto mb-3" />
</Card.Body>
<Card.Footer>
<h5>{props.product.name}</h5>
<Rating></Rating>
</Card.Footer>
</Card>
</div>
);
};
export default Product;
And using d.flex and flex-fill also nothing changes, container:
import React from "react";
import Product from "./product";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
const Flexbox = (props) => {
return (
<Container fluid>
<Row>
<Col className="col-sm d-flex">
<Product product={props.productList[0]}></Product>
</Col>
<Col className="col-sm d-flex">
<Product product={props.productList[1]}></Product>
</Col>
<Col className="col-sm d-flex">
<Product product={props.productList[2]}></Product>
</Col>
</Row>
</Container>
);
};
export default Flexbox;
Card:
import React from "react";
import "../App.css";
import "../stylesheets/product.css";
import Rating from "./rating";
import Card from "react-bootstrap/Card";
const Product = (props) => {
console.log(props.product);
return (
<div>
<Card className="card rounded shadow-sm border-0">
<Card.Body className="p-4 flex-fill">
<img src={props.product.img} class="img-fluid d-block mx-auto mb-3" />
</Card.Body>
<Card.Footer>
<h5>{props.product.name}</h5>
<Rating></Rating>
</Card.Footer>
</Card>
</div>
);
};
export default Product;
I'm losing my mind and i don't know what to do, i tried everything and nothing changes.
I think if you remove the div around the card and add back h-100 as a class it should align:
const Product = (props) => {
console.log(props.product);
return (
<Card className="card rounded shadow-sm border-0 h-100">
<Card.Body className="p-4 flex-fill">
<img src={props.product.img} class="img-fluid d-block mx-auto mb-3" />
</Card.Body>
<Card.Footer>
<h5>{props.product.name}</h5>
<Rating></Rating>
</Card.Footer>
</Card>
);
};
export default Product;

Display Block not moving to next line

I'm trying to make a responsive nav bar with tailwind. I want to list my nav links with a single item in each row when I open the navigation menu. Right now, they are all on one row. I'm using tailwind:
function Navbar() {
const [isOpen, setIsOpen] = useState(false);
return (
<header>
<div className="flex flex-col max-w-screen-xl px-4 mx-auto md:items-center md:justify-between md:flex-row md:px-6 lg:px-8">
<div className="p-4 flex flex-row items-center justify-between">
<Link href="/">
<a className="text-lg font-semibold tracking-widest text-gray-900 uppercase rounded-lg dark-mode:text-white focus:outline-none focus:shadow-outline">
Header
</a>
</Link>
<button
className="md:hidden rounded-lg focus:outline-none focus:shadow-outline"
onClick={() => {
setIsOpen(!isOpen);
}}
>
Menu
</button>
</div>
<nav className={isOpen ? "flex" : "md:flex hidden"}>
<NavLink pathTo="/" displayText="Home" />
<NavLink pathTo="/" displayText="Page 1" />
<NavLink pathTo="/" displayText="Page 2" />
</nav>
</div>
</header>
);
}
export default Navbar;
My NavLink component is as follows:
import React from "react";
import Link from "next/link";
interface NavItem {
pathTo: string;
displayText: string;
}
function NavLink(item: NavItem) {
return (
<Link href={item.pathTo}>
<a className="block lg:p-4 px-4 py-2 mt-2 border-b-2 border-white text-sm bg-transparent dark-mode:bg-transparent dark-mode:hover:border-red-400 dark-mode:focus:border-red-400 dark-mode:focus:text-white dark-mode:text-gray-200 md:mt-0 md:ml-4 hover:text-gray-900 focus:text-gray-900 hover:border-red-400 focus:border-red-400">
{item.displayText}
</a>
</Link>
);
}
export default NavLink;
What am I missing? I want the NavLinks to have one item on each row when opened from the menu. I thought display:block would do it, but it seems not to. Why's that?
You need to add a flex direction to your nav links. Adding flex-col should do the trick.

why are my rows and columns slightly to the left with react-bootstrap?

I'm using react-bootstrap to make my website responsive. I've used react-bootstrap before and I always encounter this problem: when setting my rows and cols they're slightly to the left and not centered, as if there were no margin. I've used class names to center the whole row, but it's just not responding like I want it to. This is my code and there is no css going on yet
const Projects = () => {
return (
<div className='Projects' >
<Row className='mt-5 pt-5 my-auto'>
<Col lg={4}> <img src={image} alt=""/> </Col>
<Col lg={4}> <img src={image} alt=""/> </Col>
<Col lg={4}> <img src={image} alt=""/> </Col>
</Row>
</div>
);
}
Here's what it looks like It's slightly to the left and has some space on the right. Any help is appreciated!
The Bootstrap grid must be wrapped in a <Container>:
const Projects = () => {
return (
<Container className='Projects' >
<Row className='mt-5 pt-5 my-auto'>
<Col lg={4}> <img src={image} alt=""/> </Col>
<Col lg={4}> <img src={image} alt=""/> </Col>
<Col lg={4}> <img src={image} alt=""/> </Col>
</Row>
</Container>
);
}

Align button in a Card Footer to the right in React Bootstrap

I am trying to align a button (And eventually 2 buttons) to the right in a Card.footer in React Bootstrap. i have tried style=={{display: 'flex', display: 'flex', alignItems: 'flex-end', float: 'right'}} and className='btn btn-success btn-lg float-right' and many combinations of these without anything changing.
my card is as follows:
<Card style={{ marginBottom: "15px", marginTop: "15px", cursor: 'pointer' }}>
<ListGroup>
<ListGroup.Item>
<Container>
Card Content
</Container>
</ListGroup.Item>
</ListGroup>
<Card.Footer>
<Row>
<Button className='btn btn-success btn-lg float-right' style={{ display: 'flex', alignItems: 'flex-end', float: 'right' }}>Like</Button>
</Row>
</Card.Footer>
</Card>
Use ml-auto class (margin-left: auto)
<Button className="btn btn-success btn-lg ml-auto">
https://getbootstrap.com/docs/4.5/utilities/spacing/#notation
For subsequent buttons to the right, apply ml-x e.g., ml-2
<Row>
<Button className="btn btn-success btn-lg ml-auto">Like</Button>
<Button className="btn btn-success btn-lg ml-2">Another Btn</Button>
</Row>

Resources