I am using React and I am trying to have an overlay over each grid item in a grid. But, what all I try, the overlay seems to come below each grid item and not over the grid item. Even if I use the position: absolute it is not working !
Here's the code I am using for the grid (React):
<div className="home__itemSection">
<div className="item">
<div className="item__container">
<div className="container__imageDiv">
<img
src={""}
alt=""
/>
</div>
<div className="container__detailSection">
<p>{""}</p>
<h6>{""}</h6>
</div>
</div>
<div className="item__overlay">
<h1>{""}</h1>
<h1>{""}</h1>
</div>
</div>
</div>
css:
.home__itemSection {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
margin: 30px 170px 0 170px;
transition: ease all 0.5s;
}
.item {
background-color: #f1f1f1;
height: 350px;
width: 212px;
margin: 0 3rem 1rem 3rem;
height: fit-content;
font-family: "Poppins", sans-serif;
display: flex;
align-items: flex-start;
}
.container__imageDiv {
height: 318px;
width: 212px;
}
.container__imageDiv > img {
height: 318px;
width: 212px;
}
.container__detailSection {
height: 44px;
width: 212px;
display: flex;
flex-direction: column;
align-items: flex-start;
padding-top: 5px;
}
.container__detailSection > p {
color: black;
font-size: 13px;
font-weight: 300;
line-height: initial;
}
.container__detailSection > h6 {
padding-left: 1.8rem;
font-weight: 500;
font-size: 1rem;
}
.item__overlay {
height: 252px;
width: 212px;
background-color: black;
}
Any help is greatly appreciated !
Thank You !
Please add position: relative; on item(parent tag of overlay)
.item{
position: relative;
}
.item .item__overlay{
position: absolute;
left: 0;
top: 0;
}
An element with position absolute is relative to the next parent element with relative (or absolute) positioning.
Add position relative to home__itemSection. Also use height and with to cover all the container:
.home__itemSection {
...
position: relative;
}
.item__overlay {
...
position: absolute;
height: 100%;
width: 100%;
}
Related
This may be a begginer question, but I just can't seem to figure it out.
HTML:
<div id = "upper">
<input id = "leftBigArrow" class = "button" type="button">
<div id = "picBox">
<img id = "bigPic" src="images/IMG_1744.JPG" alt="">
<div id = "overlay">
<div id = "olText">
<h1>Title</h1>
<p>Description</p>
</div>
</div>
</div>
<input id = "rightBigArrow" class = "button" type="button">
</div>
CSS:
#upper {
display: flex;
background: white;
box-shadow: 0px 0px 7px 2px #313131;
}
#picBox {
width: 800px;
height: 600px;
background-color: #8D918D;
display: flex;
}
#pigPic {
object-fit: contain;
object-position: center;
z-index: 1;
}
#overlay {
align-self: flex-end;
width: 100%;
height: 20%;
background-color: black;
color: white;
opacity: 0.6;
margin: 0;
font-size: 80%;
z-index: 2;
}
#olText {
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
justify-content: center;
}
h1, p {
margin-left: 20px;
}
h1 {
margin-top: 0;
margin-bottom: 10px;
}
p {
margin-top: 0;
margin-bottom: 0;
}
In #picBox I have 2 items, an image (#bigPic) with object-fit attribute and a div with a div with 2 text items (#overlay). I want to have the overlay over the image. Overlay height is 20% at the bottom of #picBox and the 2 text items in #olText need to be arranged as css shows. If I use position absolute and relative, it messes up the object-fit. So how can I make this work as I intended?
In CSS, if you want to overlay an element, that element's position (in this case #picBox) needs to be set to relative first. Then the position of the element you want on top (#overlay) should be set to absolute. See the example below.
#upper {
display: flex;
background: white;
box-shadow: 0px 0px 7px 2px #313131;
}
#picBox {
width: 800px;
height: 600px;
background-color: #8D918D;
display: flex;
position: relative;
}
#pigPic {
object-fit: contain;
object-position: center;
z-index: 1;
}
#overlay {
align-self: flex-end;
width: 100%;
height: 20%;
background-color: black;
color: white;
opacity: 0.6;
margin: 0;
font-size: 80%;
z-index: 2;
position: absolute;
}
#olText {
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
justify-content: center;
}
h1,
p {
margin-left: 20px;
}
h1 {
margin-top: 0;
margin-bottom: 10px;
}
p {
margin-top: 0;
margin-bottom: 0;
}
<div id="upper">
<input id="leftBigArrow" class="button" type="button">
<div id="picBox">
<img id="bigPic" src="https://www.w3schools.com/tags/img_pink_flowers.jpg" alt="">
<div id="overlay">
<div id="olText">
<h1>Title</h1>
<p>Description</p>
</div>
</div>
</div>
<input id="rightBigArrow" class="button" type="button">
</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>
I have the following snippet and am wondering why the spacing between the first three elements is not the same as the spacing between the the 3rd and 4th
.textFMT2 {
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
height: 100vh;
font-size: 1.5vw;
}
.links4 {
width: 100%;
}
.links4 span {
display: block;
background-color: #538231;
text-align: center;
width: 50%;
border-radius: 10vw;
padding: 1vw 0;
margin: 0 auto;
}
span {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
.textFMT2 .arrowsForScroll {
position: relative;
}
.arrowsForScroll {
position: absolute;
bottom: 2vw;
}
.arrowsForScroll {
display: flex;
justify-content: space-evenly;
align-items: center;
width: 100%;
}
a.left, a.right {
text-decoration: none;
font-weight: bolder;
font-size: 32px;
}
.left, .right {
display: none;
}
.sections {
background-color: #b3d7f7;
/* width: 32vw; */
color: #538231;
font-size: 16px;
text-align: center;
position: relative;
height: 100%;
}
.links4 a {
text-decoration: none;
color: white;
}
<div class="textFMT2">
<div class="links4">
<span>Sign up for our<br>Quarterly Newsletter</span>
</div>
<div class="links4">
<span>Attend an<br>Event</span>
</div>
<div class="links4">
<span>Volunteer with<br>SWAG</span>
</div>
<div class="arrowsForScroll">
<a class="left" href="#section2"><!--↑--> </a>
<div class="links4">
<span>Donate to help<br>our work</span>
</div>
<a class="right" href="#"> </a>
</div>
</div>
The different gap size is created because you are setting the position to relative here:
.textFMT2 .arrowsForScroll {
position: relative;
}
An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
So you need to explictly set it to static
.textFMT2 .arrowsForScroll {
position: static;
}
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page
EDIT: The entire issue stems from having a flex item set to position: absolute in .arrowsForScroll (which is defined twice by the way), I assume you wrote the selector .textFMT2 .arrowsForScroll to compensate for that. So the layout could also be fixed by simply removing these two selectors entirely:
.textFMT2 .arrowsForScroll {
position: relative;
}
.arrowsForScroll {
position: absolute;
bottom: 2vw;
}
There is object-fit: contain as a way to resize the image so that the entire image fits within the frame, aligning the long side of the image with the frame.
h2 {
font-family: Courier New, monospace;
font-size: 1em;
margin: 1em 0 0.3em;
}
div {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: flex-start;
height: 940px;
}
img {
width: 150px;
height: 100px;
border: 1px solid #000;
}
.narrow {
width: 100px;
height: 150px;
margin-top: 10px;
}
.contain {
object-fit: contain;
}
<div>
<h2>object-fit: contain</h2>
<img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="contain" />
<img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="contain narrow" />
</div>
However, this does not work for div tags and so on, it works only for replacement elements.
h2 {
font-family: Courier New, monospace;
font-size: 1em;
margin: 1em 0 0.3em;
}
div {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: flex-start;
height: 940px;
}
.contain-parent {
width: 150px;
height: 100px;
background: red;
border: 1px solid #000;
}
.narrow {
width: 100px;
height: 150px;
background: green;
margin-top: 10px;
}
.contain1 {
width: 50px;
height: 80px;
background: blue;
object-fit: contain;
}
.contain2 {
width: 80px;
height: 50px;
background: blue;
object-fit: contain;
}
<div>
<h2>object-fit: contain(not work)</h2>
<div class="contain-parent">
<div class="contain1"></div>
</div>
<div class="contain-parent">
<div class="contain2 narrow"></div>
</div>
</div>
How can I reproduce the same behavior as object-fit: contain on replacement elements with div tags,
span tags and so on? I want to display letter box when the aspect ratio does not match.
How do you make the contents of a 100% width span not wrap when the text is more than 100% of parent? I have monkeyed around with whitespace: nowrap; with no luck.
PROBLEM: span.firstname, span.lastname, span.email all wrap text when their contents are longer than div.name, div.personal, or div.account_view. I would like the 3 spans to go for however long their content is (no wrap).
HTML:
<div class="account_view">
<div class="personal">
<div class="picture">
<img src="example.png" />
<span>Image caption</span>
</div>
<div class="name">
<span class="first">Firstname</span>
<span class="last">Lastname</span>
<span class="email">email#email.com</span>
</div>
</div>
</div>
CSS:
div.account_view {
display: block;
height: auto;
width: 700px;
padding: 40px;
margin: 0 auto;
border: 0;
}
div.account_view div.personal {
margin: 0 auto;
clear: both;
overflow: hidden;
white-space: nowrap;
}
div.account_view div.personal div.picture {
float: left;
display: block;
width: 200px;
height: 200px;
border: 1px solid #aaa;
margin: 0 10px 0 0;
}
div.account_view div.personal div.picture img {
display: block;
width: 100%;
height: 100%;
border: 0;
padding: 0;
margin: 0;
}
div.account_view div.personal div.picture span {
display: block;
font-size: inherit;
text-align: center;
margin: 0;
position: relative;
top: -27px;
padding: 5px;
background-color: rgba(0,0,0,.4);
color: #eee;
}
div.account_view div.personal div.name {
float: left;
position: relative;
top: -25px;
}
div.account_view div.personal div.name > * {
display: block;
font-weight: 300;
white-space: normal;
}
div.account_view div.personal div.name span.first {
font-size: 125px;
font-weight: 700;
}
div.account_view div.personal div.name span.last {
position: relative;
top: -9px;
left: 5px;
font-size: 56px;
}
div.account_view div.personal div.name span.email {
font-size: 29px;
position: relative;
left: 8px;
}
GOAL: (hover over)
Add to the span's CSS
white-space: nowrap
Additionally you can use
text-overflow: ellipsis
to get a nice "..." When the text is cutoff
Give the span(s) a display: block or display: inline-block and set overflow: hidden.