Comple positioning, need pure CSS solution

时间:2016-06-18 20:18:41

标签: html css position css-position pure-css

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?".

enter image description here

Conditions:

  1. a timestamp width can vary from min-width 60px to max-width 100% of the parent block (the parent has a max-width value and float right)
  2. if a timestamp is smaller than 100% (eg 50%), the space to the left of it should be filled with a text, and the timestamp is always aligned to the right
  3. if a text and a timestamp are smaller than parents max-width - they are shown on a single line (like 1st, and 2nd messages on the screenshot)
  4. if a timestamp can't fit in a single line with a text - it drops down and aligned to the right
  5. and if text can't fit in a single line (like 3rd message on the screenshot), a timestamp is shown on the second line, aligned to the right, and does not block the space above it

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

2 个答案:

答案 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>

Fiddle

答案 1 :(得分:1)

Flexbox解决方案......只需要进行少量的HTML结构更改。

&#13;
&#13;
.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;
&#13;
&#13;