CSS as neexed width in one line












1















I want to crate a view on my page:



enter image description here



where:



1) the red item is the main container and has the width:100%



2) the green, blue and white elements are in the one container which has the width as needed and is on the center of the main container



3) the green elements have the fix width:50px



4) the blue elements have the width as needed (depends on the text length inside but the text has to be the same so both elements have the same width)



5) the white element has fix width:100px



I tried something like:



<div style="height:100px;width:100%;background:red">
<div style="height:100px;display:inline-block;margin:auto">
<div style="height:100px;width:50px;background:green;float:left"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:100px;background:white"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:50px;background:green;float:left"></div>
</div>
</div>


but the result is totally wrong...
enter image description here



How is it possible to get this effect?










share|improve this question























  • use float right in the inner blocks instead of float left

    – Himanshu Ahuja
    Nov 23 '18 at 22:32
















1















I want to crate a view on my page:



enter image description here



where:



1) the red item is the main container and has the width:100%



2) the green, blue and white elements are in the one container which has the width as needed and is on the center of the main container



3) the green elements have the fix width:50px



4) the blue elements have the width as needed (depends on the text length inside but the text has to be the same so both elements have the same width)



5) the white element has fix width:100px



I tried something like:



<div style="height:100px;width:100%;background:red">
<div style="height:100px;display:inline-block;margin:auto">
<div style="height:100px;width:50px;background:green;float:left"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:100px;background:white"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:50px;background:green;float:left"></div>
</div>
</div>


but the result is totally wrong...
enter image description here



How is it possible to get this effect?










share|improve this question























  • use float right in the inner blocks instead of float left

    – Himanshu Ahuja
    Nov 23 '18 at 22:32














1












1








1








I want to crate a view on my page:



enter image description here



where:



1) the red item is the main container and has the width:100%



2) the green, blue and white elements are in the one container which has the width as needed and is on the center of the main container



3) the green elements have the fix width:50px



4) the blue elements have the width as needed (depends on the text length inside but the text has to be the same so both elements have the same width)



5) the white element has fix width:100px



I tried something like:



<div style="height:100px;width:100%;background:red">
<div style="height:100px;display:inline-block;margin:auto">
<div style="height:100px;width:50px;background:green;float:left"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:100px;background:white"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:50px;background:green;float:left"></div>
</div>
</div>


but the result is totally wrong...
enter image description here



How is it possible to get this effect?










share|improve this question














I want to crate a view on my page:



enter image description here



where:



1) the red item is the main container and has the width:100%



2) the green, blue and white elements are in the one container which has the width as needed and is on the center of the main container



3) the green elements have the fix width:50px



4) the blue elements have the width as needed (depends on the text length inside but the text has to be the same so both elements have the same width)



5) the white element has fix width:100px



I tried something like:



<div style="height:100px;width:100%;background:red">
<div style="height:100px;display:inline-block;margin:auto">
<div style="height:100px;width:50px;background:green;float:left"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:100px;background:white"></div>
<div style="height:100px;display:inline-block;background:blue;float:left">MyText</div>
<div style="height:100px;width:50px;background:green;float:left"></div>
</div>
</div>


but the result is totally wrong...
enter image description here



How is it possible to get this effect?







html css






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 22:26









karl personkarl person

1327




1327













  • use float right in the inner blocks instead of float left

    – Himanshu Ahuja
    Nov 23 '18 at 22:32



















  • use float right in the inner blocks instead of float left

    – Himanshu Ahuja
    Nov 23 '18 at 22:32

















use float right in the inner blocks instead of float left

– Himanshu Ahuja
Nov 23 '18 at 22:32





use float right in the inner blocks instead of float left

– Himanshu Ahuja
Nov 23 '18 at 22:32












1 Answer
1






active

oldest

votes


















5














You are overcomplicating this. You simply need to use inline-block elements without float:






.main {
background:red;
font-size:0;
text-align:center;
height:100px;
}
.main > div {
display:inline-block;
font-size:initial;
height:100%;
vertical-align:top;
}

.blue {
background:blue;
}
.green {
width:60px;
background:green;
}
.white {
width:100px;
background:white;
}

<div class="main">
<div class="green"></div>
<div class="blue">text inside</div>
<div class="white"></div>
<div class="blue">text inside</div>
<div class="green"></div>
</div>





And in case the geen and white block are simply used for the visual effect you can easily replace them with some background and box-shadow:






.main {
background:red;
text-align:center;
height:100px;
overflow:hidden;
}
.main > div {
background:#fff;
font-size:0;
}
.main div {
display:inline-block;
height:100%;
vertical-align:top;
}

.blue {
background:blue;
font-size:initial;
}
.main .blue:first-child {
margin-right:50px;
box-shadow:-25px 0 0 25px green;
}
.main .blue:last-child {
margin-left:50px;
box-shadow:25px 0 0 25px green;
}

<div class="main">
<div>
<div class="blue">text</div>
<div class="blue">text inside</div>
</div>
</div>





And if you want the white part to be the dead center you can try this:






.main {
height:100px;
font-size:0;
}
.main > div {
display:inline-block;
background:red;
width:calc(50% - 50px);
height:100%;
font-size:initial;
overflow:hidden;
}
.main > div .blue {
display:inline-block;
height:100%;
vertical-align:top;
background:blue;
box-shadow:0 0 0 50px green;
}

.main > div:first-child {
text-align:right;
margin-right:50px;
}
.main > div:last-child {
text-align:left;
margin-left:50px;
}

<div class="main">
<div>
<div class="blue">text</div>
</div>
<div>
<div class="blue">text very long inside</div>
</div>
</div>








share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453497%2fcss-as-neexed-width-in-one-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You are overcomplicating this. You simply need to use inline-block elements without float:






    .main {
    background:red;
    font-size:0;
    text-align:center;
    height:100px;
    }
    .main > div {
    display:inline-block;
    font-size:initial;
    height:100%;
    vertical-align:top;
    }

    .blue {
    background:blue;
    }
    .green {
    width:60px;
    background:green;
    }
    .white {
    width:100px;
    background:white;
    }

    <div class="main">
    <div class="green"></div>
    <div class="blue">text inside</div>
    <div class="white"></div>
    <div class="blue">text inside</div>
    <div class="green"></div>
    </div>





    And in case the geen and white block are simply used for the visual effect you can easily replace them with some background and box-shadow:






    .main {
    background:red;
    text-align:center;
    height:100px;
    overflow:hidden;
    }
    .main > div {
    background:#fff;
    font-size:0;
    }
    .main div {
    display:inline-block;
    height:100%;
    vertical-align:top;
    }

    .blue {
    background:blue;
    font-size:initial;
    }
    .main .blue:first-child {
    margin-right:50px;
    box-shadow:-25px 0 0 25px green;
    }
    .main .blue:last-child {
    margin-left:50px;
    box-shadow:25px 0 0 25px green;
    }

    <div class="main">
    <div>
    <div class="blue">text</div>
    <div class="blue">text inside</div>
    </div>
    </div>





    And if you want the white part to be the dead center you can try this:






    .main {
    height:100px;
    font-size:0;
    }
    .main > div {
    display:inline-block;
    background:red;
    width:calc(50% - 50px);
    height:100%;
    font-size:initial;
    overflow:hidden;
    }
    .main > div .blue {
    display:inline-block;
    height:100%;
    vertical-align:top;
    background:blue;
    box-shadow:0 0 0 50px green;
    }

    .main > div:first-child {
    text-align:right;
    margin-right:50px;
    }
    .main > div:last-child {
    text-align:left;
    margin-left:50px;
    }

    <div class="main">
    <div>
    <div class="blue">text</div>
    </div>
    <div>
    <div class="blue">text very long inside</div>
    </div>
    </div>








    share|improve this answer






























      5














      You are overcomplicating this. You simply need to use inline-block elements without float:






      .main {
      background:red;
      font-size:0;
      text-align:center;
      height:100px;
      }
      .main > div {
      display:inline-block;
      font-size:initial;
      height:100%;
      vertical-align:top;
      }

      .blue {
      background:blue;
      }
      .green {
      width:60px;
      background:green;
      }
      .white {
      width:100px;
      background:white;
      }

      <div class="main">
      <div class="green"></div>
      <div class="blue">text inside</div>
      <div class="white"></div>
      <div class="blue">text inside</div>
      <div class="green"></div>
      </div>





      And in case the geen and white block are simply used for the visual effect you can easily replace them with some background and box-shadow:






      .main {
      background:red;
      text-align:center;
      height:100px;
      overflow:hidden;
      }
      .main > div {
      background:#fff;
      font-size:0;
      }
      .main div {
      display:inline-block;
      height:100%;
      vertical-align:top;
      }

      .blue {
      background:blue;
      font-size:initial;
      }
      .main .blue:first-child {
      margin-right:50px;
      box-shadow:-25px 0 0 25px green;
      }
      .main .blue:last-child {
      margin-left:50px;
      box-shadow:25px 0 0 25px green;
      }

      <div class="main">
      <div>
      <div class="blue">text</div>
      <div class="blue">text inside</div>
      </div>
      </div>





      And if you want the white part to be the dead center you can try this:






      .main {
      height:100px;
      font-size:0;
      }
      .main > div {
      display:inline-block;
      background:red;
      width:calc(50% - 50px);
      height:100%;
      font-size:initial;
      overflow:hidden;
      }
      .main > div .blue {
      display:inline-block;
      height:100%;
      vertical-align:top;
      background:blue;
      box-shadow:0 0 0 50px green;
      }

      .main > div:first-child {
      text-align:right;
      margin-right:50px;
      }
      .main > div:last-child {
      text-align:left;
      margin-left:50px;
      }

      <div class="main">
      <div>
      <div class="blue">text</div>
      </div>
      <div>
      <div class="blue">text very long inside</div>
      </div>
      </div>








      share|improve this answer




























        5












        5








        5







        You are overcomplicating this. You simply need to use inline-block elements without float:






        .main {
        background:red;
        font-size:0;
        text-align:center;
        height:100px;
        }
        .main > div {
        display:inline-block;
        font-size:initial;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        }
        .green {
        width:60px;
        background:green;
        }
        .white {
        width:100px;
        background:white;
        }

        <div class="main">
        <div class="green"></div>
        <div class="blue">text inside</div>
        <div class="white"></div>
        <div class="blue">text inside</div>
        <div class="green"></div>
        </div>





        And in case the geen and white block are simply used for the visual effect you can easily replace them with some background and box-shadow:






        .main {
        background:red;
        text-align:center;
        height:100px;
        overflow:hidden;
        }
        .main > div {
        background:#fff;
        font-size:0;
        }
        .main div {
        display:inline-block;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        font-size:initial;
        }
        .main .blue:first-child {
        margin-right:50px;
        box-shadow:-25px 0 0 25px green;
        }
        .main .blue:last-child {
        margin-left:50px;
        box-shadow:25px 0 0 25px green;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        <div class="blue">text inside</div>
        </div>
        </div>





        And if you want the white part to be the dead center you can try this:






        .main {
        height:100px;
        font-size:0;
        }
        .main > div {
        display:inline-block;
        background:red;
        width:calc(50% - 50px);
        height:100%;
        font-size:initial;
        overflow:hidden;
        }
        .main > div .blue {
        display:inline-block;
        height:100%;
        vertical-align:top;
        background:blue;
        box-shadow:0 0 0 50px green;
        }

        .main > div:first-child {
        text-align:right;
        margin-right:50px;
        }
        .main > div:last-child {
        text-align:left;
        margin-left:50px;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        </div>
        <div>
        <div class="blue">text very long inside</div>
        </div>
        </div>








        share|improve this answer















        You are overcomplicating this. You simply need to use inline-block elements without float:






        .main {
        background:red;
        font-size:0;
        text-align:center;
        height:100px;
        }
        .main > div {
        display:inline-block;
        font-size:initial;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        }
        .green {
        width:60px;
        background:green;
        }
        .white {
        width:100px;
        background:white;
        }

        <div class="main">
        <div class="green"></div>
        <div class="blue">text inside</div>
        <div class="white"></div>
        <div class="blue">text inside</div>
        <div class="green"></div>
        </div>





        And in case the geen and white block are simply used for the visual effect you can easily replace them with some background and box-shadow:






        .main {
        background:red;
        text-align:center;
        height:100px;
        overflow:hidden;
        }
        .main > div {
        background:#fff;
        font-size:0;
        }
        .main div {
        display:inline-block;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        font-size:initial;
        }
        .main .blue:first-child {
        margin-right:50px;
        box-shadow:-25px 0 0 25px green;
        }
        .main .blue:last-child {
        margin-left:50px;
        box-shadow:25px 0 0 25px green;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        <div class="blue">text inside</div>
        </div>
        </div>





        And if you want the white part to be the dead center you can try this:






        .main {
        height:100px;
        font-size:0;
        }
        .main > div {
        display:inline-block;
        background:red;
        width:calc(50% - 50px);
        height:100%;
        font-size:initial;
        overflow:hidden;
        }
        .main > div .blue {
        display:inline-block;
        height:100%;
        vertical-align:top;
        background:blue;
        box-shadow:0 0 0 50px green;
        }

        .main > div:first-child {
        text-align:right;
        margin-right:50px;
        }
        .main > div:last-child {
        text-align:left;
        margin-left:50px;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        </div>
        <div>
        <div class="blue">text very long inside</div>
        </div>
        </div>








        .main {
        background:red;
        font-size:0;
        text-align:center;
        height:100px;
        }
        .main > div {
        display:inline-block;
        font-size:initial;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        }
        .green {
        width:60px;
        background:green;
        }
        .white {
        width:100px;
        background:white;
        }

        <div class="main">
        <div class="green"></div>
        <div class="blue">text inside</div>
        <div class="white"></div>
        <div class="blue">text inside</div>
        <div class="green"></div>
        </div>





        .main {
        background:red;
        font-size:0;
        text-align:center;
        height:100px;
        }
        .main > div {
        display:inline-block;
        font-size:initial;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        }
        .green {
        width:60px;
        background:green;
        }
        .white {
        width:100px;
        background:white;
        }

        <div class="main">
        <div class="green"></div>
        <div class="blue">text inside</div>
        <div class="white"></div>
        <div class="blue">text inside</div>
        <div class="green"></div>
        </div>





        .main {
        background:red;
        text-align:center;
        height:100px;
        overflow:hidden;
        }
        .main > div {
        background:#fff;
        font-size:0;
        }
        .main div {
        display:inline-block;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        font-size:initial;
        }
        .main .blue:first-child {
        margin-right:50px;
        box-shadow:-25px 0 0 25px green;
        }
        .main .blue:last-child {
        margin-left:50px;
        box-shadow:25px 0 0 25px green;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        <div class="blue">text inside</div>
        </div>
        </div>





        .main {
        background:red;
        text-align:center;
        height:100px;
        overflow:hidden;
        }
        .main > div {
        background:#fff;
        font-size:0;
        }
        .main div {
        display:inline-block;
        height:100%;
        vertical-align:top;
        }

        .blue {
        background:blue;
        font-size:initial;
        }
        .main .blue:first-child {
        margin-right:50px;
        box-shadow:-25px 0 0 25px green;
        }
        .main .blue:last-child {
        margin-left:50px;
        box-shadow:25px 0 0 25px green;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        <div class="blue">text inside</div>
        </div>
        </div>





        .main {
        height:100px;
        font-size:0;
        }
        .main > div {
        display:inline-block;
        background:red;
        width:calc(50% - 50px);
        height:100%;
        font-size:initial;
        overflow:hidden;
        }
        .main > div .blue {
        display:inline-block;
        height:100%;
        vertical-align:top;
        background:blue;
        box-shadow:0 0 0 50px green;
        }

        .main > div:first-child {
        text-align:right;
        margin-right:50px;
        }
        .main > div:last-child {
        text-align:left;
        margin-left:50px;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        </div>
        <div>
        <div class="blue">text very long inside</div>
        </div>
        </div>





        .main {
        height:100px;
        font-size:0;
        }
        .main > div {
        display:inline-block;
        background:red;
        width:calc(50% - 50px);
        height:100%;
        font-size:initial;
        overflow:hidden;
        }
        .main > div .blue {
        display:inline-block;
        height:100%;
        vertical-align:top;
        background:blue;
        box-shadow:0 0 0 50px green;
        }

        .main > div:first-child {
        text-align:right;
        margin-right:50px;
        }
        .main > div:last-child {
        text-align:left;
        margin-left:50px;
        }

        <div class="main">
        <div>
        <div class="blue">text</div>
        </div>
        <div>
        <div class="blue">text very long inside</div>
        </div>
        </div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 23:08

























        answered Nov 23 '18 at 22:33









        Temani AfifTemani Afif

        71.9k94080




        71.9k94080
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453497%2fcss-as-neexed-width-in-one-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'