Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a div auto expand width to the left instead of the right

Tags:

html

css

I have a div on my website which contains a list of items which acts as a menu. I have set the CSS width:auto so that it will re-size if a menu item is too long. At the moment though, this will expand to the right and "pushes" the rest of my content to the right as well.

This is hard to explain so as an example if you go to http://redsquirrelsoftware.co.uk/ and click on the Support menu item, you can see the content being pushed across. What I want to happen is for the div to expand into the white space to the left of the menu.

The CSS I have for the div at the moment is:

.sidebar1 {
    float: left;
    width: auto;
    min-width: 25%;
    max-width: 29%;
    margin-top: 65px;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    border-radius: 12px;
}

Is there any way for it to expand the other way? Thanks in advance.

like image 723
crazyloonybin Avatar asked Sep 26 '12 11:09

crazyloonybin


People also ask

How do I make a div auto adjust width?

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.

How do I make a div fill the remaining width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

Can width be auto in CSS?

Width: autoThe initial width of block-level elements like <div> or <p> is auto , which makes them take the full horizontal space of their containing block. When an element has auto as a value for width, it can have margin, padding, and border without becoming bigger than its parent element.


2 Answers

Another way I found to do this without using floats or absolute positioning is to use direction: rtl on the parent, and then reset it on the children:

.parent { direction: rtl; }
.parent > * { direction: ltr; }
.child { width: 200px; }
.child:hover { width: 400px; }

Code pen: http://codepen.io/heisters/pen/gMgboJ

(sorry, I couldn't base the example on the OP's site, since the link appears to be dead).

like image 162
Ian Avatar answered Sep 27 '22 20:09

Ian


This idea needs testing, but it works in Chrome at least:

  • Change .content and .sidebar1 to float: right instead of float: left.
  • In the HTML, move .sidebar1 after .content.
like image 37
thirtydot Avatar answered Sep 27 '22 20:09

thirtydot