mobile div dimensions

My webpage has three divs that I want to stretch across the entire width of the browser, with each div taking up 33% of the screen. On mobile devices, I need these divs to stack on top of each other while still occupying 100% of the mobile width.

What should be the ideal width for each div to achieve this layout? When I set the width of each div to 33%, it looks good on desktop but appears squished on mobile screens.

<style type="text/css">
.wrapdiv {
 display: inline-block;
 margin: 0;
 vertical-align: top;
 padding:20px;
}
</style>


<div class="wrapdiv" style="">
</div>

<div class="wrapdiv" style="">
</div>

<div class="wrapdiv" style="">
</div>

Answer №1

Utilizing media queries is the key to achieving responsiveness in your design. By specifying certain conditions, such as when the screen width reaches 420px, you can control how elements are displayed.

@media screen and (max-width:420px){
    .wrapdiv{width:100%;}
}

Answer №2

To ensure your website is responsive, combine media queries with the viewport meta tag. Below is a basic example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>

div {
 width: 30%;
 float: left;
 padding:20px;
}

@media screen and (max-width: 600px) {
    div {
        width: 100%; 
        float: none;
    }
}

</style>
</head>
<body>

<div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec convallis sem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
</div>

<div>
    Phasellus tempus suscipit urna, ut ultricies est bibendum eget. In hac habitasse platea dictumst. Proin quis venenatis justo, sed fermentum leo. Nullam eu tincidunt ligula, non gravida quam.
</div>

<div>
    Fusce vulputate vehicula felis in semper. Suspendisse potenti. Maecenas scelerisque magna sed interdum imperdiet. Nunc consequat augue vel metus tempor vulputate.
</div>

</body>
</html>

Answer №3

Check out the demo here

Here is the HTML code snippet:

<div id="example">
    <div id="block-1" class="wrapdiv">First</div>
    <div id="block-2" class="wrapdiv">Second</div>
    <div id="block-3" class="wrapdiv">Third</div>
</div>

This is the CSS code snippet:

.wrapdiv {
    display: table-cell;
    background:#ccc
}
/* This will make each div take up 33% */
#example {
    display: table;
    width: 100%;
}
@media screen and (max-width:420px) {
    #block-1 {
        display: table-footer-group;
        border:1px solid red !important;
    }
    /* Will be displayed at the bottom of the pseudo-table */
    #block-2 {
        display: table-row-group;
        border:1px solid red !important;
    }
    /* Will be displayed in the middle */
    #block-3 {
        display: table-header-group;
        border:1px solid red !important;
    }
    /* Will be displayed at the top */
}

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

What is the best way to make a trail follow my shapes on canvas?

In my current project, I have implemented a feature where shapes are generated by spinning the mouse wheel. One specific shape in focus is the triangle, but other shapes follow the same generation process. The goal is to create a trail that slowly fades ...

What is the best way to ensure that all of my images are the same size?

I've been working on making my pictures of varying resolutions the same size here, but no matter what I try, it just doesn't seem to work. Here is what I'm getting: . When I change the state to 1, 2, 3, or 4, I want it to look like this: her ...

Dynamically add a Font Awesome icon to the background of an input text field with React and react-fa

Utilizing ReactJs and react-fa to access Font Awesome icons, I am attempting to dynamically place one of the icons inside a text input. The following is my code snippet: import React, { Component } from 'react'; import PropTypes from &a ...

Placing PHP Code - Finding the Right Location

As I start building my first website with a database using PHP, I have a simple question. Where is the best place to put your PHP code? In Codecademy, they had me embed the PHP directly into the HTML files (replacing index.html with index.php). However, ...

Changing the Div Class in Master page from a Child page is a straightforward process that involves modifying

I am trying to dynamically change the style of a div element in the master page from my child page. This is the code I am using: protected void ShowMsgText(int MsgID) { HtmlGenericControl MsgInner; MsgInner = ((HtmlGenericControl) ...

Creating a bespoke Bootstrap 4 Modal experience without the backdrop

I'm attempting to show a Bootstrap 4 modal without a backdrop. I've tried the following code with no success: .modal.right. modal-backdrop.show { background-color: transparent !important; } When I try this modification, it works (but affect ...

Import CSS and JS files into a Node.js project

I've been diving into Node.js and I've hit a roadblock. I'm facing an issue with loading css and js files in my project. Despite successfully rendering Bootstrap and Fontawesome from a CDN, my custom css and js files refuse to load. This is ...

Strategies for efficiently creating reusable HTML templates for recurring content blocks?

I am just starting out in web development and have encountered a problem with navigating through my page. The layout of the pages is the same, except for a div container. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

Combine consecutive <p> tags within a <div> container

Can you group all adjacent <p> elements together within a <div> element? For example, assuming the following structure: <div class="content"> <p>one</p> <p>two</p> <h2> not p</h2> <p>thr ...

What CSS style should be used to fill both the thumbs up and thumbs down icons of a glyphicon?

Using glyphicon icons instead of other icons (fa icons), I am able to change the color of the icon, but it does not completely fill the icon with color like Facebook's like button. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1 ...

What are the steps to create a customized app bar with React and Material-UI similar to this design?

Can anyone help me create an app bar that resembles this design: Click here to view I have managed to implement the search box in the top half of the app bar, but I am struggling with adding the bottom half. Here is the code I have written so far: ...

What is the best method to retrieve information from two tables on a webpage that have identical classes?

I am facing a challenge in retrieving or selecting data from two different tables that share the same class. My attempts to access this information using 'soup.find_all' have proven to be difficult due to the formatting of the data. There are m ...

Looping through a jQuery script to add a class if a certain condition is met in another class

$(document).ready(function() { if ($("#grid .media-box").hasClass("brand1")) { $("#grid .media-box-content").addClass("brand01") }; } }); and in body looping div grid <div id="grid"> <?php foreach($data as $items) { ?> <div cl ...

"Differences can be observed in the behavior of "this" keyword in JavaScript across different browsers,

In my code, there is a function called toggleContent() which performs the following actions: function toggleContent() { var parent = this.parentElement; toggleClass(parent,"detailsAreVisible"); } When a user clicks on a specific button, this fun ...

Unusual actions regarding flexible CSS style sheet

There have been countless times when I have utilized a CSS technique that I discovered on my own. When I need a dynamic stylesheet that can be included in an HTML document, I use CSS files that are not actually CSS files, but PHP files instead. For example ...

Implementing functionality: Removing a row and applying a CSS class upon button click

Check out the fiddle below: https://jsfiddle.net/shaswatatripathy/enq0kfqL/2/ I need help with creating a remove function and adding a CSS class to a clicked row. 1. When I click on "remove", the clicked row should be deleted. When I click on a row, ...

Having trouble with clicking a button in HTML?

I'm facing an issue with a button on my HTML page that features a background created using the popular particle library. The button is visible but not clickable. I've attempted adjusting the background size and alignment without success. How can ...

Generate random unique values from an array to populate a text input field in HTML. Display a message once all unique values have been shown

I have included an option code and text box below: <select id="sel" class="form-control input-lg" data-live-search="true"> <option value="">-- Choose Your Country--</option> </select><br/><br/><br/> &l ...

Verification checkbox in Html5 is mandatory

I have created an input checkbox that contains array values, resulting in multiple rows being generated in a table. However, the issue I am facing is that all checkboxes need to be checked in order to submit the form. This means I cannot submit the form un ...