Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase an width:auto DIV's width by X pixels using pure CSS

I have a DIV (that is set to float:left) that has its width set to auto because i want it to be just as wide as its contents.

On hover, i would like to increase width of the DIV by say, 20 pixels.

When setting a fixed width in the :hover CSS class, the container will get that width on hover, but

  1. CSS3-Transitions won't apply/work (tested in Firefox 15.0.1)
  2. If my DIV's contents are larger than that fixed width, the final width will not be correct.

How - without the usage of javascript - can i keep the content of the DIV adjusted to its content, and have it increase that width by a certain amount of pixels on hover?

See my JSFiddle here.

like image 382
SquareCat Avatar asked Oct 14 '12 00:10

SquareCat


People also ask

How do I change the width to 100% in CSS?

It seems like this should be one of the easiest things to understand in CSS. If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.

How do I set auto width in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

What does width 100% means in CSS?

if you specify width:100%, the element's total width will be 100% of its containing block plus any horizontal margin, padding and border. So, next time you find yourself setting the width of a block level element to 100% to make it occupy all available width, consider if what you really want is setting it to auto.


1 Answers

For me the easiest way to do this was to increase the content by a percentage higher than 100%. E.g.:

width: 110%;

But if you want to increase the size in n pixels as asked, you can use

width: calc(100% + 50px);
like image 194
EliuX Avatar answered Sep 26 '22 17:09

EliuX