How to use "position: relative" properly in my case? [closed] - html

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 7 years ago.
Improve this question
I have recently started to code again after a long break and now I'm trying hard to see what is it that I'm doing wrong.
I made a JSFiddle here: http://jsfiddle.net/mtsgp3gg/
This is the output I'd like to see: http://puu.sh/jwi3d/233c917986.png
I have a container with 3 images:
<div class="container">
<img src="main picture">
<img id="tape left" src="">
<img id="tape right" src="">
</div>
I would like to put some little "tape thingies" over my main picture using position: relative; and top:0; but so far I failed.
Can anyone point out what I'm doing wrong please?

css position: is somewhat confusing, especially at the start (and it is misused almost 99% of all times).
You use position: relative because you want it to be relative to the container, right? Although this is the obvious behavior, it is not what css does.
position: relative means "I'll give you top/right/... values and want that the element is moved by that amount from where it would occur normally."
You almost always want to use position: absolute which basically means "pick the boundaries of the parent (being specific: the first parent that is not position: static which is the default) and move this element to what I define with top/right/...". (There are more implications like absolute removing the element from the document flow, but that's out of scope at the moment.)
This means you have to
position your container not static. position: relative works fine here, as it does not alter the element if you don't specify top/... .
position your items with position: absolute as they will then be defined relative to their container (not relative to their original position, as they would be with position: relative).
Your example would look like
body {
background: gray;
}
.container {
position: relative;
margin-left: 20px;
margin-top: 20px;
width: 200px;
height: 200px;
background: navy;
}
.container [id] {
position: absolute;
top: -5px;
}
.container #one {
left: -5px;
}
.container #two {
right: -5px;
transform: rotate(90deg);
}
<div class="container">
<img src="http://www.animal-photography.com/thumbs/blue_eyed_white_long_hair_cat_~AP-G3KLBP-TH.jpg">
<img id="one" src="http://fenrir.info.uaic.ro/~elena.chiosa/img/scoci.png">
<img id="two" src="http://fenrir.info.uaic.ro/~elena.chiosa/img/scoci.png">
</div>

You're using position:relative when you should be using position:absolute.
body {
background: gray;
}
.container {
margin-left: 20px;
margin-top: 20px;
width: 200px;
height: 200px;
background: navy;
position: relative;
}
.container #one {
position: absolute;
top: 0;
left: 0;
transform: translate(-25%, -25%)
}
.container #two {
position: absolute;
top: 0;
left: 100%;
transform: translate(-75%, -25%) rotate(90deg);
}
<div class="container">
<img src="http://www.animal-photography.com/thumbs/blue_eyed_white_long_hair_cat_~AP-G3KLBP-TH.jpg" />
<img id="one" src="http://fenrir.info.uaic.ro/~elena.chiosa/img/scoci.png" />
<img id="two" src="http://fenrir.info.uaic.ro/~elena.chiosa/img/scoci.png" />
</div>
That said, I'd prefer not to have presentational images in the HTML at all. So I'd be using pseudo-elements using the same techniques.
body {
background: gray;
}
.container {
margin-left: 20px;
margin-top: 20px;
width: 200px;
height: 200px;
background: navy;
position: relative;
}
.container::before {
position: absolute;
content: '';
width: 20px;
height: 20px;
background-image: url(http://fenrir.info.uaic.ro/~elena.chiosa/img/scoci.png);
top: 0;
left: 0;
transform: translate(-25%, -25%);
z-index: 1;
}
.container::after {
position: absolute;
content: '';
width: 20px;
height: 20px;
background-image: url(http://fenrir.info.uaic.ro/~elena.chiosa/img/scoci.png);
top: 0;
left: 100%;
transform: translate(-75%, -25%) rotate(90deg);
}
<div class="container">
<img src="http://www.animal-photography.com/thumbs/blue_eyed_white_long_hair_cat_~AP-G3KLBP-TH.jpg" />
</div>
In this way, the presentational part is now in the CSS and the class can be re-used without having multiple instances of the tape image cluttering up your HTML.

Try to use position: absolute; instead of position: relative;
.container #one{
position: absolute;
top: 10px;
left:20px;
}
.container #two{
position: absolute;
top: 10px;
left:215px;
transform: rotate(90deg);
}
Demo here

Related

Trying to center text over image HTML [duplicate]

This question already has answers here:
Absolute position is not working
(6 answers)
Closed 10 months ago.
Trying to center text over image in HTML, but my method keeps pushing the text to the very beginning of the page at the top. My method is below...
.info {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.map-p {
font-size: 27pt;
text-align: center;
color: black;
position: relative;
z-index: 1;
}
<div class="map-section">
<img id="map" src="https://img.freepik.com/free-photo/europe-map-pins-travel-your-planning-trip_255544-1467.jpg?w=2000" alt="Image cannot be displayed" />
<div class="info">
<p class="map-p">We have locations all over!</p>
</div>
</div>
because .map-section needs to have position: relative
long answer:
in order for position absolute to use the reference of the parent. the parent needs to have position: relative
so adding
.map-section: { position: relative };
and fix the transform, should be
transform: translate(50%, -50%);
will get what you want
Two things:
1.) Set position: relative to the parent element (.map-section) to make it the reference for the absolutely positioned element.
2.) (optionally?) Limit the size of your image to its container's width and add height: auto to keep the proportion:
#map {
width: 100%;
height: auto;
}
.map-section {
position: relative;
}
.info {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.map-p {
font-size: 27pt;
text-align: center;
color: black;
position: relative;
z-index: 1;
}
<div class="map-section">
<img id="map" src="https://img.freepik.com/free-photo/europe-map-pins-travel-your-planning-trip_255544-1467.jpg?w=2000" alt="Image cannot be displayed" />
<div class="info">
<p class="map-p">We have locations all over!</p>
</div>
</div>
It should be like that
For more info : https://www.w3schools.com/css/css_positioning.asp
.info {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.map-p {
font-size: 27pt;
color: black;
position: relative;
z-index: 1;
}
.map-section{
position: relative;
}

Sticky div in CSS

I would like a div to be pushed down (see "Search while I move the map" in the screenshot), to float above the map, but so that if I use any constants for margin-top or top, then that's relative to the parent div (map), not the browser window.
How can I do so? Website link I have tried to add position: relative; to the parent #map but this is what I get (the map gets hidden):
This is my CSS code:
#map {
#searchCheckboxContainer {
position: absolute;
display: inline-table;
white-space: nowrap;
margin-top: 24px; // sure, this works, but it's 24px *from the browser window*
top: 0px; // any way to make it relative to the parent div (map)?
left: 50%;
transform: translateX(-50%);
background: rgb(255, 255, 255) !important;
}
#searchCheckboxContainer {
height: 40px;
}
}
HTML:
<div id="map" v-cloak>
<div id="searchCheckboxContainer">
<div id="searchCheckbox">
<input id="checkbox" class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Search as I move the map
</label>
</div>
</div>
<div id="mapid"></div>
</div>
Adding as an answer to avoid the comment noise:
All of the #map element children are positioned absolute. So, essentially they aren't in the normal document flow and will not affect the height of the #map div.
You need to add:
position: relative;
height: 100vh (or whatever)
To your #map div.
Then, to your #searchCheckboxContainer, add a z-index: 100 //could be anything but that worked
This will put the box above the map.
I assume you need it to look like that:
In this case you need to modify the following:
#map {
position: relative;
height: calc(100vh - 86px); // The height of header on mobile and you need to add responsive media queries to handle it.
}
#map #searchCheckboxContainer{
position: absolute;
display: inline-table;
white-space: nowrap;
margin-top: 0;
bottom: 0;
left: 0;
top: auto;
transform: none;
background: #ffffff !important;
z-index: 2;
width: 100%;
padding: 15px;
}
#searchCheckbox .form-check-input{
position: relative;
margin-top: 0;
margin-left: 0;
}
#map #mapid{
height: 100%;
width: 100%;
position: absolute;
background-color: yellow;
z-index: 1;
}

Overlay not sizing correctly CSS

I want to place an overlay on my rounded image but when I set it, the overlay doesn't display over the image correctly? It is filling the column div. Not the overlay container. Can the overlay container be made to size to the image inside of it? I have tried display:inline-block;but that doesn't work. I am using Bootstrap.
HTML Code
<div class="row" style="background-color:#ECECEC">
<div class="col-md-4 col-sm-4" >
<div class="overlaycontainer">
<img class="roundimg" src="images/george1x1.jpg" >
<div class="overlay">
<div class="overlaytext">Hello World</div>
</div>
</div>
<center><h3>George Jones <br><small>Owner and Founder</small></h3></center>
</div>
CSS
.overlay{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
border-radius: 50%;
display:inline-block
}
.overlaycontainer{
display:inline-block
}
.overlaycontainer:hover .overlay{
opacity: 1;
}
.overlaytext{
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
.roundimg{
max-width: 75%;
height: auto;
border-radius: 50%;
padding-top:10px;
display: block;
margin: 0 auto;
}
Any help would be appreciated.
Thanks
Joe
I was able to get this working a bit better by making this working demo with a placeholder image I was able to link to.
http://codepen.io/anon/pen/ryYaWx?editors=1100
and then adding position: relative to the .overlaycontainer selector, like this:
.overlaycontainer {
display: inline-block;
position: relative; /* <-- this was added*/
}
This works because you have .overlay set to position: absolute and you want the absolute positioning to be relative to .overlaycontainer instead of the entire page. Adding this line will do that.

Why does applying a CSS filter block the contained link? Can I work around this?

I have tried to create a minimal snippet that demonstrates the situation. The following HTML/CSS creates two boxes, one red and one cyan. Each one contains a clickable link. When I apply a CSS filter (as I have done to create the cyan one), the box is no longer clickable. My best guess is that this has to do with "stacking contexts," but I admit I don't know enough about them.
For the second part of the question, working around this, is there any way I can modify the CSS for the filtered class to avoid this issue? I am running into this in the context of a Chrome extension that applies CSS filters to images, so I would like a solution that does not require modifying the underlying structure of the site (the HTML) or significantly changing the way the site looks. I would consider it particularly useful if there were a solution that could be applied programmatically without introducing risk that other sites will now behave incorrectly.
.filtered {
filter: invert(100%);
}
/* I cannot modify any of the following CSS to solve this. */
div, a {
display: block;
height: 50px; width: 50px;
left: 0; top: 0;
position: absolute;
}
.outer:before {
display: block;
height: 50px; width: 50px;
left: 0px; top: 0;
position: absolute;
content: '';
z-index: 2;
}
.inner {
background: red;
}
.link {
z-index: 2;
}
<div style="position: relative">
<div class="outer">
<div class="inner">
</div>
</div>
</div>
<div style="position: relative">
<div class="outer">
<div class="inner filtered">
</div>
</div>
</div>
You should set the z-index of the .filtered element higher than the z-index of the :before pseudo class:
.filtered {
filter: invert();
z-index: 10;
}
.filtered {
filter: invert();
z-index: 10;
}
/* I cannot modify any of the following CSS to solve this. */
div, a {
display: block;
height: 50px; width: 50px;
left: 0; right; 0; top: 0; bottom: 0;
position: absolute;
}
.outer:before {
display: block;
height: 50px; width: 50px;
left: 0px; right: 0px; top: 0; bottom: 0;
position: absolute;
content: '';
z-index: 2;
}
.inner {
background: red;
}
.link {
z-index: 2;
}
<div class="outer">
<div class="inner filtered">
</div>
</div>

How can I add a "plus sign/icon" to my portfolio shots???

I am trying to add a "plus sign" (its a .png file) to my portfolio section. My goal is to make this "plus sign" visible only when customers are hovering with mouse pointer over my projects but in the same time I want to keep the background-color property which I already set up.
However, my plus sign doesn't show up!? How can I do that???
On this website you can see the similar effect: http://bjorsberg.se/
Here is my JSFiddle: http://jsfiddle.net/L8HX7/
This is a part of my CSS (from JSFiddle) that needs to be fixed:
.plus{
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
margin: -49px 0 0 -56px;
background: url(img/plus.png) center center no-repeat;
}
Here is example of a plus sign I want to add: http://icons.iconarchive.com/icons/visualpharm/icons8-metro-style/512/Very-Basic-Plus-icon.png
Here is a really broken down example.
http://jsfiddle.net/sheriffderek/UVvWm/
CSS
.block {
position: relative; /* so the .plus knows what to be relative to */
display: block;
width: 10em;
height: 10em;
background-color: red;
}
.overlay {
position: absolute;
width: 100%;
height: 100%;
top: 0; left: 0;
}
.block:hover .overlay {
background-color: rgba(0,0,0,.5);
}
.block .plus {
display: none;
}
.block:hover .plus {
display: block;
}
/* to position the .plus */
.plus {
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
HTML
<a href="#"class="block">
<div class="overlay"></div>
<img class="plus" src="http://placehold.it/100x100" />
</a>
You could use an :after psuedo element for the overlay - but I wanted to keep it simple. Keep in mind that CSS declarations read from right to left .... "any .plus - do this, when .block:hover" etc ----
The style obviously has to be applied on hover.
Just replace the background-color in .projectshot a .over:hover{ by the appropriate background. You don’t need the div.plus at all, and neither do you need div.inner (you can remove those from the HTML!):
.projectshot a .over:hover{
position: absolute;
background: url(img/plus.png) center center no-repeat rgba(51, 51, 51, 0.6);
border-radius: 8px;
height: 150px;
width: 200px;
margin: 10px;
}
Here’s the updated Fiddle: http://jsfiddle.net/L8HX7/8/