How can I adjust a centered container div to fit properly on a mobile device

Exploring the centering of a particular div:

#container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 550px;
  width: auto;
  background: rgba(28, 28, 54, 0.70);
  padding: 15px;
  border: 1px solid #1c1d29;
  border-radius: 3px;
  display: flex;
}

#actualcontainer {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 5px;
}

#content {
  width: 550px;
  height: 100%;
  background-color: #1c1c36;
  padding: 25px;
  border-radius: 3px;
  align-items: center;
  justify-content: center;
  display: inline-block;
  overflow-y: auto;
  overflow-x: clip;
  box-sizing: border-box;
}

#sidebar {
  width: 300px;
  height: 100%;
  background: transparent;
  overflow-y: none;
  text-align: center;
  display: inline-block;
  vertical-align: middle;
  box-sizing: border-box;
  padding-left: 20px;
}

#sidebar p {
  text-align: center;
}

#sidebar img {
  max-width: 300px;
  text-align: center;
}

.sideimage {
  vertical-align: top;
  margin: 0 auto;
  background-image: url('/img.jpg');
  background-size: cover;
  background-repeat: no-repeat;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  text-align: center;
}

#title {
  font-size: 175%;
  font-weight: bold;
  text-align: center;
  margin: 10px 0;
}

#description {
  font-size: 90%;
  padding: 0 10px;
  width: auto;
  max-height: 135px;
  box-sizing: border-box;
  overflow-y: auto;
  text-align: center;
  margin: -10px 0;
}

#linkcontainer {
  position: absolute;
  bottom: 0px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

#links {
  font-size: 14px;
  color: #f7e5a3;
  padding: 5px;
  overflow: hidden;
  background: rgba(28, 28, 54, 0.70);
  border-radius: 3px;
  width: 75px;
  height: 75px;
  box-sizing: border-box;
  margin: 3px 3px 1.5px 3px;
  line-height: 200%;
  display: flex;
  justify-content: center;
  align-items: center;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
}

Looking at the HTML structure:

<div id="container">
  <div id="actualcontainer">
    <div id="content">
      <h1>Text text text</h1>
      <p align="center">Text text text</p>
    </div>
    <div id="sidebar">
      <div class="sideimage"></div>
      <div id="title">Text text text</div>
      <div id="description">
        <p>Text text text Text text text Text text text</p>
      </div>
      <div id="linkcontainer">
        <div id="links"><a href="/link"><i class="fa-solid fa-house"></i><br>link</a></div>
        <div id="links"><a href="/link"><i class="fa-solid fa-house"></i><br>link</a></div>
        <div id="links"><a href="/link"><i class="fa-solid fa-house"></i><br>link</a></div>
        <div id="links"><a href="/link"><i class="fa-solid fa-house"></i><br>link</a></div>
        <div id="links"><a href="/link"><i class="fa-solid fa-house"></i><br>link</a></div>
        <div id="links"><a href="/link"><i class="fa-solid fa-house"></i><br>link</a></div>
      </div>
    </div>
  </div>
</div>

The current situation:

  • On desktop: everything is centered correctly with no parts extending beyond the window.
  • On mobile (portrait mode): also centered properly and content scales to fit screen size.
  • On mobile (landscape mode): container becomes too large for screen and top/bottom edges are cut off.

Desired outcome: To have consistent scaling in both portrait and landscape modes on mobile devices, ensuring visible space around the container without any cutoffs.

Attempts so far:

  • Adjusting sizes via CSS: The code provided reflects adjustments made but issues remain.
  • Using media queries and transform property:
    • Applying @media (max-width: 600px) and scaling #container: No noticeable change.
    • Applying @media (max-height: 600px) and scaling #container: Container shrinks but gets misaligned to bottom right requiring scrolling.
    • Scaling using @media (max-width: 600px) on body: Container shrinks but shifts to top edge cutting off part of it with no scroll option.
    • Similar results seen when applying scale to body using @media (max-height: 600px).

Answer №1

Instead of relying on scale in media queries, consider utilizing the 100vh and 100vw values in CSS. 100vw represents 100% of the viewport's width (the user's screen), while 100vh signifies 100% of the viewport's height.

By incorporating this CSS feature and incorporating padding using the calc() function, you can create a container that dynamically adjusts based on the screen size.

One possible implementation is as follows:

#container{
    max-width: calc(100vw - 20px);
    max-height: calc(100vh - 20px);
}

This approach guarantees that the container stays within the confines of the screen. Remember to enable overflow scrolling to prevent any potential issues. It's worth noting that while this solution works, it's considered poor practice. As you progress in your coding journey, strive to avoid such practices.

I hope this addresses your query. Feel free to reach out if you encounter any further challenges. Happy coding!

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 two body tags in the code of a Wordpress site can lead to malfunctioning

I've encountered a strange issue with my Wordpress sites hosted on BlueHost. There seems to be a double <head> tag appearing in the HTML code, even though there is only one. This duplicate tag is causing issues with the proper rendering of the s ...

Lock GridView headers when only scrolling vertically

I am facing an issue with my gridview on an aspx page. The gridview is wider than the page itself, so I want the headers of the gridview to scroll horizontally along with the content, while remaining fixed vertically. Can anyone help me achieve this? I hav ...

The responsive menu refuses to appear

my code is located below Link to my CodePen project I'm specifically focusing on the mobile view of the website. Please resize your screen until you see the layout that I'm referring to. I suspect there might be an issue with my JavaScript cod ...

Adaptive website backdrop

I have created a unique background for my website design, which includes 3 slices labeled as div id top, middle, and bottom. You can view the design in this image. I am interested in making this background responsive. While I have come across examples of ...

Display a fresh <p> tag when the condition in the if-statement is met

If you are looking for a questionnaire, check out this one. Now, I am interested in creating if-statements using HTML/CSS/jQuery to achieve the following scenario: Initially, only Question 1 will be visible. When the input matches a certain value like X, ...

When the CSS style sheet is not embedded within the HTML document, it fails

I'm facing an issue while trying to apply currency formatting to an HTML table in order to have it display correctly when opened in Excel. Initially, I tried doing this inline and it worked fine, like so: <td style="mso-number-format:$\##&bso ...

Understanding the Hierarchy of CSS and Bootstrap Styles

I have been using a custom CSS file along with bootstrap. Initially, I had my custom CSS written before the Bootstrap CSS. However, I recently discovered that it's recommended to include Bootstrap CSS first before any custom CSS. As a result, I'm ...

Search bar that automatically adjusts based on the size of the containing div

https://i.stack.imgur.com/wtn8K.gif I'm interested in learning how to achieve a layout similar to this. Here's an example of what I've created: <div class="header-con"> <div class="search-bar-con"> <input type ...

Yii ReCaptcha for better mobile compatibility

Incorporating the extension from http://www.yiiframework.com/extension/recaptcha/ into my Yii project has been a successful endeavor thus far. However, in an effort to enhance the responsiveness of my website, I recently made some modifications to the cod ...

Create two div elements containing responsive images using HTML and CSS

Can someone assist me in creating responsive DIVs with images inside? Currently, the second div is appearing below the first one, and I'm struggling to make them scale based on mobile and tablet screen sizes. Here is the code snippet: .new_ba ...

I am looking to bring in just the tables from a different html/php page

I am currently working on a webpage that includes a php library, header, and other elements. I only need to import specific tables and information from this page onto another screen display, with the tables arranged side by side instead of vertically. My ...

Adjust color scheme of drop-down menu

Having trouble changing the background color for my drop down menu. I tried to change the color for the sub div but it's not working. Can anyone help me fix this issue? Here is the code snippet: http://jsfiddle.net/3VBQ6/4/ #nav li:hover ul.sub li { ...

Using a variable with the :contains selector

function checkAndUpdateStatus(newName) { $('#myTable').children('tr').remove(":contains('" + newName + "')"); }; The function above is attempting to remove table rows in 'myTable' containing a string matching the ...

The functionality of CSS3 animations may sometimes be unreliable following the onLoad event

Here is a snippet of code to consider: $(function() { $('#item').css({ webkitTransform: 'translate(100px, 100px)' }); }); The element I am attempting to move has the following CSS properties: transform: translate3d(0 ...

Controlling multiple bx-sliders with a single pager using only JavaScript

Is there a way to control 3 sliders using the same pager? I attempted to use the following code but it did not work: <script language="javascript"> $('.mobile_left_mble,.mobile_right_mble,.text_btn').bxSlider({ //pagerCustom: '#bx- ...

Change the navbar brand in Bootstrap to be hidden on small screens like mobile devices

I am facing an issue with the navbar on my website where it expands in height when viewed on mobile devices due to not fitting into a single line. I have observed that if I remove the brand, it fits perfectly. However, I want to keep the brand visible on l ...

Cross-Browser Compatibility of CSS

I have noticed that lists styled with the following CSS code appear differently in Internet Explorer 8 and Firefox 4. In IE8, the rounded corners are missing, while FF4 does not change color when hovering over it. Which browser should I trust for accurate ...

table with flexible cell width and fixed table width

I've been troubleshooting this issue for days, but I just can't seem to find a solution! The problem lies within a table used on a webpage. If I don't specify table-layout, I can make one cell width dynamic, but I lose the ability to set &a ...

the deactivation of my rollover class is being caused by my float class

With the assistance of a generous contributor on stackoverflow, I was able to overlay different colored CSS boxes onto various images. These boxes could be removed upon mouseover, revealing the images beneath. You can view the code I used on fiddle here. ...

Customize your Bootstrap hamburger menu with a unique open and close button design

Currently, I am in the process of creating a hamburger menu with Bootstrap and I have managed to customize the open and close buttons according to my preferences. However, I am facing an issue where the hamburger icon is not displaying properly. See the c ...