简体   繁体   English

如何调整不同屏幕尺寸的div元素宽度?

[英]How to adjust div elements width for different screen size?

I'm working on SPA and I tried to avoid using tables for non tabular data. 我正在使用SPA,但我尝试避免将表用于非表格数据。 On my main page I have set of elements and I need them to be in side-by-side order. 在我的主页上,我有一组元素,我需要它们并排排列。 For this I used class: 为此,我使用了类:

div.nextTo {
    display: inline-block;
    margin-left: 5px;
}

This looks fine and works on the big screen. 看起来不错,并且可以在大屏幕上使用。 Once I resized my browser or if I open my app on Mobile device my elements are sitting over each other. 调整浏览器大小或在移动设备上打开应用程序后,我的元素就会相互重叠。 I also set the width in percentage for each element and seems that doesn't adjust on the different screen size. 我还为每个元素设置了百分比宽度,似乎在不同的屏幕尺寸上无法调整。 Here is example: 这是示例:

 div.pgContainer { width: 100%; height: 100%; font-family: Arial; font-size: 12px; box-sizing: border-box; padding: 5px 10px 5px 10px; } div.headerBox { width: 100%; border-top: 2px solid #000099; border-right: 2px solid #000099; border-left: 2px solid #000099; height: 25px; background-color: #000099; } div.headerBox span { font-weight: bold; color: white; font-size: 16px; } section.mainBox{ width: 100%; padding-top: 0; float: left; background-color: white; border: 2px solid #000099; } nav.xNavigation { width: 100%; margin-left: 0; margin-right: 0; padding-top: 2px; margin-bottom: 5px; background-color: #c8e2db; float: left; border-bottom: 2px solid #000099; height: 18px; } nav.xNavigation a { color: black; text-decoration: none; cursor: pointer; font-weight: bold; padding-left: 5px; padding-right: 5px; } nav.xNavigation a:hover { color: #999999; } div.nextTo { display: inline-block; margin-left: 5px; } 
 <div class="pgContainer"> <div class="headerBox"> <div style="float:left; margin-left:5px;"> <span>My App</span> </div> <div style="float:right; margin-right:5px; margin-top:2px;"> <span>Choose: <select name="hmMenu" id="hmMenu"> <option value="mainBox" selected="selected">Home</option> <option value="settingsBox">Settings</option> </select> </span> </div> </div> <section class="mainBox"> <div id="frmContainer"> <nav class="xNavigation"> <a href="#" id="chDemo">Demographic</a> | <a href="#" id="adInfo">Adult</a> | </nav> <form name="searchForm" id="searchForm" action="hmHome.cfm" method="POST" onsubmit="return false"> <div class="nextTo" style="width:5%;"> <select name="studMenu" id="studMenu"> <option value="1">Name</option> <option value="2">DOB</option> <option value="3">Age</option> <option value="4">State</option> </select> </div> <div class="nextTo" style="width:10%;"> <input type="text" name="searchFld" id="searchFld" size="24" maxlength="24" value="" title="Maximum size of the field is 24 characters." placeholder="Example: John, Miller" /> </div> <div class="nextTo" style="width:5%;"> <input type="button" name="searchBtn" id="searchBtn" value="Search"/> </div> <div class="nextTo" style="width:30%;"> <span id="searchMsg" style="display:none;"></span> </div> </form> </div> </section> </div> 

I'm wondering what is the best way to make my container adjustable for different screen size? 我想知道让我的容器适应不同屏幕尺寸的最佳方法是什么? Also what I should use for that in order to work on all browsers? 为了在所有浏览器上都能正常工作,我该使用什么? If anyone knows how this can be achived please let me know. 如果有人知道如何实现,请告诉我。 Thank you! 谢谢!

You could use @media css rule and define different style attributes depending on the viewport size. 您可以使用@media css规则并根据视口大小定义不同的样式属性。 Please see this reference . 请参阅此参考 @media browser compatibility can be verified here . @media浏览器的兼容性可以在这里验证。

You can declare width using a responsive unit like vw . 您可以使用诸如vw类的响应单元声明width

1vw is 1% of the viewport width. 1vw是视口宽度的1%。

So, if you wanted five elements to span the width of the viewport, you could style the elements something like this: 因此,如果您希望五个元素跨越视口的宽度,则可以对元素进行样式设置,如下所示:

div {
    width: 18vw;
    margin: 1vw;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM