I want to make a modal with a image and other elements to the right, something like this:
I tried making the modal content as a grid with two columns, the problems is when I try to adjust the image to fill all the avaiable space in the modal content, it overflows like this:
My css code looks like this:
<div class="modal">
<div class="content">
<div class="frame">
<img src="https://picsum.photos/400" alt="Example">
</div>
<div class="text">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis est mollitia possimus omnis nisi quo
aliquid ad accusantium dignissimos corrupti. Impedit, asperiores magnam corporis iste possimus tempore
quisquam provident dicta. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quibusdam
consequuntur, incidunt hic dolorum tempora inventore id sint ullam magnam veniam eveniet vitae, harum
dolorem tenetur libero voluptate voluptatibus. Inventore, excepturi!
</div>
</div>
</div>
.modal {
position: fixed;
height: 100%;
width: 100%;
background-color: rgba($color: #000000, $alpha: 0.6);
display: grid;
place-items: center;
}
.content {
background-color: $color-white;
width: 60%;
height: 600px;
border-radius: 5px;
display: grid;
grid-template-columns: auto 300px;
}
.frame > img {
width: 100%;
max-height: 100%;
object-fit: cover;
}
.text {
padding: 2rem 1rem;
}
The modal I tried to do has the problem of the image overflow. I setted the container div to grid with two columns, but the image column overflows the container as we can see in the picture. I want the image to take the space avaiable in the container but maintaining the 1 : 1 aspect ratio, also to be responsive, when the user change the screen size the image always has to maintain the 1:1 aspect ratio and not crop the image.I setted the height as static pixels because I want to add a element with a scrolling bar that otherwise will overflow or expand the container if its height was declared as percentages.
Just specify a max-height to the container(.frame) it will start working fine for 1:1 images because max-height:100% doesn't mean anything if you don't specify a height for its parent
and if you want to work for any ratio then remove the width property from the .frame > img because if you don't specify a width it will adjust itself according to it's height
I think the easiest way would be to use css aspect-ratio like this:
img {
width: 100%;
aspect-ratio: 1;
}
you can read more about it here
I think the problem might be the fact that your image's parent ( .frame ) does not have a set height.Try adding this:
.frame {
height:100%
}
I solved it removing the height of the modal content div. This allow the image to take all the space it needs. For the text to the right to not overgrow the image, what i did was make a div container and inside it a p tag with position absolute and with the widht and height of his parent, but with overflow: scroll. This allowed me to have mi image in 1:1 ratio taking all the space it needs in the card but without the text overflowing things. Here is how I did it.
.modal {
position: fixed;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
display: grid;
place-items: center;
}
.container {
width: 60%;
display: grid;
grid-template-columns: 1fr 30%;
background-color: #f4f4ed;
}
.frame {
width: 100%;
height: 100%;
}
.frame img {
width: 100%;
height: 100%;
object-fit: cover;
}
.content {
display: flex;
flex-direction: column;
align-items: center;
padding: 2em 0.8em;
}
.content h5 {
font-size: 1.4rem;
font-weight: 600;
color: #00f0b5;
}
.text {
flex-grow: 1;
position: relative;
height: 100%;
width: 100%;
overflow-y: scroll;
}
.text p {
font-size: 0.8rem;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.actions {
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 0.5rem;
}
.actions button {
font-family: "Roboto Condensed", sans-serif;
font-weight: 600;
text-transform: uppercase;
border: none;
outline: none;
background-color: #00f0b5;
padding: 0.2em 0;
cursor: pointer;
font-size: 0.5rem;
}
body {
font-size: 62.5%;
box-sizing: border-box;
}
h5{
margin: 0;
line-height: 1;
}
<div class="modal">
<div class="container">
<div class="frame">
<img src="https://picsum.photos/800" alt="Some lorem picsum">
</div>
<div class="content">
<h5>Title</h5>
<div class="text">
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Laborum placeat sed voluptatem optio esse facere accusamus tempore, provident dolorem nemo rerum sit accusantium labore quae eveniet ea aliquam. Ipsa, aliquam? Lorem ipsum dolor sit amet consectetur,
adipisicing elit. Laborum placeat sed voluptatem optio esse facere accusamus tempore, provident dolorem nemo rerum sit accusantium labore quae eveniet ea aliquam. Ipsa, aliquam? Lorem ipsum dolor sit amet consectetur, adipisicing elit. Laborum
placeat sed voluptatem optio esse facere accusamus tempore, provident dolorem nemo rerum sit accusantium labore quae eveniet ea aliquam. Ipsa, aliquam?
</p>
</div>
<div class="actions">
<button>Accept</button>
<button>Cancel</button>
</div>
</div>
</div>
</div>
Related
I have a wrapper div that contains two other divs, a left-column div and a right-column div. Currently the left column has a fixed min-height of 421px. The problem becomes visible once content stretches out of the right column, effectively making the wrapper div larger than the initially specified height of 421px. Here is a Fiddle
I want the left column to have a min-height of 421px and stretch all the way to the bottom of the wrapper div and set a background color to it, even if it has no content in it. How can I do that?
.wrapper{
overflow: scroll;
background: white;
font-family: "Roboto";
font-size: 14px;
line-height: 1.5;
width: 298px;
display: block;
margin: 0;
margin-left: 20px;
margin-bottom: 0.5cm;
box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.35);
-webkit-font-smoothing: antialiased;
}
.left-column {
float: left;
width: 30%;
min-height: 421px;
background-color: #144071;
color: white;
}
.right-column {
float: right;
width: 70%;
height: 100%;
color: black;
}
<div class="wrapper">
<div class="left-column"> content content </div>
<div class="right-column"> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Modi officiis iste culpa, pariatur vel dolores aspernatur quo sint fugit. Eveniet. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Modi officiis iste culpa, pariatur vel dolores aspernatur quo sint fugit. Eveniet. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Modi officiis iste culpa, pariatur vel dolores aspernatur
<div style="margin-top: 20px;">
<strong>Because the height of the right column is set to 100% and the left column to a fixed height of 421px, once content stretches out of the fixed height, the left column loses it's background color. How can I make it so that the left bkg color is stretched to the end, but has a minimal fixed height of 421px?</strong>
</div>
</div>
</div>
Use flexbox...
.wrapper {
background: white;
font-family: "Roboto";
font-size: 14px;
line-height: 1.5;
width: 298px;
display: flex;
margin: 0;
margin-left: 20px;
margin-bottom: 0.5cm;
box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.35);
-webkit-font-smoothing: antialiased;
}
.left-column {
width: 30%;
min-height: 421px;
background-color: #144071;
color: white;
}
.right-column {
width: 70%;
color: black;
}
<div class="wrapper">
<div class="left-column"> content content </div>
<div class="right-column"> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Modi officiis iste culpa, pariatur vel dolores aspernatur quo sint fugit. Eveniet. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Modi officiis iste culpa, pariatur vel dolores aspernatur
quo sint fugit. Eveniet. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Modi officiis iste culpa, pariatur vel dolores aspernatur
<div style="margin-top: 20px;">
<strong>Because the height of the right column is set to 100% and the left column to a fixed height of 421px, once content stretches out of the fixed height, the left column loses it's background color. How can I make it so that the left bkg color is stretched to the end, but has a minimal fixed height of 421px?</strong>
</div>
</div>
</div>
change your css like this:
https://codepen.io/shahry4r/pen/PowZOre
.wrapper{
background: white;
font-family: "Roboto";
font-size: 14px;
line-height: 1.5;
width: 298px;
margin: 0;
margin-left: 20px;
margin-bottom: 0.5cm;
box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.35);
-webkit-font-smoothing: antialiased;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
height: auto;
}
.left-column {
float: left;
width: 30%;
min-height: 421px;
background-color: #144071;
color: white;
}
.right-column {
float: right;
width: 70%;
height: auto;
color: black;
}
First, change the height of left column to padding-bottom. Second, change dynamic this padding-bottom for the height off the right column. Like this:
$(document).ready(function() {
var rightHeight = $('right-column').offsetHeight;
$('.left-column').css(padding - bottom: rightHeight);
})
I have some problem with creating inside border of image. Tried to do it with border, outline and box-shadow but didn't get the result.
HTML:
<div class="item">
<img src="https://i.ytimg.com/vi/vsZDSXlHqI4/maxresdefault.jpg" alt="">
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo blanditiis, distinctio. Odio eveniet vel nobis, consequuntur atque, dolorum debitis quae nesciunt esse quasi beatae, odit repudiandae dolore animi delectus ad nostrum, quas maiores hic labore?
Nisi, expedita sint, qui ullam itaque natus optio error accusantium placeat, culpa reiciendis, quos tempora.</p>
<button>Some action</button>
</div>
CSS:
div.item:hover {
//some code
img {
border-bottom: 5px solid #8cc34b;
margin-bottom: -5px;
}
}
My fiddle: JSFiddle
Here I got the outside border but I want to get inside border of image.
Thanks for help.
Wrap your img in div, and on hover use :after pseudo-element:
div.item {
min-height: 300px;
overflow: auto;
width: 300px;
background-color: white;
border: 2px solid #8cc34b;
border-radius: 5px;
text-align: center;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
div.item img {
width: 100%;
display: block;
}
div.item p {
text-align: left;
padding-left: 10px;
padding-right: 10px;
}
div.item button {
height: 35px;
width: 120px;
margin: 0 auto;
margin-bottom: 20px;
}
div.item .img-container {
position: relative;
}
div.item:hover h3 {
color: #8cc34b;
}
div.item:hover .img-container:after {
content: '';
position: absolute;
left: 0;
bottom: 0;
height: 5px;
width: 100%;
background-color: #8cc34b;
}
<div class="item">
<div class="img-container">
<img src="https://i.ytimg.com/vi/vsZDSXlHqI4/maxresdefault.jpg" alt="">
</div>
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo blanditiis, distinctio. Odio eveniet vel nobis, consequuntur atque, dolorum debitis quae nesciunt esse quasi beatae, odit repudiandae dolore animi delectus ad nostrum, quas maiores hic labore?
Nisi, expedita sint, qui ullam itaque natus optio error accusantium placeat, culpa reiciendis, quos tempora.</p>
<button>Some action</button>
</div>
I also added display: block on img, because inline img creates extra whitespace below.
Another answer:
To create a color bar like in the image you liked to, I wrapped your image in a DIV with class x and added the following CSS (including a pseudo element):
img {
width: 100%;
}
.x {
display: block;
position: relative;
}
.x:before {
content: '';
position: absolute;
z-index: -1;
bottom: 7px;
left: 0;
right: 0;
height: 5px;
background-color: #8cc34b;
}
Instead of a hover rule for the image, I just make the x DIV visible by changing its z-index:
div.item:hover {
h3 {
color: #8cc34b;
}
.x:before {
z-index: 1;
}
}
Adjust the bottom setting as desired to move it up or down:
https://jsfiddle.net/9wLx9p0f/5/
We can simply do this with outline-offset property:
outline: 1px solid #fff;
outline-offset: -10px;
Try this in the div you wants, if it works for you.
I'm facing the following issue with my angled top and bottom div. It should be something like on the picture which I'm providing here. I want to use before pseudo element for the top part of the div and after for the bottom part of the div and the bottom is kinda working but I have only problem with the top.
Any ideas how I can accomplish that effect using my code?
I want it to look like this:
Here's what I have so far:
.example {
width: 100%;
height: 700px;
position: relative;
background: red;
}
.example:after {
content: "";
width: 100%;
height: 100%;
position: absolute;
background: inherit;
z-index: -1;
bottom: 0;
transform-origin: left bottom;
transform: skewY(-10deg);
}
.example:before {
content: "";
width: 100%;
height: 100%;
position: absolute;
background: inherit;
z-index: -1;
top: 0;
transform-origin: top left;
transform: skewY(5deg);
}
<div class="example">
<h1>SOME CONTENT</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit eius excepturi at voluptates, est enim amet. Architecto eaque est assumenda, placeat ipsam repellendus atque nihil dolores, eos, commodi, provident sunt. Lorem ipsum dolor sit amet, consectetur
adipisicing elit. In dicta ut corrupti beatae maiores, officiis saepe omnis voluptatem facilis eveniet ex voluptate, ipsam libero! Recusandae ipsam, provident quam enim rem!</p>
<h2>More Content</h2>
</div>
View on JSFiddle
The issue you are having is due to the position:absolute setting of the pseudo-elements (before/after). When you set the position to absolute the element no longer has any container boundaries to conform to and is positioned based on the html body left,right,top and bottom values.
Now your main container example div's position starts from the top and your after pseudo element needs to be a bit above it to show the angled background but as the pseudo element is not a block element to actually move the main div down due to it's position:absolute setting. You need to add top margin to move the main div down so that the pseudo element set above it is shown.
Here is another way of doing it using css border properties instead of css3 transform property.
https://codepen.io/Nasir_T/pen/GNKLNQ
Hope this explains you the reason of using margins to adjust absolute positioned pseudo-elements.
I added a 150px of margin to the top of you div
EDIT I also changed the skew degree to -5% and 5% rather than +-10%
.example {
width: 100%;
h height: 700px;
position: relative;
background: red;
margin-top: 150px;
}
.example:after {
content: "";
width: 100%;
height: 100%;
position: absolute;
background: inherit;
z-index: -1;
bottom: 0;
transform-origin: left bottom;
transform: skewY(-5deg);
}
.example:before {
content: "";
width: 100%;
height: 100%;
position: absolute;
background: inherit;
z-index: -1;
top: 0;
transform-origin: top left;
transform: skewY(5deg);
}
I think it'd be easiest to skew white divs in :before and :after and then use z-index to bring your content to the front like so. Also added a margin-top to h1 which can be adjusted as needed.
.example {
width: 100%;
height: 700px;
position: relative;
background: red;
overflow: hidden;
}
.example:after {
content: "";
width: 110%;
height: 25%;
top: 0;
position: absolute;
background: white;
z-index: 1;
transform-origin: left bottom;
transform: skewY(-5deg);
}
.example:before {
content: "";
width: 110%;
height: 25%;
bottom: 0;
position: absolute;
background: white;
z-index: 1;
transform-origin: left bottom;
transform: skewY(5deg);
}
.example h1, .example p {
position: relative;
z-index: 5;
}
.example h1 {
margin-top: 205px;
}
<div class="example">
<h1>SOME CONTENT</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit eius excepturi at voluptates, est enim amet. Architecto eaque est assumenda, placeat ipsam repellendus atque nihil dolores, eos, commodi, provident sunt. Lorem ipsum dolor sit amet, consectetur adipisicing elit. In dicta ut corrupti beatae maiores, officiis saepe omnis voluptatem facilis eveniet ex voluptate, ipsam libero! Recusandae ipsam, provident quam enim rem!</p>
<h2>More Content</h2>
</div>
IF there's a static width, the effect is a LOT easier to create & control using borders:
.example {
width: 500px;
height: 700px;
position: relative;
background: red;
padding:100px 0px;
}
.example:after,
.example:before {
content:'';
position:absolute;
border-right:solid 500px transparent;
}
.example:before {
top:0px;
border-top:solid 100px #FFF;
}
.example:after {
bottom:0px;
border-bottom:solid 100px #FFF;
}
<div class="example">
<h1>SOME CONTENT</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit eius excepturi at voluptates, est enim amet. Architecto eaque est assumenda, placeat ipsam repellendus atque nihil dolores, eos, commodi, provident sunt. Lorem ipsum dolor sit amet, consectetur
adipisicing elit. In dicta ut corrupti beatae maiores, officiis saepe omnis voluptatem facilis eveniet ex voluptate, ipsam libero! Recusandae ipsam, provident quam enim rem!</p>
<h2>More Content</h2>
</div>
Why is the second div going under the first? Both of them are "float" elements. When I set a width of the second div, all works well. But I expect these two divs are being located to one row.
.one {
background-color: green;
float: left;
border: 1px solid green;
}
.two {
float: left;
background-color: red;
border: 1px solid red;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
This is because you didn't define the width's of the floating elements. If you define a max-width, say 50%, they will no longer be on the same line. I recommend giving max-width in contrast to width because, I believe you don't want to give the elements, a static width. Plus, they should be flexible to take as much as space they want, unless they shouldn't mess up with one another, which happens after 50%.
.one {
background-color: green;
float: left;
border: 1px solid green;
max-width: 50%;
}
.two {
float: left;
background-color: red;
border: 1px solid red;
max-width: 50%;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
You can use flex instead float.
Add display: flex; for a container. And flex: 0 0 auto; for the first div and flex: 1 1 auto; for the second. flex: 0 0 auto; means that element will take as much space as needed. flex: 1 1 auto; means that element will take all available space.
.container {
display: flex;
}
.one {
flex: 0 0 auto;
background: red;
}
.two {
flex: 1 1 auto;
}
<div class="container">
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
</div>
You can just give them some width options in %, or pixels using calc CSS functions, something like this...
.one {
background-color: green;
float: left;
border: 1px solid green;
display: block;
width: 10%;
}
.two {
width: 70%;
float: left;
background-color: red;
border: 1px solid red;
display: block;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
Flexbox is what you really want I think, float is great but always felt like a hack as opposed to a real solution.
You can use flexbox across all browsers, IE8 can't handle it but that's not a supported browser anymore. I suggest you read up on it on w3schools they generally have quite nice tutorials.
.flex-container {
display: flex;
}
.one,
.two {
padding: 5px;
}
.one {
background-color: green;
border: 1px solid green;
}
.two {
background-color: red;
border: 1px solid red;
}
html,
body {
margin: 0;
}
<div class="flex-container">
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
</div>
As to why it's was happening with float, unless you say otherwise anything display block will take up the entire space allotted, which is 100%, with one of the divs taking up 100% there is no room for the other one on the same line.
You could have given it a set width, or changed the display to inline and removed the float left from .two (though that would have been a little odd looking).
.one {
float: left;
background-color: green;
border: 1px solid green;
}
.two {
display: inline;
background-color: red;
border: 1px solid red;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
When I resize browsers window, my div gets down to bottom and a big space comes in top. I want that my div should remain in center of page even if resize the size of window lesser than size of div. In my school they haven't teach about this but I want to align this div in center of page
<!DOCTYPE html>
<html>
<head>
<title>CSS centering Done Right.</title>
<style>
* {
margin: 0;
padding: 0;
}
html, body, .m {
height: 100%;
width: 100%;
}
.m {
opacity:1;
font-size: 0;
overflow: auto;
text-align: center;
/*styling>>*/
background-color: #8F1C10;
}
.m::before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.m>div {
display: inline-block;
font-size: 19px;
margin: 20px;
max-width: 320px;
min-height: 20px;
min-width: 300px;
padding: 14px;
vertical-align: middle;
/*styling*/
color:white;
background-color: #140A08;
outline:none;
}
</style>
</head>
<body>
<title>CSS centering Done Right.</title>
<div class="m">
<div contenteditable="true">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis, enim, possimus, voluptates, quia voluptatibus voluptas eum quaerat iure aperiam asperiores debitis fuga itaque quibusdam a ad odio assumenda iusto voluptatem.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem, ex quia consequatur quae quasi amet veniam est quas error quos perferendis ducimus non similique in soluta magnam dolore molestias veritatis.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate, doloremque iste magni in eveniet ipsa odio mollitia eligendi magnam placeat aliquam hic reprehenderit reiciendis itaque assumenda ratione delectus. Alias, quis.</div>
</div>
</body>
</html>
margin: auto; is what you need. Try this:
<style>
* {
margin: 0;
padding: 0;
}
/*html, body, .m {
height: 100%;
width: 100%;
}*/
.m {
opacity:1;
font-size: 0;
overflow: auto;
text-align: center;
margin: auto;
/*styling>>*/
background-color: #8F1C10;
}
.m::before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.m>div {
display: inline-block;
font-size: 19px;
margin: 20px;
max-width: 320px;
min-height: 20px;
min-width: 300px;
padding: 14px;
vertical-align: middle;
/*styling*/
color:white;
background-color: #140A08;
outline:none;
}
</style>