I would like to create a div which has auto height based on the length of the text inside it (the text should be in the parent element so that it has 100% width. But also there should be 3 divs in the background each taking 1/3rd of the parent width and being 100% of the parent height.
This is an example of what I expect.
Any ideas how to create this?
I usually don't post questions like this one. But I just got stuck on how to do that and I needed help
You can use Flexbox for this
.content {
display: flex;
position: relative;
}
h1 {
text-transform: uppercase;
font-size: 45px;
margin: 0;
font-family: sans-serif;
word-spacing: 15px;
}
.divs {
position: absolute;
z-index: -1;
width: 100%;
top: 0;
left: 0;
height: 100%;
display: flex;
}
.el {
flex: 1;
border: 3px solid lightgreen;
}
.el:nth-child(1) {background: #DBEBD4;}
.el:nth-child(2) {background: #CEE2F4;}
.el:nth-child(3) {background: #D9D2EA;}
<div class="content">
<h1>Lorem ipsum dolor sit.</h1>
<div class="divs">
<div class="el"></div>
<div class="el"></div>
<div class="el"></div>
</div>
</div>
Have a container div absolutely positioned under the text and then have three child divs inside that.
The reason for the container div is that it avoids having to position each child separately.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.parent {
position: relative;
}
.underlay {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
display: flex;
border: 1px solid grey;
}
.child {
flex:1;
border:1px solid red;
background: rgba(255,0,255,0.25);
z-index:-1;
}
<div class="parent">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta sunt natus architecto deserunt eligendi repellat corporis doloribus fugit ipsam fugiat, eius vitae, magni? Quasi consequatur voluptatem eius excepturi eum qui dolores placeat maxime corporis
laborum hic, magnam ipsa voluptatibus doloremque. Dignissimos dolorum corporis sunt amet unde repellat, dolor consectetur earum.</p>
<div class="underlay">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</div>
Related
I am using vue-slick-carousel package.
I have three main elements with which I want to interact. The first one is a picture with a character with the class slide__person The second one is a text with the class slide__text and a picture with a cloud with the class banner__slider-cloud.
I need the text to overlap the cloud, and the cloud, in turn, overlaps the picture with the person.
The cloud must remain static and cannot be moved inside the slider
I want to achieve something like this.
.banner__slider-cloud {
z-index: 1;
}
.slide__text {
z-index: 2;
}
.slide__person {
z-index: 0;
}
I tried different options, interacted with the z-index property, tried to apply the position property, but I could not solve this problem
You can also see my code in the online sandbox codesandbox
<script>
import VueSlickCarousel from "vue-slick-carousel";
import "vue-slick-carousel/dist/vue-slick-carousel.css";
// optional style for arrows & dots
import "vue-slick-carousel/dist/vue-slick-carousel-theme.css";
export default {
name: "HelloWorld",
components: { VueSlickCarousel },
};
</script>
<template>
<div class="lending">
<main class="banner">
<div class="banner__slider">
<VueSlickCarousel :arrows="true" :dots="true">
<div class="slide">
<div>
<img
class="slide__person"
src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg"
/>
<p class="slide__text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Aliquam architecto, corporis deserunt distinctio dolor dolores
ea ex hic iste magnam nihil optio perferendis perspiciatis
</p>
</div>
</div>
<div class="slide">
<div>
<img
class="slide__person"
src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg"
/>
<p class="slide__text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Aliquam architecto, corporis deserunt distinctio dolor dolores
ea ex hic iste magnam nihil optio perferendis perspiciatis
</p>
</div>
</div>
</VueSlickCarousel>
<img class="banner__slider-cloud" src="../assets/cloud.png" />
</div>
</main>
</div>
</template>
<style>
img {
max-width: 100%;
}
.slick-slide > div {
display: flex;
justify-content: center;
}
.slide {
max-width: 500px;
border: 2px solid orange;
padding: 1rem;
}
.slide .slide__text {
border: 2px solid green;
padding: 8px;
}
.banner__slider-cloud {
border: 1px solid;
max-width: 960px;
margin: auto;
position: absolute;
right: 0;
left: 0;
top: 5%;
}
</style>
img {
max-width: 100%;
}
.slick-slide>div {
display: flex;
justify-content: center;
}
.slide {
max-width: 500px;
border: 2px solid orange;
padding: 1rem;
}
.slide .slide__text {
border: 2px solid green;
padding: 8px;
}
.banner__slider-cloud {
border: 1px solid;
max-width: 960px;
margin: auto;
position: absolute;
right: 0;
left: 0;
top: 5%;
}
<div class="lending">
<main class="banner">
<div class="banner__slider">
<div class="slide">
<div>
<img class="slide__person" src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg" />
<p class="slide__text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto, corporis deserunt distinctio dolor dolores ea ex hic iste magnam nihil optio perferendis perspiciatis
</p>
</div>
</div>
<img class="banner__slider-cloud" src="https://via.placeholder.com/500" />
</div>
</main>
</div>
z-index only works on elements within the same stacking context
By moving the cloud image element to be a sibling of the text and person image elements, their z-indexes will cause them to overlap the way you want
<div>
<img
class="slide__person"
src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg"
/>
<p class="slide__text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Aliquam architecto, corporis deserunt distinctio dolor dolores
ea ex hic iste magnam nihil optio perferendis perspiciatis
</p>
<img class="banner__slider-cloud" src="../assets/cloud.png" />
</div>
.slide .slide__person {
position: relative;
z-index: 0;
}
.slide .slide__text {
border: 2px solid green;
padding: 8px;
position: relative;
z-index: 2;
}
.banner__slider-cloud {
border: 1px solid;
max-width: 960px;
margin: auto;
position: absolute;
left: 17%;
top: 5%;
z-index: 1;
}
updated codesandbox
Although I believe a full solution is impossible, due to the DOM manipulation that the Vue Slick Carousel library uses, there is a workaround to the z-index limitation using transform-style: preserve-3d.
So long as all the elements in the DOM hierarchy being manipulated have the preserve-3d CSS attribute applied (this property is not automatically inherited), you can use transform: translateZ() to sort elements' layering even when they are not siblings in the stacking context. See the edited snippet below.
img {
max-width: 100%;
}
/* ### Added this block ### */
.banner * {
transform-style: preserve-3d;
}
.slick-slide>div {
display: flex;
justify-content: center;
}
.slide {
max-width: 500px;
border: 2px solid orange;
padding: 1rem;
}
.slide .slide__text {
border: 2px solid green;
padding: 8px;
transform: translateZ(1px); /* ### Added this line ### */
}
.banner__slider-cloud {
border: 1px solid;
max-width: 960px;
margin: auto;
position: absolute;
right: 0;
left: 0;
top: 5%;
}
<div class="lending">
<main class="banner">
<div class="banner__slider">
<div class="slide">
<div>
<img class="slide__person" src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg" />
<p class="slide__text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto, corporis deserunt distinctio dolor dolores ea ex hic iste magnam nihil optio perferendis perspiciatis
</p>
</div>
</div>
<img class="banner__slider-cloud" src="https://via.placeholder.com/500" />
</div>
</main>
</div>
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>
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've got 3 divs.
<div class="top"> TOP </div>
<div class="middle"> MIDDLE </div>
<div class="bottom"> BOTTOM </div>
.top{
position: fixed;
top:0;
width: 100%;
}
.bottom{
position: fixed;
bottom:0;
width: 100%;
}
middle div contains a dynamic data. So I have enabled the scrollbar there.
.middle{
overflow-y: auto;
width: 100%;
}
My question is, How can I stack the middle div and make thebottom adjusts its height automatically depending on the data in top and middle divs?
EDIT
the flex model is here to help you:
html, body {
height:100%;
margin:auto;
}
body {
display:flex;
flex-flow:column;
background:turquoise;
}
.middle {
flex:1;
overflow:auto;
background:tomato;
}
<div class="top"> TOP any height</div>
<div class="middle"> MIDDLE i scroll if too tall </div>
<div class="bottom"> BOTTOM any height</div>
demo http://jsfiddle.net/7yLFL/445/
Flex model with no restrictions on any section height:
html,
body {
height: 100%;
}
body {
font-family: sans-serif;
font-size: 22px;
line-height: 1.5em;
text-align: center;
color: #fff;
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.wrapper {
height: 100%;
display: flex;
flex-direction: column;
width: 50%;
border: 1px solid #fff;
float: left;
}
.top {
background: none #59B2FF;
}
.middle {
flex-grow: 100;
overflow-y: auto;
background: none #FFB800;
}
.bottom {
align-self: flex-end;
width: 100%;
background: none #A1FFB5;
color: #fff;
}
<div class="wrapper wrapper1">
<div class="top">top</div>
<div class="middle">middle</div>
<div class="bottom">bottom</div>
</div>
<div class="wrapper wrapper2">
<div class="top">top</div>
<div class="middle">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.?</div>
<div class="bottom">bottom</div>
</div>
Also on Fiddle playground.
I would create an .outer div that is basically a container for .top, .middle, .bottom. You would also create a css class called .inner that has position absolute:
#CSS
.outer {
#Can position however you want
}
.inner {
position: relative;
}
.top {
top: 0;
width: 100%;
}
.middle {
overflow-y: auto;
max-height: 100px; //Optional; if you want to specify the maximum height this div can take
width: 100%;
}
.bottom {
width: 100%;
bottom: 0
}
Then in your HTML:
<div class="outer">
<div class="inner top"> TOP </div>
<div class="inner middle"> MIDDLE </div>
<div class="inner bottom"> BOTTOM </div>
</div>
Here is a plunker DEMO
Just put:
.middle{
position: relative;
height: auto;
}
Since, you have put position fixed for top container, so it won't move out of screen and will be stuck at the top while the other two divs will be scrollable under the top div.
I want put one image with a caption and after the image a description of the image. When I use absolute positioning for the image, the div goes under the image. How can i fix it so the div goes after the image: http://jsfiddle.net/qhcb0616/
.slide-square {
width: 148px;
height: 348px;
border:solid 1px black;
background: red;
}
.slide-square img {
position: absolute;
}
.slide-square h1 {
position: relative;
font-size:11px;
background :blue;
text-align:right;
float:right;
color:white;
padding:5px;
margin-top:5px;
margin-right:5px;
}
.slide-square div {
display: inline-block;
font-size: 50px;
}
<div class="slide-square">
<img src="http://www.flowers-online.com.au/flower-pictures/spring-surprise-flower-bouquet-b10l.jpg" alt="" width="148" height="148">
<h1>Scientific</h1>
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit debitis error magnam sunt numquam ab quasi omnis eaque dolore, perferendis quisquam, minus qui repudiandae possimus esse tempore ipsum adipisci soluta!</div>
</div>
What you need is make absolute the h1 element. Try this:
Make the parent relative:
.slide-square {
position: relative;
}
Then position the h1 on the parent
.slide-square h1 {
position: absolute;
top: 5px;
right: 5px;
}
Remove also the absolute for the img
Check the Snippet Below
.slide-square {
width: 148px;
height: 348px;
border: solid 1px black;
background: red;
position: relative;
}
.slide-square h1 {
position: absolute;
font-size: 11px;
background: blue;
text-align: right;
color: white;
margin:0;
padding: 5px;
top: 5px;
right: 5px;
}
.slide-square div {
display: inline-block;
font-size: 50px;
}
<div class="slide-square">
<img src="http://www.flowers-online.com.au/flower-pictures/spring-surprise-flower-bouquet-b10l.jpg" alt="" width="148" height="148">
<h1>Scientific</h1>
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit debitis error magnam sunt numquam ab quasi omnis eaque dolore, perferendis quisquam, minus qui repudiandae possimus esse tempore ipsum adipisci soluta!</div>
</div>