I'm trying to make a grid layout that's responsive with a 15px coloured border, it works ok but it when there's multiple grids, it doubles up the border i.e 30px where it joins.
https://jsfiddle.net/exm8xsgx/
html,
body {
height: 100%;
width: 100%;
}
.container {
height: 100%;
width: 100%;
}
.one {
height: 50vh;
width: 25%;
border-style: solid;
border-color: red;
border-width: 15px;
float: left;
}
<div class="container">
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
</div>
This is another method I've tried. When browser width is restricted, the grids start to stack up and the border doubles up again, it should always be 15px whether they are next to each other or stacked.
https://jsfiddle.net/7bxtt82r/24/
html,
body {
height: 100%;
width: 100%;
}
.container {
height: 100%;
width: 100%;
}
.one:first-child {
height: 50vh;
width: 20%;
border-style: solid;
border-color: red;
border-left-width: 15px;
border-top-width: 15px;
border-bottom-width: 15px;
border-right-width: 15px;
float: left;
}
.one:not(:first-child) {
height: 50vh;
width: 20%;
border-style: solid;
border-color: red;
border-left-width: 15px;
border-top-width: 15px;
border-bottom-width: 15px;
border-right-width: 15px;
float: left;
margin-left: -15px;
}
<div class="container">
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
</div>
I also don't know how many grids there will be so they will just continue to stack up.
You can use CSS table, and set border-spacing to 15px, example:
body {
margin: 0;
}
.row {
display: table;
table-layout: fixed;
border-collapse: separate;
border-spacing: 15px;
width: 100%;
background: red;
}
.one {
display: table-cell;
background: white;
}
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
EDIT
If you need the items to wrap for different viewport width, you can use flexbox + box-shadow + media queries.
body {
margin: 0;
}
.row {
display: flex;
flex-wrap: wrap;
padding: 15px;
}
.one {
flex-basis: 25%;
box-shadow: 0 0 0 15px red;
background: white;
}
#media (max-width: 992px) {
.one {
flex-basis: 50%;
margin-bottom: 15px;
}
}
#media (max-width: 768px) {
.one {
flex-basis: 100%;
}
}
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
Your external borders can stay on 15px but the border which touches another one has to be devided by 2 if you want a clean look with a same border around.
I used a default border of 10px and 20px for those not touching each other. (You can do the opposite: default 20px and 10px for those touching)
I dealt with this while building my simon game here
.squareCircled {
height: 250px;
width: 250px;
border: 10px solid gray;
}
.green {
background-color:#01B600; /*green*/
border-top-left-radius: 100%;
border-top:20px solid gray;
border-left:20px solid gray;
cursor: pointer;
}
Related
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>
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>
How could I go about constructing something like this with pure CSS?
This is how far I've gotten so far: Fiddle
I'm struggling with how to get that rounded corner there, even if I continue to add additional spans.
CODE:
body {
background: #000;
}
.container {
position: relative;
width: 300px;
height: 150px;
margin: 10% auto;
}
.top-right {
position: absolute;
top: -10px;
right: 0;
width: 50px;
height: 1px;
background: white;
border-radius: 5px;
}
.box {
width: 100%;
height: 100%;
background: red;
border-radius: 15px;
display: flex;
align-items: center;
justify-content: center;
}
h3 {
color: white;
}
<div class="container">
<span class="top-right"></span>
<div class="box">
<h3>Content</h3>
</div>
</div>
you can achieve that by using pseudo elements ::before/::after in .box using the properties border and border-radius
body {
background: #000;
}
.container {
width: 300px;
height: 150px;
margin: 3% auto 0 /* changed for demo */
}
h3 {
color: white;
}
.box {
width: 100%;
height: 100%;
background: red;
border-radius: 15px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.box::before,
.box::after {
content: "";
position: absolute;
border: solid white;
width: 50px;
height: 50px;
}
.box::before {
top: -15px;
left: -15px;
border-radius: 15px 0; /* top-left */
border-width: 5px 0 0 5px;
}
.box::after {
bottom: -15px;
right: -15px;
border-radius: 0 0 15px; /* bottom-right */
border-width: 0 5px 5px 0;
}
<div class="container">
<div class="box">
<h3>Content</h3>
</div>
</div>
Using pseudo-elements would be the ideal solution.
This answer is just an alternative. Although not semantically elegant, it's crudely effective.
Create a container with four divs.
The first div will be the white border.
The last div will be your red box.
The two divs in the middle will be used to conceal areas of the white border.
The HTML is quite simple:
<div class="container">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4">
<h3>Content</h3>
</div>
</div>
With absolute positioning, .box2 (green) and .box3 (blue) can be moved to cover the border.
The order of the boxes in the source doesn't really matter. But with the HTML above there is no need for the z-index property.
Now, the only thing left is to change the background color of boxes 2 and 3 to black.
Full code:
body {
margin: 0;
height: 100vh;
background-color: black;
display: flex;
}
.container {
position: relative;
width: 300px;
height: 150px;
margin: auto;
}
.box {
position: absolute;
width: 300px;
height: 150px;
border-radius: 15px;
}
.box1 {
border: 5px solid white;
width: 320px;
height: 170px;
top: -14px;
left: -15px;
}
.box2 {
background-color: black;
top: -30px;
left: 30px;
}
.box3 {
background-color: black;
top: 30px;
left: -30px;
}
.box4 {
background-color: red;
border-radius: 15px;
display: flex;
align-items: center;
justify-content: center;
}
<div class="container">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4">
<h3>Content</h3>
</div>
</div>
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;
}