Create a block element on a specific screen width with Bootstrap 4


To create a block on a specific screen width, use the .d-*-block class.

The specific screen width can be small, medium, large and extra large. Set the class indivifually based on the screen size as shown below −

<span class="d-sm-block bg-primary">
d-sm-block
</span>
<span class="d-md-block bg-success">
d-md-block
</span>
<span class="d-lg-block bg-info">
d-lg-block<
/span>
<span class="d-xl-block bg-warning">
d-xl-block
</span>

Let us see the complete example to learn how to create a block element on a specific screen width −

Example

Live Demo

<!DOCTYPE html>
<html>
  <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  </head>

<body>
  <div class="container mt-3">
    <h2>Blocks</h2>
    <p>Resize the browser to check the effect</p>
    <span class="d-block bg-info">d-block</span>
    <span class="d-sm-block bg-primary">d-sm-block</span>
    <span class="d-md-block bg-success">d-md-block</span>
    <span class="d-lg-block bg-info">d-lg-block</span>
    <span class="d-xl-block bg-warning">d-xl-block</span>
  </div>

</body>
</html>

Updated on: 16-Jun-2020

429 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements