4

I want to be able to display a DIV in a fixed position and have its width fit to content but each time I add position: fixed;, the div gets computed to display: block; and the div becomes full length.

HTML:

<div class='veil'></div>

CSS:

.veil{
    display: inline-block;
    position: fixed;
    top: 34%;
    left: 0;
    right: 0;
    margin: 0 auto;
    background-color: lavender;
}
0

2 Answers 2

7

each time I add position: fixed;, the div gets computed to display: block; and the div becomes full length.

It's not display: block, it's position: fixed makes div stretch relatively to browser window, and since you have left: 0 and right: 0 you observe this behavior that div takes all window width.

Depending on your HTML structure you can use position: absolute instead or as pointed in comments (thanks @Paulie_D) don't use right: 0.

Sign up to request clarification or add additional context in comments.

7 Comments

@Paulie_D If you remove the rightproprety, the div will still be diplay:block.
No...it doesn't. It displays as inline-block....at least it does in the fiddle. It won't center...if that's what you mean.
Check under the computed styles for the .veil.
@Paulie_D - What do you mean by displays as inline-block? That it shrink-wraps, or something else?
Yes, sorry I deleted the comment but yes it "computes" as block but displays (thus shrink wrapping) as `inline-block"...and that I think was the point.
|
2

Just add another container. and split the contradiction in CSS between them.

HTML:

<div class='container'><div class='veil'></div></div>

CSS:

.container
{
    position: fixed;
    top: 34%;
    left: 0;
    right: 0;
}
.veil
{
    display: inline-block;
    margin: 0 auto;
    background-color: lavender;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.