Keeping HTML list items inline and overlaying text - html

I need some help understanding how to solve this problem. I've got multiple list elements that each have text with an unknown length. On each side of this text, there will be a small image/icon. Here is an example of the HTML:
.truncate>img {
width: 25px;
height: 25px;
}
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="container-fluid">
<div class="row">
<div class="col-sm-4">
Content
</div>
<div class="col-sm-4">
<ul id="myList" class="list-group">
<li class="list-group-item" value="">
<div class="truncate">
<img src="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Close_Box_Red.png">A long text string that is about this length
<img src="http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.med.png">
</div>
</li>
<li class="list-group-item" value="">
<div class="truncate">
<img src="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Close_Box_Red.png">A long text string that is about this length
<img src="http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.med.png">
</div>
</li>
<li class="list-group-item" value="">
<div class="truncate">
<img src="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Close_Box_Red.png">A long text string that is about this length
<img src="http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.med.png">
</div>
</li>
</ul>
</div>
<div class="col-sm-4">
Content
</div>
</div>
</div>
JS Fiddle.
I want the text to have an ellipsis at the end if it is longer than the available width. I know how to use text-overflow: ellipsis and I can get the text to have an ellipsis but only after the right image drops below the text.
Some things that I have tried:
I have tried surrounding the whole li element in a div and using
display: inline-block
Similarly, I've tried surrounding just the image tags and text in a
single div with inline-block which has been closest to the desired result.
I tried putting the text in a span element and various CSS styling.
Each time the right image still jumps below the text.
Desired result:
And actual result:
Let me know if I should include any other information. Thanks in advance for any advice.

One trick could be to manually position the images on left and right and then place the text inside an absolutely-positioned div:
.left { position: absolute;
left: 0px;
}
.right { position: absolute;
right: 0px;
}
.text { position: absolute;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
left: 24px;
right: 24px;
}
.container { display: inline-block;
width: 100%;
position: relative;
height: 24px;
}
<ul>
<li>
<div class="container">
<img class="left" src="http://raksy.dyndns.org/cross.png">
<div class="text">
This is a text
</div>
<img class="right" src="http://raksy.dyndns.org/disc.png">
</div>
</li>
<li>
<div class="container">
<img class="left" src="http://raksy.dyndns.org/cross.png">
<div class="text">
This is a much longer text that most probably will have to be truncated
</div>
<img class="right" src="http://raksy.dyndns.org/disc.png">
</div>
</li>
<li>
<div class="container">
<img class="left" src="http://raksy.dyndns.org/cross.png">
<div class="text">
This is a text (somewhat longer but not huge)
</div>
<img class="right" src="http://raksy.dyndns.org/disc.png">
</div>
</li>
</ul>

Probably not 100% what you want, but may get you the rest of the way:
Fiddle
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: inline-block;
width: 50%;
}
.list-group-item{
background-color:transparent;
border:none;
padding:0px 15px;
cursor: pointer;
white-space: nowrap;
}

I tried with a div wrapper, including display: inline-block, and it worked well:
http://jsfiddle.net/nozimica/y3oy723v/
The key is:
Create a div wrapper.
Assign display: inline-block;
Give it a width.

Floats work, and so does absolute positioning:
.truncate>img {
width: 25px;
height: 25px;
position: absolute;
left: 8px;
}
.truncate img:first-child {
right: 8px;
left: auto;
}
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 0 30px;
}
<li class="list-group-item" value="">
<div class="truncate">
<img src="...">
<img src="..."> A long text string that is about this length
</div>
</li>
Demo

Related

Creating a new line of text below the existing one when setting the white-space property back to normal

I'm working on a Google imitation project. Currently, I'm making the apps menu:
Screenshot of the menu
If the name of the google service is long enough to occupy two lines, then just the first line will be shown with some ellipsis, like this:
Service's name occupying one line
If you hover over the service, then the text will be shown completely, like this:
Service name complete
To imitate this effect, I put the image, within a div, and the text in a wrapper with the class name app-container, creating the following HTML:
<div class="google-apps-menu-container">
<ul class="google-apps-menu">
<li class="app-container">
<div class="img-app-container"><img src="images/applogos/profile-photo.svg" alt="" id="user-profile-picture-for-apps" class="app-logo"></div>
<p class="app-name">Cuenta</p>
</li>
<li class="app-container">
<div class="img-app-container"><img src="images/applogos/search.svg" alt="" class="app-logo"></div>
<p class="app-name">Búsquda</p>
</li>
<li class="app-container">
<div class="img-app-container"><img src="images/applogos/bussisnes-logo.svg" alt="" class="app-logo"></div>
<p class="app-name">Perfil de Empresa</p>
</li>
</ul>
</div>
To achieve the text abbreviation described before, I have the following CSS applied to the paragraph with the name of the service:
.app-name
{
position: absolute;
bottom: 0;
text-align: center;
max-width: 11ch;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Whenever you hover over the service name, I just set the white-space property to normal. It works. However, it creates a second line of text above the existing one and not below as the google web page does:
The second line of the paragraph is created below the existing one (As I want)
The second line of the paragraph is created above the existing one (As I don't want to)
I have to say that the paragraph in which the name of the service is has a position property of "absolute", whereas the wrapper of the image is just an element centered in a flexbox display.
Please, help me to solve the issue.
If you need any extra information, please tell me. It's my first time posting a question in stack overflow, so I apologize if something is wrong with the post.
Thank you very much!
Using your snippets as a starting point I was able to reproduce the error. In my case, swapping bottom: 0 to top: 0 fixed the alignment.
<head>
<style>
.google-apps-menu-container
{
background-color: red;
}
.google-apps-menu
{
background-color: blue;
display: inline-block;
}
.app-container
{
width: 50;
height: 50;
display: inline-block;
position: relative;
}
.app-name
{
position: absolute;
top: 0;
text-align: center;
max-width: 11ch;
overflow: hidden;
/* white-space: nowrap; */
text-overflow: ellipsis;
}
</style>
</head>
<body>
<div class="google-apps-menu-container">
<ul class="google-apps-menu">
<li class="app-container">
<div class="img-app-container"><img src="images/applogos/profile-photo.svg" alt="" id="user-profile-picture-for-apps" class="app-logo"></div>
<p class="app-name">Cuenta</p>
</li>
<li class="app-container">
<div class="img-app-container"><img src="images/applogos/search.svg" alt="" class="app-logo"></div>
<p class="app-name">Búsquda</p>
</li>
<li class="app-container">
<div class="img-app-container"><img src="images/applogos/bussisnes-logo.svg" alt="" class="app-logo"></div>
<p class="app-name">Perfil de Empresa</p>
</li>
</ul>
</div>
</body>
```
What you need is actually very easy. Just make all icons the same height and add text below icons. Do not use position absolute, just basic layout with flex.
.container {
width: 300px;
height: 500px;
padding: .5rem;
background-color: #222;
color: #fff;
}
.row {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.col {
width: 70px;
padding: .5rem;
margin-bottom: .5rem;
border: 1px solid red;
border-radius: 1rem;
}
.col:hover {
background-color: #666;
}
.icon {
width: 100%;
height: 60px;
margin-bottom: .25rem;
border: 1px solid yellow;
}
.text {
border: 1px solid pink;
}
<div class="container">
<div class="row">
<div class="col">
<div class="icon">ICON</div>
<div class="text">Lorem Ipsum</div>
</div>
<div class="col">
<div class="icon">ICON</div>
<div class="text">Lorem</div>
</div>
<div class="col">
<div class="icon">ICON</div>
<div class="text">Lorem</div>
</div>
<div class="col">
<div class="icon">ICON</div>
<div class="text">Lorem</div>
</div>
<div class="col">
<div class="icon">ICON</div>
<div class="text">Lorem</div>
</div>
<div class="col">
<div class="icon">ICON</div>
<div class="text">Lorem</div>
</div>
</div>
</div>

overflow: hidden and ellipsis not working with background-image before text to hide

I have some text I want to have hidden with ellipsis. But when there is a background image in a div before the text, it doesn't work, https://jsfiddle.net/ypbgkrod/6/,
.block {
display: flex;
margin-top: 0.3em;
}
.summary-content {
margin-left: 0.2rem;
width: 100%;
}
.summary-content .description {
overflow: hidden;
width: 100%;
}
.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<div class="block">
<div class="thumbnail">
<div class="thumb" role="img" style="background-image: url(''); height: 73px; width: 130px;"></div>
</div>
<div class="summary-content" data-permlink="i-want-more-of-my-life">
<div class="description">
<p class="ellipsis">Some random text that is long, long…Some random text that is long, long…Some random text that is long, long…Some random text that is long, long…</p>
</div>
<div class='slider'>
Need this to not overflow hide. Need this to not overflow hide. Need this to not overflow hide.
</div>
</div>
</div>
Removing the <div class="thumbnail"> section has the text ellipsis and overflow hidden works, with the part for the slider working as well by not being hidden.
This isn't the full original problem, as I tried to resolve the issue below by doing the above, but I couldn't get the overflow ellipsis to work on the text required.
Context of original issue:
The original code was that I have a pop up div with a Slider that needs to go over the overflow div, but it doesn't and gets cut off when the .block div ends. Removing overflow hidden on the .summary-content div has the Slider pop up fully, but then the ellipsis doesn't work.
The text ellipsis was working WITH the image, but it's when there was no extra <div class="description">. Problem is it was cutting off the Slider div with the overflow hidden on the summary-content div which the slider was also in.
.block {
display: flex;
margin-top: 0.3em;
}
.summary-content {
margin-left: 0.2rem;
width: 100%;
overflow: hidden;
}
.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<div class="block">
<div class="thumbnail">
<div class="thumb" role="img" style="background-image: url(''); height: 73px; width: 130px;"></div>
</div>
<div class="summary-content" data-permlink="i-want-more-of-my-life">
<p class="ellipsis">Some random text that is long, long…Some random text that is long, long…Some random text that is long, long…Some random text that is long, long…</p>
<div class='slider'>slider</div>
</div>
</div>
So when I added the extra encapsulation (description) for the <p class="ellipsis">, the <div class='slider'> isn't being hidden anymore, but the ellipsis doesn't work. I just wanted to let you know that the ellipsis not working is resolved when I don't have the description div, but the overflow is hiding part of the Slider popup that's within the summary-content div, and I can't have that.
If you could get the first HTML and CSS to hide the overflow on the ellipsis text, and not be applied to the slider, that would be the solution I'm looking for, as the second part of code doesn't show the whole problem fully to have the problem show up in a jsfiddle.
Thanks!
Just add min-width: 0; in summary-content CSS. Thanks
.summary-content {
min-width: 0;
width: 100%;
}
There are 2 divs .thumbnail and .summary-content. If you put width 100% for summary-content then it will go out of the screen so I added the below CSS
.summary-content {
width: 80%;
}
.block {
display: flex;
}
.summary-content {
width: 80%;
}
.summary-content .description {
overflow: hidden;
width: 100%;
}
.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<div class="block">
<div class="thumbnail">
<div class="thumb" role="img" style="background-image: url(''); height: 73px; width: 130px;"></div>
</div>
<div class="summary-content" data-permlink="i-want-more-of-my-life">
<div class="description">
<p class="ellipsis">Some random text that is long, long…Some random text that is long, long…Some random text that is long, long…Some random text that is long, long…</p>
</div>
<div class='slider'>
Need this to not overflow hide. Need this to not overflow hide. Need this to not overflow hide.
</div>
</div>
</div>

How do I set an image background, using the <img> tag rather than CSS?

I have created 4 columns. Each column consisting of an image, where I want to place text over each image.
To achieve this, I obviously need to set the image as a background. I am aware that this can be achieve through CSS (which is what I am currently doing) but I would like to place the image as a background, within my HTML file. The reason; so that I can enter the relevant 'alt' text for SEO purposes.
How can I best achieve this?
Just put the img in the col container, set that element to position: relative; then use absolute positioning to put the text over the image.
* {margin:0;}
.col {
float: left;
width: 25%;
position: relative;
}
img {
display: block;
width: 100%;
}
.overlay {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
background: black;
color: white;
padding: 1em;
}
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
#img-as-background {
position: relative;
overflow: hidden;
}
#img-as-background .container {
height: 500px;
padding: 20px;
}
#img-as-background img {
position: absolute;
width: 100%;
top: 0;
bottom: 0;
z-index: -1;
}
<div id="img-as-background">
<img src="https://image.freepik.com/free-vector/modern-abstract-background_1048-1003.jpg" alt="" />
<div class="container">
Overflow text Overflow text Overflow text Overflow text Overflow text Overflow text Overflow text
</div>
</div>

Align Img Vertically within Div HTML and CSS

I have found the following solution for aligning an img vertically within a div
https://stackoverflow.com/a/7310398/626442
and this works great for a basic example. However, I have had to extend this and I want a row with two bootstrap col-md-6 columns in it. In the first column I want a 256px image, in the second I want a h1, p and a button. I have to following HTML:
<div class="home-costing container">
<div class="row">
<div class="frame">
<span class="helper"></span>
<div class="col-md-6">
<img src="http://www.nijmegenindialoog.nl/wp-content/uploads/in.ico" height="256" width="256" />
</div>
<div class="col-md-6">
<h2>Header</h2>
<p>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br /><br/>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
</p>
<a class="btn btn-default"
href='#Url.Action("Index", "Products")'
role="button">
Learn More
</a>
</div>
</div>
</div>
</div>
and the following CSS:
.home-costing {
color: #fff;
text-align: center;
padding: 50px 0;
border-bottom: 1px solid #ff6500;
}
.home-costing h2 {
text-transform: uppercase;
font-size: 60px;
}
.home-costing p {
font-size: 18px;
}
.home-costing .frame {
height: 256px;
width: 256px;
border: 0;
white-space: nowrap;
text-align: center;
margin: 1em 0;
}
.home-costing .helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.home-costing img {
vertical-align: middle;
max-height: 256px;
max-width: 256px;
}
The problem is that now the second column is no longer contained and the text does not wrap and goes off to the right.
How can I center align my image in the first column with the text in the right column and still get the correct wrapping in the second column?
Fiddler: https://jsfiddle.net/Camuvingian/1sc40rm2/2/
Your HTML needed updated, in Bootstrap, the div order should ALWAYS go .container > .row > .col- * - *, your code however went .container > .row > .frame > .col- * - *. I have corrected your HTML and now your code works.
HTML:
<div class="home-costing container">
<div class="row">
<div class="col-md-6">
<img src="http://www.nijmegenindialoog.nl/wp-content/uploads/in.ico" height="256" width="256" class="center-block" />
</div>
<div class="col-md-6">
<h2>UserCost</h2>
<p>Hello, I'm a paragraph</p>
<a class="btn btn-default"
href='#Url.Action("Index", "Products")'
role="button">
Learn More
</a>
</div>
</div>
</div>
</div>
Link to finished code example:
Codepen - Updated & working code
This fixes the word wrap issue also on the p tag.
CSS:
p {
font-size: 18px;
word-wrap: break-word;
}

Sliding icon next to dynamic text with ellipsis

I have a list of items and to the end of some items I want to add an icon (or several of them - it's dynamic). Items' container has a fixed width and if item's text size is too big - I'd like to add ellipsis.
So the problem is that icon is added next to text and if text is long - the icon moves off the container. The template for all items has to be the same.
Note: not all items have icons and some icons may have more than one icon.
Note2: javascript solution is not applicable, want to make it in pure CSS.
Actual:
Expected:
Any help is much appreciated.
body {
font-size: 20px;
}
.container {
width: 150px;
background: #ff0;
list-style: none;
padding: 0px;
margin: 0px;
}
.container li {
border-bottom: 1px solid #000;
}
.item {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.icon {
background: #0f0;
}
<ul class="container">
<li>
<div class="item">
<span class="text">Text 1</span>
<span class="icon">12</span>
</div>
</li>
<li>
<div class="item">
<span class="text">Text 2 Text 2</span>
<span class="icon">12</span>
</div>
</li>
<li>
<div class="item">
<span class="text">Text 3 Text 3 Text 3</span>
<span class="icon">12</span>
</div>
</li>
<li>
<div class="item">
<span class="text">Text 4 Text 4 Text 4</span>
<span class="icon"></span>
</div>
</li>
</ul>
If anyone still looking for solution I figured it out:
Markup:
<div class="block-wrap">
<div class="block">
<div class="icon"></div>
<div class="text">Text text text text text text text text text text text text text text text text text text text</div>
</div>
</div>
Styles:
.block-wrap {
display: inline-block;
max-width: 100%;
}
.block {
width: 100%;
}
.text {
display: block;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.icon {
float: right;
margin-left: 10px;
width: 20px;
height: 14px;
background-color: #333;
}
https://jsfiddle.net/rezed/n1qft37L/
Try like this: Demo
Fix the width for the text next to .item class and make display as inline-block to get floated with icon.
CSS:
.item {
width:100%;
overflow: hidden;
}
.item >span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width:70%;
display:inline-block;
}
.icon {
background: #f00;
display:inline-block;
max-width:50px;
}
Hope this helps :)