Is there a css property that can limit the number of characters to be displayed in a div.For example
<div class ="main-text">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
but I need to display only
<div class ="main-text">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
</div>
I am using wordpress site with testimonial feature. So if any customer adds a testimonial, I need to display only the first few words with the link. Please Help!! Thanks!!
You can play around with a overflow property (actually there are couple of them - overflow-x, overflow-y and overflow), also you have to set the width in absolute units (read pixels or em, but not in percentage) of your div.
For example (CSS):
div.main-text {overflow-x: hidden;width: 200px;}
/*This will hide everything, which goes outside of the 200px div*/
Also you can limit your text with simple PHP code:
if (!function_exists('chop_string')) {
function chop_string($str, $len) {
if (strlen($str) < $len)
return $str;
$str = substr($str,0,$len);
if ($spc_pos = strrpos($str," "))
$str = substr($str,0,$spc_pos);
return $str . "Read more...";
}
}
Place the above function in your Wordpress theme's functions.php file and use it like this:
<?php echo chop_string('Bla bla bla', 5); ?>
The above code will show only 5 characters from your string and Read more....
EDIT:
If you want to cut the title or the generated content which comes from Wordpress, then you have to edit your page.php file (also archive.php file), they are inside your template directory. Find the following code in those files: the_content() and the_title(), those 2 functions display actually all information from database.
So cut them like this:
<?php echo chop_string(the_title(), 5); ?> or
<?php echo chop_string(the_content(), 5); ?>
There are two ways you can achieve this :
1) CSS way
add the css property text-overflow:ellipsis
.truncate {
width: 250px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
2) Trick way (HTML)
add a textarea control inside the div and add **maxlength=50** or whatever you need , hide the border using the css border:none property.
You can use css ellipsis. Just use it on the text-overflow. It will truncate adapt to the width of you box.
http://davidwalsh.name/css-ellipsis
http://css-tricks.com/snippets/css/truncate-string-with-ellipsis/
.main-text {
width: 250px;
}
.main-text p {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
One limitation is that you are restricted to use one row.
No, CSS is presentational, so not involved with the length of specific content. It can clip on presentational properties such as box size with the overflow and text-overflow properties, but not based on content. You can easily fix it on the server side though:
$text = strlen($text) > 250 ? substr($text, 0, 250).'…' : $text;
echo "<p>$text</p>";
This inserts an ellipsis after 250 characters if the text is longer than that.
With the help of CSS3 property "text-overflow" - you can set the box width, thus resulting with the exact width for ALL divs;
Text won't be cut in the middle of the word and it will add "..." at the end of the text, hinting it's a preview text;
HTML Code:
<div id="myDiv">Just some text - you won't see ME!</div>
CSS Code:
#myDiv {
background: red;
color: white;
font-size: 20px;
height: 50px;
overflow: hidden;
width: 256px;
white-space:nowrap;
text-overflow: ellipsis;
}
Tested under FF, Chrome, IE11
This is my solution demo: limit-printed-words.html
It works well with any nested level and even button content.
Source project: scriptbucket
Related
I have a div and want to have it's background color filled based the scroll position from top. Below is my code -
HTML
<div> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div>
<div id='Div'>
</div>
<div> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</div>
CSS
#Div {
background-color: red;
height: 20px;
width: 0%;
}
J-query
$(window).scroll(function(){;
w = Math.floor( $(window).scrollTop() );
if (w > 5) {
$('#Div').animate({ width: '100%' }, 1000);
} else {
$('#Div').animate({ width: '0%' }, 1000);
}
});
This is running well, but I found there is a delay in the response time. Filling is not happening quickly based on the cursor position as I need to wait for few seconds to see the filling starts.
Fiddle - http://jsfiddle.net/bogaso/5r3afz7n/11/
Is there any way to make the response time instantaneous?
Any help will be highly appreciated.
Concept
Define two named animations using css #keyframes rules, one to expand the color bar, one to shrink it. Contingent on the scroll position, start the proper animation. In order to select the right one and to restrict the animation start on crossing the scroll offset threshold, a css class colorized is introduced to record state ( in this case: scroll offset > 5 ). Only if the state has changed, an animation is started. The combination of current scroll offset and presence/absence of class colorize determine which animation to select.
Jquery is not needed to manage the animation, the DOM API does suffice.
See it in action at this fiddle.
Code
HTML
<div><!-- ... long text .....></div>
<div id='Div'>
</div>
<div><!-- ... even longer text .....></div>
CSS
#Div {
background-color: red;
height: 20px;
width: 0%;
}
#keyframes colorize {
from { width: 0%; }
to { width: 100%; }
}
#keyframes decolorize {
from { width: 100%; }
to { width: 0%; }
}
JS
$(window).scroll(function(){
let w = Math.floor( $(window).scrollTop() );
let e = document.querySelector('#Div');
if (w > 5) {
if (! e.classList.contains("colorized")) {
// Adjust the scrolling state proxy
e.classList.add("colorized");
// Animation name. Links the code to the CSS spec of start/end values of the animated attribute
e.style.setProperty ( 'animation-name', 'colorize' );
// Animation starts immediately
e.style.setProperty ( 'animation-delay', '0s' );
// It takes that long for the complete animation to complete.
e.style.setProperty ( 'animation-duration', '2s' );
// The final state of the animated attribute persists.
e.style.setProperty ( 'animation-fill-mode', 'forwards' );
// The animation is run exactly once for each triggering event
e.style.setProperty ( 'animation-iteration-count', '1' );
}
} else {
if (e.classList.contains("colorized")) {
e.classList.remove("colorized");
e.style.setProperty ( 'animation-name', 'decolorize' );
e.style.setProperty ( 'animation-delay', '0s' );
e.style.setProperty ( 'animation-duration', '2s' );
e.style.setProperty ( 'animation-fill-mode', 'forwards' );
e.style.setProperty ( 'animation-iteration-count', '1' );
}
}
});
Extension
In the version shown the colored bar jumps to full width when the decolorize animation starts. Visually this is not appealing if the inverse colorize animation is in progress. A more sophisticated solution would read the current width of the color bar div and set the start value of the respective #keyframe animation accordingly.
Is possible to select part of text in CSS3 until "br" badge whitout editing html.
I want to select this part:
"Lorem Ipsum is simply dummy text"
<p>Lorem Ipsum is simply dummy text <br/>
of the printing and typesetting industry.</p>
You can use the first-line modifier as shown below:
p:first-line {
color: red;
}
As #JordanS has correctly highlighted, there are cases where this will fail. For a robust solution you either update the HTML or use javascript.
https://jsfiddle.net/01c5tbst/
Update:
For posterity the solution actually desired to apply the style only to the first line of the first matching element on the page, so the final solution actually was this:
p:first-of-type::first-line {
color: red;
}
See this code :
html{position:relative;min-height:100%;}
and
<div style="width:100%;height:100%;overflow:auto;">
<div id="main" style="width:200px;max-height:100px;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
In addition, Textarea scrollbar and resize grabber cannot hiding too.
How can I fix it using CSS?
in Chrome(MacOS). But Safari works well.
This code is example for quick understanding.
The contents are large enough to require scrollbar.
UPDATE : https://jsfiddle.net/xu3q4m4w/9/
I guess This problem is relates to LINK.
height set into 100% create scroll bar. Try to fix into max-height:100%. as well as width. you should set into width:100% or max-width:100% to get full width, not in pixel.
UPDATED:
<div style="width:100%;height:100%;overflow:auto;">
<div id="main" style="max-width:1500px;height:200px;">
</div>
</div>
jsfiddle
I figure out this problem. It Happen by only old chrome version (48.0.2564.97)
Now fix it. (48.0.2564.103 )
Thank you for answer.
use this fiddle overflow-x:hidden
<div style="width:100%;height:100%;overflow:auto;overflow-x:hidden;">
<div id="main" style="width:1500px;height:200px;">
</div>
</div>
Link
I've created an anchor tag as follows
Top
And created following tags
<h2 id="top">Header</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
specimen book. It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged. It was popularised in the
1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum.
</p>
Once clicking on a link, it's scrolling to that target but not can't see the <h2> tag. So atleast needed to scroll above the target by 10px or 20px. Please help me overcome this and the work would be more appreciable.
Do like that: add tabindex in
<h2 id="top" tabindex="1" >Header</h2>
and use focus()
$("#top").attr("tabindex",-1).focus();
OR
or the simplest way is that add a anchor tag near h2 and add id="top"
<a id="top"></a><h2>Header</h2>
but I won't prefer this.
First thing you need to change href of anchor tag as below
Top
then you can write below jQuery code to get scroll to the target
(function() {
$('a').click(function() {
$('html, body').animate({
scrollTop: $("#top").offset().top
}, 3000);
});
});
It will work.
Personally I usually minus some pixels from the top of the div so my headings appear normal, do this:
jQuery('html, body').animate({
scrollTop: jQuery( jQuery(this).attr('href') ).offset().top - 25
/* You can minus the pixels */
}, 500);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am using a script for limit the paragraph for few words only so for that m using the script of readmore..but the problem is it collapse the paragraph wherever I click on text whereas I want it to be collapse when Clicked on >>more link..so I request anyone to help me out of this what to do that it will not accept the click on text.
<!--readmore script-->
<script type="text/javascript">
$(function () {
// Grab all the excerpt class
$('.excerpt').each(function () {
// Run formatWord function and specify the length of words display to viewer
$(this).html(formatWords($(this).html(), 15));
// Hide the extra words
$(this).children('span').hide();
// Apply click event to read more link
}).click(function () {
// Grab the hidden span and anchor
var more_text = $(this).children('span.more_text');
var more_link = $(this).children('a.more_link');
// Toggle visibility using hasClass
// I know you can use is(':visible') but it doesn't work in IE8 somehow...
if (more_text.hasClass('hide')) {
more_text.show();
more_link.html(' » hide');
more_text.removeClass('hide');
} else {
more_text.hide();
more_link.html(' « more');
more_text.addClass('hide');
}
return false;
});
});
// Accept a paragraph and return a formatted paragraph with additional html tags
function formatWords(sentence, show) {
// split all the words and store it in an array
var words = sentence.split(' ');
var new_sentence = '';
// loop through each word
for (i = 0; i < words.length; i++) {
// process words that will visible to viewer
if (i <= show) {
new_sentence += words[i] + ' ';
// process the rest of the words
} else {
// add a span at start
if (i == (show + 1)) new_sentence += ' <span class="more_text hide">';
new_sentence += words[i] + ' ';
// close the span tag and add read more link in the very end
if (words[i+1] == null) new_sentence += '</span> » more';
}
}
return new_sentence;
}
</script>
<!--readmore script-->
<body>
<p class="excerpt">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br><br>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)....</p>
</body>
What about these small script? DEMO http://jsfiddle.net/uEXvk/
HTML
<p class="excerpt">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br><br>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)....</p>
CSS
a {
color: blue;
cursor: pointer;
}
JQUERY
var orgContent = $('.excerpt').html();
var txtContent = $('.excerpt').text().substr(0, 50) + '... <a id="morelink">more</a>';
$('.excerpt').html(txtContent);
$("body").on("click", '#morelink', function(){
$('.excerpt').html(orgContent);
$('<a id="lesslink"> less</a>').appendTo('.excerpt');
});
$("body").on("click", '#lesslink', function(){
$('.excerpt').html(txtContent);
});