I have the following:
<div class="parent">
<div class="sizer" />
<div class="child" />
</div>
.sizer {
width: 200px;
}
.child {
position: relative;
width: 400px;
}
I am not allowed to explicitly set the width on the parent, and I am not allowed to set the child to position:absolute.
Using CSS, is there any other way to set the width of .child to 400 without also having the width of .parent expand from 200 to 400?
If the parent has a set width, then it will not expand with the width of the child, even if the child’s width exceeds that of the parent. See this JS Fiddle example.
UPDATE
There is no way to accomplish this without the parent having some sort of width or width-like property. If you are only opposed to using the
widthproperty on the parent, you can use several width-like alternatives.As cimmanon explained in a comment below, you could set
.parentto have amax-width. It does not necessarily have to be100%; any max-width will do, as long as it is less than the width of the child. See this JS Fiddle example, or check out this code:Alternatively, you could use
position:absoluteand set theleftandrightproperties. See this JS Fiddle example, or check out this code:But remember, these are only two of the many possible alternatives. Hope this helps!