CSS Truncate inline text only if needed - html

I'm trying to style a reusable component such that it will stay inline but truncate its contents whenever it overflows. What makes it trickier is that I need to have an icon on the right.
The main issue is that I need the icon to stay on the same line, so I compensate for it in the width of the truncated text (width: calc(100% - 40px)), which makes any non-truncating example be that much shorter than it's normal width.
See the snippet below and how the short example is barely visible.
body, .container {
width: 100%;
padding: 0;
margin: 50px 0;
}
.quantity-value {
display: inline-block;
max-width: 100%;
margin-right: 16px;
background: #f1f1f1;
}
.value-and-icon-wrapper {
display: inline-block;
width: 100%;
}
.icon {
padding-left: 5px;
}
.truncated-text {
display: inline-block;
width: calc(100% - 40px);
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
vertical-align: top;
-webkit-line-clamp: 1;
}
<!-- Example 1: short -->
<div class="container">
<div class="quantity-value">
<div class="value-and-icon-wrapper">
<span class="truncated-text">67</span>
<span class="icon">ℹ️</span>
</div>
</div>
other content
</div>
<!-- Example 2: long -->
<div class="container">
<div class="quantity-value">
<div class="value-and-icon-wrapper">
<span class="truncated-text">68 long text starting now lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet</span>
<span class="icon">ℹ️</span>
</div>
</div>
other content
</div>

This is because you are using a lot of inline-block and the width of inline-block is defined by its content so if you set 100% - 40px for a child item, it means its width minus 40px
Try to do it differently like below using flexbox:
body, .container {
width: 100%;
padding: 0;
margin: 50px 0;
}
.quantity-value {
display: inline-flex;
max-width: calc(100% - 16px); /* don't forget to account for margin here */
margin-right: 16px;
background: #f1f1f1;
}
.icon {
padding-left: 5px;
}
.truncated-text {
flex:1;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
<!-- Example 1: short -->
<div class="container">
<div class="quantity-value">
<span class="truncated-text">67</span>
<span class="icon">ℹ️</span>
</div>
other content
</div>
<!-- Example 2: long -->
<div class="container">
<div class="quantity-value">
<span class="truncated-text">68 long text starting now lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet</span>
<span class="icon">ℹ️</span>
</div>
other content
</div>
Without flexbox you can do it like below:
body, .container {
margin: 50px 0;
}
.quantity-value {
display: inline-block;
max-width: calc(100% - 16px); /* don't forget to account for margin/padding here */
margin-right: 16px;
background: #f1f1f1;
padding-right:20px;
box-sizing:border-box;
position:relative;
}
.icon {
padding-left: 5px;
position:absolute;
right:0;
top:0;
bottom:0;
}
.truncated-text {
display:block;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
<!-- Example 1: short -->
<div class="container">
<div class="quantity-value">
<span class="truncated-text">67</span>
<span class="icon">ℹ️</span>
</div>
other content
</div>
<!-- Example 2: long -->
<div class="container">
<div class="quantity-value">
<span class="truncated-text">68 long text starting now lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet</span>
<span class="icon">ℹ️</span>
</div>
other content
</div>

Try applying the style text-overflow: ellipsis to the div that contains the text to be truncated.
MDN Documentation for text-overflow

Related

Remove extra bottom padding on CSS columns

I have a CSS column layout.
I find however, that there is an unnecessary gap at the bottom (indicated by the pink arrow in screengrab).
This gap disappears if I remove display: inline-block from the CSS, but then some of the <section> elements break in two. I've also tried using break-inside: avoid but it appears to do nothing at all.
Is there a fix for this?
<div class="feed-index">
<div class="feed">
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
</div>
</div>
.feed-index .feed {
column-count: 3;
column-gap: 40px;
}
.feed-index .feed > section {
display: inline-block;
margin-bottom: 50px;
overflow: hidden;
}
Add vertical-align:top; to your CSS for .feed-index .feed > section
The reason is your overflow: hidden; actually changes the block. See more info here: Why does x-overflow:hidden cause extra space below?

Centering element vertically on mobile devices

I have problem problem with centering element on mobile devices, when height decreases, top content is hidden but on desktop is okey. Please see below link for see problem in screenshot
Desktop version
Mobile version
HTML
<div class="modal">
<div class="modal-section">
<div class="modal-area">
<div class="header">
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
<div class="items">
<div class="element">
<div class="img">
<img src="https://via.placeholder.com/150" alt="">
</div>
<div class="desc">
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
</div>
<div class="element">
<div class="img">
<img src="https://via.placeholder.com/150" alt="">
</div>
<div class="desc">
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
</div>
</div>
</div>
</div>
</div>
CSS:
.modal {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: oldlace;
color: #2F2F2F;
z-index: 99999;
overflow: auto;
}
.items {
display: flex;
margin: 20px 0;
justify-content: center;
}
.element {
overflow: hidden;
background: #2F2F2F;
color: #fff;
box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.75);
margin: 20px;
cursor: pointer;
width: 150px;
}
.img {
height: 150px;
}
example code
https://jsfiddle.net/twzud65n/3/
Seems like you have some elements that don't quite do something and have no styling applied to fit in. What is the suppose of modal-section?
Because you have position: fixed on your modal, you need to tell its children to not overflow their parent. width: 100% does that, height: auto means it can scale as much as needed allowing to scroll.
Try this:
.modal-section {
width: 100%;
height: auto;
}
It's because of justify-content: center which is centering your element (even if it's overflowing). You can turn it off and add margin:auto to .modal-section.
Example
.modal {
display: flex;
flex-flow: column nowrap;
align-items: center;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: oldlace;
color: #2F2F2F;
z-index: 99999;
overflow: auto;
}
.modal-section {
margin: auto;
}
.items {
display: flex;
margin: 20px 0;
justify-content: center;
}
.element {
overflow: hidden;
background: #2F2F2F;
color: #fff;
box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.75);
margin: 20px;
cursor: pointer;
width: 150px;
}
.img {
height: 150px;
}
<div class="modal">
<div class="modal-section">
<div class="modal-area">
<div class="header">
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
<div class="items">
<div class="element">
<div class="img">
<img src="https://via.placeholder.com/150" alt="">
</div>
<div class="desc">
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
</div>
<div class="element">
<div class="img">
<img src="https://via.placeholder.com/150" alt="">
</div>
<div class="desc">
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
</div>
</div>
</div>
</div>
</div>

Make text NOT align with sibling element text

I don't know if the title is good enough, but I'll explain it here more thoroughly. So I've got the following HTML code:
<section>
<aside>
<div class = "container">
<div class = "col-md-4 lefterino">
<p><span>Daily Posts</span>Lorem ipsum dolor sit amet consecteteur adipiscing elit lorem ipsum dolor sit amet consecteteur adipiscing elit lorem ipsum dolor sit amet consecteteur adipiscing elit</p>
<p><span>Daily Posts</span>Lorem ipsum dolor sit amet consecteteur adipiscing elit lorem ipsum dolor sit amet consecteteur adipiscing elit lorem ipsum dolor sit amet consecteteur adipiscing elit</p>
</div>
<div class = "col-md-3 midderino">
<p>asd</p>
</div>
<div class = "col-md-4 righterino">
<p>asd</p>
</div>
</div>
</aside>
</section>
And the following CSS code:
section {
height: 100%; // screen height
}
aside {
bottom: 0em;
position: absolute;
width: 100%;
}
.lefterino {
text-align: right;
color: #fff;
font-weight: 300;
}
.lefterino p {
width: 75%;
float: right;
opacity: 0.92;
margin-bottom: 4em;
}
The problem is that I want to align all the elements from the container div's at the bottom of the section. For the first div (lefterino) it works fine. For the second and third one, the elements (in my case the <p>) always gets aligned with the elements from the first div.
Here's an image that shows the effect:

left, right, and middle content position

i must use css to alter the positions; the only thing that seems to be working is the right position nav bar and the liquid layout, but the "content" and "right navigation bar" is ot being positioned properly.
I want content to be in the middle, leftnavigation on the left, and right navigation on the right.
<title>CSS liquid layout</title>
<style type="text/css">
.due {
color: #ff0000;
font-weight: bold;
}
#leftnavigation{
position:absolute;
left:10px;
top:10px;
width:250px;
}
#rightnavigation {
float:right;
width:250px;
height:800px;
}
#content {
float:center;
}
</style>
</head>
<body leftmargin="0" topmargin="0" bgcolor="#ccff99">
<div id="app">
<div id="rightnavigation">
<h1>Right Navigation</h1>
link Instructor
Course <a href="http://www,google.com">
Resume
project
</a>
</div>
<div id="content">
<h1>Sample Content</h1>
<p>
This is the content section of the page. Use structural markup
like <p></p>
to keep the page valid in XHTML.
</p>
<h2>Lorem Ipsum</h2>
</div>
<div id="leftnavigation">
<h1>Left Navigation</h1>
<p>
Page 1 Page 2 <a href="http://www,google.com">
Page
3
</a> Page 4 Page 5 <br />
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor
amum.
</p>
<h2>Lorem Ipsum</h2>
</div>
</div>
You could try this:
CSS
.app {
width: 100%
height: 100%;
}
.due {color: #ff0000;
font-weight: bold;
}
#rightnavigation {
float: left;
width: 33.333%
}
#leftnavigation{
float: left;
width: 33.333%
}
#content {
float: left;
width: 33.333%;
}
HTML
<div class="app">
<div id="leftnavigation">
<h1> Left Navigation </h1>
</div>
<div id="content"></div>
<div id="rightnavigation">
<h1>Right Navigation</h1>
</div>
</div>
Here's a live demo of the example - EXAMPLE
There are some errors in your HTML and CSS that need to be addressed before changing the styles to accomplish what you need.
In your HTML, there are still some unclosed tags. Especially the <div id="rightnavigation"> tag is never closed, so styles applied to #rightnavigation are actually applied to the entire page.
In your CSS, you apply a style to div.content. But that div has an id of content, not a class. The identifier should be div#content.
In your CSS, you give the div with id leftnavigation a position of "left". This should be "absolute" instead.
Once that is all cleaned up, the left nav is on the left, the content is in the center, and the right nav is on the right. But the center content overlaps the right nav (I assume that is unwanted behavior). To clean that up, without changing the HTML any more, you need to give your sections widths, and set their positions based on the width of their neighboring elements.
Your HTML:
<div id="rightnavigation">
<h1>Right Navigation</h1>
</div>
<div id="content">
<h1>Sample Content</h1>
<p>This is the content section of the page. Use structural markup
like <p></p>
to keep the page valid in XHTML.</p>
<p>The styled document should look like your printed version/screenshot.
Add styles to the left navigation links to give them borders and a
background color that changes when moused over (hint: Define navigation
links as display:block;). For the right side links, use a different
background color change and border as ashown.
Make the center column "liquid" or "elastic." Use an external (linked)
CSS file. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit
dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
<h2>Lorem Ipsum</h2>
<p> Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
<p><span class="due">Due Tuesday, September 22.</span> Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
</div>
<div id="leftnavigation">
<h1>Left Navigation</h1>
<p><br>Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
<h2>Lorem Ipsum</h2>
</div>
And your CSS:
#rightnavigation {
position: absolute;
top: 50px;
right: 0px;
width: 25%;
}
#content {
position: absolute;
top: 50px;
left: 25%;
width: 50%;
}
#leftnavigation{
position: absolute;
top: 50px;
left: 0px;
width: 25%;
}
.due {
color: #ff0000;
font-weight: bold;
}

Footer compatibility compared between IE and Google Chrome won't Compromise

In my code, my footer will display at the bottom of my page, with no space above or below, as long as I specify a height of my page within Google Chrome. I tried doing height: 100% and so forth but still had problems.
When comparing that to my IE 11, the footer with a specified height has space below it. I can't seem to get both browsers to compromise and I have tried various options to make them both work.
My current css code that would affect the footer is as shown:
html {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
min-width: 768px;
/*keeps footer at bottom of page for IE 11 */
display: flex;
}
/* Formating for body of Web Site */
* {margin: 0; padding: 0;}
body {
font-family: times new roman;
background-color: #ebebeb;
zoom: 75%;
/*keeps footer at bottom of page for IE 11 */
width: 100%;
background-postion: 50% 80%;
}
#screen {
/* This locks everything in place*/
top:0px;
margin: 0 auto;
width:1500px;
height: 1500px;
padding-top:0;
padding-bottom: 30px;
postion: absolute;
margin-left: 70px;
margin-bottom: 0;
}
/* footer formating */
#footer {
background-color: black;
height: 40px;
width: 1500px;
color: white;
padding-top: 10px;
position: relative center;
bottom: 0;
text-align: center;
margin-left:70px;
}
My html:
<html>
<div id = "screen">
<body>
............................................. other code
<div id = "footer">
Copyright Notice - Site Maintanence by **********
<br>
Author of Published Text Content: ************<br>
Pagetop
</div> <!-- end footer -->
</div> <!-- end screen format -->
</body>
</html>
What IE looks like:
if i got you right, you want the footer to "stick" the upper part of the website in both Chrome and IE11 browsers.
i'm not sure why you chose this CSS settings because you didn't supply a link to the full website so i don't know exactly what is going on, but, you can get what you want not just on this two, but in all the browsers, the key is in the structure and the CSS, let me show you.
first of all i arranged your HTML and CSS so it will be easier to read and folow, i also deleted not needed code parts but you can do a reference with the old code to see the changes.
the <div id="screen"> element was outside the <body> tag, so i put it in.
as you can see in the HTML code, i've put <div id="leftcol"> and <div id="centercol"> elements with "lorem ipsum" text to Illustrate the situation in your website. i assumed <div id="screen"> is the website wrapper so i wrapped it all inside it.
<div id="screen">
<div id="leftcol">lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet</div>
<div id="centercol">lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet</div>
<div class="clear"></div>
<div id="footer">
Copyright Notice - Site Maintanence by **********
<br />
Author of Published Text Content: ************
<br />
</div> <!-- end footer -->
<div class="center">
Pagetop
</div>
</div> <!-- end screen format -->
you can see in the CSS i've deleted unnecessary code like i said before, the key is to keep the things simple unless you have to make it complicated for some reason, if you will tell me the reason you need the <HTML> element with display: flex; or <div id="screen"> element with position: absolute; i will try to help you with this and solve the problem but otherwise, let's keep it simple:
*
{
margin: 0;
padding: 0;
}
html
{
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
body
{
font-family: times new roman;
background-color: #ebebeb;
background-postion: 50% 80%;
}
#screen
{
margin: 0 auto;
}
#footer
{
padding: 5px;
background-color: #000;
color: #fff;
text-align: center;
}
.center
{
text-align: center;
}
#leftcol
{
width: 30%;
float: left;
background: #B4B4B4;
}
#centercol
{
width: 60%;
float: right;
background: #fff;
}
#leftcol, #centercol
{
padding: 2%;
}
.clear
{
clear: both;
}
that's way it's working in all of the browsers include old versions of IE.
example: http://jsfiddle.net/Lvrcw4vw/3/