Determining if a line has wrapped in a browser - html

Is there any programmatic method for determining if a line has wrapped in the browser it is being displayed in? The content below this line is absolutely positioned (for purposes of source-ordering) and I need to change its position if the line wraps.

You can determine if an element was wrapped by checking its height and comparing it to a similar element with the same content but wider.
This is how you can do it:
Create a hidden element with the same content and style as the the one you want to check, but with a really big width value.
Append that created element to the element that contains the line or paragraph.
Compare the heights of the elements (if they are the same, no wrapping; if they are different, the line was wrapped).
Remove the created element.
For example:
function checkWrapping() {
// for each paragraph in the div, check if it was wrapped
$("#mydiv").find("p").each(function () {
// create a hidden paragraph with the same text and append it to the parent
var aux = $("<p style='display:none;position:absolute;width:10000000px'>" + $(this).text() + "</p>");
$(this).parent().append(aux);
// compare the height of the paragrap with the height of the one-line paragraph
if ($(this).height() > $(aux).height()) {
console.log($(this).attr("id") + " is wrapped!");
} else {
console.log($(this).attr("id") + " is NOT wrapped!");
}
// remove the hidden paragraph at the end of the check
$(aux).remove();
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="mydiv">
<p id="p0">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tristique rutrum velit, sit amet vestibulum dui consectetur at. Suspendisse scelerisque pellentesque lectus in lacinia.</p>
<p id="p1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p id="p2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tristique rutrum velit, sit amet vestibulum dui consectetur at.</p>
</div>
<input type="button" value="check if wrapped" onclick="checkWrapping();">
You can see the sample also on this jsfiddle: http://jsfiddle.net/o1ay0e71/ (it will allow you to resize the window easily to compare results).
Also, if the line has html tags, use .html() instead of .text() as shown on this update: http://jsfiddle.net/o1ay0e71/1/

Related

How to have a hidable paragraph in an HTML email template

I'm creating email template with variables in it. The variables will be replaced at run-time with one of two different sets of values as show below. The first set has an extra paragraph in the middle with a link embedded in it. The problem I'm having is getting the second and third paragraphs to have the correct spacing when they are combined. Is there some way to create a bottom margin from p3 to create that space between the second and third paragraphs?
Here is the template I am using.
Template
<style type="text/css">
body{
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
font-size: 14px;
line-height: 1.5em;
background-color: #f5f5f5;
}
.body-div{
width: 560px;
background-color: #ffffff;
padding: 10px 20px;
margin: 0 auto;
}
.below-spacing, .p3{
padding-bottom: 20px;
}
.above-spacing{
padding-top: 10px;
}
.p1:empty, .p2:empty, .p3:empty{
display:none;
}
</style>
<body>
<div class="body-div">
<div class="below-spacing above-spacing">{{FirstParagraph}}</div>
<div><span class= "p1">{{MidParagraphPart1}}</span><span class="p2">{{MidParagraphPart2}}</span><span class="p3">{{MidParagraphPart3}}</span></div>
<div>{{LastParagraph}}</div>
</div>
</body>
</html>
Example 1
In this example I've inserted values into all the variables showing all three paragraphs and the link with text in it. Notice how paragraphs two and three do not space correctly in a browser. The CSS is the same as above.
</style>
<body>
<div class="body-div">
<div class="below-spacing above-spacing">Section of text #1 Lorem ipsum dolor sit amet, dolor porta wisi, sed et dui lacinia facilisi tincidunt hendrerit, risus sodales ipsum semper nulla sit, sed cursus sapiente, aliquam tincidunt sed leo arcu in.</div>
<div><span class= "p1">Section of Text #2, Part #1. Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet </span><span class="p2">(Part #2) Link text</span><span class="p3"> Part #3 Lorem ipsum dolor sit amet, et eget. Lorem ipsum dolor sit amet, et eget.</span></div>
<div>Section of text #3 Lorem ipsum dolor sit amett, at consectetuer id sollicitudin amet posuere. </div>
</div>
</body>
</html>
Example 2
In this example I've replaced only the first and last variables with text. The middle paragraph is hidden and both paragraphs are spaced correctly. The CSS is the same as above.
</style>
<body>
<div class="body-div">
<div class="below-spacing above-spacing">Section of text #1 Lorem ipsum dolor sit amet, dolor porta wisi, sed et dui lacinia facilisi tincidunt hendrerit, risus sodales ipsum semper nulla sit, sed cursus sapiente, aliquam tincidunt sed leo arcu in.</div>
<div><span class= "p1"></span><span class="p2"></span><span class="p3"></span></div>
<div>Section of text #3 Lorem ipsum dolor sit amett, at consectetuer id sollicitudin amet posuere. </div>
</div>
</body>
</html>
Try this:
Add this to the end of your css.
.hidden{
display: none;
}
Then add the injectable variable Hidden to any element you want to hide on demand.
<div class="below-spacing {{Hidden}}"><span>{{Paragraph2}}</span><span>{{Paragraph3}}</span><span>{{Paragraph4}}</span></div>
Inject "hidden" (no double quotes) for the variable {{Hidden}}. That will hide the paragraph with the correct spacing.
You can remove the p1, p2, and p3 classes.
The span is inline tag. We can not set margin for it. You should set span to display: inline-block.
.p1,
.p2,
.p3 {
display: inline-block;
}
To hide second span you should add class empty for it:
<span class="p2 empty></span>
CSS:
.p1.empty,
.p2.empty,
.p3.empty {
display: none;
}
I saw some error in your HTML code.
your first span is missing " before the class name.
Your second span doesn't have a class. Maybe add p2 like you want.
I'm not sure to understand the whole question here, but why did you put a
display: none;
to your span?
Try
display: inline-block;
Another solution could be to remove span and add
<p>
instead and add that in your css
p{
margin-bottom:20px; //can add a class p3 to your p.
}

Repeating background not displaying

Ok, this is by far one of the strangest problems I have encountered in my css experience. I am trying to use some subtle patterns that repeat for a div on my webpage. Strange enough, whenever i try to set an pattern that is very white, the browser refuses to load the image. When I try to use a darker image, it works just fine. Now I know that the colour is obviously not the problem, but what am I missing. Here is the HTML/CSS:
#section1{
background-image: url("../images/as.png");
}
<div id="section1">
<div class="content98">
<h1 id="h1d">De ce matest.ro?</h1>
<div id="border">
<img class="show" id="show1" src="images/untitled-2.png">
<h2 id="dix">Obiective</h2>
<hr>
<div id="hided1"><p class="hiding">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus lorem, feugiat ut condimentum ac, ultricies vitae nibh. Duis sed lacinia magna.>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<br><br>
</div>
First check your body background color white or change and see. Then Check in inspect element of browser in style box that your property is strikeout or not.
#section1{
background-image: url("../images/as.png");
}
And then add background-repeat:repeat; to your image like this.
#section1{
background-image: url("../images/as.png");
background-repeat:repeat;
}
even like this you can write
#section1{
background:url("../images/as.png") repeat;
}

Display image caption with css3

I am trying to display a caption on my images. Caption should behave in two different ways. One is normal and second is hover. For better understanding I have added an image. check it.
Here I have already finished big part of this, But I can not display normal caption on the image correctly.
Can anybody tell me how I figure this out?
THIS is my HTML -
<div class="slideimages">
<div id="box-1" class="box">
<div class="fix-caption">
<h3>Fix Caption</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p>
</div>
<a href="">
<img class="image_scale" src="images/4750.jpg"/>
<span class="caption scale-caption">
<h3>Scale Caption</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</span>
</a>
</div>
</div>
MY JS FIDDLE with CSS
Thank you.
Add the following CSS
.fix-caption {
z-index: 5;
background-color: white;
}
Your caption is there, just displaying behind the image...
Alternatively put the elements with .fix-caption lower in the DOM order, which will effectively give them a higher display order (note you'll still want a background-color).
DEMO 1 (z-index) : http://jsfiddle.net/Zfr5c/3/
DEMO 2 (DOM order): http://jsfiddle.net/Zfr5c/2/

CSS indent first line of text past the right edge

It is possible to use text-indent property to indent the first line of text towards either side of left corner. Consider this example:
h1 { text-indent: -200px; margin-left: 200px; }
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula molestie imperdiet.</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris vehicula molestie
imperdiet.
|---------- 200px ----------+------ 100% - 200px ------|
|-------------------------->| left margin
|<--------------------------| negative text indent
Is there a trick to do this the other way round:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris vehicula molestie
imperdiet.
|------ 100% - 200px ------+---------- 200px ----------|
What I want is the first line (and only the first line) of text to extend 200px past the left edge. Perhaps there is a similar CSS property which applies indenting around the right edge or applies to 2nd and latter lines of text.
Here is a jsFiddle
text-indent could come from parent. with a padding.
http://jsfiddle.net/qsDST/
#wrapper {
width: 300px;
text-indent: -200px;
padding-left:200px;
}
h1 {
background: lime;
display:inline;
}
Is this you want?
h1.other-way-round {
margin-left: 0px;
text-indent:0px
}
DEMO
Second method
h1.other-way-round{
text-indent:0;
position:relative;
width:100%;
left:-200px;
}
DEMO
As I undarstand your expected result is something like this:
Lorem ipsum dolor sit amet, consectetur
adipiscing elit.
Mauris vehicula
molestie imperdiet.
The easiest way to do this might be using Javascript.
You could detect the 2nd and past lines and wrap an tag around them with a padding-right of 200px.
You may need to use jQuery or simmilar to do this.
Badly it's not possible to set width/height nor padding to the :first-line pseudoelement.

Floating divs left, make all divs height equal the tallest div in it's row?

I am attempting a design that would float a series of divs, all the same class, to the left. I want the divs to fit into rows, where in each row the divs are the same height so that there is no breakage between the rows and the design elements line up. Is there a way to make this happen, or do I essentially have to preset each div's height?
I'm floating to the left because I want the rows to be shorter if the browsers width is skinnier.
I think that's confusing. Attached is an image of what I'm trying to do.
There are three options as I see it:
State the height in your div style
Looks like the simplest answer, and since all divs (in the image) look to be the exact same height, this doesn't appear to be a problem to me:
div.class {
height: 300px;
}
Create row containers
Create a container for each row of divs and define its height, then give each child div a height of 100%:
div.row-container { height: 300px; }
div.class { height: 100%; }
Use a table
Don't be afraid to use the table element to display data in a tabular fashion. I am not sure how well your div semantically could be replaced with table rows and columns. But give it some thought as a potential candidate.
EDIT: I originally misunderstood, thinking you wanted to emulate the image exactly. My solutions are ones which assume a constant height (something you expressed a desire to possibly avoid). A counter-argument to that stance is that visually, rows which are all the same height are pleasing to the eye, and ultimately you want some control over how heigh your columns can be.
However it is possible to do exactly what you want, using no JS:
http://matthewjamestaylor.com/blog/equal-height-columns-5-column.htm
The answer is different depending on how you intend to implement it. If you're sticking with css 2, then the solution is either javascript (force all 'columns' to be the same height with javascript) or any of the multitude of methods for faking a column layout in css2. Here http://www.search-this.com/2007/02/26/how-to-make-equal-columns-in-css/ for example.
there is a css3 draft proposal to support multi column layouts. This is currently supported by most non-ie browsers (at least the most up to date versions). But youd be brave to put into a production environment if you value you're accessibility / dont have a fallback. See here http://www.w3.org/TR/css3-multicol/ for details.
I guess I would not go for a table layout solution, since tables are not meant for layout purposes. There are quit some nice javascript solutions for the equal column problem (e.g. the columnizer jquery plugin), but looking at your example image I think I would come up with something like the following (assuming the dimensions of the elements are fixed):
html:
<div class="Container">
<div class="RowContainer">
<div class="Cell"><h1>lorum ipsum lorum ipsum</h1><p>lorum ipsum epsum</p></div>
<div class="Cell"><h1>lorum ipsum</h1><p>lorum ipsum epsum</p></div>
<div class="Cell"><h1>lorum ipsum lorum ipsum</h1><p>lorum ipsum epsum</p></div>
<div class="Cell"><h1>lorum ipsum</h1><p>lorum ipsum epsum</p></div>
</div>
<div class="RowContainer">
<div class="Cell"><h1>lorum ipsum lorum ipsum</h1><p>lorum ipsum epsum</p></div>
<div class="Cell"><h1>lorum ipsum</h1><p>lorum ipsum epsum</p></div>
<div class="Cell"><h1>lorum ipsum</h1><p>lorum ipsum epsum</p></div>
<div class="Cell"><h1>lorum ipsum</h1><p>lorum ipsum epsum</p></div>
</div>
</div>
CSS:
.Container {
width:800px;
position:relative;
margin:0 auto;
}
.RowContainer {
overflow:hidden;
position:relative;
height:200px;
clear:both;
}
.RowContainer .Cell {
position:relative;
float:left;
height:100%;
width:200px;
background-color:#ff0;
}
Have just recently found a good way to take this:
As i have expericed, having elements in float style so they behave properly on a responsive enviroment isn't easy, more like hellish.
If you want that every element on the same "row" have the same height, the best aproach for IE9 and above is flexbox.
Sample, we have 4 boxes that doesnt fit on the container, so we want them to move to a new row if they dont fit but keep all the same height (Being the height value unknown):
<div class="container">
<div class="element">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et
</p>
</div>
<div class="element">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et
</p>
</div>
<div class="element">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et
</p>
<p>
Lorem ipsum dolor sit amet.
</p>
</div>
<div class="element">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et
</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et
</p>
</div>
</div>
Aplying this styles just fixes it:
.container {
display: flex;
display: -mx-flexbox;
display: -webkit-flex;
flex-grow: 0;
-ms-flex-grow: 0;
-webkit-flex-grow: 0;
flex-wrap: wrap;
width: 400px; /* Sample constraint */
background-color: red; /*Visiblity */
}
.element {
flex: none;
width: 120px; /* Sample constraint */
border: 1px solid blue; /*Visiblity */
}
Check this fiddle, it will give all you want.
https://jsfiddle.net/upamget0/
Source: CSS height 100% in automatic brother div not working in Chrome
Great info can be found here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I've been using this solution by Chris Coyier of CSS-tricks. Quick & easy, works like a charm.
// IIFE
(function() {
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array();
function setConformingHeight(el, newHeight) {
// set the height to something new, but remember the original height in case things change
el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")));
el.height(newHeight);
}
function getOriginalHeight(el) {
// if the height has changed, send the originalHeight
return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"));
}
function columnConform() {
// find the tallest DIV in the row, and set the heights of all of the DIVs to match it.
$('#page-wrap > div.yourelement').each(function() {
// "caching"
var $el = $(this);
var topPosition = $el.position().top;
if (currentRowStart != topPosition) {
// we just came to a new row. Set all the heights on the completed row
for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);
// set the variables for the new row
rowDivs.length = 0; // empty the array
currentRowStart = topPosition;
currentTallest = getOriginalHeight($el);
rowDivs.push($el);
} else {
// another div on the current row. Add it to the list and check if it's taller
rowDivs.push($el);
currentTallest = (currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (currentTallest);
}
});
// do the last row
for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
setConformingHeight(rowDivs[currentDiv], currentTallest);
}
}
// If the content might change... probably debounce this and run it.
// $(window).resize(function() {
// columnConform();
// });
// DOM Ready
// You might also want to wait until window.onload if images are the things that are unequalizing the blocks
$(function() {
columnConform();
});
})();
Source: https://css-tricks.com/equal-height-blocks-in-rows/