Responsive HTML CSS Div with Bootstrap styling

Currently, I have some div elements that display nicely on desktop and tablet devices. However, the issue arises when the view is switched to mobile, as the elements appear disorganized and do not adjust appropriately.

My goal is to have these elements automatically adjust their layout: showing 3 in a line on a tablet and 2 in a row on a mobile device, similar to the desired outcome shown here:

Mobile View Example

I believe using the col-xs-6 class in Bootstrap should achieve this responsive layout, but adding it doesn't seem to have the desired effect.

Below is the HTML code:

<div id="surveys" class="surveys col-xs-12"><div>
    /* HTML code continues */

And the corresponding CSS:

#surveys h4 {
    /* CSS code for h4 element */
}

/* Additional CSS styles */

Any suggestions on how to make these elements shift responsively would be greatly appreciated. Thank you!

EDIT - Including the updated HTML with row and col-xs-6 classes, along with the resulting output:

<div id="surveys" class="surveys col-xs-12">
    /* Updated HTML code */

View on Laptop (padding seems to be added outside the divs): Laptop View

View on Mobile (appears more scrunched up): Mobile View

EDIT - The issue may be in the CSS snippet below, as it seems to disrupt the functionality when re-added:

#surveys .survey-item {
    /* Problematic CSS */
}

Answer №1

The custom CSS code contains various elements that are causing issues, particularly those related to setting the width and margins of each item. It is recommended to allow each item to fill the designated col-* space.

#surveys .survey-item {
    display: block;
    margin: 5px 0;
    border-radius: 7px 7px 6px 6px;
    border-width: 1px;
    border-style: solid;
    position: relative;
    cursor: pointer;
    background-color: #FFFFFF;
    border-color: #dddddd;
    -moz-box-shadow: 0 2px 0 0 #cccccc;
    -webkit-box-shadow: 0 2px 0 0 #cccccc;
    box-shadow: 0 2px 0 0 #cccccc;
}

An additional recommendation is to streamline the HTML structure, as there are numerous nested levels. Ensure that col elements are always the direct child of the `row`.

For a visual reference, you can visit this link

Answer №2

Ensure that your column classes are applied to the survey-item elements. Trying to fit 5 columns within a "desktop" view using Bootstrap's 12 column layout may result in unwanted white space (it must be an even number).

For example:

<div class="survey-item col-md-3 col-xs-6">
    <div id="2697033" class="item survey">
        ...
    </div>
</div>

Don't forget to include the row class in your surveys-list-container element. The final structure should resemble the following:

<div id="surveys-list-container" class="row">
    <div class="survey-item col-md-3 col-xs-6">
        ...
    </div>
    <div class="survey-item col-md-3 col-xs-6">
        ...
    </div>
    <div class="survey-item col-md-3 col-xs-6">
        ...
    </div>
</div>

Note: The way Bootstrap columns function is based on dividing 12 by the number in the column class. For instance, col-md-3 would yield 4 columns (12/3 = 4). For 6 columns, the class would have to be col-md-2 (12/2 = 6).

Regarding your CSS for survey-item, it is recommended to adjust it as follows:

#surveys .survey-item {
    border-radius: 7px 7px 6px 6px;
    border-width: 1px;
    border-style: solid;
    position: relative;
    cursor: pointer;
    background-color: #FFFFFF;
    border-color: #dddddd;
    -moz-box-shadow: 0 2px 0 0 #cccccc;
    -webkit-box-shadow: 0 2px 0 0 #cccccc;
    box-shadow: 0 2px 0 0 #cccccc;
}

Remember, Bootstrap columns handle floating and width automatically. It's best practice to apply padding and margins to a child element within your survey-item element.

Answer №3

Upon investigation, it appears that the following code snippets were disrupting the expected behavior of the bootstrap framework:

#surveys .survey-item {
    width: 19.2%;
    margin: 5px 0.8% 5px 0;
}

By removing these lines of code, the standard responsive grid system of bootstrap was restored.

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

Using Jquery toggle to keep divs always open or automatically open them at the start

After implementing the script below, I have successfully created a toggle effect for opening and closing divs. <script type="text/javascript" src="/mainmenu/js/jquery.min.4.1.2.js"></script> <script type="text/javascript"> jQuery(documen ...

Incorporate a variable into a CSS class

Currently, I am working on developing a component that creates an animation loop with a duration that is passed as a prop to the component. The issue I am facing involves using CSS to create the animation: animate:{ -webkit-transition: 10.0s !important ...

Converting SVG circle animation into textual form - a step-by-step guide!

<svg width="1000" height="100"> <circle id="orange-circle" r="30" cx="50" cy="50" fill="orange" /> <animate xlink:href="#orange-circle" attributeName="cx" from="50" to="900" dur="2s" begin="0s" ...

Differentiating onClick events for parent and child elements

I need help with my JSX code: <div id="parent" onClick={clickOnParent} style={{ width: 100, height: 100 }}> <div id="child" onClick={clickOnChild} style={{ width: 20, height: 20 }} /> </div> When I click on the pare ...

Refreshing the page when the button is clicked to upload files using AJAX

I have created a form and I need to send a file along with some other information via Ajax to a PHP file. The issue I am facing is that when I attempt to upload the file, the page refreshes after clicking on submit. How can I prevent this page refresh? Ad ...

What is the method for applying a class to another element?

Thanks to the amazing help from ori, I've successfully implemented a script that works exactly as I wanted. However, my limited knowledge of jQuery is hindering me from getting the next phase up and running! My goal now is to enhance it further by as ...

Combining Data from Header to Body using HTML5 and JavaScript

I am struggling to figure out how to transfer variables declared in the script from the header of my code to the body. Specifically, I want to capture the user input in the script and replace a placeholder in the body. Here is an example of what I am tryin ...

Tips on changing the outline color by clicking

I'm working on a simple code where I need to change the outline color when a user clicks on a text field. <input type="text" id="box1" /> <input type="password" id="box2" /> <input type="email" id="box3" /> <input type="submit" ...

How can we integrate this icon/font plugin in CSS/JavaScript?

Check out the live demonstration on Jsfiddle http://jsfiddle.net/hc046u9u/ <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materializ ...

Incorporating dynamic content changes for enhanced user experience can be achieved more effectively with AngularJS

I am utilizing a REST service to retrieve information for a banner. The goal is to dynamically update the slider on the main page using this data. Currently, I am using $http.get to fetch the data, and then implementing interpolation with additional ng-if ...

Error: Morris.js is unable to access the property 'x' because it is undefined

Seeking assistance in utilizing Morris.js to create an area chart using data from a JSON file. The JSON data is as follows: [{"period": 0, "time": 121.0}, {"period": 1, "time": 102.0}, {"period": 2, "time": 104.0}, {"period": 3, "time": 91.0}, {"period": ...

Prettyprint XML in Angular 8+ without using any external libraries

I am working with Angular 8+ and I need to display XML content in a nicely formatted way on my HTML page. The data is coming from the backend (Java) as a string, and I would like to present it in its XML format without relying on any external libraries or ...

Ways to conceal the contents of a page behind a transparent background div while scrolling

I created a Plunkr with the following markup: <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <nav class="navbar navbar-fixed-top"> <div class="container-fluid"& ...

Escaping HTML code within a Mustache template is crucial to prevent

I have a JavaScript array that looks like this: [button: "M'AVERTIR"] When I attempt to display the string in a mustache template, it appears in the following format: M&#039;AVERTIR Here's the code for my mustache template: {{=<% %> ...

How come the dark grey in CSS appears to be lighter than the standard grey hue?

JSBIN DEMO Could this be a mistake or is it a bug? Shouldn't the dark gray shade be darker than the default grey one? HTML <div class="lightgrey">Light Grey</div> <div class="grey">Grey</div> <div class="darkgrey">Dar ...

Leveraging Javascript within MVC3, either on a master page or child page

I'm currently developing a web application utilizing MVC3 and I am curious about the best practices for incorporating JavaScript. Specifically, I am uncertain about whether to include JavaScript in the master page as well as in individual child pages. ...

jQuery DatePicker Not Displaying Calendar

I've been attempting to implement a date picker in jQuery. I've included the necessary code within the head tag: <link rel="stylesheet" type="text/css" media="screen" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jqu ...

What can I do to improve the quality of the resolution?

Check out this demonstration on my website. I'm utilizing the jQuery cycle plugin to create a slider and ensuring that the photos maintain their full height and width. However, I've noticed that when I zoom in on the page, the picture doesn&apo ...

Enhancing the appearance of multiple images with CSS and Bootstrap styling

I am struggling to arrange multiple images in my booking system as they are all displaying in a single column rather than the desired layout https://i.sstatic.net/1E8jO.png What I had hoped for was a layout like this https://i.sstatic.net/v8K34.jpg Curre ...

Non-functioning Tooltips on Google Charts

Currently, I am working on integrating Google Chart with ASP.NET and linking it to a SQL Server database. I have encountered an issue while attempting to customize the tool tip. Below is the Header Code: <script src="js/jquery/jquery-1.10.2.js" type=" ...