Diagonal shape 100% height CSS - html

Alright, here is the thing. This is what I'm trying to accomplish, which I did so far:
The problem is I use hardcoded pixels right now, but it really needs to be more responsive. So it needs a height of 100% (not 200px like now). And the total width of the diagonal and content containers needs to be 50%, like the image above (so not hardcoded 100px like now). The main problem seems to be the diagonal, cause it almost seems I can only use pixels and not percentages. So if the content block gets more content, it will expand, but the diagonal will not, which is a problem.
It looks like a position absolute could fix it, but then I can't really place the content and diagonal blocks next to each other anymore. Now I gave them two different colors to be clear, but in the live example they need to look like one shape with the same background color.
.shape {
width:400px;
margin:0 auto;
display: block;
height: 200px;
position: relative;
background-image: url('https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSQdX7yx0pVXUlaNF7WkbSJpZp5r0TflV3WdsojKKK1Xon_1hh08l4OL1yd');
}
.diagonal {
height:0;
border-width: 0px 0 200px 100px;
border-style:solid;
border-color: transparent transparent transparent #d71f55 ;
float: left;
}
.content {
height: 100%;
width: 100px;
background-color: #888;
float: left;
color: #fff;
}
<div class="shape">
<div class="content">
Content goes here
Like this
</div>
<div class="diagonal"></div>
</div>
EDIT:
By the way, I already tried using two backgrounds as well, like:
background-color: #f87f73;
background-image: -webkit-linear-gradient( -28deg, #f87f73 0%, #f87f73 60%, #292423 60%, #292423 60%);
background-image: linear-gradient( -28deg, #f87f73 0%, #f87f73 60%, #292423 60%, #292423 60%);
But that really got ugly. Too pixelated.
Edit 2:
Browser which needs to be supported:
OS: windows 8/10 :
** browsers: Chrome 47/48
** Firefox 43/44
** Internet Explorer 11
OS: mac OSX 10.9/10.10
** Chrome 47/48
** Firefox 43/44
** Safari 8/9
OS: android 5/6
** Chrome latest version
OS: iOS 8/9
** Safari latest version

You can use viewport related units for the border as described in Shape with a slanted side (responsive). This will allow you to make the shape 50% width and 100% height of the viewport and responsive:
* {
margin: 0;
padding: 0;
}
.shape {
width: 100%;
margin: 0 auto;
display: block;
height: 100vh;
position: relative;
background-image: url('https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSQdX7yx0pVXUlaNF7WkbSJpZp5r0TflV3WdsojKKK1Xon_1hh08l4OL1yd');
}
.diagonal {
height: 0;
border-width: 0 0 100vh 25vw;
border-style: solid;
border-color: transparent transparent transparent #d71f55;
float: left;
}
.content {
height: 100vh;
width: 25vw;
background-color: #888;
float: left;
color: #fff;
}
<div class="shape">
<div class="content">
Content goes here Like this
</div>
<div class="diagonal"></div>
</div>
Viewport related units (vh and vw) have good browser support. For more info, see canIuse

This is probably how I would approach it. Using a hard 50/50 gradient rather than a border makes it pretty trivial. It seems to render ok in chrome but I haven't checked other browsers. If you want this inside a container remember to set the container to position: relative
.shape {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: auto;
height: auto;
display: block;
background-image: url('https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSQdX7yx0pVXUlaNF7WkbSJpZp5r0TflV3WdsojKKK1Xon_1hh08l4OL1yd');
}
.content {
height: 100%;
width: 25%;
background-color: #888;
color: #fff;
float: left;
}
.diagonal {
height: 100%;
width: 25%;
background: linear-gradient(to bottom right, #888 50%, transparent 50%);
float: left;
border: none;
}
<div class="shape">
<div class="content">
Content goes here Like this
</div>
<div class="diagonal"></div>
</div>

Related

I need to remove a little space between my divided blocks

There is a little space between my two divided blocks.
https://i.imgur.com/l411V0t.png here you can see my problem. I've can’t figure out why the blocks act like this.
body, main {
width: 100%;
margin: 0;
background: black;
}
.container {
display: block;
height: 200px;
margin: auto;
}
.block {
width: 100%;
height: 100%;
background:
linear-gradient(-135deg, transparent 45px, wheat 0) top right,
linear-gradient(45deg, transparent 45px, wheat 0) bottom left;
background-size: 100% 50%;
background-repeat: no-repeat;
}
<main>
<div class="container">
<div class="block">
</div>
</div>
</main>
In Firefox there is no gap to be found, in Edge and IE it shows so it seems to be a sizing issue. Increasing the background size to 51% closes the gap also in Edge.
I'll try to find how the different browsers calculate background sizes.
body, main {
width: 100%;
margin: 0;
background: black;
}
.container {
display: block;
height: 200px;
margin: auto;
}
.block {
width: 100%;
height: 100%;
background:
linear-gradient(-135deg, transparent 45px, wheat 0) top right,
linear-gradient(45deg, transparent 45px, wheat 0) bottom left;
background-size: 100% 51%;
background-repeat: no-repeat;
}
<main>
<div class="container">
<div class="block">
</div>
</div>
</main>
Interesting question, while when the container height is set to even number pixel, the line will be removed. When the container height is odd pixel, the line will be displayed.
The key factor should be background-size: 100% 50%; , so you could use scss round function to skip the rounding problem here.

Background for a page thats 90% white and 10% image that scales

I want to do the following and until now I neither got it to work on my own nor did I find something similar online. I am building a page that will not be scrollable and I want a white background but with a rainbow stripe at the bottom of the page.
I kinda got it to work like this with a div (I tried it as a background for the whole page, but it blurres because css doesn't crop the image, it compresses it):
.rainbow {
position: fixed;
height: 10%;
width: 100%;
bottom: 0px;
background: white url('../img/rainbow.jpg') no-repeat bottom fixed;
background-size: contain;
}
But the problem with this code is, that it does not scale properly: Passing a certain point the image isn't visible all the way, because it gets cut on both sides(left and right) the hight works.
I hope y'all know what I want to do.
Thanks in advance
Instead of using an image, you could achieve that with HTML and CSS.
.rainbow {
position: fixed;
height: 10%;
width: 100%;
bottom: 0px;
}
.color {
position: relative;
float: left;
height: 100%;
width: calc(100% / 6);
}
#blue {
background: blue;
}
#green {
background: green;
}
#yellow {
background: yellow;
}
#orange {
background: orange;
}
#red {
background: red;
}
#purple {
background: purple;
}
<div class="rainbow">
<div id="blue" class="color"></div>
<div id="green" class="color"></div>
<div id="yellow" class="color"></div>
<div id="orange" class="color"></div>
<div id="red" class="color"></div>
<div id="purple" class="color"></div>
</div>
Use gradient as background and no need image or a lot of code:
html {
min-height:100%;
background:
linear-gradient(to right,
blue calc(1*100%/6),
green calc(1*100%/6) calc(2*100%/6),
yellow calc(2*100%/6) calc(3*100%/6),
orange calc(3*100%/6) calc(4*100%/6),
red calc(4*100%/6) calc(5*100%/6),
purple 0)
bottom/100% 10% no-repeat;
}
Or if you want it inside a fixed element:
.box {
position:fixed;
bottom:0;
left:0;
right:0;
height:10%;
background:
linear-gradient(to right,
blue calc(1*100%/6),
green calc(1*100%/6) calc(2*100%/6),
yellow calc(2*100%/6) calc(3*100%/6),
orange calc(3*100%/6) calc(4*100%/6),
red calc(4*100%/6) calc(5*100%/6),
purple 0);
}
<div class="box"></div>
height: 10%; will scale the thickness. If you want the thickness consistent when you resize, just use px instead. I found 75px was about the same size as 10% when in full screen.
For the image, I just increased the width of your image to 2500x905 px This scaled nicely with no blur in both my monitors.
.rainbow {
position: fixed;
height: 10%;
width: 100%;
bottom: 0px;
left: 0px;
background: white url('http://foregotten.net/imgs/pen/rainbow1.jpg') no-repeat bottom fixed;
background-size: contain;
}
https://jsfiddle.net/Foregotten/5yLev3bf/6/

Create a "complex" background in CSS

I would like to know if it is possible to create a background like this in CSS3.
The background should span a header div and the gradient should go from white to black independent of the screen width (always white on the left side and black on the right side).
Reason for not using the image is that it takes longer to load and that I can't get it to resize it's width when making the browser smaller than 1920px (the width of the image).
Have tried linear-gradient but I can't get it to work...
Regards,
Jens
If you also want the black bar at the top you should give dimensions to the background, stop the repeating and also position it where you want (treat it like a normal background image)
div {
background-color: black;
background-image: linear-gradient(to right, white, black);
background-repeat:no-repeat;
background-size:100% 20px; /*full width, 20px height*/
background-position:0 100%; /*gradient at bottom*/
/*just to give size to demo*/
min-height:50px;
}
<div></div>
Here's some CSS for you:
#grad {
background: gray; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left, white , black); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, white, black); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, white, black); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, white , black); /* Standard syntax */
}
Source: http://www.w3schools.com/css/css3_gradients.asp
I know the OP's question was answered. But I'll comment here anyway to deliver some more information to create a really more "complex" background.
First is you really can create multiple backgrounds stack on each other:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Backgrounds_and_Borders/Using_multiple_backgrounds
Second is you can determine position, size, etc,... of a background-image. And here the concise syntax for it: https://www.w3schools.com/cssref/css3_pr_background.asp.
Why background-image? A basic (and important) theory of background in CSS is: A background of an element can have only 1 background-color, and multiple background-images sit on top of it (even if the background-color is declared after background-image, background-color will be still placed below the background-images), and you can resize, reposition those background-images. And an important thing is linear-gradient is count as a background-image, not background-color. The 2 links above do give all detailed information about it.
Here is a quick demo on a "more complex" background from the OP question using only 1 div HTML:
div {
background:
linear-gradient(to right, white, black) 0 100%/100% 20px no-repeat,
linear-gradient(to left, white, black) 0 0/100% 20px no-repeat,
black;
height: 100px;
}
<div></div>
I'm inspired writing this long comment because from a tutorial
https://levelup.gitconnected.com/how-to-implement-netflix-slider-with-react-and-hooks-bdb9b99d1ce4, there's a section from it there're verbose hacks in HTML and CSS to achieve what I'm able to do within just a single line of CSS background, and I think it's cool to share, isn't it?
/* simpler */
.box {
width: 100%;
height: 100px;
background: linear-gradient(to right,black 0%,black 30%,transparent 75%,transparent 100%), green;
}
/* more complex */
.content {
height: 100px;
position: relative;
}
.background {
display: flex;
height: 100%;
}
.left {
background: black;
width: 30%;
position: relative;
}
.left:before {
content: '';
position: absolute;
background-image: linear-gradient(to right,#000,transparent);
top: 0;
bottom: 0;
left: 100%;
width: 275px;
}
.right {
background: green;
width: 70%;
}
.content-container {
color: white;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: 30px
}
<!-- simpler -->
<div class="box">
<div class="content-container">content here...</div>
</div>
<hr>
<!-- more complex -->
<div class="content">
<div class="background">
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="content-container">content here...</div>
</div>

Gradient help to create a slanted div

So I've been at it for a while trying to achieve this one shape with CSS with no good solutions. I need this to be an image because this div may resize and I want it to stay intact. I've also attempted to create an SVG which did not work out very well, I've seen some people work with gradient to make shapes but I'm not able to find any good guide to point me in the right direction. Any help is appreciated :)
Using gradients with angles is not fit for your case because (as already pointed out by King King in comments) as the width the increases, the angle of the gradient (or) the color stop percentages need to be modified to maintain the shape. That is very tricky and so this method can be employed only when the shape has fixed dimensions.
However gradients can still be used with the to [side] [side] syntax because gradients defined using this syntax can adapt to variations in container sizes. In this method no pseudo-elements are used.
$(document).ready(function() {
$('#increase').on('click', function() {
$('.gradient').css('width', '300px').css('height', '500px');
})
})
div {
display: inline-block;
vertical-align: top;
height: 300px;
width: 100px;
margin: 10px;
color: beige;
transition: all 1s;
}
.gradient {
padding: 10px;
background: linear-gradient(to top right, transparent 50%, tomato 50%) no-repeat, linear-gradient(to top right, transparent 0.1%, tomato 0.1%) no-repeat;
background-size: 100% 100px, 100% 100%;
background-position: 0% 100%, 0% -100px;
}
/* Just for demo */
body {
background: -webkit-radial-gradient(50% 50%, circle, aliceblue, steelblue);
background: radial-gradient(circle at 50% 50%, aliceblue, steelblue);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gradient">Some content</div>
<br>
<br>
<button id="increase">Increase Width & Height</button>
Note that it is better to make sure that the text doesn't flow into the slanted section of the shape because wrapping the text around to fit within the shape is not straight-forward.
I have attempted to make that in css as per ur image. http://jsfiddle.net/3zkme/- See if this could help. Thanks.
HTML
<div style="margin:30px">
<div class="trapezoid">
</div>
</div>
CSS
.trapezoid{
top: 150px;
vertical-align: middle;
border-bottom: 120px solid red;
border-left: 200px solid transparent;
border-top-left-radius:0px;
height: 0;
width: 150px;
transform:rotate(270deg);
-ms-transform:rotate(270deg); /* IE 9 */
-webkit-transform:rotate(270deg); /* Opera, Chrome, and Safari */
}
/* ---------- */
.trapezoid {
position:relative;
}
.trapezoid:after {
content:' ';
left:-14px;
top:10px;
position:absolute;
background:red;
border-radius:0px 0 0 0;
width:164px;
height:40px;
display:block;
}
You do not use a gradient for this, you just need to use a pseudo-element like :after.
Sample code:
#bookmark {
width: 50px;
height: 100px;
position: relative;
background: red;
}
#bookmark:after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 0;
border-bottom: 35px solid #FFF;
border-right: 50px solid transparent;
}
Live JSFiddle
If you want the shape to be filled in with a gradient, you can do that, too. Just add that to the CSS:
background: linear-gradient(to right, #ff0000 0%,#B00000 100%);

Chrome zoom out breaks linear-gradient background

I am using a gradient background to display an alternating background for absolute positioned rows.
When zooming out in Chrome the layout gets messed up.
The calculation of the gradient background size seems to be different to the calculation of the top margins.
I have created a JSFiddle to illustrate the problem: http://jsfiddle.net/4y3k2/4/. When zooming out to e.g. 75% an offset appears between the foreground and background. The offset sums up more and more so that the layout looks completely broken for the last rows.
Here is my code:
#container {
position: absolute;
height: 2000px;
width: 100px;
background: linear-gradient(red 50%, green 50%, green);
background-size: 40px 40px;
}
.row {
position: absolute;
}
<div id="container">
<div class="row" style="top: 920px;"></div>
</div>
Everything works fine on IE and Firefox.
You can do this without calculating top every single time
for each row.
Instead set the parent div to be a block and use
predefined height and width for each row while floating them to the left:
#container {
position: absolute;
height: 2000px;
width: 100px;
background: linear-gradient(red 50%, green 50%, green);
background-size: 40px 40px;
display: block;
}
.row {
float: left;
width: 100px;
height: 20px;
}
http://jsfiddle.net/4y3k2/11/