After two days of thinking and trying and experimenting with absolute positioning, inline/inline-block display, flex display and so on... I still did not find an answer to the question - "how to style a timestamp's label position, shown on the screenshot below, with pure CSS?".
Conditions:
So far I could reach it only with JS, but I believe it is possible to make it work with pure CSS.
UPD: I've added a sandbox example for you guys, with rough code, if you will want to help me http://jsbin.com/gatifevowu/2/edit?html,css,output
答案 0 :(得分:1)
我没有检查所有条件,但这应该有效:
<强> CSS 强>
.chat-message {
clear: both;
max-width: 450px;
min-height: 38px;
line-height: 18px;
padding: 10px;
box-shadow: 0 0.5px 1.5px 0 rgba(0,0,0,0.33);
border-radius: 4px;
float: right;
background-color: $white-gray;
}
.chat-message__text {
display: inline;
}
.chat-message__timestamp {
float: right;
}
<强> HTML 强>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="chat-message">
<div class="chat-message__text">Test message</div>
<span class="chat-message__timestamp">10:34 am</span>
</div>
<div class="chat-message">
<div class="chat-message__text">Test message message message message message message message message message</div>
<span class="chat-message__timestamp">10:34 am</span>
</div>
<div class="chat-message">
<div class="chat-message__text">Test message</div>
<span class="chat-message__timestamp">10:34 am</span>
</div>
</body>
</html>
答案 1 :(得分:1)
Flexbox解决方案......只需要进行少量的HTML结构更改。
.chat-message {
width: 80%;
margin: 1em auto;
border: 1px solid grey;
display: flex;
justify-content: flex-end;
}
.chat-message__text {
text-align: right;
}
.chat-message__timestamp {
white-space: nowrap;
padding-left: 1em;
background: #c0ffee;
}
&#13;
<div class="chat-message">
<div class="chat-message__text">Test message
<span class="chat-message__timestamp">10:34 am</span>
</div>
</div>
<div class="chat-message">
<div class="chat-message__text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo aperiam numquam ullam error, inventore molestias illo voluptatibus perspiciatis? Ducimus, numquam,
<span class="chat-message__timestamp">10:34 am</span>
</div>
</div>
<div class="chat-message">
<div class="chat-message__text">Lorem ipsum dolor sit.
<span class="chat-message__timestamp">10:34 am</span>
</div>
</div>
&#13;