I render a div with an image that looks like it's a video (but it's just an image, for performance). When the user clicks on the image, JS swaps out the image with the appropriate youtube embed code and plays the video.
Below the image/video div, I have another div with some action icons (share, like etc). Rather than have the actions below the image/video div, I'd like to put them over the image/video div on:hover. See below image for better description:
Here's the current structure of my code.
The HTML:
<div class="youtube-container col-sm-6 col-lg-4">
<div class="youtube-player" data-id="<%= trailer.youtube_id %>">
<div class="holder">
</div>
</div>
</div>
<div class="row icons">
<div class="icon">
<img src="icon1">
</div>
<div class="icon">
<img src="icon2">
</div>
<div class="icon">
<img src="icon3">
</div>
</div>
CSS:
.icons img {
height: 35px;
margin: -10px 0 0 5px;
}
.icons {
float:left;
margin: 30px 10px 10px 10px;
}
.icon img {
height: 55px;
}
.youtube-container {
display: block;
margin: 20px auto;
width: 100%;
width: 100%;
}
.youtube-player {
display: block; width: 100%; /* assuming that the video has a 16:9 ratio */
padding-bottom: 56.25%;
overflow: hidden;
position: relative;
width: 100%;
height: 100%;
cursor: hand;
cursor: pointer;
display: block;
}
img.youtube-thumb {
bottom: 0;
display: block;
left: 0;
margin: auto;
max-width: 100%;
width: 100%;
position: absolute;
right: 0;
top: 0;
height: auto
}
div.play-button {
height: 72px;
width: 72px;
left: 50%;
top: 50%;
margin-left: -36px;
margin-top: -36px;
position: absolute;
background: url("http://i.imgur.com/TxzC70f.png") no-repeat;
}
#youtube-iframe { <--- the iframe dynamically generated when someone clicks the image
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
After the page is loaded, Javascript goes through and 1) finds all the .holder divs 2) inserts a dynamically generated image 3) Adds an on.click event listener to .holder that, when clicked, swaps the image for a youtube iframe.
I understand how to implement the "on hover" part, by I need help with CSS and overlaying the icons on top of the image/video.
Related
I want to put the iframe inside a phone image. Phone image is .png and has a transparent background.
I don't know how to mask iframe and make it visible only inside the phone image.
.phone {
display: block;
position: relative;
right: 0;
bottom: 0;
float: right;
border: 1px solid red;
}
.phone > img {
width: 250px;
position: absolute;
}
.wrapper {
position: absolute;
top: 0;
}
<div class="phone">
<img src="https://i.ibb.co/y8HRgg4/phone-front.png">
<div id="wrapper">
<div class="phone view_2" id="phone_1">
<iframe src="https://onsen.io/samples/" id="frame_1"></iframe>
</div>
</div>
</div>
Here's a start. I've applied border radius to mask the iframe's corners, disabled pointer events on the image, and set scroll on the iframe's x-axis. Notice also that the image falls after the iframe in the document so it appears over the top of the iframe.
.wrapper {
display: inline-block;
position: relative;
width: 250px;
height: 600px;
overflow: hidden;
}
.wrapper > img, .wrapper > iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.wrapper > iframe {
overflow-x: auto;
border-radius: 60px;
}
.wrapper > img {
pointer-events: none; /* stop click & scroll on the image */
}
<div class="phone">
<div class="wrapper">
<iframe src="https://onsen.io/samples/" id="frame_1"></iframe>
<img src="https://i.ibb.co/y8HRgg4/phone-front.png">
</div>
</div>
try this
<iframe src="https://i.ibb.co/y8HRgg4/phone-front.png" frameborder="0"></iframe>
I have this image currently. The circle, the close image, and get your quotes image. (see below)
Correct image layout
I can't get all elements to be fixed together so when I change the size of the screen the elements move together instead of separately across the page. (see below)
Incorrect image layout
I have looked into the positioning of each element, but I maybe misunderstanding something, or my code is messy. Please see below:
HTML:
<body>
<!--Thanks for visiting image-->
<div id="thanks-for-visiting-img-bg" class="thanks-for-visiting-bg">
<div id="thanks-for-visiting-img-container" class="thanks-for-visiting-img-container">
<img src="../images/thanks_for_visiting_img.png" alt="Thanks for visiting" id="thanks-for-visiting-img" class="thanks-for-visiting-img">
</div>
</div>
<!--Get Quotes button-->
<div>
<img src="../images/btn.png" alt="Get Your Quotes" id="get-quotes-btn" class="get-quotes-btn">
</div>
<!--Close button-->
<div>
<img src="../images/close_green.png" alt="Close Thanks For Visiting image" id="close-btn" class="close-btn">
</div>
</body>
</html>
CSS:
/*CONTAINER*/
.thanks-for-visiting-img-container {
width: 550px;
height: 550px;
z-index: 2147483647;
left: 0px;
right: 0px;
margin: auto;
top: 0px;
bottom: 0px;
display: block;
outline: none;
max-width: none;
max-height: none;
position: fixed !important;
cursor: default !important;
}
/*IMAGE BACKGROUND*/
.thanks-for-visiting-bg {
display: block;
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
/*z-index: 2147483647;*/
overflow: auto;
background-color: rgb(0, 0, 0);
opacity: 0.83;
}
/*-- GET QUOTES BUTTON --*/
.get-quotes-btn {
cursor: pointer;
position: absolute;
background-color: rgba(0, 0, 0, 0);
background-repeat: no-repeat;
background-size: cover;
width: 288px;
height: 49px;
border-radius: 0px;
z-index: 2;
right: 0px;
bottom: auto;
left: -20px;
top: 425px;
margin: auto;
display: block;
}
.close-btn {
cursor: pointer;
position: absolute;
right: 50%;
bottom: 100px;
left: 65%;
top: 80px;
}
.thanks-for-visiting-img-container and .thanks-for-visiting-bg has to be fixed because the bg class' CSS doesn't work without it and the img container has to be fixed because the page will need to scroll underneath it. Can anyone help?
You can put all the div tags (Get Quotes, the image and the close button) in another div tag and then, add this CSS class to that new div tag:
.mainClass {
display: flex;
flex-direction: column;
}
I am currently developing a website where the user should be able to scroll horizontally through a landscape with clickable info points on it.
The webite is required to be fully responsive and to ensure this I want to put my landscape image and the info points in a container with the exact equal size of the image.
HTML:
<div id="container-main">
<div id="landscape">
<img class="background" src="image.jpg" />
<div class="point" style="top: 24%; left: 7.5%;"></div>
<div class="point" style="top: 29%; left: 17.7%;"></div>
<div class="point" style="top: 77%; left: 39%;"></div>
<div class="point" style="top: 26%; left: 68%;"></div>
<div class="point" style="top: 70%; left: 80%;"></div>
</div>
</div>
CSS:
html {
height: 100%;
}
body {
height: 100%;
min-width: 100%;
margin: 0;
padding: 0;
}
#container-main {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow-y: hidden;
}
#landscape {
position: relative;
display: inline-block;
height: 100%;
}
#landscape > img.background {
height: 100%;
display: block;
}
.point {
position: absolute;
width: 50px;
height: 50px;
margin: -25px 0 0 -25px;
background-color: white;
border-radius: 5px;
cursor: pointer;
}
My CSS works completetly fine until you resize the height of the browser window, then the info points move to wrong places.. however when you refresh the resized website it is working properly again.
Try it: Fiddle (resize the output and then click "run" again.)
In my real project I am setting the container's width to the image's width using javascript but I would love to have a clean CSS solution.
I know there are similar questions around but none of the suggested solutions works out for me.
Thank you in advance!
use vw insted of %
Adjust the code below:
<div id="container-main">
<div id="landscape">
<img class="background" src="image.jpg" />
<div class="point" style="top: 24vw; left: 7.5vw;"></div>
<div class="point" style="top: 29vw; left: 17.7vw;"></div>
<div class="point" style="top: 77vw; left: 39vw;"></div>
<div class="point" style="top: 26vw; left: 68vw;"></div>
<div class="point" style="top: 70vw; left: 80vw;"></div>
</div>
</div>
<style>
html {
height: 100%;
}
body {
height: 100%;
min-width: 100%;
margin: 0;
padding: 0;
}
#container-main {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow-y: hidden;
}
#landscape {
position: relative;
display: inline-block;
height: 100%;
}
#landscape > img.background {
height: 100%;
display: block;
}
.point {
position: absolute;
width: 50px;
height: 50px;
margin: -25px 0 0 -25px;
background-color: white;
border-radius: 5px;
cursor: pointer;
}
</style>
.....................Another solution.........................................................
change css:
html {
height: 100%;
}
body {
height: 100%;
min-width: 100%;
margin: 0;
padding: 0;
}
#container-main {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow-y: hidden;
}
#landscape {
position: relative;
display: inline-block;
width: 100%;
}
#landscape > img.background {
height: 100%;
display: block;
}
.point {
position: absolute;
width: 50px;
height: 50px;
margin: -25px 0 0 -25px;
background-color: white;
border-radius: 5px;
cursor: pointer;
}
NB: See the responsive coding standard of this site:
First off, never use position:fixed; on css unless you want the div or container to stay at the same place even when you scroll.
Second, i would recommend to use jquery to make a fluid layout. What a fluid layout does is, it keeps the content of the page in the same place even when you resize the website. If you dont want to waste time on writing a long script file, i would personally recommend you to use Dreamweaver. Dreamweaver updated their scripts to give the users easy way to make a fluid layout just by clicking some buttons. It will auto generate the script file and css for you. And if you're a manual coder like myself, than simply use dreamweaver to generate a fluid layout and start coding manually.
Hope this helps :)
It works fine on Desktop because I use width: 100%. But on Mobile I do width: 50% and try to center it.
I have tried setting the img.youtube-thumb to margin-left: auto; and margin-right: auto; I've also tried setting the images parent element to text-align: center; and having the image displayed as inline-block, and that doesn't work either.
I'm not sure what else to try, as it seems that every S.O answer I run into provides one of the two above solutions.
Here's a CodePen but I'm not sure how valuable it'll be since I cant seem to get it exact without pasting the entire project into the pen. http://codepen.io/anon/pen/yOEQKg
Here's the HTML for one of the images:
<section class="container-fluid">
<div class="section group">
<div class="col span_6_of_12">
<div class="thumbnail project">
<h3>Some title</h3>
<div class="youtube-container">
<div class="youtube-player" data-id="ZMJP-FFzaZk">
<div>
<img class="youtube-thumb" src="//i.ytimg.com/vi/ZMJP-FFzaZk/hqdefault.jpg">
<div class="play-button"></div>
</div>
</div>
</div>
<div class="caption">
<p>blablablablabla</p>
</div>
</div>
</div>
my CSS code:
/* inside media query */
.youtube-player {
position: absolute;
text-align: center;
width: 100%;
height: 100%;
}
/* inside media query*/
img.youtube-thumb {
bottom: 0;
display: inline-block;
margin-left: auto;
margin-right: auto;
max-width: 50%;
width: 50%;
height: 100%;
position: absolute;
}
.project {
text-align: center;
}
/* for entire site */
.youtube-container {
position: relative;
padding-bottom: 300px; /* 16:9 */
padding-top: 25px;
height: 0;
}
/* for entire site */
div {
text-align: left;
}
/* 12 Grid Column Setup??? */
/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
}
img.youtube-thumb {
bottom: 0;
display: inline-block;
margin-left: auto;
margin-right: auto;
max-width: 50%;
width: 50%;
height: 100%;
position: absolute;
}
into this:
img.youtube-thumb {
top: 0;
display:block;
left:0;
right:0;
margin:auto;
max-width: 50%;
width: 50%;
height: 100%;
position: absolute;
}
Hope it helps
How do I center a text over an image in css?
<div class="image">
<img src="sample.png"/>
<div class="text">
<h2>Some text</h2>
</div>
</div>
I want to do something like the one below but I'm having difficulties, here's my current css
<style>
.image {
position: relative;
}
h2 {
position: absolute;
top: 200px;
left: 0;
width: 100%;
margin: 0 auto;
width: 300px;
height: 50px;
}
</style>
When I use background-image I do not get any output from html2pdf:
<style>
#image_container{
width: 1000px;
height: 700px;
background-image:url('switch.png');
}
</style>
Print
<?php ob_start(); ?>
<div id="image_container"></div>
<?php
$_SESSION['sess'] = ob_get_contents();
ob_flush();
?>
Here's prints.php:
<?php require_once('html2pdf/html2pdf.class.php'); ?>
<?php
$html2pdf = new HTML2PDF('L', 'A4', 'en');
$html2pdf->writeHTML($_SESSION['sess']);
$html2pdf->Output('random.pdf');
?>
How about something like this: http://jsfiddle.net/EgLKV/3/
Its done by using position:absolute and z-index to place the text over the image.
#container {
height: 400px;
width: 400px;
position: relative;
}
#image {
position: absolute;
left: 0;
top: 0;
}
#text {
z-index: 100;
position: absolute;
color: white;
font-size: 24px;
font-weight: bold;
left: 150px;
top: 350px;
}
<div id="container">
<img id="image" src="http://www.noao.edu/image_gallery/images/d4/androa.jpg" />
<p id="text">
Hello World!
</p>
</div>
This is another method for working with Responsive sizes. It will keep your text centered and maintain its position within its parent. If you don't want it centered then it's even easier, just work with the absolute parameters. Keep in mind the main container is using display: inline-block. There are many others ways to do this, depending on what you're working on.
Based off of Centering the Unknown
Working codepen example here
HTML
<div class="containerBox">
<div class="text-box">
<h4>Your Text is responsive and centered</h4>
</div>
<img class="img-responsive" src="http://placehold.it/900x100"/>
</div>
CSS
.containerBox {
position: relative;
display: inline-block;
}
.text-box {
position: absolute;
height: 100%;
text-align: center;
width: 100%;
}
.text-box:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
h4 {
display: inline-block;
font-size: 20px; /*or whatever you want*/
color: #FFF;
}
img {
display: block;
max-width: 100%;
height: auto;
}
Why not set sample.png as background image of text or h2 css class? This will give effect as you have written over an image.
For a responsive design it is good to use a container having a relative layout and content (placed in container) having fixed layout as.
CSS Styles:
/*Centering element in a base container*/
.contianer-relative{
position: relative;
}
.content-center-text-absolute{
position: absolute;
text-align: center;
width: 100%;
height: 0%;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: 51;
}
HTML code:
<!-- Have used ionic classes -->
<div class="row">
<div class="col remove-padding contianer-relative"><!-- container with position relative -->
<div class="item item-image clear-border" ><img ng-src="img/engg-manl.png" alt="ENGINEERING MANUAL" title="ENGINEERING MANUAL" ></div> <!-- Image intended to work as a background -->
<h4 class="content-center-text-absolute white-text"><strong>ENGINEERING <br> MANUALS</strong></h4><!-- content div with position fixed -->
</div>
<div class="col remove-padding contianer-relative"><!-- container with position relative -->
<div class="item item-image clear-border"><img ng-src="img/contract-directory.png" alt="CONTRACTOR DIRECTORY" title="CONTRACTOR DIRECTORY"></div><!-- Image intended to work as a background -->
<h4 class="content-center-text-absolute white-text"><strong>CONTRACTOR <br> DIRECTORY</strong></h4><!-- content div with position fixed -->
</div>
</div>
For IONIC Grid layout, evenly spaced grid elements and the classes used in above HTML, please refer - Grid: Evenly Spaced Columns. Hope it helps you out... :)
as Harry Joy points out, set the image as the div's background and then, if you only have one line of text you can set the line-height of the text to be the same as the div height and this will place your text in the center of the div.
If you have more than one line you'll want to set the display to be table-cell and vertical-alignment to middle.
as of 2017 this is more responsive and worked for me.
This is for putting text inside vs over, like a badge.
instead of the number 8, I had a variable to pull data from a database.
this code started with Kailas's answer up above
https://jsfiddle.net/jim54729/memmu2wb/3/
.containerBox {
position: relative;
display: inline-block;
}
.text-box {
position: absolute;
height: 30%;
text-align: center;
width: 100%;
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
font-size: 30px;
}
.img-responsive {
display: block;
max-width: 100%;
height: 120px;
margin: auto;
padding: auto;
}
.dataNumber {
margin-top: auto;
}
<div class="containerBox">
<img class="img-responsive" src="https://s20.postimg.org/huun8e6fh/Gold_Ring.png">
<div class='text-box'>
<p class='dataNumber'> 8 </p>
</div>
</div>
A small and short way of doing the same:
.image {
position: relative;
margin-bottom: 20px;
width: 100%;
height: 300px;
color: white;
background: url('https://via.placeholder.com/600') no-repeat;
background-size: 250px 250px;
}
<div class="image">
<p>
<h3>Heading 3</h3>
<h5>Heading 5</h5>
</p>
</div>
Quick solution: Set position: relative; on the container element and set position: absolute; on child elements in that container element, with the necessary top, left, bottom, right-adjusting parameters:
.top-left {
position: absolute;
top: 2px;
left: 2px;
}
.bottom-right {
position: absolute;
bottom: 2px;
right: 2px;
}
.container {
position: relative;
text-align: center;
color: white;
float:left;color: white;
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
<div class="container" style="">
<img src="https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2#2x.png" width="100">
<div class="top-left">Wikipedia</div>
<div class="bottom-right">Everyone's Encyclopedia</div>
</div>
Center it directly in the middle with the following CSS...
top: 50%;
left: 50%;
transform: translate(-50%, -50%);