Image over image, middle position - html

This is my CSS class:
.overitem {
position: relative;
background: url(images/bg.png) no-repeat;
width:83px;
height: 83px;
}
.sit {
position: absolute;
top: 50%;
left: 50%;
}
My HTML :
<div class="overitem"><img src="/images/vcs.png" class="sit"/></div>
The problem is that I can not make the from the img tag, in a middle of the background of overitem class. What should I do, to make it appear in the middle of the bg.png image?

Since the .sit class has to have a fixed width and height (because its an image), you can use the following method:
.sit {
position: absolute;
top: 50%;
left: 50%;
width: 500px;
height: 500px;
margin-top: -250px; /* Half the height */
margin-left: -250px; /* Half the width */
}
Source: http://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/

you can set margins as
1/2(width of "overitem" minus width of "sit") and
1/2(height of "overitem" height width of "sit")
or try this:
.sit {
position: absolute;
top: 50%;
left: 50%;
margin-left: -(HALF_OF_WIDTH_OF_IMAGE);
margin-top: -(HALF_OF_HEIGHT_OF_IMAGE);
}

In the .sit class you need to have:
margin-left: -(half of width);
margin-top: -(half of height);
An alternative option is:
.overitem {
position: relative;
background: url(images/bg.png) no-repeat;
width:83px;
height: 83px;
line-height: 83px;
}
.sit {
vertical-align: middle;
}

Try this:
elems = document.getElementsByClassName('sit');
for( i = 0, j = elems.length; i < j; i++){
elems[ i ].style.marginLeft = ( elems[ i ].parentNode.offsetWidth - elems[ i ].offsetWidth ) / 2;
elems[ i ].style.marginTop = ( elems[ i ].parentNode.offsetHeight - elems[ i ].offsetHeight ) / 2;
}

Related

Setting background size in circle on image hover

I am making a hover effect on the image. I set the image to grayscale by default and when it hovered over, a circle follows the cursor and shows the colored part. Basically, there are two images. grayscale one is shown by default and on hover inside the circle, the colored part is shown.
Everything is working good except when I try to size the image using background-size the circle part doesn't follow. As the background property sets the circle part image according to its size. See the code:
I set the background-size of video card to 100% to fill up its parent container but when I do it for the circle, the image is sized inside the circle.
$('.video-card').mousemove(function(e) {
var offs = $(this).offset(),
p = {
x: offs.left,
y: offs.top
},
mPos = {
x: e.pageX,
y: e.pageY
},
x = mPos.x - p.x - 75,
y = mPos.y - p.y - 75;
$('.gray', this).css({
left: x,
top: y,
backgroundPosition: -x + 'px ' + -y + 'px'
});
});
.video-card {
position: relative;
width: 100%;
height: 950px;
overflow: hidden;
background-size: 100% !important;
}
.video-card-overlay {
content: "";
top: 0;
left: 0;
width: 100%;
height: 100%;
position: absolute;
background: gray;
filter: grayscale(100%);
background-size: 100% !important;
}
.gray {
position: absolute;
top: 0;
left: 0;
width: 150px;
height: 150px;
display: none;
border-radius: 50%;
}
.video-card:hover>.gray {
display: block;
}
.video-gallery-section .container {
max-width: 100%;
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="video-card" style="background: url('assets/img/home/1.png') no-repeat">
<div class="video-card-overlay" style="background: url('assets/img/home/1.png') no-repeat"></div>
<div class="gray" style="background: url('assets/img/home/1.png') no-repeat"></div>
</div>
How about using clip-path instead of trying to achieve the same effect through positioning?
const $overlay = $('.video-card-overlay');
$('.video-card').mousemove(function (e) {
$overlay.css({
clipPath: `circle(150px at ${e.offsetX}px ${e.offsetY}px)`
})
});
.video-card {
height: 950px;
position: relative;
width: 100%;
}
.gray,
.video-card-overlay {
background-image: url('assets/img/home/1.png');
background-position: center center;
background-size: cover;
inset: 0;
position: absolute;
}
.gray {
filter: grayscale(100%);
}
.video-card:not(:hover) .video-card-overlay {
display: none;
}
<div class="video-card">
<div class="gray"></div>
<div class="video-card-overlay"></div>
</div>
See how much shorter the code became!

Align DIV container to bottom of page with variable height element in it

I am trying to position an element on a page so that it is always spaced away from other control elements on the page. I want this element to be positioned anywhere on the page, depending on other control elements on the page, what action the user has taken etc. These are the eight valid positions:
TOP_CENTER
TOP_LEFT
TOP_RIGHT
LEFT_CENTER
RIGHT_CENTER
BOTTOM_CENTER
BOTTOM_LEFT
BOTTOM_RIGHT
What I have so far are 3 component elements: 1) a DIV container, 2) an image which should always be at the top of the DIV container, 3) some text which should always be at the bottom of the DIV container. My CSS looks as follows:
#floatingElement {
left: 60px;
top: 11%;
width: 25%;
height: 25%;
position: absolute;
pointer-events: none;
z-index: 1;
}
#floatingElement img {
display: block;
margin: auto;
max-width: 95%;
max-height: 100%;
}
#floatingElement div {
text-align: center;
background: rgba(255,255,255,0.5);
border-radius: 5%;
box-shadow: 3px 3px 2px #888888;
font-size: 2vmax;
}
The above works perfectly whenever the element is positioned "TOP" or "CENTER". However, "BOTTOM" positioning is causing problems, I think because the container DIV is somehow not taking into account its height based on the amount of text in the 3rd (text DIV) element.
How can I make it so that the element can always align to the bottom of the page whenever a "BOTTOM" position is chosen, so that the container DIV includes the height of the text DIV (which can vary depending on the amount of text in that DIV), similar to this image:
The black border is my window, the red box is my image and the green box is the text DIV. Both the red and green boxes are inside my container DIV.
Upon being altered using bottom, the scenario you want is not working as expected because you specified the height of #floatingElement to be fixed (in your case, 25%). Having a fixed value relative to the container and not considering the height of the text div, the floating element's position will definitely seem off when altered using bottom. This is because bottom places the element based on the bottom-most pixel of your element's height, which in your case may be smaller than enough to cover your whole text div's and image's height. To see what I mean, do try removing the comment on height below and inspect the element of #floatingElement.
Here's a working example, simply click the button to adjust the div's position (try using full-page mode when running as the window is a tad small).
const button = document.querySelector('.adjustPosition')
const addText = document.querySelector('.addText')
const elem = document.querySelector('#floatingElement')
const textDiv = elem.querySelector('.textDiv')
button.addEventListener('click', e => {
if (elem.classList.contains('topLeft')) {
elem.classList.add('topCenter')
elem.classList.remove('topLeft')
} else if (elem.classList.contains('topCenter')) {
elem.classList.add('topRight')
elem.classList.remove('topCenter')
} else if (elem.classList.contains('topRight')) {
elem.classList.add('rightCenter')
elem.classList.remove('topRight')
} else if (elem.classList.contains('rightCenter')) {
elem.classList.add('bottomRight')
elem.classList.remove('rightCenter')
} else if (elem.classList.contains('bottomRight')) {
elem.classList.add('bottomCenter')
elem.classList.remove('bottomRight')
} else if (elem.classList.contains('bottomCenter')) {
elem.classList.add('bottomLeft')
elem.classList.remove('bottomCenter')
} else if (elem.classList.contains('bottomLeft')) {
elem.classList.add('leftCenter')
elem.classList.remove('bottomLeft')
} else if (elem.classList.contains('leftCenter')) {
elem.classList.add('topLeft')
elem.classList.remove('leftCenter')
}
})
addText.onclick = () => {
textDiv.innerText += "Added some more text so that the div can be larger"
}
html,
body {
height: 100%;
width: 100%;
margin: 0;
}
#floatingElement {
width: 25%;
/* height: 25%; This causes the height to be fixed */
position: absolute;
pointer-events: none;
z-index: 1;
}
#floatingElement img {
display: block;
width: 100%;
height: 100px;
margin: auto;
object-fit: cover;
}
#floatingElement .textDiv {
text-align: center;
background: rgba(255, 255, 255, 0.5);
border-radius: 5%;
box-shadow: 3px 3px 2px #888888;
}
.adjustPosition {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 25px;
height: 25px;
background: #12121299;
border-radius: 50%;
cursor: pointer;
}
.addText {
position: absolute;
top: 60%;
left: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
}
.topLeft {
top: 10px;
left: 10px;
}
.topCenter {
top: 10px;
left: 50%;
transform: translateX(-50%);
}
.topRight {
top: 10px;
right: 10px;
}
.leftCenter {
top: 50%;
left: 10px;
transform: translateY(-50%);
}
.rightCenter {
top: 50%;
right: 10px;
transform: translateY(-50%);
}
.bottomLeft {
bottom: 10px;
left: 10px;
}
.bottomRight {
bottom: 10px;
right: 10px;
}
.bottomCenter {
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
<div id="floatingElement" class="topLeft">
<img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png">
<div class="textDiv">Variable height here because it has a long text</div>
</div>
<div class="adjustPosition"></div>
<div class="addText">Add Text</div>

How To Place Buttons Around A Circle with HTML5 and CSS 3?

I want to arrange 4 buttons (or bootstrap tabs,doesn't matter) around a circle and load a content in the middle of that circle,something like this:
How can i do that?
you could use a small bit of jquery to load your center circle with the text you want.
$(document).ready(function() {
$('.quart').click(function() {
var ind = $(this).index();
switch (ind) {
case 0:
var tex = "div 1";
break;
case 1:
var tex = "div 2";
break;
case 2:
var tex = "div 3";
break;
case 3:
var tex = "div 4";
break;
}
$('.center').text(tex);
});
});
.wrap {
height: 300px;
width: 300px;
border-radius: 50%;
position: relative;
overflow: hidden;
}
.quart {
position: absolute;
height: 50%;
width: 50%;
background: tomato;
transition: all 0.4s;
}
.quart:first-child {
top: 0;
left: 0;
}
.quart:nth-child(2) {
top: 0;
left: 50%;
}
.quart:nth-child(3) {
top: 50%;
left: 0;
}
.quart:nth-child(4) {
top: 50%;
left: 50%;
}
.center {
height: 80%;
width: 80%;
position: absolute;
top: 10%;
left: 10%;
background: lightgray;
border-radius: 50%;
text-align: center;
line-height: 160px;
}
.quart:hover {
background: dimgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
<div class="quart"></div>
<div class="quart"></div>
<div class="quart"></div>
<div class="quart"></div>
<div class="center">Click My non-existent Corners!</div>
</div>
This probably won't be as efficient as an svg solution, but colud be altered to your needs.
You can use the HTML <map> tag - more info

Full page rotating rays

I have tried to make rotating rays but when it rotate you can see the rays is not on full screen it has empty area from the right and left
you can see the result and code from here: JSFiddle
<style>
#me {
-webkit-animation: rotation 5s infinite linear;
}
#-webkit-keyframes rotation {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(359deg);}
}
.win-boxx-container{
z-index:1111111111111111111;
position: fixed;
width:100%;
height:100%;
background-size:100%;
left:0;
top:0;
background: rgba(0, 0, 0, 0.75);
}
.win-lights-bg {
width: 100%;
height: 100%;
left: 0%;
top: 0%;
left: -59%;
top: -57%;
position: absolute;
z-index:11111111111111111112;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 810px -926px;
}
.win-boxx-box{
position:absolute;
margin:auto;
background:url('images/win-boxx.png');
background-size:100%;
background-repeat: no-repeat;
width: 572px;
height: 337px;
text-align:center;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index:1111111111111111111123;
}
.centering{
text-align:center;
}
body{
position:fixed;
}
#media all and (max-width: 1000px) and (min-width: 520px) {
.win-boxx-box{
width: 352px;
height: 227px;
}
.win-boxx-stars{
width: 50%;
}
}
#media all and (max-width: 550px) and (min-width: 220px) {
.win-boxx-box{
width: 352px;
height: 227px;
}
.win-boxx-stars{
width: 50%;
}
}
.win-lights-bg{
width: 100%;
height: 100%;
left: 0;
top: 0;
position: absolute;
z-index: 0;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 810px -926px;
}
</style>
<div class="win-boxx-container" >
<img id="me" src="http://store1.up-00.com/2015-01/1421959145661.png" class="win-lights-bg" />
</div>
You have a lot of overlapping, unnecessary CSS in your code.
What I did here is create a fucntion that determines the size your spinner should be based on the current window size and sets it up in vanilla js (javascript).
JSFIDDLE DEMO
Script:
function setUpSpinner() {
//get height and width and the spinning element and assign them to variables
var height = window.innerHeight;
var width = window.innerWidth;
var winlightsbg = document.getElementsByClassName('win-lights-bg')[0];
determine the largest and the adjustment factor (I used 1.5 to accomodate the corners when rotating - you may be able to finesse this a bit)
var largest;
if (height > width) {
largest = height;
} else {
largest = width;
}
var adjust = largest * 1.5;
//set up new height, width and offsets
winlightsbg.style.height = adjust + "px";
winlightsbg.style.width = adjust + "px";
winlightsbg.style.left = -((adjust - largest) / 2) + "px";
winlightsbg.style.top = -((adjust - largest) / 2) + "px";
}
//on page load run our spinner set up function once then bind it to the window resize event
window.onload = function() {
setUpSpinner();
window.addEventListener('resize', setUpSpinner, false);
};
CSS:
.win-lights-bg {
width: 100%;
height: 100%;
position: absolute;
z-index:100;
}

CSS transparent overlay over Google Maps

There are the following code:
<div class="google-wrapper">
<div id="google-map"></div>
<div id="google-map-overlay">
<p>Loading...</p>
</div>
</div>
I want that google-map-overlay is over google-map and has red color and I could see google-map, i.e. google-map-overlay should be transparent div. Some styles:
.google-wrapper {
position: relative;
}
#google-map {
width : 500px;
height : 380px;
}
#google-map-overlay {
width : 500px;
height : 380px;
background: red;
position: absolute;
top: 0px;
left: 0px;
z-index: 99;
}
But I don't know how I can do a transparent overlay. Thanks in advance.
you need add opacity like this DEMO
#google-map-overlay {
width : 500px;
height : 380px;
background: red;
position: absolute;
opacity: 0.5;/*add this*/
top: 0px;
left: 0px;
z-index: 99;
}
it offhand, I would like to see exactly what you want to eventually get