2

I have been looking everywhere for help on this issue with Css layout width I have been running into.

Whenever I float a div to the right its width won't automatically adjust to the total width of its children. I have observed this effect on all common browsers (Firefox, Chrome and IE11/Edge). What happens is that the last child will just be displayed bellow all the others which is what I do not want.

Here is the css and html I have been using.

https://jsfiddle.net/xqpf9s95/2/

    *

<div id="header-container">
        <div id="header-top-container">

          <div id="header-logo">
            <a href="/GlobalImagens/pages/imagens.xhtml?categoria=ultima-hora">
              <img src="../resources/images/logo_globalimagens.jpg" alt="Global Imagens"></a>
          </div>

          <div class="header-top-right-corner">
            <form id="language" name="language" method="post" action="/GlobalImagens/pages/imagens.xhtml" enctype="application/x-www-form-urlencoded">
              <input name="language" value="language" type="hidden">

              <div id="newsletter" class="newsletter">
                <a href="http://72.e-goi.com/w/Te1LetVmefgyM9g90LOh" target="_blank" style="font-size: 10px;">Subscrever Newsletter</a>
              </div>

              <div style="float: right; padding-left: 6%;">
                <script type="text/javascript" src="/GlobalImagens/javax.faces.resource/jsf.js.xhtml?ln=javax.faces&amp;stage=Development"></script>
                <a href="#" style="text-decoration:none; " onclick="mojarra.jsfcljs(document.getElementById('language'),{'language:j_idt31':'language:j_idt31','localeCode':'en'},'');return false">
                  <img src="../resources/images/flag_uk.jpg" border="0"></a>
              </div>

              <div style="float: right;">
                <a href="#" style="text-decoration:none;" onclick="mojarra.jsfcljs(document.getElementById('language'),{'language:j_idt35':'language:j_idt35','localeCode':'pt'},'');return false">
                  <img src="../resources/images/flag_pt.jpg" border="0"></a>
              </div>
              <input name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="215900126811062761:3093351618596041247" autocomplete="off" type="hidden">
            </form>
          </div>

          <div id="admin-container">

            <div>
              <span class="admin-menu1" style="padding-left: 1.5%;"><a href="/GlobalImagens/pages/entrar.xhtml" title="Iniciar Sessão">Iniciar Sessão</a>
                        </span>
              <span class="dotted-separator"></span>
              <span style="padding-left: 1.5%;"><a href="/GlobalImagens/pages/registo.xhtml" title="Registo">Registo</a>
                        </span>

              <span class="admin-menu3"><a href="/GlobalImagens/pages/entrar.xhtml">
                            <img src="../resources/images/bt_minhaconta.jpg" title="A Minha conta" alt="A Minha conta"></a>
                    </span>
              <span class="dotted-separator"></span>
              <span class="admin-menu4"><a href="/GlobalImagens/pages/entrar.xhtml">
                            <img src="../resources/images/bt_carrinho.jpg" title="Meu carrinho" alt="Meu carrinho"></a>
                    </span>
            </div>
            <div>

              <div align="right">
                <span style="color: #83266f; padding-right: 5px;">Não pode adquirir imagens</span><a href="/GlobalImagens/pages/consumos.xhtml" style="color:#83266f;" title="detalhes">(detalhes)</a>
              </div>

            </div>
          </div>

        </div>
    /*tables header*/
.admin-menu1 {
  padding-right: 1.5%;
}
.dotted-separator {
  border: none;
  border-left: 1px dotted #83256f;
  color: #fff;
  /*   background-color:#dadada;
  height:17px;
  width:0%;
  margin: 0%; */
}
.admin-menu2 {
  padding-left: 10px;
  background: url(../images/background_dot.jpg) no-repeat right;
}
.admin-menu-logged-in-3 {
  padding-left: 1.5%;
}
.admin-menu3 {
  /* width: 75px; */
}
.admin-menu4 {
  /*    width: 28px; */
}
/*******************************HEADER*******************************/
#header-container {
  /* height: 180px; */
  /* float: left; */
}
#header-top-container {
  /*    width: 983px; */
  /* height: 100px; */
  /* float: left; */
}
#header-logo {
  padding-top: 1%;
  float: left;
}
#header-logo img {
  border: none;
  border-style: none;
}
.newsletter {
  float: left;
  padding-top: 1%;
}
.header-top-right-corner {
  float: right;
  padding-top: 1%;
  width: 11%;
}
#admin-container {
  padding-top: 1%;
  font-size: 10px;
  clear: right;
  float: right;
  box-sizing: border-box;
}
#admin-container a {
  text-decoration: none;
  color: #493641;
}
#admin-container a:hover {
  text-decoration: underline;
}

*

And my issue is with the div "#admin-container".

How do I fix this so as to make that div auto adjust to the correct width and display without breaking its children elements?

Cheers and thank you.

EDIT: I have editted the code as asked by @Dzijeus. As I have commented, the images don't matter for the issue. My issue is with why the width won't auto adjust on the '#admin-container' to fit all its children.

João Rebelo
  • 79
  • 14
  • Try using less CSS in your HTML (style-attribute), Your question isn't very clear; when you use float: right on an element inside admin-container, it will jump below the sibling elements, you want to solve that? – hreitsma Apr 07 '16 at 10:46
  • Hello hretsma. Yes, that is my issue (even though the images don't load because they're local to my machine) you can see that my element '#admin-container" is floating right, but its children are all being displayed correctly except the last one, with class admin-menu4, which falls down. I just don't understand this behaviour as I was expecting the browser to auto-adjust the width accordingly as I'm using a box-sizing:border-box layout. – João Rebelo Apr 07 '16 at 10:54
  • Additionaly, if I set the width manually the effect that I wish obtained will do, but thinking about the future I don't want to remember that rule, ence my question here on stack :) – João Rebelo Apr 07 '16 at 10:58
  • 1
    I would help a lot if you provided a [mcve] – Djizeus Apr 07 '16 at 11:13
  • @Djizeus done! please see if it helped. – João Rebelo Apr 07 '16 at 11:25
  • 1
    It's extraordinarily difficult to read your JSFiddle. Can you please write a new version that uses the *bare minimum* HTML/CSS to reproduce the issue you're having? – Chuck Le Butt Apr 07 '16 at 11:32

1 Answers1

1

Thanks for updating the code, it was better, but still far from a minimum verifiable example. A minimum example is when you strip as much as you can from the code while still reproducing the problem.

In your case, if you had done the exercise, you would probably have come to something like this:

<div id="admin-container">
      <span class="admin-menu1">Iniciar Sessão</span>
      <span>Registo</span>
      <span>A Minha conta</span>
      <span>Meu carrinho</span>
</div>

.admin-menu1 {
  padding-right: 1.5%;
}

#admin-container {
  clear: right;
  float: right;
}

And you would immediately have seen the interest of doing this, AND solved the problem. Because from here, it is easy to notice that the problem is coming from using a relative padding. Switch to for example padding-right: 2px, and the display is now as you expected it.

As a general rule, padding and margin does not apply to inline elements such as span. To apply padding or margin you should use display: block or display: inline-block

Djizeus
  • 4,161
  • 1
  • 24
  • 42
  • Awesome! :) Now I understand what you mean by MV, and thank you for helping me out on this one. Taking out the padding solved the issues indeed. But now if I need to add some spacing between those elements, you say I should use a `div`with `display: inline-block` is that it? Edit: I wanted to keep the % padding if possible... – João Rebelo Apr 07 '16 at 12:28
  • From what I have read having the parent (#admin-container) have a `box-sizing: border-box` should have taken that into account (borders and padding)... Do you know why that didn't do the trick? – João Rebelo Apr 07 '16 at 12:30
  • 1
    @JoãoRebelo Unfortunately no, sorry. Even I when I debugged your code I tried to play with different solutions, but couldn't figure out what to do to to keep a % value... – Djizeus Apr 07 '16 at 15:07