简体   繁体   English

如何使表格正确自动缩放?

[英]How can I make my form autoscale correctly?

Ideally I want my form's textarea and everything below it to stay centered as the width of the browser changes. 理想情况下,我希望表单的textarea及其下方的所有内容随着浏览器宽度的变化而保持居中。 I am testing on Chrome. 我正在Chrome上进行测试。 How do I make the form always stay centered and have the textarea width decrease as the browser's width decreases? 如何使表单始终居中并随着浏览器宽度的减小而使textarea宽度减小?

Note - I could only post 2 links in question, so please see my comment for a third image. 注意-我只能发布有问题的2个链接,因此请查看我的评论以获取第三个图像。

Half Screen (this is when you simply drag the right end of the browser to the left. Notice that the line where you add money has aligned left instead of center) 半屏(这是您仅将浏览器的右端向左拖动时的情况。请注意,您添加资金的行已向左对齐而不是居中对齐)

Minimum width screen (this is the furthest you can drag the edge of the browser to the left. The problem here is that the textarea's handle (bottom right corner) is no longer visible. I want the textarea's width to adjust to the browser's smaller dimension): 最小宽度屏幕(这是您可以将浏览器的边缘向左拖动的最远的屏幕。这里的问题是文本区域的手柄(右下角)不再可见。我希望将文本区域的宽度调整为浏览器的较小尺寸):

HTML 的HTML

<form class="form-inline" align="center">

    <!-- Section to type question-->
        <div class="form-group" style="display:block">
            <textarea class="form-control" style="width:100%" rows="5"></textarea>
        </div>

    <!--Section to add money---->
        <div class="input-group" id="AddMoney">
          <span class="input-group-addon">$</span>
          <input id="EnterMoney" type="text" class="form-control" onkeypress='validate(event)'>
        </div>

    <!--Section to add file-->
        <div id="AddFile">
            <a href="#" onclick="document.getElementById('uploadFile').click(); return false;" />Add File</a>
            <input type="file" id="uploadFile" style="visibility: hidden;" />
        </div>

        <div id="imagePreview"></div>

    <!--Section for the Submit button-->
        <div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </form>

CSS 的CSS

form {
    width:500px;
    margin:50px auto;
}
#imagePreview {
    width: 100px;
    height: 100px;
    background-position: center center;
    background-size: cover;   
    display: inline-block;
}
#AddMoney {
    width:150px;
    top:10px;
}
#AddFile {
    position:relative;
    top:10px;
}

Javascript Java脚本

<!-- jQuery -->
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 

<!-- jQuery ui -->
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

Since you added jQuery as a tag, I'm going to assume you're ok with using a jquery solution. 由于您将jQuery添加为标签,因此我假设您可以使用jquery解决方案。 What I recommend is a simple function that centers elements on window resize. 我建议您使用一个简单的功能,将元素置于窗口调整大小的中心。 It could look something like this: 它可能看起来像这样:

function center(){
    var width = $(window).width();
    var width2 = width - $('#elementToCenter').width();
    var newMargin = width2 / 2;
    $('#elementToCenter').css("margin-left", newMargin);
}
$(window).resize(center);

The variable width represents the total width of the window. 可变width表示窗口的总宽度。 To get the whitespace on either side of the element in question, subtract its width from that total to get width2 . 要获得所讨论元素两侧的空白,请从该总数中减去其宽度,以得到width2 Now, divide that by 2 to get the amount of whitespace that belongs on each side. 现在,将其除以2可得到每一侧的空白量。 Finally you can set that ( newMarin ) to the margin-left (or margin-right if you feel so inclined) and the element should display in the center of the page. 最后,您可以将( newMarin )设置margin-left margin-right如果感觉很倾斜,则设置为margin-right ),并且该元素应显示在页面的中央。

You might prefer this solution because the only framework it requires you to bring in is jQuery, and it looks like you've already done that. 您可能更喜欢此解决方案,因为它需要您引入的唯一框架是jQuery,而且看起来您已经完成了此工作。

Cheers! 干杯!

Just get rid off your css. 只要摆脱你的CSS。 Bootstrap takes care of everything itself. Bootstrap会自己处理所有事情。 In your super simple case you don't even have to use media tags. 在超级简单的情况下,您甚至不必使用媒体标签。

  form {
    max-width:500px;
    margin:50px auto;
  }

You can resize screen in plnkr and see that everything is resizing. 您可以在plnkr中调整屏幕大小,然后看到一切都在调整大小。

I think this will work, but... 我认为这会起作用,但是...

Just get inside Boostrap.css to learn all this stuff. 只需进入Boostrap.css即可学习所有这些知识。 Download a free Bootstrap template to start learning. 下载免费的Bootstrap模板开始学习。

CSS FIX CSS修复

form {
    width: 100%;
    max-width: 500px;
    margin:50px auto 50px auto;
}

#imagePreview {
    width: 100px;
    height: 100px;
    background-position: center center;
    background-size: cover;   
    display: inline-block;
}

#AddMoney {
    width:150px;
    margin-top:10px;
}

#AddMoney input {
width: 90%;
float:right;
}

#AddMoney span
{
width:5%;
}

#AddFile {
    display: block;
    position:relative;
    margin-top:10px;
}

form input,
form textarea
{
    width: 100%;
    max-width: 500px;
}

/* Small to Medium screens or higher */
@media (min-width: 768px) {
    form,
    form input,
    form textarea
    {
        max-width: 800px;
    }
}

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

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