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.
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.
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.
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.
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).
This idea needs testing, but it works in Chrome at least:
.content
and .sidebar1
to float: right
instead of float: left
..sidebar1
after .content
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With