icon next to dynamic text with ellipsis - html

my question is different but similar to Sliding icon next to dynamic text with ellipsis
Showing an icon after truncated text with ellipsis.
want to show icon ONLY if text is truncated and ellipsis shown.
Here is sample I tried.
https://jsfiddle.net/poonkave/6sfbhu9w/
issue is icon is still displayed even if text is short.
any CSS solution?
<div class="block-wrap">
<div class="block">
<div class="icon">
</div>
<div class="desc">
This is a very very very
very very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very long description
</div>
</div>
.block-wrap {
display: inline-block;
max-width: 100%;
}
.block {
width: 100%;
}
.desc {
display: block;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.icon {
float: right;
margin-left: 10px;
width: 20px;
height: 14px;
background-color: #333;
}

There is no pure CSS Solution but you can use javascript.
What I've done is first getting the width of the container in which the text is present (in my case .outer) and comparing it to the width of the text which holds the text or has ellipsis property (in my case .text) and show the icon if condition matches.
Have a look at the snippet below:
var outer_width_one = $('.outer.one').width();
var text_width_one =$('.outer.one .text').width();
if(text_width_one == outer_width_one) {
$('.outer.one .text').addClass('show');
}
$('.results .outer_one_result .outer_one_div_width').text(outer_width_one);
$('.results .outer_one_result .outer_one_text_width').text(text_width_one);
var outer_width_two = $('.outer.two').width();
var text_width_two =$('.outer.two .text').width();
if(text_width_two == outer_width_two) {
$('.outer.two .text').addClass('show');
}
$('.results .outer_two_result .outer_two_div_width').text(outer_width_two);
$('.results .outer_two_result .outer_two_text_width').text(text_width_two);
.outer {
padding: 10px 20px 5px;
border-bottom: 1px solid #ccc;
}
.text {
position: relative;
display: inline-block;
max-width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.text:after {
display: none;
content: '\f007';
font-family: 'FontAwesome';
width: auto;
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
}
.text.show:after {
display: block;
}
body {
margin: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="outer one">
<strong>One (full length - shows icon):</strong><br>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat voluptatibus nam ducimus praesentium, deserunt, facilis aut minus incid corporis laborum velit optio illo quos! Corporis cupiditate pariatur, ratione voluptas! Quam.</div>
</div>
<div class="outer two">
<strong>Two: (small length - no icon):</strong><br>
<div class="text">Lorem ipsum dolor</div>
</div>
<div class="results">
<div class="outer_one_result">
<ul>
<li>Outer Width (One): <strong class="outer_one_div_width"></strong></li>
<li>Text Width (One): <strong class="outer_one_text_width"></strong></li>
</ul>
</div>
<div class="outer_two_result">
<ul>
<li>Outer Width (Two): <strong class="outer_two_div_width"></strong></li>
<li>Text Width (Two): <strong class="outer_two_text_width"></strong></li>
</ul>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hope this helps!

Related

Aligning multiple <p> tags as text

How can i aligning texts in multiple html p tags:
<div>
<p>aaaaaa</p>
<p>bbbbbbbbbbb</p>
<p>cccccccccccc</p>
</div>
Making them have word-wrap property like normal text and break line like below?
aaaaaa bbbbbbb
bbbb ccccccccc
ccc
Text in p tags needs to be dynamic and trigger seperate events onClick. Is there any css tricks like
"display: flex" that can achieve this?
edit:
Sorry for being unclear, I would try to explain again.
I would like to put the 3 p tags into a container that have uninsuffiecnt width to display all in one line.
And the desired beviour would be something like this: desired
<style>
div {
display: flex;
max-width: 200px;
border: 1px solid black;
line-break: anywhere;
}
</style>
<div>
<p onclick="someEvent()">aaaaaaa</p>
<p onclick="someEvent()">bbbbb</p>
<p onclick="someEvent()">ccccccccccccccccccccccccccccccc</p>
</div>
Above Snippet is my failed attempt, hope this helps in explaining my question
You can achieve your desired output by using the following code:
Run this snippet to show the output.
.container {
max-width: 200px;
border: 1px solid black;
background: yellow;
}
p {
word-break: break-all;
white-space: normal;
display: inline;
}
<div class="container">
<p class="word-break" onclick="someEvent()">aaaaaaa</p>
<p class="word-break" onclick="someEvent()">bbbbb</p>
<p class="word-break" onclick="someEvent()">ccccccccccccccccccccccccccccccc</p>
</div>
Aside from that, here is a demo of my JS fiddle. : https://jsfiddle.net/rutikpatel/d1s7t64g/
Screenshot of output :
I would recommend flexbox:
<style>
div {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
justify-content: flex-start;
}
</style>
<div>
<p onclick="someEvent()">aaaaaa</p>
<p onclick="someEvent()">bbbbbbbbbbb</p>
<p onclick="someEvent()">cccccccccccc</p>
</div>
You can also use text-align to align the text in multiple <p> tags within the <div/>.
<style>
div {
text-align: justify;
white-space: pre-wrap;
}
</style>
<div>
<p onclick="someEvent()">aaaaaa</p>
<p onclick="someEvent()">bbbbbbbbbbb</p>
<p onclick="someEvent()">cccccccccccc</p>
</div>
Use the CSS' property word-break: break-all to achieve the desired result.
.my-block {
max-width: 75px;
padding: 5px;
word-break: break-all;
}
.my-block--design {
border: 1px solid red;
border-radius: 3px;
box-shadow: 0 0 3px 0 red;
}
<p class="my-block my-block--design">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Pariatur officiis cupiditate placeat. Numquam, error dicta ea voluptatem cupiditate dolore, aperiam aspernatur at, quaerat est repellat possimus!
</p>
Please Note: Decrease the max-width manually for now, to see every letter-by-letter break. You can also use relative unit(s). For demo purposes, I've used an absolute unit. More here on JSFiddle

How to have a button that leads to another part of the webpage?

I am building a Portfolio website that will have a few sections like home, about me, skills, services, projects, and contact. I built a section of every part, then I have usually a header and some paragraph or a photo. At the center bottom of the every section I would like to have a button that leads to another section/part. So for example in the home section I wish to have a button that will direct me to another page which will be about me.
I have written a HTML code for the section and have a design in CSS, but not sure how to make, the button" work"
I don't want to have a separate HTML file for every section, this is why I am thinking if there is any better way to do it.
.btn {
margin-top: 1rem;
display: inline-block;
padding: 1rem 3rem;
font-size: 1.7rem;
color: #d9296f;
border: 0.2rem solid #d9296f;
border-radius: 5rem;
cursor: pointer;
background: none;
}
.btn:hover {
background: #d9296f;
color: #08122f;
}
<section class="home" id="home">
<div class="content"><span data aos="fade-up" data-aos-delay="150">Hi, I'm Emilia</span>
<h3 data-aos="fade-up" data-aos-delay="300">Your Web Developer</h3>
<p data-aos="fade-up" data-aos-delay="450">Lorem ipsum dolor, sit ametconsectetur adipisicing elit. Consequatur doloremque eius sit ratione eveniet laborum inventore recusandae consequuntur ipsum, nostrum aspernatur, ab, libero quos natus iusto et quasi sed est!</p>
about me
</div>
</section>
As you already use, you can use the anchor. You just have to use #id as hyper reference such as: Button Name. This will move your site to the elemnt with that id. The default behavior is an instant jump which you can change to scroll by using: scroll-behavior: smooth;
/* only for styling purpose - no actual use */
html {
scroll-behavior: smooth;
}
body {
margin: 0;
}
a {
color: white;
}
section {
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
}
section:nth-child(1) {
background-color: blue;
}
section:nth-child(2) {
background-color: orange;
}
section:nth-child(3) {
background-color: red;
}
section:nth-child(4) {
background-color: green;
}
<section id="section-1">
<h1>This is Section 1</h1>
Go to Section 2
</section>
<section id="section-2">
<h1>This is Section 2</h1>
Go to Section 3
</section>
<section id="section-3">
<h1>This is Section 3</h1>
Go to Section 4
</section>
<section id="section-4">
<h1>This is Section 4</h1>
Go to Section 1
</section>

If sentence inside span is too long, don't show sentence, show dots

I don't want regular text clamp, because I don't want something like this: Really long sentence ---> Really long.... I want to get only 3 dots like .... I already tried with
.truncate-text {
display: inline-block;
white-space: nowrap;
overflow: hidden !important;
text-overflow: ellipsis;
}
Is there any other solution?
My code
<div class="w-12/12">
<p class="font-medium text-14 text-color-primary max-w-860 truncate-text">
<span v-for="tag in tags" :key="tag.id" class="pr-2">
#{{ tag.name }}
</span>
</p>
</div>
You can try a visual hack like below:
.box {
border: 1px solid;
width: 200px;
font-size:25px;
height: 1.2em;
overflow: hidden;
}
.box::before {
content:"...";
display:inline-block;
width:0;
text-indent:5px;
}
.box > span {
display:inline-block;
padding:0 5px;
white-space:nowrap;
background:#fff;
}
<div class="box"><span>Lorem </span></div>
<div class="box"><span>Lorem ipsum</span></div>
<div class="box"><span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur </span></div>

Looking for alternative to adding an <img> tag inside of a <h> header tag

I see a lot of posts on adding the "img" tag inside of a "header" tag and from what I see it's bad practice even though it does what intended for me. I would like to know an alternate way to properly format the way I want.
I have an image icon that I am trying to place left of the "h4" tag on the same line. The only way I could figure out on doing this is as follows:
<div class="col-md-4 service-header-bar">
<h4 class="service-heading"><img src="img/icons/wheel-icon.png" class="icon-size" alt="Furnace Servicing">Furnace Servicing</h4>
<p class="text-muted service-paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima maxime quam architecto quo inventore harum ex magni, dicta impedit.</p>
</div>
.page-section .service-header-bar .icon-size {
margin-right: 20px;
max-width: 50px;
}
.page-section h2.section-heading {
font-size: 45px;
margin-top: 0;
margin-bottom: 15px;
font-family: 'Gotham';
font-weight: 700;
}
.page-section p.service-paragraph {
margin-left: 71px;
margin-right: 35px;
}
.service-heading {
margin: 15px 0;
text-transform: none;
}
Here is a screenshot of what my code produces, although I would like the same result without using an img tag inside of a h tag.
https://gyazo.com/f139612f2902d5c0152f2eb33026af2c
Thanks in advance,
Nick
To avoid using an img tag you can use a background image for the heading instead. And then extend the class for different images. Fiddle here.
Markup:
<h1 class="heading-icon">Heading</h1>
<h1 class="heading-icon heading-icon-blue">Heading</h1>
CSS:
.heading-icon {
padding-left: 50px;
background: url("http://placehold.it/25x25") left center no-repeat;
background-size: contain;
}
.heading-icon-blue {
background: url("http://placehold.it/25x25/1382d2") left center no-repeat;
}
Thanks for all the feed back and help, adding "float: left" in:
.page-section .service-header-bar .icon-size {
float: left;
margin-right: 20px;
max-width: 50px;
}
did exactly what I wanted.
Nick

Underline only final line of text when centered

I want to underline only the last line of some text. When the text wraps to more rows, still only the last line has to be underlined.
I found this Solutions. But that does not work when the text is centered. Because the line stretches all the way to the left on the last row when the text gets wrapped.
p{
position: relative;
display: inline
}
p:after {
position: absolute;
left: 0;
bottom: -15px;
width: 100%;
height: 1px;
border-bottom: 10px solid #000;
content: ""
}
<div style="text-align:center;">
<p>Een lijn onder alleen de laatste regel, werkt ook op mobiel als de tekst over meerdere regels valt</p>
</div>
Jsfiddle
Anyone has an idea?
Thx!
I guess that's what OP wants:
.underlined {
position: relative;
}
.text {
display: inline-block;
text-align: center;
}
.line {
color: transparent;
display: inline;
position: relative;
left: 50%;
}
.line:after {
content: "";
display: block;
width: 100%;
height: 1px;
border-bottom: 10px solid black;
position: absolute;
left: -50%;
top: 0;
}
<p class="underlined">
<span class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui sed ratione voluptatum ducimus unde velit debitis asperiores expedita, a deleniti repellat quis officia. Voluptate, earum rerum itaque, iste eligendi velit!</span>
<span class="line">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui sed ratione voluptatum ducimus unde velit debitis asperiores expedita, a deleniti repellat quis officia. Voluptate, earum rerum itaque, iste eligendi velit!</span>
</p>
I don't like this solution because it requires to duplicate the content, but maybe someone has an idea to improve it...
JSFiddle
Edit: Adding a screenshot of my result:
Is doesn't work in Firefox 50.0
I answered similar question. It can't be done in pure css.
I have created the fiddle using javascript.
http://jsfiddle.net/VHdyf/89/
Javascript part
var parentEl = document.getElementsByClassName('customBtn');
for(var i=0;i<parentEl.length;i++){
var currentEl = parentEl[i];
var button = currentEl.childNodes[1];
var words = button.innerText.split(/[\s]+/); // An array of allthe words split by spaces, since that's where text breaks by default. var
var lastLine = []; // Putall words that don't change the height here.
var currentHeight = currentEl.clientHeight; // The starting height.
while(1){
lastLine.push(words.pop());
button.innerText = words.join(' ');
if (currentEl.clientHeight < currentHeight) {
var span = document.createElement('span');
span.classList=['underline'];
span.innerText = ' '+lastLine.reverse().join(' ');
button.appendChild(span);
break;
}
currentHeight = parentEl[i].clientHeight;
if(!words.length){
break;
}
}
}
Try the following CSS
div > p:last-child:after {
position: absolute;
left: 0;
bottom: -15px;
width: 100%;
height: 1px;
border-bottom: 10px solid #000;
content: ""
}
Here is updated JSfiddle