カード
Bootstrapのカードは、複数のバリアントとオプションを備えた、柔軟で拡張可能なコンテンツコンテナを提供します。
基本例
import Button from 'react-bootstrap/Button';import Card from 'react-bootstrap/Card';function BasicExample() {return (<Card style={{ width: '18rem' }}><Card.Img variant="top" src="holder.js/100px180" /><Card.Body><Card.Title>Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text><Button variant="primary">Go somewhere</Button></Card.Body></Card>);}export default BasicExample;
コンテンツタイプ
本文
<Card.Body>
を使用して、<Card>
内のコンテンツにパディングを追加します。
import Card from 'react-bootstrap/Card';function BodyOnlyExample() {return (<Card><Card.Body>This is some text within a card body.</Card.Body></Card>);}export default BodyOnlyExample;
あるいは、本文のみで他の子を持たないカードには、この省略記法を使用できます。
import Card from 'react-bootstrap/Card';function BodyShorthandExample() {return <Card body>This is some text within a card body.</Card>;}export default BodyShorthandExample;
タイトル、テキスト、リンク
<Card.Body>
内に<Card.Title>
、<Card.Subtitle>
、<Card.Text>
を使用すると、それらがきれいに整列します。<Card.Link>
は、リンクを互いに並べるために使用されます。
<Card.Text>
はコンテンツの周囲に<p>
タグを出力するため、複数の<Card.Text>
を使用して別々の段落を作成できます。
import Card from 'react-bootstrap/Card';function TextExample() {return (<Card style={{ width: '18rem' }}><Card.Body><Card.Title>Card Title</Card.Title><Card.Subtitle className="mb-2 text-muted">Card Subtitle</Card.Subtitle><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text><Card.Link href="#">Card Link</Card.Link><Card.Link href="#">Another Link</Card.Link></Card.Body></Card>);}export default TextExample;
リストグループ
フラッシュリストグループを使用して、カード内にコンテンツのリストを作成します。
import Card from 'react-bootstrap/Card';import ListGroup from 'react-bootstrap/ListGroup';function ListGroupExample() {return (<Card style={{ width: '18rem' }}><ListGroup variant="flush"><ListGroup.Item>Cras justo odio</ListGroup.Item><ListGroup.Item>Dapibus ac facilisis in</ListGroup.Item><ListGroup.Item>Vestibulum at eros</ListGroup.Item></ListGroup></Card>);}export default ListGroupExample;
import Card from 'react-bootstrap/Card';import ListGroup from 'react-bootstrap/ListGroup';function ListGroupWithHeaderExample() {return (<Card style={{ width: '18rem' }}><Card.Header>Featured</Card.Header><ListGroup variant="flush"><ListGroup.Item>Cras justo odio</ListGroup.Item><ListGroup.Item>Dapibus ac facilisis in</ListGroup.Item><ListGroup.Item>Vestibulum at eros</ListGroup.Item></ListGroup></Card>);}export default ListGroupWithHeaderExample;
キッチンスインク
import Card from 'react-bootstrap/Card';import ListGroup from 'react-bootstrap/ListGroup';function KitchenSinkExample() {return (<Card style={{ width: '18rem' }}><Card.Img variant="top" src="holder.js/100px180?text=Image cap" /><Card.Body><Card.Title>Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body><ListGroup className="list-group-flush"><ListGroup.Item>Cras justo odio</ListGroup.Item><ListGroup.Item>Dapibus ac facilisis in</ListGroup.Item><ListGroup.Item>Vestibulum at eros</ListGroup.Item></ListGroup><Card.Body><Card.Link href="#">Card Link</Card.Link><Card.Link href="#">Another Link</Card.Link></Card.Body></Card>);}export default KitchenSinkExample;
ヘッダーとフッター
<Card.Header>
コンポーネントを追加することで、ヘッダーを追加できます。
import Button from 'react-bootstrap/Button';import Card from 'react-bootstrap/Card';function WithHeaderExample() {return (<Card><Card.Header>Featured</Card.Header><Card.Body><Card.Title>Special title treatment</Card.Title><Card.Text>With supporting text below as a natural lead-in to additional content.</Card.Text><Button variant="primary">Go somewhere</Button></Card.Body></Card>);}export default WithHeaderExample;
<CardHeader>
は、<as>
プロップを通して見出し要素を渡すことでスタイルを設定できます。
import Button from 'react-bootstrap/Button';import Card from 'react-bootstrap/Card';function WithHeaderStyledExample() {return (<Card><Card.Header as="h5">Featured</Card.Header><Card.Body><Card.Title>Special title treatment</Card.Title><Card.Text>With supporting text below as a natural lead-in to additional content.</Card.Text><Button variant="primary">Go somewhere</Button></Card.Body></Card>);}export default WithHeaderStyledExample;
import Card from 'react-bootstrap/Card';function WithHeaderAndQuoteExample() {return (<Card><Card.Header>Quote</Card.Header><Card.Body><blockquote className="blockquote mb-0"><p>{' '}Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integerposuere erat a ante.{' '}</p><footer className="blockquote-footer">Someone famous in <cite title="Source Title">Source Title</cite></footer></blockquote></Card.Body></Card>);}export default WithHeaderAndQuoteExample;
import Button from 'react-bootstrap/Button';import Card from 'react-bootstrap/Card';function HeaderAndFooterExample() {return (<Card className="text-center"><Card.Header>Featured</Card.Header><Card.Body><Card.Title>Special title treatment</Card.Title><Card.Text>With supporting text below as a natural lead-in to additional content.</Card.Text><Button variant="primary">Go somewhere</Button></Card.Body><Card.Footer className="text-muted">2 days ago</Card.Footer></Card>);}export default HeaderAndFooterExample;
画像
カードには、画像を操作するためのいくつかのオプションがあります。カードの両端に「画像キャップ」を追加したり、画像をカードコンテンツに重ね合わせたり、画像をカードに単純に埋め込んだりするなど、いくつかの方法があります。
画像キャップ
ヘッダーとフッターと同様に、カードには、カードの上部または下部に画像を追加できる「画像キャップ」を含めることができます。
import Card from 'react-bootstrap/Card';function ImageAndTextExample() {return (<><Card><Card.Img variant="top" src="holder.js/100px180" /><Card.Body><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card><Card.Body><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body><Card.Img variant="bottom" src="holder.js/100px180" /></Card></>);}export default ImageAndTextExample;
画像オーバーレイ
画像をカードの背景に変え、カードのテキストを重ねます。画像によっては、追加のスタイルやユーティリティが必要になる場合があります。
import Card from 'react-bootstrap/Card';function ImgOverlayExample() {return (<Card className="bg-dark text-white"><Card.Img src="holder.js/100px270" alt="Card image" /><Card.ImgOverlay><Card.Title>Card title</Card.Title><Card.Text>This is a wider card with supporting text below as a natural lead-into additional content. This content is a little bit longer.</Card.Text><Card.Text>Last updated 3 mins ago</Card.Text></Card.ImgOverlay></Card>);}export default ImgOverlayExample;
ナビゲーション
React BootstrapのNavコンポーネントを使用して、カードのヘッダー(またはブロック)にナビゲーションを追加します。
import Button from 'react-bootstrap/Button';import Card from 'react-bootstrap/Card';import Nav from 'react-bootstrap/Nav';function NavTabsExample() {return (<Card><Card.Header><Nav variant="tabs" defaultActiveKey="#first"><Nav.Item><Nav.Link href="#first">Active</Nav.Link></Nav.Item><Nav.Item><Nav.Link href="#link">Link</Nav.Link></Nav.Item><Nav.Item><Nav.Link href="#disabled" disabled>Disabled</Nav.Link></Nav.Item></Nav></Card.Header><Card.Body><Card.Title>Special title treatment</Card.Title><Card.Text>With supporting text below as a natural lead-in to additional content.</Card.Text><Button variant="primary">Go somewhere</Button></Card.Body></Card>);}export default NavTabsExample;
import Button from 'react-bootstrap/Button';import Card from 'react-bootstrap/Card';import Nav from 'react-bootstrap/Nav';function NavPillsExample() {return (<Card><Card.Header><Nav variant="pills" defaultActiveKey="#first"><Nav.Item><Nav.Link href="#first">Active</Nav.Link></Nav.Item><Nav.Item><Nav.Link href="#link">Link</Nav.Link></Nav.Item><Nav.Item><Nav.Link href="#disabled" disabled>Disabled</Nav.Link></Nav.Item></Nav></Card.Header><Card.Body><Card.Title>Special title treatment</Card.Title><Card.Text>With supporting text below as a natural lead-in to additional content.</Card.Text><Button variant="primary">Go somewhere</Button></Card.Body></Card>);}export default NavPillsExample;
カードスタイル
背景色
<bg>
と<text>
プロップを変更することで、カードの外観を変更できます。
import Card from 'react-bootstrap/Card';function BgColorExample() {return (<>{['Primary','Secondary','Success','Danger','Warning','Info','Light','Dark',].map((variant) => (<Cardbg={variant.toLowerCase()}key={variant}text={variant.toLowerCase() === 'light' ? 'dark' : 'white'}style={{ width: '18rem' }}className="mb-2"><Card.Header>Header</Card.Header><Card.Body><Card.Title>{variant} Card Title </Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card>))}</>);}export default BgColorExample;
枠線の色
import Card from 'react-bootstrap/Card';function BorderExample() {return (<><Card border="primary" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Primary Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card border="secondary" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Secondary Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card border="success" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Success Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card border="danger" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Danger Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card border="warning" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Warning Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card border="info" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Info Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card border="dark" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Dark Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /><Card border="light" style={{ width: '18rem' }}><Card.Header>Header</Card.Header><Card.Body><Card.Title>Light Card Title</Card.Title><Card.Text>Some quick example text to build on the card title and make up thebulk of the card's content.</Card.Text></Card.Body></Card><br /></>);}export default BorderExample;
カードレイアウト
カードグループ
import Card from 'react-bootstrap/Card';import CardGroup from 'react-bootstrap/CardGroup';function GroupExample() {return (<CardGroup><Card><Card.Img variant="top" src="holder.js/100px160" /><Card.Body><Card.Title>Card title</Card.Title><Card.Text>This is a wider card with supporting text below as a natural lead-into additional content. This content is a little bit longer.</Card.Text></Card.Body><Card.Footer><small className="text-muted">Last updated 3 mins ago</small></Card.Footer></Card><Card><Card.Img variant="top" src="holder.js/100px160" /><Card.Body><Card.Title>Card title</Card.Title><Card.Text>This card has supporting text below as a natural lead-in toadditional content.{' '}</Card.Text></Card.Body><Card.Footer><small className="text-muted">Last updated 3 mins ago</small></Card.Footer></Card><Card><Card.Img variant="top" src="holder.js/100px160" /><Card.Body><Card.Title>Card title</Card.Title><Card.Text>This is a wider card with supporting text below as a natural lead-into additional content. This card has even longer content than thefirst to show that equal height action.</Card.Text></Card.Body><Card.Footer><small className="text-muted">Last updated 3 mins ago</small></Card.Footer></Card></CardGroup>);}export default GroupExample;
グリッドカード
Row
のグリッドカラムプロップを使用して、行ごとに表示するカードの数を制御します。
import Card from 'react-bootstrap/Card';import Col from 'react-bootstrap/Col';import Row from 'react-bootstrap/Row';function GridExample() {return (<Row xs={1} md={2} className="g-4">{Array.from({ length: 4 }).map((_, idx) => (<Col key={idx}><Card><Card.Img variant="top" src="holder.js/100px160" /><Card.Body><Card.Title>Card title</Card.Title><Card.Text>This is a longer card with supporting text below as a naturallead-in to additional content. This content is a little bitlonger.</Card.Text></Card.Body></Card></Col>))}</Row>);}export default GridExample;