Responsively Positioning Elements Absolutely over Background - html

I have three elements that I want to keep in the same place as the image responsively shrinks.
.main
{
position: relative;
}
.container
{
display: inline;
}
.point
{
display: inline;
position: absolute;
max-width: 15%;
margin-right: 10px;
padding: 3px 7px 3px 5px;
font-size: 12px;
font-weight: bold;
color: #fff;
background: #ff0000;
border-radius(5px);
box-shadow(1px 2px 5px rgba(0,0,0,0.5));
}
.one
{
top: 40%;
left: 10%;
}
.two
{
top: 50%;
left: 40%;
}
.three
{
top: 75%;
left: 20%;
}
<div class="main">
<div class="container">
<div class="point one">1</div>
<div class="point two">2</div>
<div class="point three">3</div>
</div>
<img src="https://i.ytimg.com/vi/M5SHKCxKDgs/hqdefault.jpg" alt="Husky">
</div>

I believe you want it also to scale as the image scales down responsively, so this achieves that effect.
.wrapper {
position: relative;
display: inline-block;
}
.wrapper img { max-width: 100%; }
.point
{
position: absolute;
max-width: 15%;
margin-right: 10px;
padding: 3px 7px 3px 5px;
font-size: 12px;
font-weight: bold;
color: #fff;
background: #ff0000;
border-radius(5px);
box-shadow(1px 2px 5px rgba(0,0,0,0.5));
}
.one
{
top: 40%;
left: 10%;
}
.two
{
top: 50%;
left: 40%;
}
.three
{
top: 75%;
left: 20%;
}
<div class="main">
<span class="wrapper">
<img src="https://i.ytimg.com/vi/M5SHKCxKDgs/hqdefault.jpg" alt="Husky">
<span class="point one">1</span>
<span class="point two">2</span>
<span class="point three">3</span>
</span>
</div>
I am using inline-block to automatically allow the wrapper element to "wrap" around the image no matter what size the image is. I also set max-width: 100% to turn the image into a responsive image (well, it just scales down when the window resizes). Since the points are all %-based, they stay in the right position as the image scales down.
✔ No requirement to have a fixed width and height image/wrapper, so it's responsive
✔ Less HTML required
✔ Works on pretty much any browser besides unsupported old ones
This is a nice trick I've used to do things like "banners" across images and other techniques to position things over images for effects.

Make your container as position relative and set a height and width on it since the child of your container is absolute. Also make your image absolute positop and top 0. See snippet.
.container
{
width: 480px;
height: 360px;
position: relative;
z-index: 100;
position: relative;
}
.main img{
position:absolute;
top:0;}
.point
{
display: inline;
position: absolute;
max-width: 15%;
margin-right: 10px;
padding: 3px 7px 3px 5px;
font-size: 12px;
font-weight: bold;
color: #fff;
background: #ff0000;
border-radius(5px);
box-shadow(1px 2px 5px rgba(0,0,0,0.5));
}
.one
{
top: 40%;
left: 10%;
}
.two
{
top: 50%;
left: 40%;
}
.three
{
top: 75%;
left: 20%;
}
<div class="main">
<div class="container">
<div class="point one">1</div>
<div class="point two">2</div>
<div class="point three">3</div>
</div>
<img src="https://i.ytimg.com/vi/M5SHKCxKDgs/hqdefault.jpg" alt="Husky">
</div>

Set width and height for your img tag.
.main
{
position: relative;
}
.container
{
display: inline;
}
.point
{
display: inline;
position: absolute;
max-width: 15%;
margin-right: 10px;
padding: 3px 7px 3px 5px;
font-size: 12px;
font-weight: bold;
color: #fff;
background: #ff0000;
border-radius(5px);
box-shadow(1px 2px 5px rgba(0,0,0,0.5));
}
.one
{
top: 40%;
left: 10%;
}
.two
{
top: 50%;
left: 40%;
}
.three
{
top: 75%;
left: 20%;
}
img{
width:100%;
height:100%;
}
<div class="main">
<div class="container">
<div class="point one">1</div>
<div class="point two">2</div>
<div class="point three">3</div>
</div>
<img src="https://i.ytimg.com/vi/M5SHKCxKDgs/hqdefault.jpg" alt="Husky">
</div>

Related

How do I get my elements inline in the top left corner?

Element 2 is an image for the youtube logo. Element 1 is a button with a visual hover effect with three bars stacked on top of each other.
I want the button on the left and the image right next to it.
I need them in the upper left corner.
Here is a screenshot so far https://postimg.cc/Mn2B8wCR
Here is the code I have so far
.element1 {
display: inline-block;
width: 500px;
}
.element2 {
display: inline-block;
width: 500px;
}
.wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.hamburger {
width: 100px;
height: 100px;
position: relative;
cursor: pointer;
background: #636363;
border-radius: 5px;
}
.hamburger>div {
position: absolute;
height: 10px;
background: rgb(255, 255, 255);
border: 5px;
}
.first {
width: 55px;
top: 25px;
left: 20px;
}
.second {
width: 40px;
top: 45px;
left: 20px;
}
.third {
width: 50px;
top: 65px;
left: 20px;
}
.hamburger:hover div {
width: 60px;
transition: width 0.3s ease;
}
<div class="element1">
<div class="wrapper">
<div class="hamburger">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
</div>
</div>
</div>
<div class="element2">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Logo_of_YouTube_%282015-2017%29.svg" alt="Youtube logo" class="logo">
</div>
You can wrap it in a container and use display: flex;.
.nav-container{
display: flex;
width: 100%;
}
.element1{
display: inline-block;
width: 120px;
}
.wrapper{
padding-left: 10px;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.hamburger{
width: 100px;
height: 100px;
position: relative;
cursor: pointer;
background: #636363;
border-radius: 5px;
}
.hamburger > div{
position: absolute;
height: 10px;
background: rgb(255, 255, 255);
border: 5px;
}
.first{
width: 55px;
top: 25px;
left: 20px;
}
.second{
width: 40px;
top: 45px;
left: 20px;
}
.third{
width: 50px;
top: 65px;
left: 20px;
}
.hamburger:hover div{
width: 60px;
transition: width 0.3s ease;
}
.element2{
display: inline-block;
width: 500px;
}
<div class="nav-container">
<div class="element1">
<div class="wrapper">
<div class="hamburger">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
</div>
</div>
</div>
<div class="element2">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Logo_of_YouTube_%282015-2017%29.svg" alt="Youtube logo" class="logo">
</div>
</div>
In HTML, wrap all of element 1 and 2 with a 'container' div:
<div class="container">
<div class="element1">
<div class="wrapper">
<div class="hamburger">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
</div>
</div>
</div>
<div class="element2">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Logo_of_YouTube_%282015-2017%29.svg" alt="Youtube logo" class="logo">
</div>
</div>
In CSS, make div container flex and into a row.
.container {
display: flex;
flex-direction: row;
}
Change your positions of your hamburger and wrapper class:
.wrapper{
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.hamburger{
width: 100px;
height: 100px;
cursor: pointer;
background: #636363;
border-radius: 5px;
}
Is this you want to do?
Edit:
Here I done below changes:
Removed all styles of the .wrapper which don't need there
Added vertical-align: middle for .element1 and .element2
Removed width from .element1 which don't need there
Added margin-left to .element2 for space
.element1{
display: inline-block;
vertical-align: middle
}
.element2{
margin-left: 30px;
display: inline-block;
vertical-align: middle
}
.hamburger{
width: 100px;
height: 100px;
position: relative;
cursor: pointer;
background: #636363;
border-radius: 5px;
}
.hamburger > div{
position: absolute;
height: 10px;
background: rgb(255, 255, 255);
border: 5px;
}
.first{
width: 55px;
top: 25px;
left: 20px;
}
.second{
width: 40px;
top: 45px;
left: 20px;
}
.third{
width: 50px;
top: 65px;
left: 20px;
}
.hamburger:hover div {
width: 60px;
transition: width 0.3s ease;
}
.logo {
width: 300px
}
<div class="element1">
<div class="wrapper">
<div class="hamburger">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
</div>
</div>
</div>
<div class="element2">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Logo_of_YouTube_%282015-2017%29.svg" alt="Youtube logo" class="logo">
</div>
you can use this
.container {
display:inline-block;
margin-right:auto }
/* the margin right auto is to force the elements to be on the left corner */ps it should be only the imge and the button inside this div(class='container')

Absolutely positioned black screen covers the text of a div but not the image and I don't understand why [duplicate]

This question already has answers here:
Position absolute but relative to parent
(5 answers)
Closed 2 years ago.
As you can see in the code below, the absolutely positioned black screen div covers the members's username but not the actual image. The image stays over the black screen as if it has high z-index, but it doesn't. Is there any way to make the black screen cover the image without completely rewriting the CSS?
.members {
width: 239px;
background-color: #2f3136;
}
.member {
display: flex;
align-items: center;
padding: 30px 20px 30px 20px;
}
.member-image-container {
position: relative;
margin-right: 10px;
}
.member-image {
width: 32px;
height: 32px;
border-radius: 50%;
}
.member-username {
font-weight: 500;
color: #8e9297;
margin-right: 10px;
}
.crown {
color: #faa61a;
}
.online-indicator,
.offline-indicator
{
width: 10px;
height: 10px;
border-radius: 50%;
position: absolute;
bottom: 0;
right: 0;
}
.online-indicator {
background-color: #43b581;
}
.offline-indicator {
background-color: #747f8d;
}
.black-screen {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.8);
}
<div class='navigation'>
<div class='modal'>
<div class="black-screen"></div>
</div>
</div>
<div class='server'>
<div class="members">
<div class="member">
<div class="member-image-container">
<img class='member-image' src="https://api.adorable.io/avatars/100/BozhidarKabzamalov">
<div class='online-indicator'></div>
</div>
<span class='member-username'>Username</span>
</div>
</div>
</div>
Just add position: relative; in member-username
Hope it will work.
.members {
width: 239px;
background-color: #2f3136;
}
.member {
display: flex;
align-items: center;
padding: 30px 20px 30px 20px;
}
.member-image-container {
position: inherit;
margin-right: 10px;
}
.member-image {
width: 32px;
height: 32px;
border-radius: 50%;
}
.member-username {
font-weight: 500;
color: #8e9297;
margin-right: 10px;
position: inherit;
}
.crown {
color: #faa61a;
}
.online-indicator,
.offline-indicator
{
width: 10px;
height: 10px;
border-radius: 50%;
position: absolute;
bottom: 0;
right: 0;
}
.online-indicator {
background-color: #43b581;
}
.offline-indicator {
background-color: #747f8d;
}
.black-screen {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.8);
}
<div class='navigation'>
<div class='modal'>
<div class="black-screen"></div>
</div>
</div>
<div class='server'>
<div class="members">
<div class="member">
<div class="member-image-container">
<img class='member-image' src="https://api.adorable.io/avatars/100/BozhidarKabzamalov">
<div class='online-indicator'></div>
</div>
<span class='member-username'>Username</span>
</div>
</div>
</div>

z-index positioning of pseudo element

I can't seem to figure out how to get the line positioned underneath the circles. Z-index isn't working and I've got the divs positioned non-static. How can I accomplish this? The idea would be that the portion of the line that overlaps the circles would be positioned behind the circles.
div.step-line {
margin: 0;
}
div.step-col {
padding: 30px 0;
text-align: center;
}
div.step-line div.step-title:before {
right: 50%;
}
div.step-line div.step-number {
font-size: 26px;
border-radius: 50%!important;
display: inline-block;
margin: auto;
padding: 9px;
border: 3px solid #e5e5e5;
position: relative;
z-index: 5;
height: 60px;
width: 60px;
/*text-align: center;*/
}
div.step-line div.step-title {
position:relative;
font-size: 20px;
font-weight: 400;
text-transform: uppercase;
}
div.step-line div.step-title:after {
content: '';
height: 3px;
width: 100%;
position: absolute;
background-color: #e5e5e5;
z-index: 4;
transform: translateY(-100%);
top: -90%;
left: 0%;
}
div.step-line div.step-col.first div.step-title:after {
width: 50%;
left: 50%;
}
div.step-line div.step-col.last div.step-title:after {
width: 50%;
right: 50%;
}
<div class="row step-line">
<div class="col-md-4 step-col first">
<div class="step-number">1</div>
<div class="step-title">This is a step title</div>
<div class="step-content">This is a description</div>
</div>
<div class="col-md-4 step-col">
<div class="step-number">2</div>
<div class="step-title">This is a step title</div>
<div class="step-content">This is a description</div>
</div>
<div class="col-md-4 step-col last">
<div class="step-number">3</div>
<div class="step-title">This is a step title</div>
<div class="step-content">This is a description</div>
</div>
</div>
What i understand from your question is, you want your lines to be behind those circles, just add background color background-color: white; in your CSS
Final code:
div.step-line div.step-number {
font-size: 26px;
border-radius: 50%!important;
display: inline-block;
margin: auto;
padding: 9px;
border: 3px solid #e5e5e5;
position: relative;
z-index: 5;
height: 60px;
width: 60px;
/*text-align: center;*/
background-color: white;
}

Horizontal line in the middle of divs

I want to make a line in the middle of the divs. In the following image, the line should be in the middle of the red boxes.
I'm trying to do that using the line height, but not able to.
Here's the code:
HTML/CSS:
.wrap {
text-align: center;
margin: 20px;
}
.links {
padding: 0 10px;
border-top: 1px solid #000;
height: 1px;
line-height: 0.1em;
}
.dot {
width: 20px;
height: 20px;
background: red;
float: left;
margin-right: 150px;
position: relative;
top: -10px;
}
<div class="wrap">
<div class="links">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
Demo:
https://jsfiddle.net/nkq468xg/
You can use Flexbox on links and for line you can use :before pseudo-element on wrap element.
.wrap {
text-align: center;
margin: 20px;
position: relative;
}
.links {
padding: 0 10px;
display: flex;
justify-content: space-between;
position: relative;
}
.wrap:before {
content: '';
position: absolute;
top: 50%;
left: 0;
border-top: 1px solid black;
background: black;
width: 100%;
transform: translateY(-50%);
}
.dot {
width: 20px;
height: 20px;
background: red;
}
<div class="wrap">
<div class="links">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
Here's one where the line is actually on top, but it does add another element to the HTML:
https://jsfiddle.net/nkq468xg/2/
.wrap {
text-align: center;
margin: 20px;
}
.links {
height: 20px;
position: relative;
}
hr {
border: 0;
height: 1px;
background: black;
position: absolute;
top: 1px;
width: 100%;
}
.dot {
width: 20px;
height: 20px;
background: red;
float: left;
margin-right: 150px;
}
<div class="wrap">
<div class="links">
<hr>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
You can use pseudo element, like ::after
.links {
padding: 0 10px;
overflow: auto; // Your div will have the height of the overflowing elements
}
.links::after {
content: '';
width: 100%;
height: 1px;
background: black;
display: block;
position: relative;
top: 10px;
}
Check your code snippet in your question here on SO ("Run code snippet" blue button), is that what you need?
Added position: relative; top: -10px; in your code for .dot.
.dot {
position: relative;
top: -10px;
}
Fiddle: https://jsfiddle.net/nkq468xg/3/

An Inline Relative Div with Absolute Divs In It

The following is my markup:
.play-controls {
.fa-play, .fa-pause {
font-size: 25px;
}
}
.volume-controls {
display: inline-block;
position: relative;
.overlay {
background-color: $highlight;
height: 10px;
border-radius: 5px;
width: 0px;
position: absolute;
z-index: 15;
}
.background {
background-color: $text-color;
width: 100px;
height: 10px;
border-radius: 5px;
position: absolute;
z-index: 10;
}
.circle {
border-radius: 100%;
position:absolute;
background-color: #fff;
border: 1px solid;
height: 15px;
width: 15px;
z-index: 20;
top: -3px;
}
}
.player {
#album-artwork {
width: 80px;
height: 80px;
vertical-align: middle;
display:inline-block;
margin-right: 10px;
}
.wrapper {
display:inline-block;
.information {
margin-bottom: 5px;
#song-title {
font-size: 22px;
font-weight:bold;
margin-right: 5px;
}
#artist-album {
font-size: 18px;
}
}
.progress-bar {
position: relative;
.overlay {
background-color: $highlight;
height: 10px;
border-radius: 5px;
width: 0px;
position: absolute;
z-index: 15;
}
.background {
background-color: $text-color;
width: 600px;
height: 10px;
border-radius: 5px;
position: absolute;
z-index: 10;
}
.circle {
border-radius: 100%;
position:absolute;
background-color: #fff;
border: 1px solid;
height: 15px;
width: 15px;
z-index: 20;
top: -3px;
}
}
}
}
<div class="play-controls">
<i class="fa fa-play" id="playpause"></i>
</div>
<div class="volume-controls">
<div class="background"></div>
<div class="circle"></div>
<div class="overlay"></div>
</div>
<div class="player">
<img id="album-artwork" src="build/images/guero.jpg">
<div class="wrapper">
<div class="information">
<span id="song-title">Go It Alone</span>
<span id="artist-album">Beck - Guero</span>
</div>
<div class="progress-bar">
<div class="background"></div>
<div class="circle"></div>
<div class="overlay"></div>
</div>
</div>
</div>
The divs with classes background, circle, and overlay in volume-controls are all position: absolute; with volume-controls as position: relative;.
Upon making play-controls, volume-controls, and player inline, play-controls is inline with volume-controls, but volume-controls is overlapping the player.
How would I be able to set everything in one line, without any overlapping?
EDIT: JSFiddle
You could float:left; the 3 main parts or display:inline-block; them the issue the player is over the volume-controls is because of the absolute positioned elements in the volume-controls. You could add a width to volume-controls.
.volume-controls {
display: inline-block;
position: relative;
width:150px;
}
Here is the fiddle