I have a container that has a string text (which could be very long) and a floating inline element as depicted here.
HTML
<div class="container">
<span>RIGHT</span>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.
</div>
CSS
span {
float: right;
border-radius: 2px;
background-color: #26a69a;
padding: 0 6px;
}
.container {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
In case the string text is long it gets truncated and this works fine in chrome. Running it on mozilla, on the other hand, displays the truncated text behind(in front) of the floated inline object. Could someone please explain to me why this is so and how to correct it.
Whenever you are providing text-overflow: ellipsis, you need to apply fix width to the container stating the browser to make it ellipsis after that fixed width.
Chrome generally inherits the fix width of the parent or container and applies the properties. However, you need to manually mention it in Firefox.
For ellipsis, its also better if you change the structure to have the text content inside a span or a div.
Check updated fiddle.
Refer code:
span {
float: right;
border-radius: 2px;
background-color: #26a69a;
padding: 0 6px;
}
.wrapper {
width: 540px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: inline-block;
vertical-align: top;
}
<div class="container">
<span>RIGHT</span>
<div class="wrapper">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.
</div>
</div>
Please try this
body{
float: left;
margin: 0;
max-width: 100%;
}
.container {
box-sizing: border-box;
display: block;
float: left;
overflow: hidden;
padding-right: 80px;
position: relative;
text-overflow: ellipsis;
white-space: nowrap;
width:100%;
}
span {
background-color: #26a69a;
border-radius: 2px;
display: block;
float: right;
padding: 0 6px;
position: absolute;
right: 0;
top: 0;
}
<div class="container">
<span>RIGHT</span>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.
</div>
JS Fiddle demo : https://jsfiddle.net/geogeorge/gzjf2psq/6/
Related
I've text to show inside a tooltip on hover of an icon.
The tooltips width need to be:
max-width:300px
if the text fit in one line and less than 300px, adjust to text width
if the text need many lines, the tooltip width need to be 300px
How can I define the .tooltip .content-tooltip to work like describe before ?
I've tried using white-space: nowrap; and some word-break ...
.tooltip {
position: relative;
margin-right: 5px;
display: block;
text-align: center;
width: 75px;
padding: 5px;
border: 1px dotted #000;
margin: 70px auto;
}
.tooltip .content-tooltip {
position: absolute;
bottom: calc(100% + 13px);
background: #000;
color: #fff;
display: block;
left: 50%;
transform: translateX(-50%);
padding: 5px 10px 6px;
display: none;
text-align: left;
max-width: 300px;
width: auto;
}
.tooltip:hover .content-tooltip {
display: block;
}
<h1>Example</h1>
<span class="tooltip">
short text
<span class="content-tooltip">
Lorem
</span>
</span>
<span class="tooltip">
long text
<span class="content-tooltip">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla nunc massa, eleifend a feugiat ac, varius eu eros.
Praesent at vulputate risus. Pellentesque dictum pulvinar lectus.
</span>
</span>
<hr><hr>
<h1>What I expect</h1>
<span class="tooltip">
short text
<span class="content-tooltip">
Lorem
</span>
</span>
<span class="tooltip">
long text
<span class="content-tooltip" style="width: 300px">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla nunc massa, eleifend a feugiat ac, varius eu eros.
Praesent at vulputate risus. Pellentesque dictum pulvinar lectus.
</span>
</span>
I fixed the issue by taking out the tooltip from the text content element, and wrapped the tooltip and text content width another div which will act container for both of them, so that tooltip won't take up the width as per the text content instead it will take the width as specified.
This how you should do it:
body {
display: flex;
gap: 30px;
padding: 1rem;
}
.text {
padding: 10px;
}
.tooltip {
display: none;
position: absolute;
background: #000;
color: #fff;
max-width: 300px;
padding: 10px;
}
.text-contianer:hover > .tooltip {
display: inline-block;
}
<div class="text-contianer">
<div class="text">
Short
</div>
<span class="tooltip">
Lorem
</span>
</div>
<div class="text-contianer">
<div class="text">
Long
</div>
<span class="tooltip">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla nunc massa, eleifend a feugiat ac, varius eu eros.
Praesent at vulputate risus. Pellentesque dictum pulvinar lectus.
</span>
</div>
The desired solution can be attained if you set a height value to the tooltip.
I see that you've used max-width, which is the desirable here but one thing which I guess you missed is that the default value of height for tooltip is auto, due to which if you reduce the width of the tooltip, the component will adjust to increase the height. One simple solution as I mentioned is to set a height value, and overflow to hidden
Updated CSS Code:
.tooltip {
position: relative;
margin-right: 5px;
display: block;
text-align: center;
width: 75px;
padding: 5px;
border: 1px dotted #000;
margin: 70px auto;
}
.tooltip .content-tooltip {
position: absolute;
bottom: calc(100% + 13px);
background: #000;
color: #fff;
display: block;
left: 50%;
transform: translateX(-50%);
padding: 5px 10px 6px;
display: none;
text-align: left;
max-width: 320px;
height: 15px; //height value set to avoid component auto adjust
overflow: hidden; //overflow hidden
}
.tooltip:hover .content-tooltip {
display: block;
}
Found myself a solution :
.tooltip {
position: relative;
margin-right: 5px;
display: block;
text-align: center;
width: 75px;
padding: 5px;
border: 1px dotted #000;
margin: 70px auto;
}
.tooltip .sizing-tooltip {
position: absolute;
bottom: calc(100% + 13px);
left: 50%;
transform: translateX(-50%);
display: none;
width: 300px;
}
.tooltip .content-tooltip {
background: #000;
color: #fff;
padding: 5px 10px 6px;
display: block;
text-align: left;
max-width: 300px;
width: auto;
margin: 0 auto;
}
.tooltip:hover .sizing-tooltip {
display: flex;
}
<h1>Example</h1>
<span class="tooltip">
short text
<span class="sizing-tooltip">
<span class="content-tooltip">
Lorem
</span>
</span>
</span>
<span class="tooltip">
long text
<span class="sizing-tooltip">
<span class="content-tooltip">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla nunc massa, eleifend a feugiat ac, varius eu eros.
Praesent at vulputate risus. Pellentesque dictum pulvinar lectus.
</span>
</span>
</span>
image of what I am trying to do
I am trying to have an image aligned left with a colored text box that overlaps (positioned off-center) to the image. I can get that by creating a div class="mycontainer" with div class= my text-box" and css (see below), but I can't seem to figure out how to make text wrap around the colored text box. It wraps around the image, but keeps pushing the text box down.
.mycontainer {
position: relative;
font-family: Arial;
width: 100%;
}
.mytext-block {
position:absolute;
margin-top: 50px;
margin-left: 150px;
background-color:turquoise;
color: black;
padding-left: 10px;
padding-right: 10px;
}
Hi Rachel here is a way to wrap text around an image which is inset into a colored box.
.mycontainer {
position: relative;
font-family: Arial;
width: 100%;
margin: auto;
max-width: 670px;
}
.mytext-block {
width: 300px;
height: 400px;
float: left;
vertical-align: middle;
background-color: turquoise;
color: black;
}
#image {
width: 250px;
height: 100px;
position: relative;
float: left;
left: -190px;
shape-outside: content-box;
margin: 140px -170px 0 0;
}
#text {
font-size: 140%;
padding: 30px;
}
<div class="mycontainer">
<div class="mytext-block"></div>
<img id="image" src="https://lorempixel.com/300/100/">
<div id="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellen tesque placerat dolor sed dolor euismod hendrerit. Integer eget elit nibh. Vestibulum posuere. Sed elementum bibendum magna, nec tempus augue egestas quis. Pellentesque lacus justo, vehicula vitae nisl sed, semper euismod dui.
</div>
</div>
<!-- Click the "Run code snippet" below to see it. Hope this helps. -->
I am making a simple messaging app UI. I am trying to make the messages anchor to the bottom of the screen like most modern messaging applications. So far, here is the bare bones of my messaging UI:
HTML
<div class="main-wrapper">
<div class="contact-list">
contacts here
</div>
<div class="conversation-area">
<div class="msg msg-them">this is Alison</div>
<div class="msg msg-me">this is me!</div>
<div class="msg msg-them">you are so cool! :)</div>
<div class="msg msg-them">seriously.</div>
</div>
</div>
SASS
body, html {
height: 100%;
}
.main-wrapper {
height: 100%;
margin: 0px auto;
overflow: hidden;
.contact-list{
float: left;
width: 200px;
height: 100%;
background-color: #aaa;
border-right: 2px solid #777;
}
.conversation-area{
overflow: hidden;
height: 100%;
background-color: #ccc;
.msg{
vertical-align: bottom;
border: 1px solid black;
&-them{
background-color: blue;
float: left;
max-width: 250px;
display: inline;
clear: both;
}
&-me{
background-color: red;
float: right;
max-width: 250px;
display: inline;
clear: both;
}
}
}
}
Whenever a new message comes in, I'll insert it as the last child of the .conversation-area div. I have the messages stacking just like I want them. I just need to make them stick to the bottom of the .conversation-area div.
I've tried messing with position attributes of both the parent and child divs, as well as tried to get vertical-align to work, but so far I haven't gotten it functioning.
How can I make my messaging app look exactly the same EXCEPT the messages stick to the bottom rather than the top?
Here's the jsFiddle: https://jsfiddle.net/63vufn7u/1/
You can achieve this with display:table-cell; and vertical-align:bottom;
I have made some changes to your code but im sure you can adapt now its working:
.main-wrapper {
width: 100%;
height: 200px;
font-family:sans-serif;
}
.contact-list {
width:25%;
display: table-cell;
height: 200px;
background: #555;
color: #fff;
text-align: center;
float: left;
}
#conversation-area {
height: 200px;
width: 300px;
background: steelblue;
display: table-cell;
vertical-align: bottom;
}
.msg {
display: block;
margin: 15px 10px;
}
.msg p {
border-radius:5px 5px 5px 5px;
background: #fff;
display: inline;
padding: 5px 10px;
box-shadow: 3px 3px 5px 0px rgba(0,0,0,0.75);
}
.msg-me {
text-align: left;
}
.msg-me p {
border-radius:15px 15px 15px 0px;
}
.msg-them {
text-align: right;
}
.msg-them p {
border-radius:15px 15px 0px 15px;
}
<div class="main-wrapper">
<div class="contact-list">
Contacts
</div>
<div id="conversation-area">
<div class="msg msg-them"><p>this is Alison</p></div>
<div class="msg msg-me"><p>this is me!</p></div>
<div class="msg msg-them"><p>you are so cool! :)</p></div>
<div class="msg msg-them"><p>seriously.</p></div>
</div>
</div>
Your friendly neighborhood Flexbox solution:
On the container, you could also use the justify-content property to align it's contents to the bottom:
.conversation-area {
display:flex;
flex-direction:column;
justify-content:flex-end;
}
Learn more about flexbox here: http://www.flexboxin5.com
The easiest way I've found is to use flex-direction: column-reverse;.
The drawback to flex-direction: column; is that it starts you at the top and you have to scroll down for older texts. And that's not how chat app interfaces tend to work.
column-reverse makes the texts stick to the bottom. The tradeoff is you have to insert your messages in ascending time stamp order instead of reversed like you normally would, because flex-box does the reversing in css. Same with any animations. So new text bubble animation would be applied to the :first child rather than the :last child.
Here's an example without animations: https://jsfiddle.net/ut1Lybsj/1/
<style>
.container {
height: 200px;
width: 200px;
overflow-y: scroll;
display: flex;
flex-direction: column-reverse;
border: 1px solid black;
}
.container div {
margin-top: 20px;
}
</style>
<div class="container">
<div style="background:red;">First Item<br/> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce malesuada semper purus, non rutrum nulla mollis id. Nunc faucibus hendrerit nunc, eu rhoncus nisi congue non. </div>
<div style="background: skyblue;">Second Item<br/> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce malesuada semper purus, non rutrum nulla mollis id. Nunc faucibus hendrerit nunc, eu rhoncus nisi congue non. </div>
<div style="background: green;">Third Item<br/> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce malesuada semper purus, non rutrum nulla mollis id. Nunc faucibus hendrerit nunc, eu rhoncus nisi congue non. </div>
</div>
I'm not a clever man, just trying to make a responsive page with a left sidebar, so far that's okay, but I cannot figure out how to align the content of the main div to the top.
This is the code: https://jsfiddle.net/kissja74/df8vkn2a/
#content {
position: relative;
max-width: 400px;
margin: 0 auto;
}
#menu {
position: relative;
width: 50px;
height: 30px;
margin-left: -50px;
background-color: blue;
}
<div id='content'>
<div id='menu'>menu</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi interdum porttitor accumsan. Aliquam at egestas lacus, sed ultrices dui.</p>
</div>
Add to your style
#menu {float: left}
I want to center a DIV which width is unknown. It should be as width as the actual content. I tried:
<div style="background-color: red; width: 300px; text-align: center; margin: 0 auto;">dsffsffsfsddf</div>
it only works when "width" is set. Auto wont help.
display: table; margin: 0 auto;
http://jsfiddle.net/vabxz/
div{
display:table
margin:0 auto;
}
Take a look at this previous answer: centering variable width divs
But one answer is to basically use css-floats
<style type="text/css">
#hideoverflow { overflow: hidden; }
#outer { position: relative; left: 50%; float: left; }
#inner { position: relative; left: -50%; float: left; }
</style>
<div id="hideoverflow">
<div id="outer">
<div id="inner">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id velit vel augue fringilla rhoncus at et odio. Suspendisse potenti. Aliquam justo libero, commodo ut iaculis in, placerat vel purus.
</div>
</div>
</div>
It's quite simple (http://jsfiddle.net/bukfixart/tYyJN/):
<div style="border:1px solid #000;text-align:center;">
<div style="border: 1px solid #f00; display:inline-block;">some content</div>
</div>
The key points are text-align:center for the outer box and display:inline-block for the inner box
Check out my fiddle: http://jsfiddle.net/PRUBd/
CSS
body {
text-align: center;
}
#container {
text-align: left;
border: 1px solid black;
padding: 10px;
display: inline-block;
}
HTML
<div id="container">
This div is as wide as its content and centered!
</div>