React-Bootstrap Rows/Columns are not the full width of the screen (Only 50% of it)

Liam G picture Liam G · Aug 21, 2019 · Viewed 11.3k times · Source

My experience with CSS and Bootstrap, when it comes to positioning and resizing elements on the screen, has been bloody awful ever since I started writing HTML. All I am trying to do (in my React project) is position 2 elements on the screen side by side, the element on the left needs to be 80% of the screens width and the element on the right has to be 20% of the screens width. Obviously for bootstrap to be able to do this, it requires 4 weeks writing code, 20+ stack overflow questions and 1 human sacrifice.

Below is the main app component with a <React.Fragment/> tag and a <Container/> with <Row/> and <Col/> inside that:

import React, { Component } from "react";
import { Container, Row, Col } from "react-bootstrap";

class App extends Component {
  state = {};
  render() {
    return (
      <React.Fragment>
        <Container>
          <Row>
            <Col>Hello</Col>
            <Col>Hello2</Col>
          </Row>
        </Container>
      </React.Fragment>
    );
  }
}

export default MainContent;

Expected result? Hello will be on the left of screen and Hello2 will be on the right of screen smack bang in the middle. Just as the docs suggest here.

But of course that doesn't happen. The row is centered in the middle of the screen and is about half of the screen size in total width.

To illustrate: 25% Whitespace | Hello1 | Hello2 | 25% Whitespace

Honestly, I have no idea what to do. Setting the margins to 0 will put it on the left of screen but it is still not the full width.

Someone please help me, I've had enough :(

Answer

ravibagul91 picture ravibagul91 · Aug 21, 2019

By default in bootstrap, container width and max-width is set to some px like,

@media (min-width: 768px)
.container {
    max-width: 720px;
}

@media (min-width: 576px)
.container {
    max-width: 540px;
} 

.container {
    width: 100%;
    padding-right: 15px;
    padding-left: 15px;
    margin-right: auto;
    margin-left: auto;
}

You have to set max-width: 100% like,

.container {
  max-width: '100%'  //This will make container to take screen width
}

Or, Another way is, you can add fluid prop to Container,

fluid - Allow the Container to fill all of it's availble horizontal space.

<Container fluid>
  ...
</Container>

Note: Here Col will take by default equal space.

The Col lets you specify column widths across 5 breakpoint sizes (xs, sm, md, large, and xl).

For example,

<Row>
    <Col xs={6} md={8}>
      xs=6 (6 parts of screen on small screen) md=8 (8 parts of screen on medium screen)
    </Col>
    <Col xs={6} md={4}>
      xs=6 md=4
    </Col>
</Row>