I have a project that uses clip-paths to render a slant throughout the design. The scope of the project has changed and I now need to support IE/Edge, which do not support clip-paths.
There is a common repeated design element where the clip-path is applied to an image/text component wrapper, and clips the bottom right corner (you can see this in the snippet below).
I am not certain how to do this via other means so that it will work in IE/Edge. Is there another way of doing this that doesn't involve me having to export the images with the slant already applied?
.image-text-wrapper {
clip-path: polygon(0 0, 100% 0, 100% 75%, 0% 100%);
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
width: 600px;
background-color: aliceblue;
}
.image-text-wrapper .image {
width: 50%;
overflow: hidden;
}
.image-text-wrapper .text {
width: 50%;
text-align: center;
}
<div class="image-text-wrapper">
<div class="image">
<img src="https://img.purch.com/rc/640x415/aHR0cDovL3d3dy5zcGFjZS5jb20vaW1hZ2VzL2kvMDAwLzA4Mi8yMzEvb3JpZ2luYWwvbTMzLmpwZw==" />
</div>
<div class="text">
Content is here
</div>
</div>
One easy way that is supported but doesn't make the clipped part transparent is to add an overlay above with the same shape and you make its color the same as the background.
Here is an idea with linear-gradient:
.image-text-wrapper {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
width: 600px;
background-color: aliceblue;
position:relative;
}
.image-text-wrapper::before {
content:"";
position:absolute;
bottom:0;
left:0;
right:0;
height:25%;
background:linear-gradient(to bottom right,transparent 49.5%,#fff 50%);
}
.image-text-wrapper .image {
width: 50%;
overflow: hidden;
}
img {
display:block;
}
.image-text-wrapper .text {
width: 50%;
text-align: center;
}
<div class="image-text-wrapper">
<div class="image">
<img src="https://img.purch.com/rc/640x415/aHR0cDovL3d3dy5zcGFjZS5jb20vaW1hZ2VzL2kvMDAwLzA4Mi8yMzEvb3JpZ2luYWwvbTMzLmpwZw==" />
</div>
<div class="text">
Content is here
</div>
</div>
Another idea with transparency is to consider skew transformation but you have to adjust the HTML slightly:
.wrapper {
display:inline-block;
overflow:hidden;
}
.skew {
display:inline-block;
transform:skewY(-10deg);
transform-origin:bottom left;
overflow:hidden;
}
.skew > * {
transform:skewY(10deg);
transform-origin:bottom left;
}
.image-text-wrapper {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
width: 600px;
background-color: aliceblue;
}
.image-text-wrapper .image {
width: 50%;
overflow: hidden;
}
img {
display:block;
}
.image-text-wrapper .text {
width: 50%;
text-align: center;
}
body {
background:pink;
}
<div class="wrapper">
<div class="skew">
<div class="image-text-wrapper">
<div class="image">
<img src="https://img.purch.com/rc/640x415/aHR0cDovL3d3dy5zcGFjZS5jb20vaW1hZ2VzL2kvMDAwLzA4Mi8yMzEvb3JpZ2luYWwvbTMzLmpwZw==" />
</div>
<div class="text">
Content is here
</div>
</div>
</div>
</div>
Related
I have a page that overflows the viewport both horizontally and vertically, and I'd like to sticky a nav so that it is always at the top and horizontally centered.
Right now, I can get sticky top working, but the centering does not work. Can anyone help?
body {
text-align: center;
}
#header {
background-color: yellow;
width: max-content;
position: sticky;
top: 0;
left: 50%;
translate: -50%
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
<div id="header">
I should always be at the top and centered
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
CodePen: https://codepen.io/hbchin/pen/bGjpQLJ
After doing some digging I found this:
Why is my element not sticking to the left when using position sticky in css?
Essentially, it's not sticking because the body is automatically expanding to the width of the size of the very big box.
Putting it in an inline-block container will make the width not auto-expand to children, and thus allow sticking behavior.
So this works:
body {
text-align: center;
}
#header {
background-color: yellow;
width: max-content;
position: sticky;
top: 0;
left: 50%;
translate: -50%
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
#whole-thing {
display: inline-block;
}
<div id="whole-thing">
<div id="header">
I should always be at the top and centered
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
</div>
Unlike position: sticky and vertical positioning, left: 50% isn't a dynamic positioning option; it just sets the initial position. A horizontal scrollbar will still cause it to move, so that it remains "50% from the left edge".
To achieve a fixed left-right position, add a header container with position: fixed around your header, and within that, your header div can get auto margins:
body {
text-align: center;
max-width:100vw;
overflow:scroll;
}
/*added*/
#headercontainer{
position:fixed;
width:100vw;
left:0;
top:0;
}
#header {
background-color: yellow;
width: max-content;
/*left: 50%;*/ /*Removed*/
margin:auto;/*added*/
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
<div id="headercontainer"> <!-- added -->
<div id="header">
I should always be at the top and centered
</div>
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
You mean something like this?:
<div id="header-container">
<div id="header">
I should always be at the top and centered
</div>
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
body {
text-align: center;
}
#header-container {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 20px;
overflow: auto;
background-color: yellow;
}
#header {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
width: max-content;
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
I'm pretty new to coding, and I'm trying to use a grid.
But I got stuck a little bit.
I have the following codePan project:
* {
box-sizing:border-box
}
body {
height:100vh;
width: 100%;
background-color: red;
padding:0;
margin:0;
}
#wrapper {
background-color:blue;
height:100%;
width:100%;
margin:0;
padding:0;
display: grid;
grid-template-areas:
"nav"
"content"
"footer" ;
grid-template-columns: 1fr;
grid-template-rows: .8fr 1fr .8fr ;
}
#nav {
background-color: yellow;
grid-area: "nav";
position: sticky;
top: 0;
}
#content {
background-color: pink;
grid-area: "nav";
display:flex;
flex-direction: column;
}
footer {
background-color: brown;
grid-area: "nav";
}
#box1 {
background-color:purple;
height:50%;
}
#box2 {
background-color: blue;
height:60%;
display:flex;
flex-direction: column;
}
.image {
height: auto;
width: 400px;
position: relative;
top:-150px;
}
button {
height: 90px;
width: 400px;
background-color: #262A58;
color: white;
font-size: 1.5em;
font-family: 'EightOne';
border-radius: 10px;
border-style: none;
cursor: pointer;
}
<div id="wrapper">
<div id="nav"> nav </div>
<div id="content">
<div id="box1">
<h1>flower</h1>
</div>
<div id="box2">
<img class="image" src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.goldpetal.net%2Fmedia%2Fimages%2Fproduct_gallery%2FGerbera_Flower_12.jpg&f=1&nofb=1" alt="">
<button> buy flower </button>
</div>
</div>
<footer>
footer
</footer>
</div>
what i want is the similar like on this picture:
picture
unfortunatly i can not postitioning, the contents in the Box2, becuase the button goes out completly out from grid.
I tried several options, but at the end it was always some issue with the content in the Box2.
if someone could give me an advice. Probably I'm using wrong way Grid...
Thank you!
Let your grid define the size of the areas, and use Flexbox to lay out your content.
You can absolutely achieve this layout with CSS Grid. The button was overflowing its content area because #box2 has a fixed height of 60% of the content grid area, but your .image and the button are taller than that. Basically your image pushes down the button.
You don’t need to set explicit heights on your content, because that’s taken care of the grid.
The white-blue background is a bit tricky, because it doesn’t align exactly with the edges of your image. But you can achieve this affect with a background gradient on your content container.
Check out this working solution as inspiration:
body {
height: 100vh;
padding: 0;
margin: 0;
}
#wrapper {
height: 100%;
width: 100%;
display: grid;
grid-template-areas:
'nav'
'content'
'footer';
grid-template-columns: 1fr;
grid-template-rows: 64px 1fr 96px;
}
#nav {
grid-area: nav;
position: sticky;
top: 0;
background-color: white;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}
#content {
grid-area: content;
display: flex;
flex-direction: column;
justify-content: center;
background: linear-gradient(to bottom, transparent 60%, #aad 60%);
align-items: center;
padding: 32px;
}
#content .wrapper {
display: flex;
flex-direction: column;
width: 400px;
}
footer {
grid-area: footer;
}
.image {
border: solid 16px #aad;
margin: 16px;
}
button {
padding: 8px 16px;
background-color: #262a58;
color: white;
font-size: 1.5em;
font-family: 'EightOne';
border-radius: 10px;
border-style: none;
cursor: pointer;
margin-top: 32px;
align-self: center;
}
<div id="wrapper">
<div id="nav">nav</div>
<div id="content">
<div class="wrapper">
<h1>flower</h1>
<img
class="image"
src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.goldpetal.net%2Fmedia%2Fimages%2Fproduct_gallery%2FGerbera_Flower_12.jpg&f=1&nofb=1"
alt="Flower"
/>
<button>buy flower</button>
</div>
</div>
<footer>footer</footer>
</div>
Grid is not really used to make a full webpage. Everything is automatically stacked on top of eachother and should not have a fixed height. The height should changed based on the content.
Here is an example of my code :
.flex-container {
height: 300px;
width: 300px;
display: flex;
align-items: center;
justify-content: center;
background-color: #ffeeee
}
#item1 {
width: 100px;
height: 100px;
background-color: blue;
}
#item2 {
width: 120px;
height: 120px;
background-color: red;
}
<div class="flex-container">
<div id="item1"></div>
<div id="item2"></div>
</div>
https://jsfiddle.net/vL508wax/
I want the blue square to be centered and the top of the red square to be flush with the top of the blue square.
I know I can do this with margin-top for example but I don't think that's a good way to go. Can I do this with flexbox directly ?
A CSS grid solution since it would be difficult with flexbox:
.flex-container{
height:300px;
width:300px;
display:grid;
grid-auto-flow:column; /* side by side */
grid-template-rows:100px; /* the blue height here */
/* center everything*/
align-content:center;
justify-content:center;
/**/
background:
linear-gradient(green 0 0) center/100% 1px no-repeat,
#ffeeee
}
#item1{
width:100px;
height:100%;
background-color:blue;
}
#item2{
width:120px;
height:120px;
background-color:red;
transition:1s all;
}
#item2:hover {
height:160px;
}
<div class="flex-container">
<div id="item1">
</div>
<div id="item2">
</div>
</div>
I'm not sure about your use case nor have I done enough testing with this, But if there will always be 2 elements, you can make use of flex wrap alongside align-content: center;
.flex-container {
height: 300px;
width: 300px;
display: flex;
align-content: center;
flex-wrap: wrap;
justify-content: center;
background-color: #ffeeee
}
#item1 {
width: 100px;
height: 100px;
background-color: blue;
}
#item2 {
width: 120px;
height: 160px;
background-color: red;
}
<div class="flex-container">
<div id="item1"></div>
<div id="item2"></div>
</div>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Did try already several flex arguments but none of them worked like selg-align and self-content.
So the idea is the fit the image to the square and center it vertically and horizontally...
Does anybody can help with this thanks...
I am unsure of the why i need to edit this topic... it's just a simple question on how to fit the image in the square and center it vertically and horizontally (obvious to such square)... Don't understand where is the confusion about the question...
My examples is at https://jsfiddle.net/ej3814sn/
.five {
height: 20%;
border: 1px solid #fff;
}
.five-a {
float: left;
color: white;
}
.five-b {
float: right;
color: white;
}
Thanks in advance
You need to wrap your img in a div and outside of five - Using float is not a good idea at all in modern browsers.
Use flex to achieve your desired results and it is very responsive in modern browsers as well. Also set the height of .one to auto make sure img always centered and below the numbers.
Live Demo:
#import url('https://fonts.googleapis.com/css2?family=Montserrat:wght#400;500;600&display=swap');
* {
font-family: 'Montserrat', sans-serif;
}
html,
body {
height: 100%;
width: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.one {
width: 80%;
height: 80%;
display: flex;
flex-direction: row;
background: #232323;
opacity: 0.9;
}
.two {
width: 50%;
}
.four {
width: 100px;
height: auto;
margin-left: 10px;
margin-top: 10px;
border: 2px solid #fff;
display: flex;
flex-direction: column;
}
.five {
height: 20%;
display: flex;
justify-content: space-between;
}
.five-a {
color: white;
margin-left: 5px;
}
.five-b {
color: white;
margin-right: 5px;
}
img {
width: 90%;
height: auto;
}
.img-div {
display: flex;
justify-content: center;
align-items: center;
}
/*fit image to the square and center it*/
<body>
<div class="one">
<div class="two">
<div id="tree">
<div id="0" class="four">
<div class="five">
<div class="five-a">1</div>
<div class="five-b">10</div>
</div>
<div class="img-div">
<img src="https://logodownload.org/wp-content/uploads/2015/04/whatsapp-logo-1-1.png">
</div>
</div>
</div>
</div>
</div>
</body>
The best way, to position elements, is to use position property. Notice that I have made a change in HTML code as well. Put the image out of five element. Now talking about CSS, position both img and five as absolute. You would have to set top to 0, width to 100% for five. And for img, just set self-align to center.
#import url('https://fonts.googleapis.com/css2?family=Montserrat:wght#400;500;600&display=swap');
* {
font-family: 'Montserrat', sans-serif;
}
html,
body {
height: 100%;
width: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.one {
width: 80%;
height: 80%;
display: flex;
flex-direction: row;
background: #232323;
opacity: 0.9;
}
.two {
width: 50%;
}
.four {
float: left;
width: 100px;
height: 100px;
margin-left: 10px;
justify-content: center;
margin-top: 10px;
border: 2px solid #fff;
display: flex;
flex-direction: column;
position: relative;
}
.five {
height: 20%;
width: 100%;
position: absolute;
top: 0;
}
.five-a {
float: left;
color: white;
margin-left: 5px;
}
.five-b {
float: right;
color: white;
margin-right: 5px;
}
img {
width: 90%;
position: absolute;
height: auto;
align-self: center;
}
/*fit image to the square and center it*/
<body>
<div class="one">
<div class="two">
<div id="tree">
<div id="0" class="four">
<div class="five">
<div class="five-a">1</div>
<div class="five-b">10</div>
</div>
<img src="https://logodownload.org/wp-content/uploads/2015/04/whatsapp-logo-1-1.png">
</div>
</div>
</div>
</div>
</body>
I think you are looking to center the image in the .five div, yes?
EDIT: Remove the image tag and place your image as a background of the element you wish to center it in... Then add no-repeat, 0% to position and set the bg size to 100%, however change the height of the element to 100% as well...
.five {
height: 100%;
background-image: url(https://logodownload.org/wp-content/uploads/2015/04/whatsapp-logo-1-1.png);
background-repeat: no-repeat;
background-position: 0% 0%;
background-size: 100%;
}
#import url('https://fonts.googleapis.com/css2?family=Montserrat:wght#400;500;600&display=swap');
* {
font-family: 'Montserrat', sans-serif;
}
html,
body {
height: 100%;
width: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.one {
width: 80%;
height: 80%;
display: flex;
flex-direction: row;
background: #232323;
opacity: 0.9;
}
.two {
width: 50%;
}
.four {
float: left;
width: 100px;
height: 100px;
margin-left: 10px;
margin-top: 10px;
border: 2px solid #fff;
display: flex;
flex-direction: column;
}
.five {
height: 100%;
background-image: url(https://logodownload.org/wp-content/uploads/2015/04/whatsapp-logo-1-1.png);
background-repeat: no-repeat;
background-position: 0% 0%;
background-size: 100%;
}
.five-a {
float: left;
color: white;
margin-left: 5px;
}
.five-b {
float: right;
color: white;
margin-right: 5px;
}
/*fit image to the square and center it*/
<div class="one">
<div class="two">
<div id="tree">
<div id="0" class="four">
<div class="five">
<span class="five-a">1</span>
<span class="five-b">10</span>
<img src="">
</div>
</div>
</div>
</div>
</div>
How can I centre align text over an <img> preferably using FlexBox?
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
}
.center-aligned {
display: box;
display: flex;
box-align: center;
align-items: center;
box-pack: center;
justify-content: center;
}
.background-image {
position: relative;
}
.text {
position: absolute;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" />
<div class="text">SOME TEXT</div>
</section>
To center text over an image you don't need flexbox. Just use CSS positioning properties.
.height-100vh {
height: 100vh;
position: relative; /* establish nearest positioned ancestor for
absolute positioning */
}
.text {
position: absolute;
left: 50%; /* horizontal alignment */
top: 50%; /* vertical alignment */
transform: translate(-50%, -50%); /* precise centering; see link below */
}
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
display: flex; /* establish flex container */
flex-direction: column; /* stack flex items vertically */
position: relative; /* establish nearest positioned ancenstor for absolute positioning */
}
.text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: white;
font-weight: bold;
}
.center-aligned {
display: flex;
align-items: center;
justify-content: center;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg/revision/latest?cb=20090904155448" />
<div class="text">SOME TEXT</div>
</section>
Revised Codepen
The code above centers the text both vertically and horizontally over the image:
For a more detailed explanation of the centering method see:
Element will not stay centered, especially when re-sizing screen
You can just wrap an image with relatively positioned inline-block <div> and give the text this way:
* {margin: 0; padding: 0; list-style: none;}
.img-holder {position: relative; display: inline-block;}
.img-holder img {display: block;}
.img-holder p {position: absolute; top: 50%; left: 0; right: 0; transform: translate(0, -50%); text-align: center; color: #fff; text-shadow: 0 0 15px;}
<div class="img-holder">
<img src="http://bit.ly/1YrRsis" alt="">
<p>Text Aligned Centrally Vertical & Horizontal.</p>
</div>
Now the <div> is an inline kinda element that you can style.
Preview:
I've added another wrapper div surrounding the img and text as well as using flex to position the text. See this codepen
HTML:
<section class="height-100vh center-aligned">
<div class="wrapper">
<img class="background-image" src="http://bit.ly/1YrRsis" />
<div class="text">SOME TEXT</div>
</div>
</section>
CSS:
#import "bourbon";
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
}
.center-aligned {
#include display(flex);
#include align-items(center);
#include justify-content(center);
}
.wrapper {
position: relative;
}
.text {
justify-content: center;
align-items: center;
display: flex;
color: #fff;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
You also can center align text onto an image using display: inline-block; to the wrapper element.
.height-100vh {
display: inline-block;
position: relative;
}
.text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: #fff;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" />
<div class="text">SOME TEXT</div>
</section>
I added 2 more divs
<div class="text box" >
<img src="images/woodenbridge.jpg" alt="Wooden Bridge" />
<div class="slide-box flex">
<div class="flex-item"></div>
</div>
</div>
and then styled as follows :
.flex-item {
display: inline;
align-self: center;
}
Responsive centered text content over image
.height-100vh {
height: 100vh;
background: lightgrey;
}
.center-aligned {
display: flex;
align-items: center;
justify-content: center;
}
.background-image {
opacity: .3;
width: 100%;
object-fit:cover;
height: 100%;
}
.text {
position: absolute;
color: white;
font-family: 'Gill Sans', 'Gill Sans MT';
background: darkcyan;
padding: 20px;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="https://www.humanesociety.org/sites/default/files/styles/768x326/public/2018/08/kitten-440379.jpg?h=f6a7b1af&itok=vU0J0uZR" />
<div class="text">I'm in center</div>
</section>