skip to Main Content

How to change the styles according to the screen size in bootstrap?

And how to style the element do we have to declare our own class and change according to the screen size or how?

How to do it? and how do we link it with our main index.html file?? Or do we have to create the separate style.css file and write the code.

2

Answers


  1. In Bootstrap, you can use responsive utility classes to change styles according to screen size. Here’s how you can do it:

    Using Bootstrap’s Responsive Utility Classes: Bootstrap provides predefined classes that you can use to show/hide elements or adjust their styles based on screen size.

       <div class="text-center text-lg-left"> <!-- Center text by default, left-aligned on large screens and above -->
           Content
       </div>
    

    Here, text-center is a Bootstrap class that centers text by default, while text-lg-left aligns the text to the left on screens larger than or equal to the large breakpoint.

    Custom Styling with Media Queries: If Bootstrap’s predefined classes don’t meet your requirements, you can write custom CSS using media queries to define styles for different screen sizes.

    /* Custom CSS */
       @media (min-width: 768px) {
           .custom-class {
               /* Styles for screens wider than 768px */
               color: red;
           }
       }
    
       @media (min-width: 992px) {
           .custom-class {
               /* Styles for screens wider than 992px */
               color: blue;
           }
       }
    

    Linking CSS to HTML File: You can link your custom CSS file or include custom styles within <style> tags in your HTML file.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Responsive Styling</title>
        <link rel="stylesheet" href="styles.css"> <!-- Link to your custom CSS file -->
    </head>
    <body>
        <!-- Your content here -->
        <div class="custom-class">
            Content with custom styling
        </div>
    </body>
    </html>
    

    Separate CSS File: It’s a good practice to keep your CSS in a separate file styles.css for better organization and maintenance.

    Example structure:

    project/
    ├── index.html
    └── styles.css
    

    You can link this CSS file to your HTML file using the <link> tag within the <head> section, as shown above.

    By using either Bootstrap’s responsive utility classes or custom CSS with media queries, you can effectively style your elements according to different screen sizes.

    Login or Signup to reply.
  2. first you must import Bootstrap in HTML file , the some like css file

    • Include via CDN
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    

    or

    and after go to this site

    https://getbootstrap.com/docs/4.1/layout/overview/
    and you will find all the information with image .and you import the class like this

    <div class="container">
    <!-- Content here -->
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search