Font-size depends on div width and height - html

Consider the following markup and styles:
<div>
Some text Some text Some text
</div>
div{
width: 100px;
}
How can I do that the text-content of div have a font-size's property value such that there is maximum font-size value in which text-content of div lie on one line entirely? jsFiddle

Try this:
HTML:
<div id="container">
<span id="text_container">whatever text you want</span>
</div>
CSS:
#container {
background:cyan;
width:200px;
}
#text_container {
white-space:nowrap;
}
JS:
var container = $("#container"),
text_container = $("#text_container"),
start_size = 100,
container_width = container.width();
text_container.css('font-size', start_size + 'px');
while (text_container.width() > container_width) {
text_container.css('font-size', start_size--+'px');
}
DEMO

Do this instead:
div{
min-width: 200px;
}
By setting a minimum width you ensure that the div never gets small enough to collapse the text to multiple lines.
Demo: http://jsfiddle.net/96daR/4/

Give a id to the div, say id="myDiv"
Get height using one of this
javascript:
var myDiv = document.getElementById("myDiv");
h=myDiv.clientHeight;
h=myDiv.scrollHeight;
h=myDiv.offsetHeight;
or in jquery:
h=$("#myDiv").height();
h=$("#myDiv").innerHeight();
h=$("#myDiv").outerHeight();
Next set the font-size using this:
document.getElementById("myDiv").style.font-size=h+"px";

Related

Make input element auto-size like a span

A <span> knows what horizontal size to be without being told. It's horizontal size is no greater than its content.
I'm trying to figure out the CSS to make an <input type='text'> automatically size itself horizontally according to the length of its value, like a <span> would with its innerText.
In other words, without specifying a CSS width: or size attribute on the <input>.
I can't figure out how to do this. Any ideas?
If you want to expand or increase the width of input field as you type you could do something like this
<div>
<span contenteditable="true">sdfsd</span>
</div>
CSS
span{
border: solid 1px black;
}
div{
max-width: 200px;
}
JSFiddle Demo
Or You could accomplish this using some jQuery
<input size="1" />
jQuery
$('input').on('keydown', function(evt) {
var $this = $(this),
size = parseInt($this.attr('size'));
if ( evt.which === 8 ) {
// backspace
$this.attr('size', size - 1);
} else {
// all other keystrokes
$this.attr('size', size + 1);
}
});
JSFiddle Demo
If I understand your question correctly, you want the span and input to be the same width no matter what, correct?
If this is the case then this is the way I would go about it:
Wrap both the span and input with a div
then,
span, input {
display: inline-block;
width: 100%;
}
And set your wrapping div to whatever width you want and the two elements should be aligned (automatically, no matter what) and the same width.

HTML/CSS - Automatically set height width from background image?

Longshot... I don't think this is possible but I've been shocked before!
I anchor tags, all of which have background images, all 300px wide but their heights all vary. Is there anyway to set these without actually having to type out the height? Sort of setting it to the bg url's dimensions?
Thanks!
I don't think people understand - My fault for rushing the question.
Here's code as an example:
#ex-1 {width: 300px; height: 410px; background: url('/image-1.jpg');}
#ex-2 {width: 300px; height: 420px; background: url('/image-2.jpg');}
#ex-3 {width: 300px; height: 430px; background: url('/image-3.jpg');}
#ex-4 {width: 300px; height: 440px; background: url('/image-3.jpg');}
I'd like to NOT set the height, and it set automatically using CSS only. I don't want to use image tags.
I wasn't sure if this was possible, I assume not.
Thanks
A simple way of doing this is to add an image like this and then make it hidden i used visibility:hidden http://jsfiddle.net/gztpsfkw/1/
i just saw that you don't want to use <img> tags but as for here the image is being hidden and it takes up the space.
<img src="http://placekitten.com/300/301" />aa
And apply the css
a{
display:block;
background-image:url('http://placekitten.com/300/301');
width:100px;
height:auto;
}
img{
visibility:hidden;
}
We can use a visibility: hidden way:
<img src="http://lorempixel.com/100/200/" />
CSS
a {background: url("http://lorempixel.com/100/200/") center center no-repeat; width: 100px;}
a img {visibility: hidden;}
Fiddle: http://jsfiddle.net/praveenscience/vhjxfgtw/
JavaScript Solution
Procedure
To set the height, dynamically, you need to use JavaScript. So, you can get the computed value by adding a <img /> tag and computing the value by setting the src. The pseudo code would have been like this:
Get the computed value of background-image.
Attach it to a new <img /> element in the DOM.
Get the height of the new <img /> element.
Set the height of the fake background <div>.
JavaScript
$(document).ready(function () {
bg = $(".bg").css("background-image");
$("body").append('<img id="faux" src="' + bg.substring(4, bg.length-1) + '" />');
height = $("#faux").outerHeight();
$("#faux").remove();
$(".bg").height(height);
});
Fiddle: http://jsfiddle.net/praveenscience/rcL3xj0x/
If you don't want to use inline CSS, you can use this:
$("style").append('.bg {height: ' + height + 'px}');
If you're looking for a way to make the background images fill all the space available then use background-size: cover
I think you're looking for something like this:
function setBackgroundImage(element, src) {
var img = new Image();
img.onload = function() {
element.style.height = img.height+'px';
element.style.backgroundImage = 'url('+img.src+')';
}
img.src = src;
}
Or, if you need to scale the images for the width:
function setImage(element, src) {
var img = new Image();
img.onload = function() {
var sizeRatio = element.offsetWidth / img.width;
element.style.height = (sizeRatio * img.height)+'px';
element.style.backgroundImage = 'url('+img.src+')';
element.style.backgroundSize = 'cover';
}
img.src = src;
}
Side Note: The <a> tag is not a block level element. In order for the <a> to have a height and a width you need to make it a block level element to show the background image.
You would use: display: block
Now for your question... In order to get the background image, with out having to manual type it in you can use a little jQUery to make your life a lot easier. I have modified your CSS and HTML a little bit to accomodate the jQuery.
CodePen Example
#links { overflow: hidden; }
#links a { display: block; float: left; width: 300px; height: 200px;
/* generic height set in case there is no background image */ }
#ex-1 { background: url('http://www.google.com/images/srpr/logo11w.png');}
#ex-2 { background: url('http://www.bing.com/s/a/hpc12.png');}
#ex-3 { background: url('http://www.google.com/images/srpr/logo11w.png');}
#ex-4 { background: url('http://www.bing.com/s/a/hpc12.png');}
<div id="links">
</div>
Here is the jquery. It will loop through all your images and set the height according to your background image
$(document).ready(function(){
$('#links a').each(function(){
var temp = $(this).css('background-image');
temp = temp.replace('url("', '').replace('")', '');
var newImg = new Image();
newImg.src = temp;
imageHeight = newImg.height;
imageWidth = newImg.width;
$(this).css('height', imageHeight);
});
});

How to set height of child div while hovering over a parent div in css?

I'm making a drop-down menu that I want to display a submenu when you hover over one of the main menus. All of the child_menus have an initial height of 0. My HTML is roughly as follows...
<div class = "parent_menu">Parent 1
<div class = "child_menu">Child 1 text</div>
<div class = "child_menu">Child 2 text</div>
</div>
<div class = "parent_menu">Parent 2
<div class = "child_menu">Child 1 text</div>
<div class = "child_menu">Child 2 text</div>
</div>
I know that you can set properties with the :hover selector, but can I do something like .parent_menu:hover THEN FIND .child_menu {height:auto;}
By the way I know I can do it in JavaScript, but there has to be a way in CSS too.
Try this:
http://jsfiddle.net/r83FD/1/
.parent_menu div {
height: 0;
overflow: hidden;
}
.parent_menu:hover div {
height: auto;
}
Try below code it should work
.parent_menu:hover a.child_menu{
height:auto;
}
I think you might mean something like this:
.parent_menu:hover .child_menu {
height: auto;
}
Try a demo of this here: http://jsfiddle.net/M4NUP/

CSS3 Transition: how to add transition on hover to this div?

I add some content on this div that will be visible on hover.
It work till here: jsbin.com/ipajas/
But as the content is loaded dynamically, I don't know it's exactly the content's height. So I change the height from height:250px; to height:auto;
And the problem is: in that case, the transition don't work anymore. jsbin.com/ipajas/2
Here is the code:
HTML:
<a>
<div class="divabsence" id="id" onClick="expand()">
chose your color:
<p> red <input type="checkbox" id="button"></p>
<p> blue <input type="checkbox" id="button"></p>
<p> green <input type="checkbox" id="button"></p>
<p> white <input type="checkbox" id="button"></p>
</div>
</a>
CSS:
.divcolor
{
width:120px;
height:30px;
background:red;
transition: 1s;
overflow:hidden;
}
.divcolor:hover
{
height:auto;
}
UPDATE
It seems to be impossible so, I've just add this as a temporary solution:
height: 150px; overflow:scroll; jsbin.com/ulodil/4
According to the CSS Specs only length, percentage, or calc are valid types, not auto.
Instead of height you can use max-height with a big enough value:
.divcolor
{
max-height:30px;
...
}
.divcolor:hover
{
max-height: 1000px; <- something just big enough for your needs
}
You could load everything into a <ul> and set the height dynamically based off the number of <li> elements. Depending on how the content is loaded dynamically you could do it through JavaScript or some server-side scripting.
You could add as many or as little <li> elements and the height will adjust.
JSBIN - Sample Here
function adjustHeight()
{
var itemList = document.getElementById('color-list');
var items = itemList.getElementsByTagName('li');
var listHeight = items.length * 25;
document.getElementById('id').style.height = (50 + listHeight) + "px";
}

Custom height in Lightbox2

Is there any way to set a fixed/custom height for ligtbox2?
#lightbox img{ width: auto; height: 600px;}
This only resizes the img and not the outer container.
With that declaration you are styling the img within #lightbox
Try removing the img so that you are only styling #lightbox
does this work?
#lightbox { width: auto; height: 600px;}
If you go through the HTML it creates you can see that it's wrapped in a div with id="lightbox" and within that a div with id="outerImageContainer". The latter has a style attribute with the height of the image. Try targeting that. Either overwriting it in your CSS or changing the height after it's loaded.
<html>
<head>
<style type="text/css">
#lightbox { border : solid 2px #000000; position:absolute; }
#lightbox img { width:auto; height: 600px;}
</style>
</head>
<body>
<div id="lightbox">
<img src="Desert.jpg" alt="desert" />
</div>
</body>
</html>
None of these solutions worked, but thanks for your help guys. I had to get my hands dirty in the js... here's my hacked code:
lightbox.js
...
// once image is preloaded, resize image container
imgPreloader.onload = (function(){
var scale = 600 / imgPreloader.height; //modified
this.lightboxImage.src = this.imageArray[this.activeImage][0];
this.resizeImageContainer((imgPreloader.width * scale), //modified imgPreloader.height);
}).bind(this);
imgPreloader.src = this.imageArray[this.activeImage][0];
},
//
// resizeImageContainer()
//
resizeImageContainer: function(imgWidth, imgHeight) {
// get current width and height
var widthCurrent = this.outerImageContainer.getWidth();
var heightCurrent = this.outerImageContainer.getHeight();
// get new width and height
var widthNew = (imgWidth + LightboxOptions.borderSize * 2);
var heightNew = (600 + LightboxOptions.borderSize * 2); //modified
...