一些HTML元素不以移动浏览器为中心,使用带引导程序的rails(Some HTML elements not centering on mobile browser using rails with

编程入门 行业动态 更新时间:2024-10-09 09:21:15
一些HTML元素不以移动浏览器为中心,使用带引导程序的rails(Some HTML elements not centering on mobile browser using rails with bootstrap)

我正在使用带有Rails的Twitter Bootstrap并尝试其响应式设计功能。 我有两个HTML元素(下面的评论中引用的h1元素和表单)在移动设备上偏离中心。 然而,当我缩小它时,它们在我的浏览器中居中(见截图)。 我无法弄清楚如何确保元素在移动设备上居中。

我在视图中有以下代码:

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><%= full_title(yield(:title)) %></title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <header class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <div class="container"> <%= link_to "ReRide", root_path, id: "logo" %> <nav> <ul class="nav pull-right"> <li><%= link_to "Home", root_path %></li> <li><%= link_to "About", about_path %></li> </ul> </nav> </div> </div> </header> <div class="container"> <% flash.each do |key, value| %> <div class="alert alert-<%= key %>"><%= value %> <a class="close" data-dismiss="alert">×</a> </div> <% end %> <div class="center hero-unit"> <h1>Welcome to ReRide!</h1> <!--THIS IS OFF CENTER--> <p>Where selling a used bike is painless </p> <p> <%= simple_form_for @contact, :html => {:class => "form-inline"} do |f| %> <!--THIS IS OFF CENTER--> <%= f.input_field :email, placeholder: 'Email', label: false, input_html: { class: 'input-medium' } %> <%= f.input_field :potential_relationship, input_html: { class: 'input-medium' }, collection: Contact::RELATIONSHIP_CHOICES.invert, prompt: "I am a:", label: false%> <%= f.button :submit, 'Stay Informed', :class => 'btn btn-primary' %> <% end %> </p> </div> <footer class="footer"> <small> <a href="http://www.reridebikes.com/">ReRide Bikes</a> </small> </footer> </div> </body> </html>

CSS文件:

@import "bootstrap"; body { padding-top: 60px; } @import "bootstrap-responsive"; /*Everything below is overriding Bootsrap CSS*/ /* mixins, variables, etc. */ $grayMediumLight: #eaeaea; @mixin box_sizing { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; } /* universal */ html { overflow-y: scroll; width: auto; overflow-x: hidden; } body { width: auto; overflow-x: hidden; } section { overflow: auto; } textarea { resize: vertical; } .center { text-align: center; h1 { margin-bottom: 10px; } } /* typography */ h1, h2, h3, h4, h5, h6 { line-height: 1; } h1 { font-size: 3em; letter-spacing: -2px; margin-bottom: 30px; text-align: center; } h2 { font-size: 1.2em; letter-spacing: -1px; margin-bottom: 30px; text-align: center; font-weight: normal; color: $grayLight; } p { font-size: 1.1em; line-height: 1.7em; } /* footer */ footer { margin-top: 45px; padding-top: 5px; border-top: 1px solid $grayMediumLight; color: $grayLight; a { color: $gray; &:hover { color: $grayDarker; } } small { float: left; } ul { float: right; list-style: none; li { float: left; margin-left: 10px; } } } /* forms */ input, textarea, select, .uneditable-input { border: 1px solid #bbb; } input { height: auto !important; } #error_explanation { color: #f00; ul { list-style: none; margin: 0 0 18px 0; } } .field_with_errors { @extend .control-group; @extend .error; }

在缩小的broswer:

在iPhone上:

I am using Twitter Bootstrap with Rails and experimenting with its responsive design functionality. I have two HTML elements (the h1 element and form referenced in comments below) that are off-center on mobile devices. They are however centered in my browser when I shrink it (see screenshots). I cannot figure out how to ensure the elements are centered on mobile.

I have the following code in the view:

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><%= full_title(yield(:title)) %></title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <header class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <div class="container"> <%= link_to "ReRide", root_path, id: "logo" %> <nav> <ul class="nav pull-right"> <li><%= link_to "Home", root_path %></li> <li><%= link_to "About", about_path %></li> </ul> </nav> </div> </div> </header> <div class="container"> <% flash.each do |key, value| %> <div class="alert alert-<%= key %>"><%= value %> <a class="close" data-dismiss="alert">×</a> </div> <% end %> <div class="center hero-unit"> <h1>Welcome to ReRide!</h1> <!--THIS IS OFF CENTER--> <p>Where selling a used bike is painless </p> <p> <%= simple_form_for @contact, :html => {:class => "form-inline"} do |f| %> <!--THIS IS OFF CENTER--> <%= f.input_field :email, placeholder: 'Email', label: false, input_html: { class: 'input-medium' } %> <%= f.input_field :potential_relationship, input_html: { class: 'input-medium' }, collection: Contact::RELATIONSHIP_CHOICES.invert, prompt: "I am a:", label: false%> <%= f.button :submit, 'Stay Informed', :class => 'btn btn-primary' %> <% end %> </p> </div> <footer class="footer"> <small> <a href="http://www.reridebikes.com/">ReRide Bikes</a> </small> </footer> </div> </body> </html>

CSS file:

@import "bootstrap"; body { padding-top: 60px; } @import "bootstrap-responsive"; /*Everything below is overriding Bootsrap CSS*/ /* mixins, variables, etc. */ $grayMediumLight: #eaeaea; @mixin box_sizing { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; } /* universal */ html { overflow-y: scroll; width: auto; overflow-x: hidden; } body { width: auto; overflow-x: hidden; } section { overflow: auto; } textarea { resize: vertical; } .center { text-align: center; h1 { margin-bottom: 10px; } } /* typography */ h1, h2, h3, h4, h5, h6 { line-height: 1; } h1 { font-size: 3em; letter-spacing: -2px; margin-bottom: 30px; text-align: center; } h2 { font-size: 1.2em; letter-spacing: -1px; margin-bottom: 30px; text-align: center; font-weight: normal; color: $grayLight; } p { font-size: 1.1em; line-height: 1.7em; } /* footer */ footer { margin-top: 45px; padding-top: 5px; border-top: 1px solid $grayMediumLight; color: $grayLight; a { color: $gray; &:hover { color: $grayDarker; } } small { float: left; } ul { float: right; list-style: none; li { float: left; margin-left: 10px; } } } /* forms */ input, textarea, select, .uneditable-input { border: 1px solid #bbb; } input { height: auto !important; } #error_explanation { color: #f00; ul { list-style: none; margin: 0 0 18px 0; } } .field_with_errors { @extend .control-group; @extend .error; }

In shrunken broswer:

On iPhone:

最满意答案

您需要媒体查询来动态更改该大家伙的字体大小。 例如:

@media only screen and (max-width : 320px) { h1 { font-size: 2em; } }

这意味着,在宽度最大为320像素的屏幕上,制作h1字体大小2em(与原始帖子中的默认3em相对)

这是一个关于媒体查询的小页面

You need media queries to dynamically change the font size of that big guy. For example:

@media only screen and (max-width : 320px) { h1 { font-size: 2em; } }

This means, on a screen that's width is maximim 320 px, make the h1 font-size 2em (as opposed to the default 3em you have in the original post)

Here's a small page on media queries

更多推荐

class,<%,px,电脑培训,计算机培训,IT培训"/> <meta name="description&qu

本文发布于:2023-04-29 07:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不以   元素   浏览器   程序   中心

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!