I am using CSS Grids. I have a requirement to have a vertical separator between two DIVs. However, the vertical separator needs to be smaller than the height of the actual DIVs, so I don't believe I can use the DIV's border.
.content-container {
display: grid;
width: 100%;
text-align: center;
}
.content {
display: grid;
grid-template-columns: 750px 21px 550px;
margin: auto;
}
.block {
background-color: #fff;
padding-top: 10px;
padding-bottom: 10px;
background-color: #eee;
height: 100px;
}
.divider {
content: '';
margin-top: 20px;
margin-bottom: 20px;
margin-left: 10px;
margin-right: 10px;
border-left: 1px solid black;
}
<div class="content-container">
<div class="content">
<div class="block">test</div>
<div class="divider"></div>
<div class="block">test</div>
</div>
</div>
This works and I'm able to use the margin-top and margin-bottom to control the divider's height.
Ideally, I'd prefer this divider to be a CSS pseudo element but I can't get this working in any way. Is this possible to achieve the desired result or must I stick with an actual element in the HTML code.
Remove the divider element and make content:after your divider by adding grid-column: 2 and grid-row: 1 to this pseudo element (forces it into the middle column).
See demo below:
.content-container {
display: grid;
width: 100%;
text-align: center;
}
.content {
display: grid;
grid-template-columns: 750px 21px 550px;
margin: auto;
}
.block {
background-color: #fff;
padding-top: 10px;
padding-bottom: 10px;
background-color: #eee;
height: 100px;
}
.content:after { /* Now a pseudo element */
content: '';
margin-top: 20px;
margin-bottom: 20px;
margin-left: 10px;
margin-right: 10px;
border-left: 1px solid black;
grid-column: 2; /* ADDED */
grid-row: 1; /* ADDED */
}
<div class="content-container">
<div class="content">
<div class="block">test</div>
<div class="block">test</div>
</div>
</div>
If you want a divider after each block then you can have a grid-gap between each block and put an absolutely positioned pseudo element in that space - see demo below:
.content-container {
display: grid;
width: 100%;
text-align: center;
}
.content {
display: grid;
grid-template-columns: repeat(3, 150px); /* CHANGED */
margin: auto;
grid-gap: 20px; /* ADDED */
}
.block {
background-color: #fff;
padding-top: 10px;
padding-bottom: 10px;
background-color: #eee;
height: 100px;
position: relative; /* ADDED */
}
.block:after { /* ADDED */
content: '';
position: absolute;
border-left: 1px solid black;
right: -10px; /* adjust this */
height: 80%; /* adjust this */
}
.block:last-child:after { /* ADDED */
display: none; /* Hide the divider for the last block */
}
<div class="content-container">
<div class="content">
<div class="block">test</div>
<div class="block">test</div>
<div class="block">test</div>
</div>
</div>
one way to do it
.content-container{
display: grid;
width: 100%;
text-align: center;
}
.content{
display: grid;
grid-template-columns: 75px 55px;
margin:auto;
grid-gap: 20px;
}
.block{
background-color: #fff;
padding-top: 10px;
padding-bottom: 10px;
background-color: #eee;
height: 100px;
position: relative;
}
.block:not(:last-child):after {
position: absolute;
content: '';
left: calc(100% + 9px) ;
color:red;
width: 2px;
height: 80%;
top: 10%;
background-color: black;
}
.divider{
content: '';
margin-top: 20px;
margin-bottom: 20px;
margin-left: 10px;
margin-right: 10px;
border-left:1px solid black;
}
<div class="content-container">
<div class="content">
<div class="block">
test
</div>
<div class="block">
test
</div>
</div>
</div>
The following is an amelioration of #kukkuz's answer.
#kukkuz's answer is not responsive because his right property's value's expressed in px instead of vw. If the final user decreases or increases his browser's width, your vertical separators will move at a different rate than the grid layout's columns are resized. They won't be visually synchronized. Results: your separators would overflow your columns' content.
Solution: use vw instead of px for the right property of #kukkuz.
Related
I need your help. I've this three boxes including a text below:
#boxes {
display: flex;
margin-right: -20px;
}
.box {
width: 33.33333%;
margin-right: 20px;
height: 300px;
border-radius: 3px;
border: 1px solid;
}
#footer {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 25px;
}
#headline {
font-size: 16px;
font-weight: 700;
padding-bottom: 12px;
}
<div id="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div id="footer">
<span id="headline">Headline</span>
<span>Small text</span>
</div>
Now I need to somehow connect the boxes together with the text below. For that I want to draw a line down and to the middle text (vertical center) from the left and right box. The problem is that the lines should start at the middle of the left and right boxes and be also centered when the width of the boxes changes - for example when I resize the browser (responsive).
I've first tried using ::before and ::after but I was only able to draw the horizontal lines with total imperfection...
Does anyone has an idea how I can do this?
Go ahead and create a DIV underneath, margin it up, and add borders! You can add a background to the text to section it out.
Big thanks to XLIME for the right hint. This is the answer for my problem:
#boxes {
display: flex;
margin-right: -20px;
}
.box {
width: 33.33333%;
margin-right: 20px;
height: 300px;
border-radius: 3px;
border: 1px solid;
}
#footer {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 25px;
position: relative;
}
#footer-inner {
display: flex;
flex-direction: column;
align-items: center;
background: #ffffff;
z-index: 1;
padding: 0 25px;
}
#headline {
font-size: 16px;
font-weight: 700;
padding-bottom: 12px;
}
#connection-border {
position: absolute;
width: 68%;
height: 98%;
border: 3px solid;
border-top: none;
top: -25px;
}
<div id="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div id="footer">
<div id="footer-inner">
<span id="headline">Headline</span>
<span>Small text</span>
</div>
<div id="connection-border"></div>
</div>
While the "just make another <div> with borders" answer will probably make your life easier, here's an example built with pseudoelements.
In short, it hangs both the vertical and the horizontal lines off of the first and last .box elements, and makes use of both the :before and :after pseudoelements to create those lines.
The last bit of the illusion (lines stopping before the footer text) is accomplished by setting a page-color background on the footer spans, and giving them enough padding to create the appearance of a gap.
In this snippet, I've made the vertical lines green and the horizontal lines blue so it's easier to trace what's happening.
/* Original styling ===================== */
#boxes {
display: flex;
margin-right: -20px;
}
.box {
width: 33.33333%;
margin-right: 20px;
height: 300px;
border-radius: 3px;
border: 1px solid;
}
#footer {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 25px;
}
#headline {
font-size: 16px;
font-weight: 700;
padding-bottom: 12px;
}
/* Additional styling ===================== */
:root {
--footer-gap-height: 48px;
}
.box {
position: relative;
}
/* vertical lines */
.box:first-child:before,
.box:last-child:before {
border-left: 1px solid #0f0; /* green */
content: '';
display: block;
height: var(--footer-gap-height);
left: 50%;
position: absolute;
top: 100%;
transform: translateX(-50%);
width: 0;
}
.box:first-child:after,
.box:last-child:after {
border-bottom: 1px solid #00f; /* blue */
content: '';
position: absolute;
top: calc(100% + var(--footer-gap-height));
width: 100%;
}
.box:first-child:after {
left: 50%;
right: auto;
}
.box:last-child:after {
left: auto;
right: 50%;
}
#footer {
position: relative;
}
#footer span {
background-color: #fff;
display: block;
padding-left: 1em;
padding-right: 1em;
}
<div id="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div id="footer">
<span id="headline">Headline</span>
<span>Small text</span>
</div>
I'm looking to align three divs side by side without any flexbox and grid.
This is the style that I'm looking for: Image
This is what I'm currently getting: Image
.container {
width: 600px;
border: 1px solid black;
}
.box-1 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
.box-2 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
.box-3 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
<div class="container">
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
</div>
Note: Just asking about alignment, not the border, background etc
Edit: The parent container has width 600px. It cannot be changed. And the children have 180px width and 100px height, and margin of 10px.
You can resolve it using display: flex; layout on .container class.
The default direction of flex layout is row (flex-direction: row).
.container {
width: 600px;
border: 1px solid black;
display: flex;
}
.box-1 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
.box-2 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
.box-3 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
<div class="container">
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
</div>
Hey increase the width of the container as follow:
.container {
width: 1000px;
border: 1px solid black;
}
.box-1 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
.box-2 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
.box-3 {
width: 180px;
height: 100px;
background: grey;
margin: 10px;
display: inline-block;
}
<div class="container">
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
</div>
or use display : flex in your container both will help....
If you dont want to use Flexbox, You can simply try increasing the width such that all 3 divs are aligned in a single line
like Aahad said...
I need a card list layout for this I use flex. in large device everything is ok but when device becomes small and two cards can't be next to each other and go to the next line, my content it's not center
in other words, I need to center my content in all device size and when two cards come together should be space-between and center
.container-card {
background-color: grey;
direction: rtl;
}
.container-holder {
background-color: gold;
width: calc(100% - 28px);
max-width: 1004px;
margin: auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.request-box {
width: 481px;
height: 417px;
border-radius: 15px;
border: solid 1px #adadad;
background-color: #ffffff;
margin-top: 15px;
margin-bottom: 15px;
margin: 15px 2px;
}
<div class="container-card">
<div class="container-holder">
<div class="request-box"></div>
<div class="request-box"></div>
<div class="request-box"></div>
</div>
</div>
Please check the example above in full-page and change the responsive size
Hope this is helpful for you all in center align and i`m not use any mediaquery same code work on small screens .
.container-card {
background-color: grey;
}
.container-holder {
background-color: #ffd700;
width: calc(100% - 28px);
max-width: 1004px;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
margin: 0 auto;
text-align: center;
}
.request-box {
width: 481px;
height: 417px;
border-radius: 15px;
border: solid 1px #adadad;
background-color: #ffffff;
margin-top: 15px;
margin-bottom: 15px;
margin: 15px 2px;
}
<div class="container-card">
<div class="container-holder">
<div class="request-box"></div>
<div class="request-box"></div>
<div class="request-box"></div>
</div>
</div>
Use CSS grid for this:
.container-card {
background-color: grey;
direction: rtl;
}
.container-holder {
background-color: gold;
width: calc(100% - 28px);
max-width: 1004px;
margin: auto;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(481px, 1fr));
place-items: center;
grid-column-gap: 30px;
}
.request-box {
width: 481px;
height: 417px;
border-radius: 15px;
border: solid 1px #adadad;
background-color: #ffffff;
margin: 15px 0;
}
<div class="container-card">
<div class="container-holder">
<div class="request-box"></div>
<div class="request-box"></div>
<div class="request-box"></div>
</div>
</div>
to set the cards netx to each other in small devices you can use this media query:
#media(max-width: 400px){
.container-holder {
justify-content: space-around;
}
.request-box {
width: 45%;
}
}
test it . it works nicely!
<div class="container">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>
.container{
display: flex;
flex-direction: column;
justify-content: space-between;
height: 140;
}
.a{
margin-top: 10px;
height: 17px;
}
.b{
//??
height: 40px;
}
.c{
margin-bottom: 10px;
height: 18px;
}
I have three <div>s a, b and c in the container <div>. I want an element a to be positioned 10px to the top of the container, b to be positioned 50px to the top of the container and c to be positioned 10px to the bottom of the container.
How should I set b's style in order to achieve this? I want to avoid using absolute position if possible since when I tried to use it, somehow the width of b changed. I want to know if there is a way to set a customized distance between children of a flexbox.
Its easier that you adjust the distances here with margins - so you can remove justify-content: space-between.
Add margin-top: 23px to b and margin-top: auto to c. Check the below demo (you can inspect the red lines to verify that the distances are correct):
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
/*justify-content: space-between;*/
height: 140px;
border: 1px solid;
position: relative;
}
.a {
margin-top: 10px;
height: 17px;
}
.b {
height: 40px;
margin-top: 23px; /* ADDED */
}
.c {
margin-bottom: 10px;
height: 18px;
margin-top: auto; /* ADDED */
}
/* STYLING */
.a,.b,.c {
border: 1px solid blue;
background: cadetblue;
}
.a:before,.b:before,.c:before {
content: '';
top: 0;
width: 2px;
position: absolute;
left: 100px;
background: red;
}
.a:before {
height: 10px;
}
.b:before {
height: 50px;
left: 200px;
}
.c:before {
left: 100px;
top: unset;
bottom: 0;
height: 10px;
}
<div class="container">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>
HTML:
<div id="wrap">
<div id="block1"></div>
<div id="block2"></div>
</div>
CSS:
div#wrap{
margin-top: 3em;
border: solid 1px black;
text-align: center;
}
div#wrap *{
display: inline-block;
width: 12.5em;
margin-top: 1em;
height: 8em;
}
div#wrap *:not(:last-child){
margin-right: 8em;
}
#block1{
background: orange;
}
div#wrap #block2{
background: magenta;
}
These 2 blocks are supposed to be centered in responsive design mode. When the screen is wide enough to have 2 blocks in a row, the code works. But when I narrow the screen down, the top block is shifted to the left because of the margin:
fiddle
Is it possible to fix this without media queries?
Edit
I tried flex-box:
div#wrap{
margin-top: 3em;
border: solid 1px black;
display: flex;
flex-flow: row wrap;
justify-content: center;
}
fiddle2
A solution is to use flex and justify-content:space-around and remove margin:
div#wrap {
margin-top: 3em;
border: solid 1px black;
justify-content:space-around;
display: flex;
flex-wrap: wrap;
}
div#wrap * {
display: inline-block;
width: 12.5em;
margin-top: 1em;
height: 8em;
}
#block1 {
background: orange;
}
#block2 {
background: magenta;
}
<div id="wrap">
<div id="block1"></div>
<div id="block2"></div>
</div>
If you use a container with negative margin, you don't need to vary the margin for the endpoints of the rows at different breakpoints and you can just go with inline-block. I set font-size to zero in the container so I can calculate my widths using percents without worrying about white space.
div#wrap {
margin-top: 3em;
border: solid 1px black;
padding: 20px;
text-align: center;
}
.block {
display: inline-block;
width: 12.5em;
margin: 20px;
height: 8em;
font-size: 16px;
}
.block-container {
margin: -20px;
font-size: 0;
}
#block1 {
background: orange;
}
#block2 {
background: magenta;
}
<div id="wrap">
<div class="block-container">
<div class="block" id="block1"></div>
<div class="block" id="block2"></div>
</div>
</div>