I'm doing a simple exercise with margin and padding, it should look something like this:
I tried to do it by setting the padding of the outer div to a fix value and setting the margin of the inner div to 0. But my result looks like this:
Inspector in google shows a margin to the right of the inner div, I have no idea where it comes from.
Here are the html and css codes
#box1,
#box2,
#box3,
#box4 {
width: 200px;
height: 200px;
border-width: 4px;
border-style: solid;
margin: 8px;
/* Aufgabe 2 */
display: inline-block;
/* Aufgabe 3 */
padding: 50px;
}
#box1 {
border-color: red;
}
#box2 {
border-color: green;
}
#box3 {
border-color: violet;
}
#box4 {
border-color: yellow;
}
#inbox1,
#inbox2,
#inbox3,
#inbox4 {
width: 100px;
height: 100px;
/* Aufgabe 3 */
margin: 0px;
}
#inbox1 {
background-color: royalblue;
}
#inbox2 {
background-color: pink;
}
#inbox3 {
background-color: black;
}
#inbox4 {
background-color: turquoise;
}
<div id="box1">
<div id="inbox1"></div>
</div>
<div id="box2">
<div id="inbox2"></div>
</div>
<div id="box3">
<div id="inbox3"></div>
</div>
<div id="box4">
<div id="inbox4"></div>
</div>
Can someone explain where that margin comes from and how I can get rid of it?
It's not margin, it's just the size of your elements. Your boxes have an explicit width of 200px while the inboxes have an explicit width of 100px. So the extra space is due to that difference.
You should also use classes to share styles between elements:
.box {
border-width: 4px;
border-style: solid;
margin: 8px;
display: inline-block;
padding: 50px;
}
#box1 {
border-color: red;
}
#box2 {
border-color: green;
}
#box3 {
border-color: violet;
}
#box4 {
border-color: yellow;
}
.inbox {
width: 100px;
height: 100px;
margin: 0px;
}
#inbox1 {
background-color: royalblue;
}
#inbox2 {
background-color: pink;
}
#inbox3 {
background-color: black;
}
#inbox4 {
background-color: turquoise;
}
<div id="box1" class="box">
<div id="inbox1" class="inbox"></div>
</div>
<div id="box2" class="box">
<div id="inbox2" class="inbox"></div>
</div>
<div id="box3" class="box">
<div id="inbox3" class="inbox"></div>
</div>
<div id="box4" class="box">
<div id="inbox4" class="inbox"></div>
</div>
I believe your issue resulted from providing additional width and height, and padding in order to create a gap around the inner box - you should only use one of these methods!
The following code reduces the size of the parent to 100 x 100, and sets the size of the child to 100% of its parent. Then, the padding alone creates the gap:
.box {
width: 100px;
height: 100px;
border-width: 4px;
border-style: solid;
margin: 8px;
display: inline-block;
padding: 50px;
}
.box1 { border-color: red; }
.box2 { border-color: green; }
.box3 { border-color: violet; }
.box4 { border-color: yellow; }
.inbox {
width: 100%; height: 100%;
}
.inbox1 { background-color: royalblue; }
.inbox2 { background-color: pink; }
.inbox3 { background-color: black; }
.inbox4 { background-color: turquoise; }
<div class="box box1">
<div class="inbox inbox1"></div>
</div>
<div class="box box2">
<div class="inbox inbox2"></div>
</div>
<div class="box box3">
<div class="inbox inbox3"></div>
</div>
<div class="box box4">
<div class="inbox inbox4"></div>
</div>
You can center horizontally and vertically adding flexbox (modern solution) to each box container, if you want to support older browser see this.
As other answers suggested add classes to your css for better readability and more.
In addition be aware of the dimensions of the boxes.
Here is the solution:
#box1,
#box2,
#box3,
#box4 {
width: 200px;
height: 200px;
border-width: 4px;
border-style: solid;
margin: 8px;
/* Aufgabe 3 */
padding: 50px;
/* ADD THIS */
display: inline-flex;
justify-content: center;
align-items: center;
/* END ADD THIS */
}
#box1 {
border-color: red;
}
#box2 {
border-color: green;
}
#box3 {
border-color: violet;
}
#box4 {
border-color: yellow;
}
#inbox1,
#inbox2,
#inbox3,
#inbox4 {
width: 100px;
height: 100px;
margin: 0;
}
#inbox1 {
background-color: royalblue;
}
#inbox2 {
background-color: pink;
}
#inbox3 {
background-color: black;
}
#inbox4 {
background-color: turquoise;
}
<div id="box1">
<div id="inbox1"></div>
</div>
<div id="box2">
<div id="inbox2"></div>
</div>
<div id="box3">
<div id="inbox3"></div>
</div>
<div id="box4">
<div id="inbox4"></div>
</div>
Hope it helps :)
Related
I am wondering if there is any possibility to make previous block overlap the next one with CSS.
.container {
padding: 70px;
width: 500px;
margin: auto;
display: flex;
}
.block {
margin-left: -30px;
width: 200px;
height: 100px;
border: 1px dotted green;
transform: skewY(20deg)
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.black {
background-color: black;
}
<div class="container">
<div class="block red">Text...</div>
<div class="block green">Text...</div>
<div class="block blue">Text...</div>
<div class="block black">Text...</div>
</div>
I could write for each block z-index with CSS, but what if the number of blocks will be for example one hundred?
Codepen:
https://codepen.io/pen/?template=zYoJVdp
Consider a 3d rotation:
.container {
padding: 70px;
width: 500px;
margin: auto;
display: flex;
transform-style: preserve-3d; /* important for the trick */
}
.block {
margin-left: -30px;
width: 200px;
height: 100px;
border: 1px dotted green;
transform: rotateY(-1deg) skewY(20deg) /* a tiny rotation here */
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.black {
background-color: black;
}
<div class="container">
<div class="block red">Text...</div>
<div class="block green">Text...</div>
<div class="block blue">Text...</div>
<div class="block black">Text...</div>
</div>
More detail here : Why can't an element with a z-index value cover its child?
You would probably need to use JS or jQuery.
For example, with jQuery you could use a really simple function like this which increase the z-index for each block by one:
$(".block").each(function(i) {
i++
$(this).css("z-index", i);
});
.container{
padding: 70px;
width: 500px;
margin: auto;
display: flex;
}
.block{
margin-left: -30px;
width: 200px;
height: 100px;
border: 1px dotted green;
transform: skewY(20deg)
}
.red{
background-color: red;
}
.green{
background-color: green;
}
.blue{
background-color: blue;
}
.black{
background-color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="block red">Text...</div>
<div class="block green">Text...</div>
<div class="block blue">Text...</div>
<div class="block black">Text...</div>
</div>
I typed this code and i want boxes like in a row (horizontal) , but it appears like in a column(vertical).
I just want these colour boxes horizontal with spaces between them. (like top layer)
Ex- main box(grey) , and 7 boxes in the top ( with margins & paddings)
Like this : 4 color Boxes in a bigger grey color box.
I used "div" tag , but it only work for first one only
How could i align those boxes and have spaces between them ?
Thanks in advance.
.header_box {
width: 100%;
background: #ccc;
display: -webkit-box;
}
.box {
width: 150px;
height: 40px;
margin: 20px;
}
.box1 {
background: white;
}
.box2 {
background: #ccc;
}
.box3 {
background: #ccc;
}
.box4 {
background: #ccc;
}
.box5 {
background: #ccc;
}
.box6 {
background: #ccc;
}
.box7 {
background: #ccc;
}
.middle_box {
width: 100%;
background: white;
display -webkit-box;
padding: 20px;
}
.box0 {
width: 150px;
height: 150px;
margin: 20px;
padding: 10px;
}
.box11 {
background: blue;
}
.box12 {
background: yellow;
}
.box13 {
background: red;
}
.box14 {
background: green;
}
.circle1 {
height: 100px;
width: 100px;
background-color: yellow;
border-radius: 50%;
}
.circle2 {
height: 100px;
width: 100px;
background-color: yellow;
border-radius: 50%;
border: 2px solid red;
}
.circle3 {
width: 100px;
height: 50px;
/* as the half of the width */
background-color: gold;
border-top-left-radius: 110px;
/* 100px of height + 10px of border */
border-top-right-radius: 110px;
/* 100px of height + 10px of border */
border: 4px solid gray;
border-bottom: 0;
}
.img-circle {
width: 100px;
height: 100px;
border-radius: 50%;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<section>
<div class="header_box">
<div class="box box1" align="center">
logo
</div>
<div class="box box6" align="center">
</div>
<div class="box box7" align="center">
</div>
<div class="box box2" align="center">
TEXT 01
</div>
<div class="box box3" align="center">
TEXT 02
</div>
<div class="box box4" align="center">
TEXT 03
</div>
<div class="box box5" align="center">
TEXT 04
</div>
</div>
<div>
<img src="C:\Users\Dimuth De Zoysa\Pictures\sample3.png" width="100%">
</div>
<div class="middle_box">
<div class="box0 box11">
</div>
<div class="box02 box12">
</div>
<div class="box0 box13">
</div>
<div class="box0 box14">
</div>
</div>
<div class="circle1">
</div>
<div class="circle2">
</div>
<div class="circle3">
</div>
<div>
<img class="img-circle" src="C:\Users\Dimuth De Zoysa\Downloads\sunsetmypht.jpg">
</div>
</section>
</body>
</html>
Your example doesn't work because you forgot a colon in the display directive.
You have:
display -webkit-box;
Use:
display: -webkit-box;
That being said, look into flex and flex-direction (I usually use this article).
To get the circles on a horizontal row you need to wrap them in a div which has the same display rule as the boxes (for example by assigning the middle_box class to the div).
I want to give remaining width to .inner div. At the Same time it's siblings (a & span tags) can be of dynamic width.
Any idea?
Code below & at https://jsfiddle.net/pge8rqw0/
.top {} .inner {
border-bottom: 1px solid black;
background-color: green;
width: auto;
float: left;
width: 50px;
}
a {
float: left;
}
span {
float: right;
background-color: red;
}
<div class="top">
Visit W3Schools.com!
<div class="inner"></div>
<span>Html is good</span>
</div>
Thanks
Solution using display: flex.
.top {
display: flex;
}
.inner {
border-bottom: 1px solid yellow;
background-color: green;
min-width: 50px;
flex: 1;
}
a {
background-color: #f5f5f5;
}
span {
background-color: red;
}
<div class="top">
Visit W3Schools.com!
<div class="inner"></div>
<span>Html is good</span>
</div>
If you can change the order of elements in your code then you can achieve it without flex as follows:
.top {overflow: hidden;}
a {
float: left;
}
span {
float: right;
background-color: red;
}
.inner {
border-bottom: 1px solid black;
background-color: green;
overflow: hidden;
}
<div class="top">
Visit W3Schools.com!
<span>Html is good</span>
<div class="inner">Inner Content</div>
</div>
I'm trying to put 3 divs(with different widths respectively : 10%,70% & 20%) in the same row but the middle one always go full width of the page.
Here is my code:
#left-bar {
width: 10%;
background-color: #FF0000;
}
#middle-bar {
width: 70%;
background-color: #6600FF;
}
#right-bar {
width: 20%;
background-color: #99FF99;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
By default div is a block level element that's why they aren't in the same row.
You have a few options to fix this:
option with CSS flexbox:
.row {
display: flex;
width: 100%
}
.row>div {
/*demo purposes */
height: 30px;
}
#left-bar {
flex: 0 10%;
background-color: #F00;
}
#middle-bar {
flex: 1;
background-color: #60F;
}
#right-bar {
flex: 0 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
(old options)
option with display:inline-block
.row {
/*fix inline-block gap*/
font-size: 0;
}
.row>div {
display: inline-block;
/*demo purposes */
height: 30px;
}
#left-bar {
width: 10%;
background-color: #F00;
}
#middle-bar {
width: 70%;
background-color: #60F;
}
#right-bar {
width: 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
option with display:table-[cell]
.row {
display: table;
width: 100%
}
.row>div {
display: table-cell;
/*demo purposes */
height: 30px;
}
#left-bar {
width: 10%;
background-color: #F00;
}
#middle-bar {
width: 70%;
background-color: #60F;
}
#right-bar {
width: 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
The table-cell option actually doesn't work in some internet explorer versions. But the same result can be achieved with the property float:
#left-bar{
width:10%;
background-color: #FF0000;
}
#middle-bar{
width:70%;
background-color: #6600FF;
}
#right-bar{
width:20%;
background-color: #99FF99;
}
.row > div {float:left;}
<div class="row">
<div id="left-bar">a</div>
<div id="middle-bar">b</div>
<div id="right-bar">c</div>
</div>
#left-bar{
width:10%;
background-color: #FF0000;
float:left;
}
#middle-bar{
width:70%;
background-color: #6600FF;
float:left;
}
#right-bar{
width:20%;
background-color: #99FF99;
float:left;
}
If that doesn't work, please provide more html and css because the problem will be somewhere else. Also, verify that you have heights set for your divs.
I am trying to make multiple divs, specifically five and center them all. I have used the display:inline-block to get them to be side by side but then when I use margin: 0 auto, the display:inline-block seems to get negated and then it's a vertical strip going down the page.
Below is my code:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
I tried looking at the other relevant posts on SO but they don't do it with as many divs or they use static positioning which I don't want to use.
Can someone point me in the right direction?
This happens cause the width of the container is 50px. One quick solution is to set width of container to 100%:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 100%;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
You can align to center using text-align center to container:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 100%;
text-align: center;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
To achieve both and vertical and horizontal align you can use position: absolute to the container top: 50% left: 50% and margin-top: -150px; /* Half the height */ margin-left: -135px; /* Half the width */:
div {
width: 50px;
height: 300px;
border-radius: 0;
display:inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 270px;
position: absolute;
top: 50%;
left:50%;
margin-top: -150px; /* Half the height */
margin-left: -135px; /* Half the width */
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
You can set text-align: center on .container. Updated you code:
.container {
width: 100%;
text-align: center;
}
.container > div{
width: 50px;
height: 300px;
border-radius: 0;
display:inline-block;
}
http://jsfiddle.net/jermund/wzdLrs0m/