I understand that if I float an element, following elements will eventually disappear behind that element if they do not have a float set themselves or at least clear the float. Just like it is happening with »box three« in this example. But why is the content of box three jumping out of the div? Isn't the Number 3 or any potential content of the box supposed to be inside »box three«?
http://jsfiddle.net/7vw4Leg5/
<div class="box one">1</div>
<div class="box two">2</div>
<div class="box three">3</div>
.box {
text-align: center;
font-size: 30px;
color: red;
margin: 5px;
width: 200px;
height: 100px;
background: grey;
}
.two {
border: 2px solid red;
float: left;
opacity: 0.66;
}
.three {
opacity: 0.33
}
*Edit:
Here another example to explain the issue I don't understand.
Why isn't the number two just inside the blue box? #Terry: Okay, if I reduce the first box' width, the content jumps up a line and enters the div. But why isn't it there in the first place? Isn't there enough space available as the box is completely empty?
http://jsfiddle.net/utsc84pq/
<div class="box one">1</div>
<div class="box two">2</div>
.box {
font-size: 40px;
margin: 5px;
width: 300px;
height: 150px;
}
.one{
float: left;
border: 5px solid rgba(255, 154, 188, 0.9);
background-color: rgba(255, 165, 0, 0.25);
}
.two {
position: relative;
top: 170px;
border: 5px solid rgba(35, 154, 255, 0.5);
background-color: rgba(100, 165, 255, 0.25);
}
Try this.
.box {
text-align: center;
font-size: 30px;
display:block;
color: red;
margin: 5px;
width: 200px;
height: 100px;
line-height:100px;
background: grey;
}
.one{
border:1px solid green;
display:block;
}
.two {
border: 1px solid red;
opacity: 0.66;
}
.three {
border: 1px solid yellow;
opacity: 0.66;
}
<div class="box one">1</div>
<div class="box two">2</div>
<div class="box three">3</div>
Yes, the content should be inside box 3, but since you have set all the boxes with the same widths, this means that the content in box 3 cannot be pushed to the side but only to the bottom—you can see how float interacts with the content of box 3 if you reduce it's width:
.box {
text-align: center;
font-size: 30px;
color: red;
margin: 5px;
width: 200px;
height: 100px;
background: grey;
}
.two {
border: 2px solid red;
float: left;
opacity: 0.66;
width: 25px;
}
.three {
opacity: 0.3;
}
<div class="box one">1</div>
<div class="box two">2</div>
<div class="box three">3 random content here</div>
Related
I have boxes with different dimensions - some fixed and some dynamic. I need a wrapper-div that applies a border without changing the HTML code or the child's CSS (only change .one's CSS)
How can I do this so both boxes in the below snippet keep their respective size but the parent is not larger than them?
.one {
border: solid red 1px;
}
#two {
height: 60px;
width: 320px;
background-color: firebrick;
}
#three {
height: 60px;
width: 100%;
background-color: lightcoral;
}
<div class="one">
<div id="two"></div>
</div>
<br>
<div class="one">
<div id="three"></div>
</div>
There are many ways to 'shrink' .two.
My approach would be using a static width, with an inline-block on .one:
.one {
border: solid red 1px;
display: inline-block;
}
.two {
height: 60px;
width: 320px;
max-width: 320px;
background-color: firebrick;
}
<div class="one">
<div class="two"></div>
</div>
Another option, using display: inline-flex; on the .one, with an static with on .two:
.one {
border: solid red 1px;
display: inline-flex;
}
.two {
height: 60px;
width: 320px;
max-width: 320px;
background-color: firebrick;
display: inline-table;
}
<div class="one">
<div class="two"></div>
</div>
Using flex, you can let .two 'grow' to the size of .one, the only downside to this, is that you'll need the 320px on .one, but in the end, using flex offer more options
.one {
border: solid red 1px;
display: inline-flex;
height: 60px;
width: 320px;
}
.two {
flex-grow: 1;
background-color: firebrick;
}
div {
display: flex;
}
<div class="one">
<div class="two"></div>
</div>
As #Tacoshy mentioned in the comments, another way is using width: min-content on the .one. Should be noted that IE doesn't yet support that property.
.one {
border: solid red 1px;
display: inline-flex;
width: min-content;
}
.two {
height: 60px;
min-width: 320px;
background-color: firebrick;
}
<div class="one">
<div class="two"></div>
</div>
If it's only about a visual style use a filter like below. The more filter you add the more you get close to a solid edge
.one {
filter:
drop-shadow(0 0 0.5px blue)
drop-shadow(0 0 0.5px blue)
drop-shadow(0 0 0.5px blue)
drop-shadow(0 0 0.5px blue)
drop-shadow(0 0 0.5px blue)
drop-shadow(0 0 0.5px blue)
drop-shadow(0 0 0.5px blue)
drop-shadow(0 0 0.5px blue);
}
#two {
height: 60px;
width: 320px;
background-color: firebrick;
}
#three {
height: 60px;
width: 100%;
background-color: lightcoral;
}
<div class="one">
<div id="two"></div>
</div>
<br>
<div class="one">
<div id="three"></div>
</div>
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 :)
I have two squares inside of a container that are overlapping using transform: translate and I want to remove the padding to the right of the blue square so that the container perfectly fits the width of the children. Please see image for clarification.
Picture of issue
I’ve tried sizing the container to 90px, which should be the width of the children (50px + 50px - 10px), but when I do this the blue box drops to the next row. Why does it do this? I also tried applying padding-right: 0 but nothing changed.
.container {
width: 110px;
border: 1px solid black;
}
.box {
height: 50px;
width: 50px;
display: inline-block;
}
.one {
background: red;
}
.two {
background: blue;
transform: translate(-10px, 15%);
}
<div class="container">
<div class="box one"></div>
<div class="box two"></div>
</div>
I would like there to be no left or right padding.
Use position: absolute with top and left/right, don't forget position: relative on the container:
.container{
width: 90px;
border: 1px solid black;
padding-right: 0;
position: relative;
}
.box{
height: 50px;
width: 50px;
display: inline-block;
}
.one{
background: red;
}
.two{
background: blue;
position: absolute;
left: 40px;
top: 15%;
}
<div class="container">
<div class="box one"></div>
<div class="box two"></div>
</div>
Just add a negative margin-right to second square
.container {
width: 94px;
height: 57px;
border: 1px solid black;
}
.box {
height: 50px;
width: 50px;
display: inline-block;
}
.one {
background: red;
}
.two {
background: blue;
margin-right: -10px;
transform: translate(-10px, 15%);
}
<div class="container">
<div class="box one"></div>
<div class="box two"></div>
</div>
If I stack a number of boxes (divs) together using float: left and set border: 1px solid black, there will be a 2px border between adjacent boxes. (Between boxes vertically, and also horizontally when boxes move to the next line.)
//html
<div class=boxes>
<div class="box">1</div>
<div class="box">1</div>
<div class="box">1</div>
<div class="box">1</div>
<div class="box">1</div>
<div class="box">1</div>
<div class="box">1</div>
<div class="box">1</div>
<div class=last></div>
</div>
//css
.boxes {
display: inline-block;
}
.last {
clear: both;
}
.box {
width: 200px;
height: 50px;
border: 1px solid gray;
line-height: 50px;
text-align: center;
float: left;
position: relative;
}
How can I achieve a similar layout with only a single pixel border between adjacent blocks?
I thought to add a "top left" border to the parent container, and then only set "right bottom" border for individual boxes. This partly works, but if the list of boxes flows over to the next line, then there will be a visible line on the top.
.boxes {
border-top: 1px solid gray;
border-left: 1px solid gray;
display: inline-block;
overflow: hidden;
}
.last {
clear: both;
}
.box {
width: 200px;
height: 50px;
border-bottom: 1px solid gray;
border-right: 1px solid gray;
line-height: 50px;
text-align: center;
float: left;
position: relative;
}
.boxes {
height: auto;
overflow: hidden;
padding: 1px 0 0 0;
border-left: 1px solid gray;
}
.box {
width: 200px;
height: 50px;
border: 1px solid gray;
border-left: 0;
line-height: 50px;
text-align: center;
float: left;
position: relative;
margin-top: -1px;
}
<div class="boxes">
<div class="box box1">1</div>
<div class="box box2">1</div>
<div class="box box3">1</div>
<div class="box box4">1</div>
<div class="box box1">1</div>
<div class="box box2">1</div>
<div class="box box3">1</div>
<div class="box box4">1</div>
</div>
Basically the outer container just set the left border and the inner boxes have a border on the other sides (not the left one). The trick is to shift all those blocks with a negative margin-top: -1px and place a padding-top: 1px on the parent container (so you can still see the top border of the first row).
I've also removed the empty element you inserted only for clearing purpose: height: auto and overflow: hidden on the parent container is enough (or look for the clearfix class which doesn't affect the overflow).
If you try to resize the viewport the borders are never overlapping.
You can use a trick with border-box and float the elements to be more responsive
.boxes {
float: left;
}
.box {
height: 50px;
width: 200px;
float: left;
box-shadow:
1px 0 0 0 gray,
0 1px 0 0 gray,
1px 1px 0 0 gray,
1px 0 0 0 gray inset,
0 1px 0 0 gray inset;
}
Example
All you need is:
.boxes {
margin-top: 1px;
margin-left: 1px;
}
.box {
width: 200px;
height: 50px;
border: 1px solid gray;
line-height: 50px;
text-align: center;
float: left;
margin-top: -1px;
margin-left: -1px;
}
<div class=boxes>
<div class="box box1">1</div>
<div class="box box2">1</div>
<div class="box box3">1</div>
<div class="box box4">1</div>
<div class="box box1">1</div>
<div class="box box2">1</div>
<div class="box box3">1</div>
<div class="box box4">1</div>
</div>
There is no need to add any other borders to anything but to .box directly and margin will do the magic
EDIT:
If you are making your design pixel-perfect then you should make your .boxes like:
.boxes {
margin-top: 1px;
margin-left: 1px;
margin-bottom: -1px;
margin-right: -1px;
}
and it will cancel out any effect created by margins
Here is how I'll do it, using a CSS variable, because it's cool:
(See comments in my code)
/* Added CSS variable, because it's nice to modify only here if needed */
:root{
--border: 1px solid gray;
}
.boxes {
display: inline-block;
width: auto;
margin: auto;
/* Added the below to make .last useless */
height: auto;
overflow: hidden;
/* Added for borders */
border-top: var(--border);
border-left: var(--border);
}
.box {
width: 200px;
display: inline-block;
height: 50px;
line-height: 50px;
text-align: center;
float: left;
position: relative;
/* Added for borders */
border-right: var(--border);
border-bottom: var(--border);
}
<div class=boxes>
<div class="box box1">1</div>
<div class="box box2">1</div>
<div class="box box3">1</div>
<div class="box box4">1</div>
<!-- <div class="last"></div> No need for that -->
</div>
Hope it helps.
If a user is signed up to my site, in their login area I have 3 divs as follows:
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
These divs all have a width of 32% and sit inline with each other.
#psts-cancel-link {
background: white;
border-left: 3px solid #ccc;
padding: 1em;
width: 32%;
min-height: 270px;
float: left;
}
.psts-receipt-link {
background: white;
border-left: 3px solid #ccc;
min-height: 270px;
float: left;
width: 32%;
margin: 0 10px;
padding: 20px;
}
#psts-signup-another {
background: white;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
width: 32%;
min-height: 270px;
float: left;
}
When a user is not signed up, only one of the divs displays:
<div id="psts-signup-another"></div>
Is it possible to change the styling of this so that it's width is 100% when div1 and div2 aren't displayed?
So far I have tried this, but with no success:
#psts-cancel-link ~ .psts-receipt-link ~ #psts_existing_info #psts-signup-another {
width:100%;
}
Table Layout Implementation
Use a table layout. Specify display: table on the parent and display: table-cell on the child elements.
#psts-cancel-link {
background: tomato;
border-left: 3px solid #ccc;
padding: 1em;
min-height: 270px;
display: table-cell;
word-wrap: break-word;
}
.psts-receipt-link {
background: lightblue;
border-left: 3px solid #ccc;
min-height: 270px;
margin: 0 10px;
padding: 20px;
display: table-cell;
word-wrap: break-word;
}
#psts-signup-another {
background: tomato;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
min-height: 270px;
display: table-cell;
word-wrap: break-word;
}
.container {
display: table;
width: 100%;
table-layout: fixed;
}
Logged in
<div class="container">
<div id="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logged out
<div class="container">
<div id="psts-signup-another"></div>
</div>
Flexbox Layout Implementation
You can also use flexbox which expands and shrinks the child items according to the parent container.
#psts-cancel-link {
background: tomato;
border-left: 3px solid #ccc;
padding: 1em;
min-height: 270px;
flex: 1;
}
.psts-receipt-link {
background: lightblue;
border-left: 3px solid #ccc;
min-height: 270px;
margin: 0 10px;
padding: 20px;
flex: 1;
}
#psts-signup-another {
background: tomato;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
min-height: 270px;
flex: 1;
}
.container {
display: flex;
}
Logged in
<div class="container">
<div id="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logged out
<div class="container">
<div id="psts-signup-another"></div>
</div>
You could simply use :first-child if it's indeed the only child in the second case.
#psts-signup-another:first-child {}
You can use the adjacent selector. Have a look at the following snippet:
#psts-signup-another {padding: 5px; background: #f99;}
div + div + #psts-signup-another {padding: 5px; background: #99f;}
<h2>Div when three divs are present</h2>
<div class="theDivs">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
<h2>Div when three divs are not present</h2>
<div class="theDivs">
<div id="psts-signup-another"></div>
</div>
i think you should use another container div with a new class when user logout.
Logged:
<div class="container">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logout:
<div class="container logout">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
CSS:
.container.logout > div {
display:none;
}
.container.logout > .psts-signup-another {
display:block;
}