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

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";
}

Related

How to let div grow with content while not lay over other content

I have a code for html/css tabbing. Unfortunately, it's scrolling for too long content. Since I don't know in advance, how long my texts will be, I want the text div to grow with the content.
I think the problem should be the position: absolute; but when I change this to relative (even just the div, not the input), the tabs are broken and the whole text div lays over other content of the page.
My full example is here: http://jsfiddle.net/0f8hno5o/
Here some parts:
<div class="tabreiter">
<ul>
<li>
<input type="radio" name="tabreiter-0" checked="checked" id="tabreiter-0-0" /><label for="tabreiter-0-0">Tab 1</label>
<div>
Long Text ...
</div>
</li>
</ul>
</div>
Part of my CSS:
.tabreiter
{
width: 100%;
height: 220px;
}
.tabreiter ul,
.tabreiter li
{
margin: 0;
padding: 0;
list-style: none;
}
.tabreiter,
.tabreiter input[type="radio"]:checked + label
{
position: relative;
}
.tabreiter li,
.tabreiter input[type="radio"] + label
{
display: inline-block;
}
.tabreiter li > div ,
.tabreiter input[type="radio"]
{
position: absolute;
}
I don't think that is possible with only html/css so I'm thinking you'll need some javascript/jquery. (i'm going to write a small snippet of jQuery, but if you don't use that you'll have to rewrite it to plain javascript)
I'd hook it on the change event of the radio buttons, since your tabs work with those.
Also, you will have to change the code of the tabs just a little bit, because otherwise I think jquery can't work out the height of the contents.
<div class="tabreiter">
<ul>
<li>
<input type="radio" name="tabreiter-0" checked="checked" id="tabreiter-0-0" /><label for="tabreiter-0-0">Tab 1</label>
<div>
<div> <!-- added extra div which will have the height we need -->
Long Text ...
</div>
</div>
</li>
</ul>
</div>
the jQuery:
<script>
//being extra specific here because I don't know if you've got other radio buttons on the page somewhere
$('.tabreiter ul li input[type=radio]').on('change', function() {
//calculate height of contents:
// you'll need to add the height of the tabs + the margins /top offsets set by your css, I haven't checked them precisely so these are rough estimates
var newHeight = $(this).parent('li').find('div>div').height() + 34 + 16 + 38;
set the height of the container div to match the newly calculated height
$('.tabreiter').height(newHeight);
});
//this triggers the change function to set correct height on page load
$('.tabreiter ul li input[type=radio]:checked').change();
</script>

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.

Font-size depends on div width and height

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";

CSS :target random #keyword, do something with same div

I have some links, and each of these have a unique href.
lets say link one have href="#first".
and link two have href="#second".
What would then be the CSS to do something with a div? (with the ID box)
I have tried lots of things like:
#first:target #box{
something..
}
#second:target #box{
something else..
}
#linkOne:hover #box{ width:200px; }
This changes the size of #box by hovering #linkOne I want the same to happen with :target, like change the size of #box by clicking the link
If you want to select the currently targeted element, you can simply do :target
html
first link
second link
<div id="first">first div</div>
<div id="second">second div</div>
css
:target {
border: 1px solid red;
}
The div with the corresponding id of the link clicked will have a red border.
http://jsfiddle.net/wk3rR/2/
UPDATE
Judging by your comment, it appears you want to manipulate the same box with different current targets, which won't be straight-forward, but can be done if you nest the <div>s with the IDs and then your original css should work:
first link
second link
<div id="first">
<div id="second">
<div class="box">box</div>
</div>
</div>
css
#first:target .box {
border: 1px solid red;
}
#second:target .box {
border: 1px solid yellow;
}
http://jsfiddle.net/wk3rR/3/
If I am not wrong, you are looking for something like the below. This will set the height of the currently targeted element to 20px. Transition effect will also be applied.
HTML:
<a href='#first'>First</a>
<a href='#second'>Second</a>
<div id='first'>This is first div</div>
<div id='second'>This is second div</div>
CSS:
#first, #second {
height: 0px;
overflow: hidden;
transition: height 1s ease-in;
}
#first:target {
height: 20px;
}
#second:target {
height: 20px;
}
Demo Fiddle
EDIT: I know you haven't tagged jQuery/JavaScript and was looking for a CSS solution. But if you have many such links and are ok to have a JS based solution, you can try the below.
This script has a mapping between the id of the link that is clicked and the height that the box is supposed to have when the link is clicked. Based on it, the #box element's height is modified. Transitions will also work as can be seen in this fiddle.
window.onload = function () {
document.getElementsByTagName('body')[0].onclick = function(e){
var box = document.getElementById('box');
var heights = { first: '20px', second: '40px', third: '30px' };
if(e.target.id)
box.style.height = heights[e.target.id];
};
}
I think you need the use of javascript if you want manipulate DOM. You have to remember CSS is only for style the page, not for making actions with the elements.

Wrapper layout and div problems

Image
I tried this with the following CSS and HTML. It looks fine when the browser is of full width and scrambled when browser is resized. I WANT the elements to be where there and a HORIZONTAL SCROLL has to appear when the BROWSER is RESIZED. Pretty new to web programming. Text-align:center for positioning the center column would not work because, every time a new text is added in the left or right, it gets relocated and also center column element in ROW1(text) and ROW2(Button) do not appear along the same line. That is, text appears a bit right and the button a bit left. Text-align won't work here.
CSS:
#charset "utf-8";
/* CSS Document */
body
{
background-color:#000;
}
.wrapper
{
width:70%;
margin:0 auto;
padding:2px;
background-color:#fff;
}
.second_row
{
padding:2px;
margin-top:10px;
}
.center_container
{
width:30%;
margin:0 auto;
}
.left_container
{
width:33%;
float:left;
}
.right_container
{
width:33%;
float:right;
}
.topelements
{
margin-top:0px;
color:#777;
padding:2px;
}
.topelements a:link
{
color:#29a3cc;
}
.topelements a:active a:hover
{
color:#29a3cc;
}
.logo
{
overflow:hidden;
}
HTML code:
<div class="wrapper">
<span class="topelements float_left" >Mail us: admin#admin.com</span>
<span class="topelements float_right">Left links My xyz</span>
<span class="topelements center_container">Welcome to xyz ! Sign in or Signup.</span>
</div>
<div class="wrapper second_row">
<span class="left_container">Srini</span>
<span class="right_container">Vas</span>
<form class="center_container">
<input type="text" placeholder="Goooooooooooo!" />
<input type="submit" value="Search" />
</form>
</div>
<div class="wrapper">
If you want to align you object in the center, there are a couple of different ways. First of all, there is the text-align:center; which you don't need right now. There is also object-position:center; which basically does the same, but with an object. This way isn't the best, but you could add a certain percentage of padding to either side but that's not recommended. Lastly, there's alignment-adjust:central;. This may not be perfect for your situation but just try out all of these and see if they work. Good luck!
One way that would work is to set your wrapper width to a fixed value (something in 800px for example). As long as this width was longer than all the content you are putting within that wrapper, everything should work as you want. The browser will automatically place a horizontal scroll bar when the window gets smaller than the width of the wrapper.
This is just a small error I found in the CSS and I don't know if this will help too much. The div you added was not referred to as a div, but a class. For example, if you wanted to style a div in CSS, you would do this:
#divname {
CSS for div goes here...
}
On the other hand, if you wanted to add a little style to a class, you would go like this:
.classname {
CSS for class goes here...
}
If you were wondering what the difference for each is, the answer is simple. A class can define multiple objects while the divs are just limited to one object or element.