CSS:仅修改类中的一个元素

时间:2016-12-10 21:38:37

标签: html css ruby-on-rails

请查看以下图片:

我目前的用户界面:https://i.stack.imgur.com/9dlyJ.png

我有一个类,<div class="newsfeed">修改了每个创建的帖子。

我遇到的问题是我想要修改&#39;编辑&#39;并且&#39;删除&#39; glyphicon按钮不会影响课程的其余部分。具体来说,我想将它们都移到帖子的右上角。

现在,我知道我可以创建一个单独的类(或ID);但是,班级怎么会知道我引用了新闻源课程?例如,如果我想将它们固定在右侧,那么他们怎么知道要粘在盒子的右侧而不是页面上?

非常感谢所有帮助。对于能够为我提供所需CSS的人来说,这是一个巨大的奖励。 :)

全部谢谢!

编辑 - 下面是我视图中包含的所有当前代码:

   <% if @posts.any? %> 
  <% @posts.each do |post| %>
    <div id="post_<%= post.id %>">
      <div class ="newsfeed">
        <div class="well">
          <% if post.user.has_image? %>
            <div class="profile_pic"><%= image_tag post.user.images.first.file(:profile), class: "img-thumbnail", alt: "Cinque Terre", width: "50", height: "50" %></div>
          <% else %>
            <div class ="profile_pic"><%= image_tag "default_user_image.png",class: "img-thumbnail", alt: "Cinque Terre", width: "50", height: "50" %></div>
          <% end %>
        <div class="name_and_time">
          <a href="/users/<%= post.user.id %>/show"><%= post.user.first_name %> <%= post.user.last_name %></a><br>
          <span><%= post.created_at.strftime("%d %b %Y %H:%M") %></span>
        </div>
        <% if !post.image.exists? %>
          <h2> <%= post.text %> </h2>
        <% else %>
          <h2> <%= link_to post.text, post_path(post) %> </h2>
          <%= link_to post_path(post) do %>
            <p><%= image_tag post.image.url(:medium) %></p>
            <% end %>
        <% end %>
        <% if @user %>
          <% if current_user.voted_up_on?(post) %>
            <%= link_to "Like", dislike_post_path(post), method: :put %>
          <% else %>
            <%= link_to "Like", like_post_path(post), method: :put %>
          <% end %>
        <% end %>
        <%= "#{post.get_upvotes.size} | " %>
        <div class="edit_and_delete_posts">
        <% if post.user == current_user %>
          <%= link_to edit_post_path(post) do %>
            <span class="glyphicon glyphicon-pencil"></span>
          <% end %>
          <%= link_to post_path(post), method: :delete do %>
            <span class="glyphicon glyphicon-trash"></span>
          <% end %>
        </div>
          <div class='comments_div'>
            <%= render post.comments %>
          </div>
          <% if current_user %>
            <%= form_for [post, post.comments.new ], remote: true do |f| %>
              <%= f.text_area :text, placeholder: 'Add a comment' %>
              <%= f.submit 'Comment' %>
              <% end %>
          <% else%>
            <p>You need to <%= link_to "sign in", new_user_session_path %> to comment</p>
          <% end %>
        <% end %>
        </div>
      </div>
    </div>
  <% end %>
<% else %>
    No posts have been added!
<% end %>

1 个答案:

答案 0 :(得分:3)

如果您发布完整编译的HTML和CSS,我可以向您展示一个有效的演示,但您需要添加的是:

.newsfeed {
    position:relative;
}
.edit_and_delete_posts {
    position:absolute;
    top:0;
    right:0;
}

通过将position:absolute添加到.edit_and_delete_posts div,它会从流中退出,这样它就不会影响其余元素。然后,您可以设置topright个位置;这将它设置在具有相对位置的最近祖先元素的右上角。由于.newsfeed具有position:relative,因此它是最近的祖先,因此将.edit_and_delete_posts定位相对到此。

然后,您可以根据需要添加额外的边距或填充以进行微调。