This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 2 years ago.
I wanted to have 3 divs side by side in a HTML document and I managed to achieve it where it looks something like this:
But whenever I tried adding objects such as text or any other objects, the div is always shifting down:
Could anyone help me out on this?
Edit
Thanks for the response but i forgot that i wanted a logo at the top left, then followed by the 3 divs below the logo, but adding "flex" property to the container leads to this:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: #fff;
}
.container {
width: 100%;
height: 100%;
display: flex;
border: 1px solid black;
}
.input {
width: 450px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-top: 5px;
margin-left: 5px;
display: inline-block;
}
.output {
width: 650px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-left: 20px;
display: inline-block;
}
.output_2 {
width: 300px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-left: 20px;
display: inline-block;
}
<!--
this is the outermost shell
-->
<div class="container">
<!-- to add a logo at the top left -->
<div class = "sun_lg">
<img src = "images/sun.png" height = "50">
</div>
<div class="input">
<p>Hi</p>
</div>
<div class="output">
</div>
<div class="output_2">
</div>
</div>
Just add display:flex to your container.
To learn more about flexbox read the documentation.
You can also use grid
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: #fff;
}
.container {
width: 100%;
height: 100%;
border: 1px solid black;
display: flex;
flex-direction:column;
/* new */
}
.wrapper{
width: 100%;
height:auto;
display: flex;
}
.input {
width: 450px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-top: 5px;
margin-left: 5px;
}
.output {
width: 650px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-left: 20px;
display: inline-block;
}
.output_2 {
width: 300px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-left: 20px;
display: inline-block;
}
/* update for logo */
.sun_lg {
border: 1px solid #000;
flex: 1 1 100%;
}
<div class="container">
<!-- to add a logo at the top left -->
<div class="sun_lg">
<img src="https://via.placeholder.com/50x50" height="50">
</div>
<div class="wrapper">
<div class="input">
<p>Hi</p>
</div>
<div class="output">
</div>
<div class="output_2">
</div>
</div>
</div>
Define vertical-align to set the exact behavior of divs against texts baseline. I will use vertical-align:top in all child divs:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: #fff;
}
.container {
width: 100%;
height: 100%;
border: 1px solid black;
}
.input {
width: 450px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-top: 5px;
margin-left: 5px;
display: inline-block;
vertical-align:top;
}
.output {
width: 650px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-left: 20px;
display: inline-block;
vertical-align:top;
}
.output_2 {
width: 300px;
height: 680px;
border-radius: 5px;
border: 3px solid black;
margin-left: 20px;
display: inline-block;
vertical-align:top;
}
<!--
this is the outermost shell
-->
<div class="container">
<div class="input">
<p>Hi</p>
</div>
<div class="output">
</div>
<div class="output_2">
</div>
</div>
Related
This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 2 years ago.
I drew this in microsoft paint and wanted to make this in html/css
The numbers labeled are the box numbers
This is what I've done to try to achieve this
html file
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="utf-8">
<title>test</title>
<link rel="stylesheet" type="text/css" href="box.css">
</head>
<body>
<div id="box1"></div>
<div id="box2"></div>
<div id="box3">
<div id="box4"></div>
<div id="box5"></div>
<div id="box6"></div>
<div id="box7"></div>
<div id="box8"></div>
</div>
</body>
</html>
css file
html, body {
margin: 0px;
height: 100%;
width: 100%;
}
#box1 {
border: solid black 3px;
height: 10%;
}
#box2 {
border: solid black 3px;
height: 3%;
}
#box3 {
border: solid black 3px;
height: 84%;
}
#box4 {
border: solid black 1px;
width: 50%;
height: 95%;
float: left;
margin: 5px;
}
#box5 {
border: solid black 1px;
width: 23%;
height: 25%;
float:left;
margin-left: 10px;
margin-top: 6px;
}
#box6 {
border: solid black 1px;
width: 23%;
height: 30%;
float:left;
margin-top: 10px;
margin-left: 10px;
}
#box7 {
border: solid black 1px;
width: 23%;
height: 30%;
float:left;
margin-top: 10px;
margin-left: 10px;
}
How it looks
I couldn't get box8 to show up on the right side I tried float right it messes it up. Also the boxes inside box3 are all inconsistent. If I full screen the boxes go right side. I used percentages for responsiveness but it didn't work. Anyone know how to do this ?
This can be achieved with flexbox - but note that you will need to use wrapper divs and apply different flex-directions to each in order to make the grid layout work.
body, html {
height: 100%;
width: 100%;
overflow: hidden;
}
.box-wrapper {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
#box1 {
padding:10px;
height:30px;
line-height:30px;
border: solid 1px red
}
#box2 {
height: 15px;
padding: 8px;
border: solid 1px blue
}
#box3 {
padding: 10px;
flex-grow:1;
display: flex;
flex-direction: row;
border: solid 1px green
}
#box4 {
flex-grow:2;
border: solid 1px orange
}
.middle-column {
flex-grow:1;
display: flex;
flex-direction: column;
}
.middle-column div{
flex-grow:1;
margin: 0 8px;
border: solid 1px #6e6e6e;
}
.middle-column div + div {
margin-top: 8px
}
#box8 {
flex-grow:1;
border: solid 1px black
}
<div class="box-wrapper">
<div id="box1">1</div>
<div id="box2">2</div>
<div id="box3">
<div id="box4">4</div>
<div class="middle-column">
<div id="box5">5</div>
<div id="box6">6</div>
<div id="box7">7</div>
</div>
<div id="box8">8</div>
</div>
</div
My css and html looks like below. mRow is the main div and within that is my mRowLeft and mRowRight.
However instead of left and right I see them appear top left and bottom right.
div.mRow {
padding: 2px 25px 2px 0;
margin-top: 4px;
margin-bottom: 3px;
float: left;
text-align:left;
width: 350px;
/*border:1px solid green;*/
}
.mRowLeft {
padding: 2px 25px 2px 0;
margin-top: 4px;
margin-bottom: 3px;
float: left;
text-align:left;
width: 48%;
/*border:1px solid green;*/
}
.mRowRight {
padding: 2px 25px 2px 0;
margin-top: 4px;
margin-bottom: 3px;
float: right;
text-align:left;
width: 48%;
/*border:1px solid green;*/
}
///....
<div class="mRow">
<div class="mRowLeft"></div> --label
<div class="mRowLeft"></div> --10rows
<div class="mRowRight"></div> --label
<div class="mRowRight"></div> --10rows
</div>
...//
You should be putting your label and content under the same left/right div.
<div class="mRow">
<div class="mRowLeft">
<div>--label</div>
<div>10rows</div>
</div>
<div class="mRowRight">
<div>label</div>
<div>10rows</div>
</div>
</div>
Then you can either use inline-blocks:
.mRow {
white-space: nowrap;
width: 350px;
}
.mRowLeft,
.mRowRight {
display: inline-block;
white-space: normal;
width: 50%;
}
or use flexbox:
.mRow {
display: flex;
flex-direction: row;
width: 350px;
}
.mRowLeft,
.mRowRight {
width: 50%;
}
.mRow
{
display:flex;
justify-content:space-around;
}
<div class="mRow">
<div class="mRowLeft">
dfsf
<div class="mRowLeft">sdfvs</div>
</div>
<div class="mRowRight">
sdfs
<div class="mRowRight">sdfsd</div>
</div>
</div>
I have this simple html and css
body{
background-color: black;
padding:0;
margin: 0;
box-sizing: border-box;
}
#ctr{
height: 500px;
width: 200px;
margin: 0 auto;
background-color: red;
}
#ctr > .box{
background-color: white;
height: 200px;
width: 200px;
border: 1px solid black;
margin-top:10px;
}
<div id = "ctr">
<div class = "box">
<div class="something1"></div>
<div class="something2"></div>
</div>
</div>
I'm trying to add margin-top to the white box (box class) inside the red div(ctr id), but the whole red div is getting the margin and not just the white div.
here's a jsfiddle of the example.
https://jsfiddle.net/6tvrwxhg/
Add overflow:auto (or hidden) to the parent (#ctr)
body {
background-color: black;
padding:0;
margin: 0;
box-sizing: border-box;
}
#ctr {
height: 500px;
width: 200px;
margin: 0 auto;
background-color: red;
overflow: auto;
}
#ctr > .box{
background-color: white;
height: 200px;
width: 200px;
border: 1px solid black;
margin-top:10px;
}
<div id="ctr">
<div class="box">
<div class="something1"></div>
<div class="something2"></div>
</div>
</div>
more info in a good answer
https://www.w3.org/TR/CSS2/box.html#collapsing-margins
That is Parent/First child margin collapsing, and that happens if there is no border, padding, inline content or clearance to separate parent and child. So you can add border-top on parent
body {
background-color: black;
padding: 0;
margin: 0;
box-sizing: border-box;
}
#ctr {
height: 500px;
width: 200px;
margin: 0 auto;
background-color: red;
border-top: 1px solid transparent;
}
#ctr > .box {
background-color: white;
height: 200px;
width: 200px;
border: 1px solid black;
margin-top: 10px;
}
<div id="ctr">
<div class="box">
<div class="something1"></div>
<div class="something2"></div>
</div>
</div>
Or you can add padding to parent element or use overflow: hidden
body {
background-color: black;
padding: 0;
margin: 0;
box-sizing: border-box;
}
#ctr {
height: 500px;
width: 200px;
margin: 0 auto;
background-color: red;
overflow: hidden
}
#ctr > .box {
background-color: white;
height: 200px;
width: 200px;
padding-top: 1px;
margin-top: 10px;
}
<div id="ctr">
<div class="box">
<div class="something1"></div>
<div class="something2"></div>
</div>
</div>
You can read more about this at Mastering margin collapsing
This might be an easy one. Below is a structure which I want to create:
But I always end up with either this:
Or this:
Here is my code:
HTML
.newdiv2,
.newdiv3,
.newdiv4,
.newdiv5 {
width: 25px;
height: 25px;
margin-bottom: 5px;
border: 3px solid black;
}
.newdiv6 {
width: 150;
height: 150;
border: 3px solid black;
}
.newdiv {
height: 250px;
width: 450px;
float: left;
border: 3px solid black;
}
.divwrapper {
float: left;
border: 3px solid blue;
}
.mainwrapper {
display: block;
}
<div class="mainwrapper">
<div class="newdiv"></div>
<div class="divwrapper">
<div class="newdiv2"></div>
<div class="newdiv3"></div>
<div class="newdiv4"></div>
<div class="newdiv5"></div>
</div>
</div>
<div class="newdiv6"></div>
This looks like the second image above (in my Chrome browser).
You can also reset the block formating context of the main container, so it minds inside and outside floatting elements.
here the simpliest is to add : overflow:hidden; since no size are involved
.newdiv2,
.newdiv3,
.newdiv4,
.newdiv5 {
width: 25px;
height: 25px;
margin-bottom: 5px;
border: 3px solid black;
}
.newdiv6 {
width: 150px;
height: 150px;
border: 3px solid black;
}
.newdiv {
height: 250px;
width: 450px;
float: left;
border: 3px solid black;
}
.divwrapper {
float: left;
border: 3px solid blue;
}
.mainwrapper {
display: block;
/* reset bfc */
overflow:hidden;
}
<div class="mainwrapper">
<div class="newdiv"></div>
<div class="divwrapper">
<div class="newdiv2"></div>
<div class="newdiv3"></div>
<div class="newdiv4"></div>
<div class="newdiv5"></div>
</div>
</div>
<div class="newdiv6"></div>
You need to clear the <div>. Use clear: both; on .newdiv6:
.newdiv2,
.newdiv3,
.newdiv4,
.newdiv5 {
width: 25px;
height: 25px;
margin-bottom: 5px;
border: 3px solid black;
}
.newdiv6 {
width: 150px;
height: 150px;
border: 3px solid black;
clear: both;
}
.newdiv {
height: 250px;
width: 450px;
float: left;
border: 3px solid black;
}
.divwrapper {
float: left;
border: 3px solid blue;
}
.mainwrapper {
display: block;
}
<div class="mainwrapper">
<div class="newdiv"></div>
<div class="divwrapper">
<div class="newdiv2"></div>
<div class="newdiv3"></div>
<div class="newdiv4"></div>
<div class="newdiv5"></div>
</div>
</div>
<div class="newdiv6"></div>
Also use px after the numbers, else it won't work.
Preview
I just started learning CSS, now stuck at this part. what makes the brand class to move down when information class is inline-block-ed? Doesn't information comes after brand so it shouldn't affect the brand class?
body {
margin: 0;
padding: 0;
}
.header {
height: 34px;
background-color: #ACDACD;
}
.brand {
border: 2px solid red;
height: 34px;
display: inline-block;
}
.information {
border: 2px solid blue;
height: 34px;
display: inline-block;
}
<div class="header">
<div class="brand">AKKJKJKJKJKJFKJDKFJDKJF
</div>
<div class="information">
</div>
</div>
<div class="mainbody">
</div>
By default the vertical-alignment of text is baseline. The difference in the height is what makes it. If you have this CSS rule:
vertical-align: top;
Or whatever it is perfect, it looks alright. See below:
body {
margin: 0;
padding: 0;
}
.header {
height: 34px;
background-color: #ACDACD;
}
.brand {
border: 2px solid red;
height: 34px;
display: inline-block;
}
.information {
border: 2px solid blue;
height: 34px;
display: inline-block;
vertical-align: top;
}
<div class="header">
<div class="brand">AKKJKJKJKJKJFKJDKFJDKJF</div>
<div class="information"></div>
</div>
<div class="mainbody">
</div>
And now the difference or the white line is because of the border, which can be made by using box-sizing: border-box.
* {
box-sizing: border-box
}
body {
margin: 0;
padding: 0;
}
.header {
height: 34px;
background-color: #ACDACD;
}
.brand {
border: 2px solid red;
height: 34px;
display: inline-block;
}
.information {
border: 2px solid blue;
height: 34px;
display: inline-block;
vertical-align: top;
}
<div class="header">
<div class="brand">AKKJKJKJKJKJFKJDKFJDKJF</div>
<div class="information"></div>
</div>
<div class="mainbody">
</div>