Author avatar

Gaurav Singhal

How to Create Smaller `Input` in React-Bootstrap

Gaurav Singhal

  • May 11, 2020
  • 4 Min read
  • 134 Views
  • May 11, 2020
  • 4 Min read
  • 134 Views
Web Development
Front End Web Development
Client-side Framework
React

Introduction

Styling is a crucial aspect of any web or mobile app. You can't always use the default styles provided by Bootstrap or any other library, but you would want to have a consistent and flexible design across different screens. Fortunately, you can override Bootstrap's default styles using custom CSS. Bootstrap also has additional utility classes that you can use to change the look.

One style change you might want to make is to decrease the size of the input to match with the style guide of your client or the component design by the design team. In this guide, you will learn how to decrease the size of an input in React-Bootstrap.

Decreasing Size with Props

React Bootstrap is a React implementation of the Bootstrap library. React Bootstrap has components that can be customized using props.

To decrease the size of an input, you can use the size prop of the Form.Control component.

First, import the Form component from react-bootstrap.

1
2
3
4
5
6
7
8
9
10
11
12
import React from "react";
import { Container, Form } from "react-bootstrap";

const App = () => {
  return (
    <Container>
      <Form.Control size="sm" name="foo" placeholder="Smaller Input" />
    </Container>
  );
};

export default App;
jsx

The size="sm" prop will decrease the size of the form input field. Another possible value for the size prop is lg, which will increase the size of the field.

Using Custom CSS

The traditional way of styling is by using CSS classes. You can specify additional CSS classes in the className prop of the Form component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React from "react";
import { Container, Form } from "react-bootstrap";

const App = () => {
  return (
    <Container>
      <Form.Control
        className="smaller-input"
        name="foo"
        placeholder="Smaller Input"
      />
    </Container>
  );
};
jsx

Now, add a .smaller-input style rule in your CSS file.

1
2
3
4
.smaller-input {
  font-size: 12px;
  padding: 2px 5px;
}
css

You can also specify inline style using the style prop. The style takes in an object of style rules. Each key in the object is a camel-cased version of a CSS property.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React from "react";
import { Container, Form } from "react-bootstrap";

const App = () => {
  return (
    <Container>
      <Form.Control
        style={{ fontSize: 12, padding: 3 }}
        name="foo"
        placeholder="Smaller Input"
      />
    </Container>
  );
};
jsx

Using Styled Components

For some developers, writing camel-cased CSS properties might be a little uncomfortable. Generally, developers are used to traditionally writing CSS. That's where styled-components come in the picture.

Styled components allow you to write actual CSS code to style your components. They utilize tagged template literals to map the CSS styles to the component.

Install styled components by running the following npm command.

1
npm install --save styled-components
console

To create a styled component, you need to pass the React component in the styled() method. Inside the tagged literal string, you can write your CSS style rules without camel casing the property names.

1
2
3
4
5
6
7
import styled from "styled-components";
import { Container, Form } from "react-bootstrap";

const StyledInput = styled(Form.Control)`
  font-size: 12px;
  padding: 2px 5px;
`;
jsx

Now you can use this StyledInput component to render an input field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React from "react";
import styled from "styled-components";
import { Container, Form } from "react-bootstrap";

const StyledInput = styled(Form.Control)`
  font-size: 12px;
  padding: 2px 5px;
`;

const App = () => {
  return (
    <Container>
      <StyledInput name="foo" placeholder="Smaller Input" />
    </Container>
  );
};
jsx

The styled() method will work correctly for any component as long as it attaches the className prop to the DOM element.

Conclusion

In this guide, you learned multiple ways to make a React Bootstrap input smaller in size. As a developer, it's helpful to have an idea of different ways to handle or solve particular problems. This can come handy when troubleshooting issues—you'll always have an alternative solution to go to if the current one fails.

1