简体   繁体   English

将绝对文本对齐到父div的右下角

[英]Aligning absolute text to bottom right of parent div

I have a simple comment system where users can leave comments on pages. 我有一个简单的评论系统,用户可以在页面上发表评论。 Other users can then reply to these "main comments" resulting in "sub comments". 然后其他用户可以回复这些“主要评论”,从而得到“子评论”。 This only goes down one level (there are no "sub sub comments"). 这仅下降了一层(没有“子子注释”)。 I am trying to align the "Reply" link to the bottom right of a main comment. 我正在尝试将“回复”链接与主要评论的右下角对齐。 The problem is, because the main comment div contains all the children comments, that would just align it to the bottom of the last sub comment. 问题在于,由于主注释div包含所有子注释,因此只能将其与最后一个子注释的底部对齐。

I have tried doing top: 35% but as you can see in the fiddle below, depending on the amount/length of the comments, this doesn't work. 我尝试做top: 35%但是正如您在下面的小提琴中看到的那样,根据评论的数量/长度,这是行不通的。

JSFiddle: http://jsfiddle.net/cqjnjy8f/ JSFiddle: http : //jsfiddle.net/cqjnjy8f/

I've set sub-comment-2 to display: none . 我将sub-comment-2display: none If you get rid of that you'll see what I mean about the reply link's height varying. 如果您摆脱了这一点,您会发现我对回复链接的高度变化的意思。

All I need is for it to stay at the bottom right of the red area and to stay there no matter how many sub comments there are/the length of the main comment. 我需要的是,无论它有多少子注释/主注释的长度,它都应留在红色区域的右下角并保持在该位置。

Put the main comment in a div and add the reply button to that div. 将主要评论放在div中,然后将回复按钮添加到该div中。 Then set position:relative to that div and position: absolute; bottom: 0 然后设置position:relative对于该div和position: absolute; bottom: 0 position: absolute; bottom: 0 to reply. position: absolute; bottom: 0回复。 http://jsfiddle.net/cqjnjy8f/2/ http://jsfiddle.net/cqjnjy8f/2/

I think the problem here is one of structure. 我认为这里的问题是结构之一。

You only need the "Reply" as an adjunct to the initial comment so the element (and a span would be better than a heading here), so if we use the initial comment as the parent, we can position it absolutely at the bottom. 您仅需要将“ Reply”作为初始注释的附件,因此该元素(并且span会比此处的标题更好),因此,如果我们将初始注释用作父项,则可以将其绝对定位在底部。

 * { margin: 0; padding: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .main-comment { background-color: red; position: relative; padding-bottom: 1.5em; } .sub-comment-2 { background-color: blue; } .comment .reply { position: absolute; right: 0; bottom: 0; cursor: pointer; color: white; } .sub-comment { background-color: green; } .sub-comment-2 { background-color: blue; } 
 <div class="comment"> <h4 class="main-comment"> Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nunc vel tincidunt leo. Nam sit amet ultrices elit, vitae imperdiet massa. Maecenas luctus vulputate mauris, id consectetur sapien consectetur sed. Aliquam feugiat nibh eget risus tristique <span class="reply">Reply</span> </h4> <div class="sub-comment"> <h4> Donec blandit ex in ex ullamcorper pellentesque. Aenean a neque luctus nisl posuere hendrerit et sed nibh. Sed lacinia, orci quis dapibus venenatis, odio sem ultricies quam, in euismod ante metus ac neque. Fusce rutrum gravida ligula sit amet accumsan. Suspendisse eget congue orci </h4> </div> <div class="sub-comment-2"> <h4> Donec blandit ex in ex ullamcorper pellentesque. Aenean a neque luctus nisl posuere hendrerit et sed nibh. Sed lacinia, orci quis dapibus venenatis, odio sem ultricies quam, in euismod ante metus ac neque. Fusce rutrum gravida ligula sit amet accumsan. Suspendisse eget congue orci </h4> </div> </div> 

This can be resolved by adjusting the structure of your HTML, providing this is possible. 只要有可能,可以通过调整HTML的结构来解决。

You should have the 'Reply' button within your main comment div, and then wrap the entire thing (comments, and replies to the comment) in a div container. 您应该在主注释div中具有“答复”按钮,然后将整个内容(注释和对注释的答复)包装在div容器中。

 .comment { background-color: red; height: 5em; position: relative; } .comment button { position: absolute; bottom:0; right: 0; } .comment-reply { background-color: blue; margin-bottom: 0.2em; } 
 <div class="container-comment"> <div class="comment"> This is a comment <button>Reply to comment</button> </div> <div class="comment-reply"> This is a reply to the comment </div> <div class="comment-reply"> This is another reply to the comment </div> </div> 

You want something like this: 您想要这样的东西:

http://jsfiddle.net/cqjnjy8f/3/ http://jsfiddle.net/cqjnjy8f/3/

Your main issues were that you had not a good structure for what you wanted to achieve. 您的主要问题是您没有想要实现的良好结构。

For example, each comment should be it's own <div> element, and all comments, either main or replies, should have a common comment class. 例如,每个注释应该是它自己的<div>元素,并且所有注释(无论是主注释还是答复)都应具有一个公共comment类。

Regarding the Reply button, when you set the position of an element to absolute this position is absolute relative to the most immediate parent element, of the absolute positioned element, that has a relative position. 关于“回复”按钮,当您将元素的位置设置为absolute位置时,该位置相对于具有relative位置的absolute位置的元素中最直接的父元素是absolute

for example if I had 例如,如果我有

<div class="relative-positioned">
    <div class="static-positioned">
        <div class="absolute-positioned"></div>
    </div>
</div>

The absolute-positioned element would be positioned relative to the relative-positioned element and not the static-positioned one absolute-positioned元素将相对于relative-positioned元素而不是static-positioned relative-positioned元素static-positioned

Also, use <div> s instead of header tags such as h4 and h5 . 另外,使用<div>代替标题标签(例如h4h5

If you want to keep you mark-up exactly as you have it, then you can try using float: right on the .reply element. 如果您想使标记保持原样,则可以尝试在.reply元素上使用float: right .reply

You might want to adjust your margins on the h4 elements to improve the spacing. 您可能需要调整h4元素上的边距以改善间距。

Also, add clear: both to the .sub-comment rule to prevent text from wrapping around the reply button. 另外,在.sub-comment规则中添加clear: both ,以防止文本环绕在“回复”按钮上。

This solution fixes the positioning problem. 该解决方案解决了定位问题。 You can add margins and padding to style the button as needed. 您可以根据需要添加边距和填充以设置按钮样式。

 .comment { background-color: red; position: relative; } .comment h4 { margin: 0; } .sub-comment h4 { margin-top: 2.00em; } .sub-comment { background-color: green; clear: both; } .sub-comment-2 { background-color: blue; /* display: none; */ } .comment .reply { cursor: pointer; color: white; border: 1px solid blue; margin: 0px; float: right; } 
 <div class="comment"> <h4> Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nunc vel tincidunt leo. Nam sit amet ultrices elit, vitae imperdiet massa. Maecenas luctus vulputate mauris, id consectetur sapien consectetur sed. Aliquam feugiat nibh eget risus tristique </h4> <h5 class="reply">Reply</h5> <div class="sub-comment"> <h4> Donec blandit ex in ex ullamcorper pellentesque. Aenean a neque luctus nisl posuere hendrerit et sed nibh. Sed lacinia, orci quis dapibus venenatis, odio sem ultricies quam, in euismod ante metus ac neque. Fusce rutrum gravida ligula sit amet accumsan. Suspendisse eget congue orci </h4> </div> <div class="sub-comment sub-comment-2"> <h4> Donec blandit ex in ex ullamcorper pellentesque. Aenean a neque luctus nisl posuere hendrerit et sed nibh. Sed lacinia, orci quis dapibus venenatis, odio sem ultricies quam, in euismod ante metus ac neque. Fusce rutrum gravida ligula sit amet accumsan. Suspendisse eget congue orci </h4> </div> </div> 

Have you tried to add a margin style to Reply class? 您是否尝试向Reply类添加边距样式?

.reply {
position: absolute;
margin: 0 5% 5% 0}

In this way it should be located in the bottom-right part of your div. 这样,它应该位于div的右下部分。

Try this way: https://jsfiddle.net/cqjnjy8f/7/ 尝试这种方式: https : //jsfiddle.net/cqjnjy8f/7/

<div class="comment">
    <div class="main-comment"> 
    <span>
        Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nunc vel tincidunt leo. Nam sit amet ultrices elit, vitae imperdiet massa. Maecenas luctus vulputate mauris, id consectetur sapien consectetur sed. Aliquam feugiat nibh eget risus tristique
    </span>
        <a href="#" class="reply">Reply</a>
    </div>

    <div class="sub-comment">
        <span>
            Donec blandit ex in ex ullamcorper pellentesque. Aenean a neque luctus nisl posuere hendrerit et sed nibh. Sed lacinia, orci quis dapibus venenatis, odio sem ultricies quam, in euismod ante metus ac neque. Fusce rutrum gravida ligula sit amet accumsan. Suspendisse eget congue orci
        </span>
    </div>
    <div class="sub-comment-2">
        <span>
            Donec blandit ex in ex ullamcorper pellentesque. Aenean a neque luctus nisl posuere hendrerit et sed nibh. Sed lacinia, orci quis dapibus venenatis, odio sem ultricies quam, in euismod ante metus ac neque. Fusce rutrum gravida ligula sit amet accumsan. Suspendisse eget congue orci
        </span>
    </div>
</div>


.comment{
    border:1px solid #ccc;
    padding:3px;
}

.main-comment {
    display:table;
}

.sub-comment, 
.sub-comment-2{
    margin: 20px;
}

.reply {
    margin:20px 0;
    vertical-align: bottom;
    text-align:left;
    float:right;
}

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

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