I am currently exploring options on how to eliminate the excess space at the top of the screen when viewing on smaller devices like mobile phones and tablets

Utilizing bootstrap for my layout, I have a row with 4 columns where the content within each div has varied top margins. This creates a cascading effect and looks great on desktop screens. However, the issue arises when viewing it on smaller screens as the margin remains, creating a large gap between the content. My goal is to have the divs stack with a small (10px) space between them. I've attempted adjusting the CSS and incorporating @media queries but haven't been successful in reducing the space.

Below is the HTML code:

<div class="container" style='min-height:600px'><br>
    <div class='row'>
        <div class='col-sm-3' id='col-1'>
            <div class='col-1-box'></div>
        </div>
        <div class='col-sm-3' id='col-2'>
            <div class='col-2-box'></div>
        </div>
        <div class='col-sm-3' id='col-3'>
            <div class='col-3-box'></div>
        </div>
        <div class='col-sm-3' id='col-4'>
            <div class='col-4-box'></div>
        </div>
    </div>
</div>

And here is the UPDATED CSS styling:

<style>
@media only screen and (max-width: 600px) {
  .col-1-box, .col-2-box, .col-3-box, .col-4-box {
    margin-top: 3%;
  }
}
#col-1{
    background-color:;
}
#col-2{
    background-color:;
}
#col-3{
    background-color:;
}
#col-4{
    background-color:;
}
.col-1-box{
    margin-top:0%;
    padding:0px 10px 0 10px;
    min-height:400px;
    background-color:white;
    box-shadow: 2px 4px 7px -2px #6E6E6E;
}
.col-2-box{
    margin-top:25%;
    padding:0px 10px 0 10px;
    min-height:400px;
    background-color:white;
    box-shadow: 2px 4px 7px -2px #6E6E6E;
}
.col-3-box{
    margin-top:50%;
    padding:0px 10px 0 10px;
    min-height:400px;
    background-color:white;
    box-shadow: 2px 4px 7px -2px #6E6E6E;
}
.col-4-box{
    margin-top:75%;
    padding:0px 10px0 10px;
    min-height:400px;
    background-color:white;
    box-shadow: 2px 4px 7px -2px #6E6E6E;
}
</style>

Answer №1

To adjust the padding for mobile devices, simply incorporate a media query into your CSS code.

@media only screen and (max-width: 600px) {
  .col-2-box, .col-3-box, .col-3-box, .col-4-box {
    margin-top: 10px;
  }
}

Furthermore, there appears to be an error in the .col-4-box styling. Rectify this by changing the padding to padding:0px 10px 0 10px;

Answer №2

In order to address the large gap between the content, the issue is caused by setting the min-height. To resolve this, try setting it to zero and adjusting the margin-top to 10px. Here's a solution:

@media (min-width: 481px) and (max-width: 767px) {
  #col-1, #col-2, #col-3, #col-4{
    min-height:0px;
  }
  .col-2-box, .col-3-box, .col-4-box{
    margin-top: 10px;
  }
}

#col-1{
        background-color:;
        min-height:500px;
    }
    #col-2{
        background-color:;
        min-height:500px;
    }
    #col-3{
        background-color:;
        min-height:500px;
    }
    #col-4{
        background-color:;
        min-height:500px;
    }
    .col-1-box{
        margin-top:0%;
        padding:0px 10px 0 10px;
        min-height:200px;
        background-color:white;
        box-shadow: 1px 1px 3px #6E6E6E;
    }
    .col-2-box{
        margin-top:25%;
        padding:0px 10px 0 10px;
        min-height:200px;
        background-color:white;
        box-shadow: 1px 1px 3px #6E6E6E;
    }
    .col-3-box{
        margin-top:50%;
        padding:0px 10px 0 10px;
        min-height:200px;
        background-color:white;
        box-shadow: 1px 1px 3px #6E6E6E;
    }
    .col-4-box{
        margin-top:75%;
        padding:0px 10px0 10px;
        min-height:200px;
        background-color:white;
        box-shadow: 3px 3px 8px -4px #6E6E6E;
    }
    
    @media (min-width: 481px) and (max-width: 767px) {
      #col-1, #col-2, #col-3, #col-4{
        min-height:0px;
      }
      .col-2-box, .col-3-box, .col-4-box{
        margin-top: 10px;
      }
    }
<div class="container" style='min-height:600px'>
    <br>
    <div class='row'>
        <div class='col-sm-3' id='col-1'>
            <div class='col-1-box'></div>
        </div>
        <div class='col-sm-3' id='col-2'>
            <div class='col-2-box'></div>
        </div>
        <div class='col-sm-3' id='col-3'>
            <div class='col-3-box'></div>
        </div>
        <div class='col-sm-3' id='col-4'>
            <div class='col-4-box'></div>
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I desire a three-column layout where the middle column will expand in width if columns 1 and 3 are vacant

I have a three-column layout designed without using tables. <div id="main"> <div id="left"></div> <div id="content"></div> <div id="right"></div> </div> This is the CSS code: #left {width: 200px;fl ...

Responsive columns with maximum width in Bootstrap

Currently, I'm working on coding a portion of a single-page responsive Bootstrap layout that features two columns of text placed side by side. The request from the designer is for these columns to have a maximum width, but to be centered until they hi ...

What could be causing the unusual alignment of the 'pixels' on my resized HTML5 canvas?

Currently, I am in the process of creating a simple HTML5 canvas/JavaScript snake game inspired by the classic Nokia Snake. This is my first attempt at developing a game using HTML5 canvas and JavaScript, and I must admit, I am still a novice programmer! ...

How to Manage Empty Lines in HTML Documents within WordPress

Just recently, I launched my brand new website. Everything seems to be functioning properly except for one issue that I discovered in the HTML source code. There are 15 blank lines before "<!doctype html>", which is causing some problems with SEO and ...

Styling your Kendo grid in ASP.NET MVC with custom CSS styling

I am facing a challenge with displaying two kendo grids side by side on a single view page. To modify the CSS style of a kendo grid using ASP.NET, I typically use: .HtmlAttributes(new { style="width:50%"}) However, when attempting to change more than one ...

Changing datetime-local format from European to American dates

<input type="datetime-local"> While this input retrieves the local time from your PC, I'm curious if there's a way to make it display as US time specifically. ...

Comparing DOM Creation in PHP and JavaScript

My website currently does not have any ajax requests, and here is a simplified version of my code: class all_posts { public function index($id){ $statement = $db->prepare("SELECT * FROM mytable WHERE id = :id"); $statement->exe ...

What is the best way to create dynamic cards like the ones displayed here?

I am experiencing an issue with the black box displaying the date. I created it, but it does not adjust properly to different screen sizes. .date { width: 20%; height: 15%; background-color: black; z-index: 1; position: absolute; top: 45%; ...

button that stays in place on smaller screens

Hello everyone, I am currently facing an issue while trying to create a button with fixed positioning. The problem arises on smaller screens where the button completely disappears. Here is my CSS code: .sound_button{ background: #FFD700 url("Images/ ...

Tips for sending information to select2 version greater than 4.0

I'm exploring the use of select2 for the first time and wondering if someone could assist me. I want to pull data from a static array. Can you offer any guidance on how to achieve this? This is the code I currently have: $(document).ready(function( ...

When the height of a sibling element restricts the inner content and scroll bar due to the parent element's height being defined in vh

I'm currently working on creating a sidebar that occupies a fixed percentage of the viewport. Inside this sidebar, I want to have an element that remains fixed at the top while allowing the rest of the content to scroll if it exceeds the height of the ...

What is the best way to position two divs side by side while maintaining their individual padding?

When I remove the padding as shown, the website functions correctly with two div columns next to each other. However, upon adding padding, the #right div shifts downwards. How can I ensure it works as intended with padding included? HTML: Two divs direct ...

What steps should be followed to effectively incorporate Google Fonts into a Material UI custom theme for typography in a React/TypeScript project

Hey there, I'm currently working on incorporating Google fonts into a custom Material UI theme as the global font. However, I'm facing an issue where the font-weight setting is not being applied. It seems to only display the normal weight of 400. ...

Tips for arranging HTML image sources to prepare for future updates

Incorporated into the HTML are a series of images, each accompanied by text and/or transitions. To streamline positioning, each image set along with other elements (text, video, etc...) is enclosed within a div. Furthermore, every image is labeled with a n ...

Troubleshooting: Issues with CSS fixed positioning

Whenever I try to scroll down, my navbar moves along with the page. Even when I resize the window, it keeps shifting despite using position:fixed. What mistake am I making? nav li:nth-child(1) { position: fixed; border: 1px solid #15317E; font-si ...

Tips for aligning a logo in the center of a navigation bar

I'm struggling to center a logo within the navigation bar of a website I am designing. Despite my attempts, I have not been successful in achieving the desired result. My goal is to have the logo positioned in the middle of the navigation bar, with ot ...

Can a single link have a customized background color and different properties set for when it is clicked on?

I need assistance with setting the background-color of this <ul> to none while also adjusting the link's background-color to black. Here is the CSS code I have used: a:link { background-color: black } And here is the HTML code snippet: <di ...

"Utilizing margins to create space between elements within a Bootstrap

I am currently utilizing bootstrap 4 and have a list of items that I am displaying in the following manner: <div class="container"> <div class="row"> <div class="card col-md-4 col-sm-6 col-12 item-wr" *ngFor="let item of items"> ...

What is the best way to identify key presses using Javascript?

Exploring various resources online, I have come across multiple recommendations (such as using window.onkeypress or jQuery) but each option comes with its own set of criticisms. How can the detection of a keypress be achieved in Javascript? ...

The GridCalendar view of FullCalendar v6.1 is generating 50 `ARIA attribute unsupported or prohibited` errors when analyzed with the SiteImprove browser extension

The Challenges Creating a website that adheres to accessibility laws can be quite daunting. I am currently using SiteImprove, a free browser extension, to check for any issues on my site. Unfortunately, the GridCalendar view is showing 50 errors. ARIA a ...