When the parent element has a fixed width, it will not adjust to the width of its child, even if the child's width is larger than that of the parent. To see an example of this in action, take a look at this JS Fiddle demonstration.
.parent {
width: 300px;
height: 50px;
}
.child {
width: 400px;
height: 50px;
position: relative;
}
UPDATE
I am curious if there is any workaround that would allow me to achieve this without specifying a width for the parent.
Unfortunately, there is no way to achieve this effect without some kind of width or equivalent property applied to the parent element. If you prefer not to use the width
property specifically, there are other options available that mimic its behavior.
For instance, as mentioned by cimmanon in a comment below, you could set a max-width
for the .parent
element. This max-width does not have to be set to 100%
; any value less than the child's width will suffice. Check out this JS Fiddle example, or refer to this code:
.parent {
max-width: 200px;
}
.child {
width: 400px;
}
Alternatively, you can utilize position:absolute
and adjust the left
and right
properties. See this JS Fiddle demonstration, or take a look at this code:
.parent {
position:absolute;
left:0;
right:0;
}
Keep in mind that these are just a couple of possible solutions among many others. Hopefully, this information proves helpful!