CSS Deformable text - css

I need to create a text that adapts to the size of its container DIV, distorting itself to always fill the whole space inside the DIV. So it should be always width=100% and height=100% and resize according to the browser window.
I know this can be done with an image, my doubt is if there is any way to do the same to a editable text, deforming the font shapes itself. Which property should I use?
Thank you in advise.

Option 1
We are going to use Javascript to change dynamically the font-size and line-height
First, cache the div so that the browser doesn't have to find it every time the window is resized.
var $div = $('.foo');
Run the following when the window is resized, and also trigger it once to begin with.
$(window).resize(function () {
Get the current height of the div and save it as a variable.
var height = $div.height();
Set the font-size and line-height of the text within the div according to the current height.
$div.css({
'font-size': (height/2) + 'px',
'line-height': height + 'px'
})
}).trigger('resize');
Demo: https://jsfiddle.net/nanilab/6tpztvnc/
Option 2
You can use FitText.js
FitText makes font-sizes flexible. Use this plugin on your responsive design for ratio-based resizing of your headlines.
Option 3
You can use slabText.js
A jQuery plugin for producing big, bold & responsive headlines

Check out the CSS Viewport percentage lengths, this might be what you're looking for: https://css-tricks.com/viewport-sized-typography/
I put together a quick container with text that will change size based on the size of the viewport. You can check it out here: http://codepen.io/sktwentysix/pen/GZzvEx
<div class="main">
<p>
lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum lorum ipsum!
</p>
</div>
.main {
border: 1px solid #000;
padding: 1rem;
width: 80%;
height: 500px;
}
p {
margin: 0;
font-size: 4vw;
}
Hope this helps!

only CSS will not be possible unless the lenght of your text is known.
If it is known, vw units + transform will do .
Problems : long text on small window could be too small to be read
Short text on large screen will be awfully stretched
h1 {
height: 3vw;
background: #147EBD;
text-align: center;
}
span {
display: inline-block;
height: inherit;
line-height: inherit;
font-size: 10vw;
transform-origin: top center;
transform: scale(1, 0.3);
}
h1 + h1 span {
line-height: 40vw;
font-size: 40vw;
transform: scale(1, 0.08);
}
<h1> <span>longer text to strecth </span></h1>
<h1> <span>short</span></h1>
As you can see, javascript will be needed to find out text lenght before to apply a font-size and a proper scale to start with .
It would be wise somehow to still set a max and min font-size so it remains readable at anytime time, and eventually let break a line on small screen.
edit: i did understand 100% of container, not window's height but scale() will do also in this case.

Related

Difference in rendering of negative text indent layout between Chrome & Firefox

I use padding and a negative text-indent to create an indented layout for responsive forms but now want to make the bottom paragraph a block level element so it always wraps.
The layout works fine when the bottom row is display inline or inline-block but as soon as the bottom row is display block then the entire layout in Chrome changes.
I don't know which browser is right, but it is Firefox's interpretation of the layout that I am after.
This is a simplified version of a layout I use for forms to attempt to fix this problem.
http://codepen.io/rachelreveley/pen/rxxxRj
<div>
<div>
<p class="top">Lorem ipsum dolor sit amet</p>
<p class="bottom">Lorem ipsum dolor sit amet</p>
</div>
</div>
<style type="text/css">
div div {padding-left: 36%; margin: 0; text-indent: -18%; background-color: #cee; width: 300px;}
p {text-indent: 0;}
p.top {display: inline-block; background-color: #ffc;}
p.bottom {display: block; width: 200px; background-color: #fcf;}
</style>
Real world example of how this code is being used.
I have fixed this by changing from using padding to margin on the parent container and then adjusting the sizes after that.

prioritize vertical scrolling over horizontal in column flex direction with wrap

In this example I'm using flex-direction:row
http://jsfiddle.net/9a1d2ddz/
When space is not enough to fit elements, a vertical scrollbar appears
I want to achieve the same, but with "top to bottom" box ordering
http://jsfiddle.net/ebd8rsnx/
but instead of getting an horizontal scrollbar I want to keep the vertical one
basically the same of the above, but with top to bottom box ordering instead of left to right
I thought it was something I could do with max-height:min-content but it seems to have no effect.
thank you in advance
div
{
overflow:auto;
border:2px blue solid;
box-sizing:border-box;
flex-direction:column; /* try column|row */
display:flex;
flex-wrap:wrap;
}
span
{
min-width:150px;
min-height:150px;
flex:1 1 auto;
border:1px red solid;
display:block;
overflow:hidden;
box-sizing:border-box;
}
I tried specifying the overflow direction, but that doesn't seem to work in this case. I think the problem is that you're trying to make your overflow direction as your wrap direction.
for example, try making it do the same type of thing horizontally. It'll create the same problem because you are attempting to flow and overflow in the same direction. That said, I don't think you'll be able to do what you want with straight css. You may want to consider a js tool like Columnizer or Masonry. The reason being that it can't just flow. It needs to calculate the columns first, then adjust the elements to fill in the columns before it determines its content length in the overflow direction. With a flow, there's no way for a browser to determine when it should break to a new row/column if your overflow is in the same direction as your flow. I've made something like this work before, working horizontal, but the way it worked was to put everything into 1 row, which isn't what you're really going for here.
CSS3 "column" feature works well for me:
http://caniuse.com/#feat=multicolumn
The column content can be anything, not text-only. Set column-width on the div containing the content, and wrap it into another div with fixed height.
.columns-container {
max-height: 50vh;
overflow-y:auto;
}
.columns {
-webkit-column-width: 15em;
-moz-column-width: 15em;
column-width: 15em;
/*optional column-count*/
/*
-webkit-column-count:4;
-moz-column-count: 4;
column-count: 4;
*/
-webkit-column-gap: 0;
-moz-column-gap: 0;
column-gap: 0;
}
<div class="columns-container">
<div class="columns">
<div>Lorem ipsum dolor sit amet. Sample text 1</div>
<div>Lorem ipsum dolor sit amet. Sample text 2</div>
...
<div>Lorem ipsum dolor sit amet. Sample text 1000</div>
</div>
</div>

How can I constrain the width of a child within an inline-block element?

How can I constrain the width of a child within an inline-block element?
Assume that I am targeting newest browsers.
Given an element displayed using inline-block, how can I constrain a child element so that it does not scale beyond the parent's witch.
In practical terms, I am trying to built a system that will take an image of any width and keep the caption constrained to the width of the parent container:
without having to specify width
without using jQuery or other DOM manipulation
CSS
<style>
div {
width:800px;
background-color:silver;
text-align: center;
}
figure {
display:inline-block;
background-color: orange;
padding: 1em;
margin: 0;
}
figcaption {
background-color:pink;
}
</style>
HTML
<div>
<figure>
<img src="http://i.techrepublic.com.com/blogs/11062012figure_a.gif" />
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</figcaption>
</figure>
</div>
In my opinion adding the following code can help you:
figure {width: 100%;}
img {width: 100%;}
In any case if you want to do this only with CSS, then you need play with percentages.

Extra line added :after pseudo-element when using HTML5 DTD (versus XHTML1)

For a good long while I've been using a really nice column spanning method I stumbled across, wherein I can have a div with the class .boxcontainer and child .box elements, and using an :after pseudo-element on .boxcontainer, my .box columns justify nice and evenly across the page. Here are the all-important definitions:
.boxcontainer {
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
background-color: orange;
}
.boxcontainer:after {
content: '';
display: inline-block;
width: 100%;
height: 0px;
font-size: 0px;
line-height: 0px;
}
Most of my previous projects have been XHTML1 Transitional (which I have subsequently learned uses a limited quirks mode when compared to other DTDs), and using this method in XHTML1 the parent .boxcontainer always wrapped perfectly flush around the child .box elements.
However, working on a new project in HTML5, I've discovered that there appears to be an extra line added underneath the justified .box elements. You can see an example here: http://jsfiddle.net/RZQTM/1/ - click on Fiddle Options and change the DTD to just about anything else and you'll see what I mean - an orange 'band' appears underneath the justified blue boxes.
I think this is down to something in the :after pseudo-element being rendered almost like an additional line of content, but I have no idea how to fix it. Any tips on how to remove the extra space under the boxes would be most gratefully received.
The trick i use to make this extra line to vanish is to apply line-height:0; on parent ,
and reset line-height to 1.2em or whatever line-hight you had setted.
vertical-align:top;/* or bottom */ on :after elements ends up to swallow any vertical gaps left.
one exemple : http://codepen.io/gcyrillus/pen/dlvCp
A work around with extra markup and using CSS table-cells
I sometimes use the following. In the HTML, I add an extra element div.spacer:
<div class="boxcontainer">
<div class="box two">
<h3>This is the title</h3>
Lorem ipsum dolor amit ...</div>
<div class="spacer"></div>
<div class="box two">
<h3>This is the title</h3>
Lorem ipsum dolor amit ...</div>
<div class="spacer"></div>
<div class="box two">
<h3>This is the title</h3>
Lorem ipsum dolor amit ...</div>
<div class="spacer"></div>
<div class="box two">
<h3>This is the title</h3>
Lorem ipsum dolor amit ...</div>
</div>
For the CSS, I use display: table on the parent container and then display: table-cell on the .box child elements:
.boxcontainer {
background-color: orange;
display: table;
width: 100%;
}
.box {
vertical-align: top;
display: table-cell;
background-color: blue;
color: white;
font-family: Trebuchet MS;
}
The demo fiddle is: http://jsfiddle.net/audetwebdesign/xWfk2/
Internally, the CSS re-purposes the .spacer as sibling table-cells and the default spacing tends to be even because the .box elements have a fixed width.
This approach is ideal if the .spacer elements serve a useful purpose (have real content) and if you have other reasons to use table-cells, say vertical positioning control and so on.

How to make the long text message within the div

i have a long message inside the div box, and i am looking for a way to show the that text message within the div with scroll bar (vertical) and i have two links to it as you can see:
1)disclaimer
2)Behavior
the above links are hyperlink so when i click those links i display the below divs based on the selection. so is that possible to show the div underneath the hyperlink with nice model div or something?
i am not a CSS guy so if possible make the div box look awesome :)
<span ><a href="#" >Disclaimer-1</a></span> <span class="slide1">
| Behavior</span>
<div id="one" >
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
</div>
<div id="second" >
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
</div>
If you want a scroll bar, set a height and then make the overflow value auto or scroll.
div#one,
div#two
{
height: 120px;
overflow: auto;
}
As for your latest edit, you're looking at JavaScript to implement something like that.
Hi for make scroll bar in left and format the page please find basic css you can use.
<style type="text/css">
#one{
overflow-y: scroll;
padding: 15px;
height: 150px;
line-height: 25px;
margin: 20px 0px 20px 0px;
}
#second{
overflow-y: scroll;
padding: 15px;
height: 150px;
line-height: 25px;
}
span a{
padding-left:15px;
color:blue;
text-decoration: none;
}
span a:hover{
padding-left:15px;
color:blue;
text-decoration: none;
}
</style>
Please specify the point when i click on the div i just need the div to be underneath the links.
thanks..

Resources