center elements within a polygon div container - html

currently I have a div container with text in it.
div {
height: 200px;
background: red;
}
p {
text-align: center;
}
<div>
<p>
Text goes here
</p>
</div>
and I want this div container being a parallelogram with a vertically centered text in it.
div {
height: 200px;
background: red;
clip-path: polygon(0 25%, 100% 0, 100% 25%, 0 50%);
}
p {
text-align: center;
}
<div>
<p>
Text goes here
</p>
</div>
as you can see here, the text completely disappears because the css only works for the div container.
How can I make the text appear in the vertical center of this parallelogram?
Edit:
I don't know if using
clip-path: polygon(0 25%, 100% 0, 100% 25%, 0 50%);
is the best way to create a div container that is skew.

Use gradient to create the shape as background and you simply need to center the text using any common way. You will have better support than clip-path.
div.container {
height: 120px;
line-height:120px;
background-image:
/*Top triangle*/
linear-gradient(to bottom right,transparent 49%,red 51%),
/*Bottom triangle*/
linear-gradient(to top left,transparent 49%,red 51%);
background-position:top, bottom; /* One on the top and the other on the bottom*/
background-size:100% 50%; /*both will be 100% width and 50% height*/
background-repeat:no-repeat; /*don't repeat*/
}
p {
text-align: center;
}
<div class="container">
<p>
Text goes here
</p>
</div>
And if you want to rely on clip-path better use these values to cover the whole div and you simply need to adjust the height of div to control the height of shape:
div.container {
height: 120px;
line-height:120px;
background:red;
-webkit-clip-path: polygon(0 50%, 100% 0%, 100% 50%, 0% 100%);
clip-path: polygon(0 50%, 100% 0%, 100% 50%, 0% 100%);
}
p {
text-align: center;
}
<div class="container">
<p>
Text goes here
</p>
</div>

Related

Can this skewed box be created in HTML and CSS? [duplicate]

Looking for the code to make this particular shape with CSS..
Any help much appreciated!
You can do it with some rotation and perspective:
.box {
width: 150px;
height: 120px;
background: #f540a8;
margin: 20px;
transform: perspective(180px) rotateX(15deg) rotateY(20deg) rotateZ(-3deg);
}
<div class="box">
</div>
Or using SVG:
<svg viewBox="0 0 200 200" width=200>
<polygon points="20,0 150,20 170,130 0,150" fill="#f540a8" />
</svg>
And also using gradient (but without transparency):
.box {
width: 150px;
height: 120px;
background:
linear-gradient(to top right, transparent 46%,#fff 50%) right/10px 100%,
linear-gradient(to top right, transparent 46%,#fff 50%) top/100% 10px,
linear-gradient(to bottom right, transparent 46%,#fff 50%) bottom/100% 10px,
linear-gradient(to top left, transparent 46%,#fff 50%) left/10px 100%,
#f540a8;
background-repeat:no-repeat;
margin: 20px;
}
<div class="box">
</div>
You can use clip-path.
The clip-path CSS property creates a clipping region that defines
what part of an element should be displayed. More specifically, those
portions that are inside the region are shown, while those outside are
hidden.
Try this code snippet.
div{
width: 150px;
height: 150px;
-webkit-clip-path: polygon(5% 7%, 91% 14%, 98% 91%, 0% 100%);
clip-path: polygon(5% 7%, 91% 14%, 98% 91%, 0% 100%);
background: pink;
}
<div></div>
you can use:
clip-path: polygon(30px 0 , 250px 0, 200px 300px, 0 0);
Please see the example here: https://codepen.io/shakogele/pen/ZMZGGK

text overflow on skew div containers

currently I use clip-path for containers that should be skew.
.box {
height: 150px;
line-height: 150px;
text-align: center;
background: yellow;
}
#first {
clip-path: polygon(0 20%, 100% 0%, 100% 80%, 0 100%);
}
#second {
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 80%);
}
#spacing {
height: 100px;
}
<div id="first" class="box">
<p>
first container with a very very very long text. It's really long and won't fit here. Some text may disappear when the screen size gets smaller.
</p>
</div>
<div id="spacing">
</div>
<div id="second" class="box">
<p>
second container with a longer text
</p>
</div>
If the window gets smaller the text will not break into a new line it will just disappear.
How can I make the missing part of the text appear in the next line?
You can find an example of what I want to do on this page
https://www.thenativeweb.io/#
I believe below approach solves your issue. I deleted defined height and line-height for #box, and added padding: 30px 0, so to make some space to clip. Now text acts more naturally. You can adjust precise values.
.box {
height: auto;
text-align: center;
background: yellow;
padding: 30px 0;
}
#first {
clip-path: polygon(0 20%, 100% 0%, 100% 80%, 0 100%);
}
#second {
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 80%);
}
#spacing {
height: 100px;
}
<div id="first" class="box">
<p>
first container with a very very very long text. It's really long and won't fit here. Some text may disappear when the screen size gets smaller.
</p>
</div>
<div id="spacing">
</div>
<div id="second" class="box">
<p>
second container with a longer text
</p>
</div>
"If the window gets smaller the text will not break into a new line it will just disappear." - problem is coming because of line-height of box class and have to remove height:150px from box class.
.box {
height: auto;
line-height: auto;
text-align: center;
background: yellow;
padding: 80px 20px;
}
#first {
clip-path: polygon(0 20%, 100% 0%, 100% 80%, 0 100%);
}
#second {
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 80%);
}
#spacing {
height: 100px;
}
<div id="first" class="box">
<p>
first container with a very very very long text. It's really long and won't fit here. Some text may disappear when the screen size gets smaller.
</p>
</div>
<div id="spacing">
</div>
<div id="second" class="box">
<p>
second container with a longer text
</p>
</div>

feather div edges over background images

So I have an image slider behind a div with scrolling text content. I want the edges of the scrolling text div to feather into whatever is displaying behind it, so the text appears/disappears nicely when it scrolls in/out.
I tried adding an inset box shadow and linear gradients, but when the scrolling text has a transparent background you can see the shading around the text, fading into black, rather than transparent.
This is what I have:
This is what I want:
How do I do it with CSS?
You shouldn't need my code as it's not a bug, but here's some anyway.
<style>
#slider, #text, #text_overlay {
position:absolute;
width: 100%;
}
#slider {
height: 350px;
}
#text, #text_overlay {
top: 200px;
height: 100px;
}
#text {
z-index: 10;
}
#text_overlay {
z-index: 20;
background-image: linear-gradient(to right, rgba(0,0,0,1) 0%, rgba(0,0,0,0) 3%, rgba(0,0,0,0) 97%, rgba(0,0,0,1) 100%);
}
</style>
<div id="slider">
<img src='https://www.w3schools.com/howto/img_fjords.jpg' />
</div>
<div id="text">Some text etc blah blah blah</div>
<div id="text_overlay"></div>
Solved this issue using -webkit-mask-image and a gradient as the mask on the text div. No need for the text_overlay div anymore, and works with changing backgrounds :)
#text {
-webkit-mask-image: linear-gradient(
to right, /* gradient direction */
rgba(0,0,0,0) 0%, /* transparent left 3% */
rgba(0,0,0,1) 3%, /* visible in the middle start */
rgba(0,0,0,1) 97%, /* visible in the middle end */
rgba(0,0,0,0) 100% /* transparent right 3% */
);
}
Full code:
<style>
#container {
position:relative;
height: 350px;
width: 100%;
}
#slider, #text {
position:absolute;
width: 100%;
}
#slider {
height: 100%;
}
#text {
top: 200px;
height: 100px;
z-index: 10;
-webkit-mask-image: linear-gradient(
to right, /* gradient direction */
rgba(0,0,0,0) 0%, /* transparent left 3% */
rgba(0,0,0,1) 3%, /* visible in the middle start */
rgba(0,0,0,1) 97%, /* visible in the middle end */
rgba(0,0,0,0) 100% /* transparent right 3% */
);
}
</style>
<div id="container">
<div id="slider">
<img src='https://www.w3schools.com/howto/img_fjords.jpg' />
</div>
<div id="text">Some text etc blah blah blah</div>
</div>

Create a transparent div that cuts into another div

I have two divs on top of each other. I need the bottom div to have a slanted angle like this:
I only need help with slant of the top of the blue div, I can handle to bottom slant myself.
I could create a psuedo element and skew it, but the issue is that the blue div has a gradient and making a psuedo element with the same gradient makes the two elements not flow together with their gradients.
I think my only solution is to create a transparent div, skew it and place it on top of the blue div. I was wondering if this is even possible to create a skewed transparent div and have it cut into the blue div, slanting the blue div while showing the image in the background.
I'm open to any other ideas to achieve this slanted div.
Ive created a simple jsfiddle with the divs for anyone to mess around with.
Here is the basic mark up:
<div class="main">
<div class="main-top">
</div>
<div class="main-bottom">
</div>
</div>
.main-top {
background: url("http://stock-wallpapers.com/wp-content/uploads/2015/01/Huawei_P7_home_wallpaper_02_.jpg") center center no-repeat;
background-size: cover;
height: 300px;
width: 600px;
}
.main-bottom {
height: 300px;
width: 600px;
background-image: -moz-linear-gradient( -51deg, rgb(28,35,80) 0%, rgb(27,31,71) 41%, rgb(25,26,62) 100%);
background-image: -webkit-linear-gradient( -51deg, rgb(28,35,80) 0%, rgb(27,31,71) 41%, rgb(25,26,62) 100%);
position: relative;
top: -150px;
}
Thanks
It is in fact very easy if you use this site
http://bennettfeely.com/clippy/
.main-top {
background: url("http://stock-wallpapers.com/wp-content/uploads/2015/01/Huawei_P7_home_wallpaper_02_.jpg") center center no-repeat;
background-size: cover;
height: 300px;
width: 600px;
}
.main-bottom {
height: 300px;
width: 600px;
background-image: -moz-linear-gradient( -51deg, rgb(28, 35, 80) 0%, rgb(27, 31, 71) 41%, rgb(25, 26, 62) 100%);
background-image: -webkit-linear-gradient( -51deg, rgb(28, 35, 80) 0%, rgb(27, 31, 71) 41%, rgb(25, 26, 62) 100%);
position: relative;
top: -150px;
-webkit-clip-path: polygon(0 0, 100% 32%, 100% 100%, 0 68%);
clip-path: polygon(0 0, 100% 32%, 100% 100%, 0 68%);
}
<div class="main">
<div class="main-top">
</div>
<div class="main-bottom">
</div>
</div>

45 Degree Angle cut on divs

I'm trying to cut out the top right corner of most of my div elements on my site. These divs are all different sizes. I'm trying to find a responsive way of doing this. I ran into this site here: http://bennettfeely.com/clippy/ which allows you to cut out a custom polygon shape.
Here is what I have so far:
div {
width: 280px;
height: 280px;
background: #1e90ff;
-webkit-clip-path: polygon(0% 100%, 100% 100%, 100% 9%, 89% 0%, 0% 0%);
clip-path: polygon(0% 100%, 100% 100%, 100% 9%, 89% 0%, 0% 0%);
}
/* Center the demo */
html, body { height: 100%; }
body {
background-image: url('http://www.planwallpaper.com/static/images/recycled_texture_background_by_sandeep_m-d6aeau9_PZ9chud.jpg');
display: flex;
justify-content: center;
align-items: center;
}
<div></div>
My question is I'm trying to read these clippings and find out how to make the perfect 45 degree angle cut off the top right corner. As of right now this polygon was created by me freehand. And I'm trying to see what percentages I would need to use to make the perfect 45 degree angle cut from the top right.
With the solution I will be adding the cutoff to most of my divs, buttons, and images.
I found other ways of doing this on Stack Overflow using border-left and right with absolute position, but the problem is I need the div cutoff to be transparent because some of them have background images behind it.
Here is a JS Fiddle that's set up: https://jsfiddle.net/xyvz5z8m/1/
You should be able to do an exact 45 degree clip by using CSS calc, to work out the positions to clip from, instead of the percentages. e.g.
-webkit-clip-path: polygon(0% 100%, 100% 100%, 100% 30px, calc(100% - 30px) 0%, 0% 0%);
clip-path: polygon(0% 100%, 100% 100%, 100% 30px, calc(100% - 30px) 0%, 0% 0%);
div {
width: 100px;
height: 100px;
background: #1e90ff;
-webkit-clip-path: polygon(0% 100%, 100% 100%, 100% 30px, calc(100% - 30px) 0%, 0% 0%);
clip-path: polygon(0% 100%, 100% 100%, 100% 30px, calc(100% - 30px) 0%, 0% 0%);
/* Resizing this div just to show that this will remain at 45 degrees */
animation: resize 5s infinite;
}
html, body {
height: 100%;
}
body {
background: #ededed;
display: flex;
justify-content: center;
align-items: center;
}
#keyframes resize {
0% { width: 100px; height: 100px; }
25% { width: 50px; height: 100px; }
50% { width: 50px; height: 50px; }
75% { width: 150px; height: 50px; }
100% { width: 100px; height: 100px; }
}
<div></div>
The key part being that we use pixel sizes for the positioning of the clipped area, and calc(100% - 30px) to get an exact position from the far side of the element, though bare in mind this may have very limited browser support.