This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
CSS: Width in percentage and Borders
(5 answers)
Two inline-block elements, each 50% wide, do not fit side by side in a single row
(9 answers)
Closed 1 year ago.
I'm trying to keep these two divs side by side. I've set 50% width for them but still they are under eachother. What's wrong?
div{
border: 1px solid;
display: inline-block;
width: 50%;
}
<form>
<div>
two
</div>
<div>
one
</div>
</form>
I don't want to use flex display. Noted that, it would be ok if I set 49% for the width property, but doesn't look standard to me. Also as you know, that 1px border has nothing to do with the issue.
How can I keep them next to each other?
Thanks in advance
There are two issues here:
You need to set a box-sizing so that the width of the element includes the border
You need to remove the newline between the divs since it takes an extra space
div{
border: 1px solid;
display: inline-block;
box-sizing: border-box;
width: 50%;
}
<form>
<div>
two
</div><!-- you need to remove space here --><div>
one
</div>
</form>
Related
This question already has answers here:
Image inside div has extra space below the image
(10 answers)
Closed 2 years ago.
I feel like such a newb asking this, but I can't figure out why any div container seems to always be taller than the content that it contains. See https://codepen.io/grayayer/pen/XWjBJWZ for working example.
.container {
background-color:red;
height: fit-content;
width: fit-content;
padding: 0;
}
<div class="container">
<img src="https://via.placeholder.com/350" height="350" width="350">
</div>
It's like there's always 5px of padding on the bottom, even though I've specifically said not to.
Images are rendered inline by default, so that space at the bottom is accommodating text decenders. You can set display: block; on the image to remove the space.
More information in this answer.
This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
CSS vertical alignment of inline/inline-block elements
(4 answers)
Closed 3 years ago.
I have 3 divs placed side by side. When I'm trying to put a header (or any element) inside of the middle div (or any div), that div floats way down. Why does it do that?
CSS I used for divs:
div {
display: inline-block;
background-color: lightgray;
height: 600px;
width: 300px;
}
Add *{box-sizing:border-box} to your CSS. It defines how the width and height of an element are calculated.
This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Closed 4 years ago.
Learning CSS in earnest, and a bit confused.
I have a table in a div.
.bigdiv {
background-color: red;
padding: 10px;
}
table {
background-color: aquamarine;
margin-left: 300px;
margin-bottom: 100px;
padding: 10px;
}
<div class="bigdiv">
<table>
<tr>
<td>foo</td>
</tr>
</table>
</div>
which works as I expect, with a big 110px red swath below the aquamarine box.
But when I take the padding out of the div, the margin-bottom overflows the div, and the visual appearance is an aquamarine box at the edge of a red div.
I'd like to understand the rules behind this behavior. Is this something specific about divs, or does the container generally have to have a nonzero padding in order for the content's margin to appear in the container?
Margins collapse which means when you have an elemeht with a bottom margin and another one with a top margin below, it will only display the biggest one.
This is true for parent/child margins, too. Only the biggest margin is displayed and that outside the parent.
There are 2 css workarounds:
overflow:auto
padding:1px
Both css rules can be added to the parent to solve the problem.
For further examples and more explanation you can find something e.g. here:
https://css-tricks.com/what-you-should-know-about-collapsing-margins/
The keyword you need to search for is "margin collapsing"
This question already has answers here:
Fill the remaining height or width in a flex container
(2 answers)
Closed 5 years ago.
I would like to lay out two divs horizontally
parent div width=500px
- left div: shall grow horizontally to fit to contents (not more or less)
- right div: shall grow horizontally and take up all the space to the right edge
How can I achieve this?
Using flex and flex-grow
div {
display: flex;
width: 500px;
}
.grow {
flex-grow: 1;
background: #eee;
}
<div>
<span>content</span>
<span class="grow">grow</span>
</div>
This question already has answers here:
CSS Margin Collapsing
(2 answers)
CSS Text Bottom Margin Ignored
(3 answers)
Closed 7 years ago.
I am using two divs, one on top with no content or height and the other below it with some content or just height. I wish to put some space between them and instead of using margin-bottom alone on the first div or margin-top alone on the second div I am evenly sharing the space between both, i.e. half the space on the first div's margin-bottom and half the sapce on the second div's margin-top. For some reason only one of the two seem to apply and the other is ignored.
Check out this example
http://jsfiddle.net/hkgq22x8/
body {
margin:0px;
}
.element1 {
margin-bottom:10px;
}
.element2 {
margin-top:10px;
border:1px solid #000;
height:30px;
}
<div class="parent">
<div class="element1"></div>
<div class="element2"></div>
</div>
try removing either margin-top on .element2 or margin-bottom on .element1, the space remains the same, but remove both and the space disappears.