~ / archivo / stackoverflow / 44313124
div that are a bit offset
-
So I'm trying to do some styling with CSS, but I've got a problem.
body { margin: 0 auto; } #headLeft { background-color: red; height: 50px; width: 30%; display: inline-block; } #headRight { background-color: blue; height: 50px; width: 70%; margin-left: -4px; display: inline-block; } #logo { margin-left: 10px; border-width: 1px; border-color: red; border-style: solid; }<body> <div id="headLeft"> <div id="logo"> Here's the logo! </div> </div> <div id="headRight"></div> </body>Can someone please tell me why the text "Here's the logo!" makes the whole thing not work? The left of the header is 15px above what it should be.
Strangely, if I remove the text (so "Here's the logo!"), everything is perfect & back to normal!
Screenshot of the result without any text, which is exactly what I want:
Screenshot of the result if I add some text (here: "logo"), where the red rectangle at the left is too much above:
- DrunkDevKek 2017-06-01 16:54 You havn't told us what your problem was, making the answer difficult to provide.
- Gerard 2017-06-01 16:54 Add * { box-sizing: border-box; } in the top of your CSS code.
- Michael 2017-06-01 16:55 I had but someone deleted it. What I want is the red box to be on the same level as the blue box. Simple as that. Thanks
- Daniel Beck 2017-06-01 17:18 I've restored the question text (looks like it was accidentally deleted during a code reformat)
-
just use
display:flexproperty to your body.body { margin: 0 auto; display:flex; } #headLeft { background-color: red; height: 50px; width: 30%; display: inline-block; } #headRight { background-color: blue; height: 50px; width: 70%; margin-left: -4px; display: inline-block; } #logo { margin-left: 10px; border-width: 1px; border-color: red; border-style:solid; display: inline-block; }<body> <div id="headLeft"> <div id="logo"> <p>Here's the logo!</p> </div> </div> <div id="headRight"> </div> </body> -
The
display:inline-blockis what's causing issues and is not really the best way to do this. It causes the element to be laid out differently than it otherwise would have been (inline elements vertically align differently than block elements do) and it causes the CSS Box Model to calculate total sizes differently.Instead, just use
float:leftfor the two headers and then applyclear:both(actually, could just beclear:left) to the next element that comes after the last floated element to clear the floats and go back to regular layout.Also:
The CSS Box Model divides up each element into a "box" that has an area for its content. By default, when you specify a
heightor awidth, you are only specifying the size for this inner-most content area. Outside of that area is the padding, followed by the border and then the margin. When you setwidthfor an element, padding, border and margin are not included. So, if you set thewidthof an element to100%and then added a 1px thick border, your element would actually wind up being100%+2px(1 px for the left and 1 px for the right) wide.In your case, your sizing thus becomes: 30% + 70% + 1px + 1px (Don't forget the borders!), which is more than 100% and can cause problems.
You can alter the way the box model sizes elements so that it includes the padding and borders if you set
box-sizing:border-box. This way the padding and borders will be taken into account when you set awidth./* Have all width and height settings include the padding and borders */ * { box-sizing:border-box; } body { margin: 0 auto; } #headLeft { background-color: red; height: 50px; width: 30%; float:left; } #headRight { background-color: blue; height: 50px; width: 70%; float:left; } #logo { margin-left: 10px; border-width: 1px; border-color: red; border-style: solid; }<body> <div id="headLeft"> <div id="logo"> Here's the logo! </div> </div> <div id="headRight"></div> </body> -
The actual reason for your problem is because the default vertical alignment of inline elements is
baseline(as you can see). To change this, set it to either middle or top:body { margin: 0 auto; } #headLeft { background-color: red; height: 50px; width: 30%; display: inline-block; } #headRight { background-color: blue; height: 50px; width: 70%; margin-left: -4px; display: inline-block; vertical-align: top; } #logo { margin-left: 10px; border-width: 1px; border-color: red; border-style: solid; }<body> <div id="headLeft"> <div id="logo"> Here's the logo! </div> </div> <div id="headRight"></div> </body> -
Axion respuesta 2017-06-01 16:54
Add float: left; to #headleft
https://codepen.io/anon/pen/pwzeBZ
HTML
<div id="headLeft"> <div id="logo"> Here's the logo! </div> </div> <div id="headRight"> </div>CSS
body { margin: 0 auto; } #headLeft { background-color: red; height: 50px; width: 30%; display: inline-block; float: left; } #headRight { background-color: blue; height: 50px; width: 70%; display: inline-block; } #logo { margin-left: 10px; border-width: 1px; border-color: red; border-style:solid; }- Scott Marcus 2017-06-01 17:02 Please don't post external links when there is no reason you can't post the working code here, in a code snippet. Also, answers should explain what the problem was and why the solution solves it.
-
Lal respuesta 2017-06-01 16:55
Replacing
display: inline-block;withfloat:left;would solve the problem.body { margin: 0 auto; } #headLeft { background-color: red; height: 50px; width: 30%; float: left; } #headRight { background-color: blue; height: 50px; width: 70%; margin-left: -4px; float: left; } #logo { margin-left: 10px; border-width: 1px; border-color: red; border-style: solid; }<body> <div id="headLeft"> <div id="logo"> Here's the logo! </div> </div> <div id="headRight"> </div> </body> -
The in-line property isn't applying to the child elements of #headLeft and #headRight.
In that case both elements are aligned but #logo is messing up that alignment.If you delete the #logo from #headLeft you'll notice this statement.
To fix this you could:
- Add float property to #logo
- Add a new div with the same height that #logo in #headRight
Here I just used float.
body { margin: 0 auto; } #headLeft { background-color: red; height: 50px; width: 30%; display: inline-block; } #headRight { background-color: blue; height: 50px; width: 70%; margin-left: -4px; display: inline-block; } #logo { margin-left: 10px; border-width: 1px; border-color: red; border-style: solid; float: left; }<body> <div id="headLeft"> <div id="logo"> Here's the logo! </div> </div> <div id="headRight"></div> </body>