0

我正在使用带有 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;
}

在缩小的浏览器中: 在此处输入图像描述

在 iPhone 上: 在此处输入图像描述

4

1 回答 1

0

您需要媒体查询来动态更改那个大人物的字体大小。例如:

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

这意味着,在宽度最大为 320 像素的屏幕上,将 h1 字体大小设置为 2em(而不是原始帖子中的默认 3em)

这是有关媒体查询的小页面

于 2013-09-18T05:12:08.993 回答