CSS Two columns layout + gradient separation - css

I am trying to show a gradient vertical separation between two columns, and a bottom horizontal gradient separation between the divs.
The following code (scss) doesn't show the bottom border:
.col-container {
.column-box {
border-width: 2px;
border-style: solid;
border-image: linear-gradient(to right, white, #efedf4, white) 1 stretch;
min-height: 200px;
}
& > div:nth-child(odd) {
border-width: 2px;
border-style: solid;
border-image: linear-gradient(to bottom, white, #efedf4) 0 90%;
}}
While the following code only shows the bottom border:
.col-container {
.column-box {
border-width: 2px;
border-style: solid;
border-image: linear-gradient(to right, white, #efedf4, white) 1 stretch;
min-height: 200px;
}}
Here is the code, as you will see the left div losses its bottom border:
https://jsfiddle.net/0nsvzqxg/
Any idea how to show only the vertical separator in between the 2 divs and the bottom border on all of them?
Thanks

You have 2 things to solve.
draw the border on two sides but from the same gradient
select the right boxes via the nth-child() selector
Since the width of the border image is quiet small, the gradient can be drawn like a diagonal. from a top corner to the opposite bottom corner and repeated once . It could be linear-gradient(to bottom left, white, #efedf4, white, #efedf4, white).
while you need to draw one or 2 borders, you need to update the slice values for each borders. See https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-slice
When four positions are specified, they create slices measured from the top, right, bottom, and left in that order (clockwise)
div {
border-width: 2px;
border-style: solid;
border-image: linear-gradient(to bottom left, red, blue, red, blue, red)1 / 2px 2px 2px 2px stretch;
/* what would be gradient image */
background: linear-gradient(to bottom/* or top */ left/* or right*/ , blue, red, blue, red, blue);
box-shadow: inset 0 0 0 2px white;/* break border from background */
/* demo purpose */
height: 50vh;
width: 50vw;
padding: 1em;
color: white;
}
html,
div {
display: flex;
align-items: center;
justify-content: center;
}
p {
margin: 1em;
padding: 1em;
}
html {
min-height: 100vh;
}
/* end demo */
<div>
<p>To figure out the gradient image needed for the border, you may draw it in the background to tune it</p>
<p> from horizontal / vertical to diagonal, it needs to be repeated at least once.</p>
</div>
Note: shorthand values : border-image:/* source | slice | width | outset | repeat */
About the nth-child() selector, you can use a counter CSS to see better where each child stands .
.col-container {
counter-reset: box
}
.column-box:nth-child(odd) {
color: tomato
}
.column-box::before {
counter-increment: box;
content: 'child N°:'counter(box)' ';
color: green
}
<div class="col-container">
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
Once put together, it can be :
.col-container {
overflow: auto;
}
.col-container > * {
float: left;
width: 50%;
box-sizing: border-box;
}
.col-container .column-box {
border-width: 2px;
border-style: solid;
border-image: linear-gradient(to bottom left, white, #efedf4, white, #efedf4, white)
2/ 0 0 2px 0 stretch;
min-height: 30vh;
}
.col-container > div:nth-child(odd) {
border-width: 2px;
border-style: solid;
border-image: linear-gradient(to bottom left, white, #efedf4, white, #efedf4, white)
2/0 2px 2px 0;
}
<div class="col-container">
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
<div class="column-box">
<div class="cover">
Image
</div>
</div>
</div>
Note: flex + flex-wrap would be a better option than float, each boxes of each rows will be the same height.

Related

How can I make a gradient top border with a radius?

I've been trying to make a gradient top border for a box with a radius. The gradient border should only be at the top of the box, and it needs to have the same radius (5px) as the rest of the box. The end result should look like this:
My current code:
.card {
text-align: center;
margin: 0 1%;
background-color: #252a41;
border-radius: 5px;
border: none;
width: 25%;
padding: 3% 0;
}
.instagram {
border-top: 3px solid rgb(243, 242, 241);
border-radius: 5px;
border-image: linear-gradient(to right, #fdc468, #ffa263, #ff806c, #f9607f, #df4996) 1 / 3px 0px 0px 0px / 0px 0px 0px 0px round;
position: relative;
}
<div class="card instagram">
<div class="row">
<img src="images/icon-instagram.svg" alt="instagram icon" />
<span class="small-text username">#realnathanf</span>
</div>
<h1 class="followers-count">11k</h1>
<p class="small-text followers">FOLLOWERS</p>
<div class="row">
<img src="images/icon-up.svg" alt="icon up" />
<span class="change change-up">1099 Today</span>
</div>
</div>
Apparently the border-image property isn't compatible with border radius, so I have to make a before/after pseudo-element. However I couldn't figure out how to make one for the desired result.
You can do it with only background:
.box {
width:200px;
height:200px;
border-radius:5px;
background:
linear-gradient(to right, #fdc468, #ffa263, #ff806c, #f9607f, #df4996) top/100% 5px no-repeat,
#000;
}
<div class="box">
</div>

Line divs up using % width

I have a page where there is a line of 4 cards, each with width: 25%. Below that I have a line with 3 cards that I want to line up with the first 3 cards of the line above. The cards also have a margin which means that when it divides the card's width with 25% the 3 cards are bigger. Any idea how to fix this?
.section-wrapper {display: flex}
.sessions-card, .earnings-card {flex:0 0 25%; margin: 0 10px; border: 2px solid black; height: 200px}
.session-wrapper:first-child {margin-left: -10px}
.session-wrapper:last-child {margin-right: -10px}
<h1>Sessions</h1>
<div class="section-wrapper">
<div class="sessions-card approved"></div>
<div class="sessions-card completed"></div>
<div class="sessions-card pending"></div>
<div class="sessions-card cancelled"></div>
</div>
<h1>Earnings</h1>
<div class="section-wrapper">
<div class="earnings-card"></div>
<div class="earnings-card"></div>
<div class="earnings-card"></div>
</div>
Instead of the width, you can do it with the flex property and if 20px is the desired margin between them, you'd need to use some calc(), esp. if you want for the first and last one to be touching the parents edges:
.section-wrapper {display: flex; background: lightblue}
/* can also do it with the "justify-content: space-between;" and "margin-right: auto" set on the last ".earnings-card" */
.sessions-card, .earnings-card {
/*width: 25%;*/
flex: 0 0 calc(25% - 15px); /* -15px because of the -20px left & right margin devided by 4 = 5px; 20px - 5px = 15px */
margin: 0 10px;
border: 2px solid black;
height: 200px;
box-sizing: border-box;
}
.sessions-card:first-child,
.earnings-card:first-child {
margin-left: 0;
}
.sessions-card:last-child,
.earnings-card:last-child {
margin-right: 0;
}
<h1>Sessions</h1>
<div class="section-wrapper">
<div class="sessions-card approved"></div>
<div class="sessions-card completed"></div>
<div class="sessions-card pending"></div>
<div class="sessions-card cancelled"></div>
</div>
<h1>Earnings</h1>
<div class="section-wrapper">
<div class="earnings-card"></div>
<div class="earnings-card"></div>
<div class="earnings-card"></div>
</div>

How to change div background on dragover when also contains image

I have a dropzone where user can drop an image, it has some associated CSS that makes the dropzone background go grey as they drag the image, giving a visual cue.
Html
<div>
<figure>
<div id="dropzone">
</div>
</figure>
</div>
CSS
#dropzone {
border: 3px solid black;
min-width:300px;
min-height:100px;
max-width:300px;
max-height:100px;
}
#dropzone.dragover {
background: rgba(0, 0, 0, .5);
}
But I don't want the dropzone to be empty I would like it to have a starting image (which says something like 'Drop Files Here')
<div>
<figure>
<div id="dropzone">
<img src="dropzone.jpg"/>
</div>
</figure>
</div>
and if I add this, then the dropzone doesnt grey out when drag an image over, how do I fix this?
Try setting the background image with CSS property background-image
#dropzone{
background-image: url(path/to/your/image);
/* rest of the css goes here*/
}
#dropzone {
border: 3px solid black;
min-width:300px;
min-height:100px;
max-width:300px;
max-height:100px;
background-image:url(https://images.pexels.com/photos/577775/pexels-photo-577775.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940);
background-repeat: no-repeat;
background-position: center;
background-size: auto;
}
#dropzone.dragover {
background: rgba(0, 0, 0, .5);
}
<div>
<figure>
<div id="dropzone">
</div>
</figure>
</div>
I got two solutions for you:
First I display the image as a background-image and then I filter it in the fist solution to be grayscale (More about filter here) and in the second solution I just overwrite the background to just be gray.
#dropzone1,
#dropzone2 {
border: 3px solid black;
min-width: 300px;
min-height: 100px;
max-width: 300px;
max-height: 100px;
background-image: url(https://picsum.photos/300/300);
background-repeat: no-repeat;
background-position: center;
background-size: auto;
}
#dropzone1:hover {
filter: grayscale(100%);
}
#dropzone2:hover {
background: rgba(0, 0, 0, .5);
}
<div>
<figure>
<div id="dropzone1">
</div>
</figure>
</div>
<div>
<figure>
<div id="dropzone2">
</div>
</figure>
</div>
You can use opacity on the dropzone.
#dropzone.dragover {opacity:0.5}
Because background is only the background-color of the element.

Gradient overlay on element on the right side

I have created a container with elements that is scrollable. I want the element on the right side to have a gradient overlay. How can I create that with CSS?
.container {
display: flex;
flex-wrap: nowrap;
overflow-x: scroll;
width: 1000px;
}
.element {
min-width: 200px;
height: 100px;
background-color: red;
margin-right: 10px;
}
<div class="container">
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
</div>
https://codepen.io/naomilea/pen/RjZGaM
If you are just looking for the last element to have a gradient color, you can code your css like this:
.container {
display: flex;
flex-wrap: nowrap;
overflow-x: scroll;
width: 1000px;
}
.element {
min-width: 200px;
height: 100px;
background-color: grey;
margin-right: 10px;
}
.element:last-child
{
background: -webkit-linear-gradient(to right, grey, white);
background: -o-linear-gradient(to right, grey, white);
background: -moz-linear-gradient(to right, grey, white);
background: linear-gradient(to right, grey, white);
}
<div class="container">
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
</div>
Check updated codepen
Add this to the div element you want to add the gradient style
.element {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left, red , white); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(left, red, white); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(left, red, white); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, red , white); /* Standard syntax */
}

How should I attach a image to border-bottom?

http://bootply.com/89567
I attached a pattern to the border-bottom, since there isn't a specific borde-image selector for that, I've used :after to attach it. However the image doesn't scale with the rest.
Should I maybe create the border pattern in canvas? How could I accomplish that?
Thanks in advance
.redbrd {
height: 400px;
width: auto;
background: #D20000;
}
.redbrd:after {
content: url("http://i.imgur.com/iKXPOF0.png");
position: relative;
top: 400px;
}
<section class="col-md-12">
<div class="row">
<article class="col-md-6">
</article>
<article class="col-md-6">
<div class="col-md-6 col-xs-6">
<div class="redbrd"></div>
</div>
<div class="col-md-6 col-xs-6">
<div class="redbrd"></div>
</div>
</article>
</div>
</section>
Try this:
.redbrd:after {
content: " ";
position: relative;
top: 400px;
display: block;
background: url("http://i.imgur.com/iKXPOF0.png") repeat-x;
height: 30px;
}
You can add border-image property for the div. And disable all the border except bottom border.
eg:
div
{
-webkit-border-image:url(border.png) 30 30 round; /* Safari 5 */
-o-border-image:url(border.png) 30 30 round; /* Opera */
border-image:url(border.png) 30 30 round;
border-top:0px;
border-left:0px;
border-right:0px;
}
Or try this
div
{
border-image-slice: 0 0 27 0; border-image-width: 20px 20px 20px 20px;
border-image- outset: 0px 0px 0px 0px; border-image-repeat: stretch stretch;
border-image-source: url("https://mdn.mozillademos.org/files/6007/border-image-1.png");
}
I think this link will be helpful for you https://developer.mozilla.org/en-US/docs/Web/CSS/Tools/Border-image_generator
.

Resources