I am trying to display an image, centered vertically and horizontally on a screen (which in my case, is a modal screen). The image must be a maximum of 80% high, and 80% wide. It can be landscape, square or portrait.
I have achieved that. But now I need to add a div, fitted tightly around the image, so that I can apply a text-over effect.
This is what I have so far.
https://jsfiddle.net/Cralis/1w8d4zx5/
<div class="imageContainer">
<img id="imgContainer" class="fullimage animated fadeIn" src="http://cameraflare.com/photostore/23d255d8-e259-47ee-be2d-fae68c730f6d/afda43dd-5a81-40b2-8161-d1f222689d3f.jpg">
<h2>
<span>The one and only:
<span class='spacer'></span>
<br />
<span class='spacer'></span>
The Hulk
</span>
</h2>
</div>
I am trying to achieve what this tutorial shows, except, their demo is aligned left on the screen. My image is centered.
https://css-tricks.com/text-blocks-over-image/
How can I get the imageContainer div to 'hug' the image?
Not pretty sure but i think what you are looking for is the following:
Set your .imageContainer as display: inline-block and add it text-align: left
Set the parent of image container to text-align: center
So you will get your div centered but you will also wrap/hug the image with the border. Take a look here:
https://jsfiddle.net/0k3wrfxt/
you can use css3 flexbox concept to achieve this.
add the following styles to the parent container of your image
display:flex;
align-items:center;
justify-content:center;
and remove the margin:100px; on your .imageContainer ,it will work fine .I'm added the snippet below.
html,body{
width:100%;
height:100%;
display:flex;
align-items:center;
justify-content:center;
}
.fullimage {
vertical-align: top;
display: block;
margin: auto;
border-radius: 7px;
}
.imageContainer {
max-height: 80vh;
max-width: 80vw;
border: solid;
/*margin: auto;
margin-left:100px;*/
position: absolute;
padding: 0;
}
h2 {
position: absolute;
top: 200px;
left: 0;
width: 100%;
}
h2 span {
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
}
h2 span.spacer {
padding: 0 5px;
}
<div class="imageContainer">
<img id="imgContainer" class="fullimage animated fadeIn" src="http://cameraflare.com/photostore/23d255d8-e259-47ee-be2d-fae68c730f6d/afda43dd-5a81-40b2-8161-d1f222689d3f.jpg">
<h2>
<span>The one and only:
<span class='spacer'></span>
<br />
<span class='spacer'></span>
The Hulk
</span>
</h2>
</div>
Related
I'm trying to get an image (img) to fill all available space inside a div BUT also I want the img itself to be inside an equaly sized container with position:relative so that I can place markers on the image with absolute position.
I have tried using div background-image and that works perfect except that my markers positions are out of place (of course) since the parent div is not the same size.
And with I get the markers to work perfect but then the image doesn't scale with parent container.
I need the image to behave like background-image but I also need the parent container to be same size.
Here I have 2 pens showing what I mean.
First one with background-image. pen:background-image
body { height: 90vh;
background: aliceblue;}
.page-inner {
aspect-ratio: 16 / 9;
position: relative;
padding: 1em 1em 0 1em;
display: flex;
flex-direction: column;
align-items: center;
height:100%;
border: 1px solid black
}
.main-container {
width: 100%;
flex: 1;
padding: 2em 1em 2em 1em;
background:yellow;
}
.working-area {
position:relative;
}
.work-item {
position:absolute;
top:10%;
left:10%;
background:white;
outline: 2px solid black;
}
<div class="page-inner">
<h3 class="" >Some text</h3>
<div class="main-container">
<div class="working-area" style="height:100%;width:100%;background-position: center;background-image:url('');background-size: contain;background-repeat: no-repeat">
<div class="work-item">X</div>
</div>
</div>
<div>
<h3 class="" >Some text again</h3>
</div>
</div>
Second one with img: pen:img
body { height: 90vh;
background: aliceblue;}
.page-inner {
aspect-ratio: 16 / 9;
position: relative;
padding: 1em 1em 0 1em;
display: flex;
flex-direction: column;
align-items: center;
height:100%;
border: 1px solid black
}
.main-container {
width: 100%;
flex: 1;
padding: 2em 1em 2em 1em;
background:yellow;
}
.main-container img {
width: 100%;
height: auto;
}
.working-area {
position:relative;
}
.work-item {
position:absolute;
top:10%;
left:10%;
background:white;
outline: 2px solid black;
}
<div class="page-inner">
<h3 class="" >Some text</h3>
<div class="main-container">
<div class="working-area">
<img src="" style="" />
<div class="work-item">X</div>
</div>
</div>
<div>
<h3 class="" >Some text again</h3>
</div>
</div>
I want to combine these 2 into one :-) I need full scaling of image at the same time as I need to get image size and position relative so I can place my markers.
The image can be in any size and the screen can be resized at any time, so I need a solution that really is responsive.
thanks!
I have tried width:100%;height:auto; nad like 200 other different things. I have also started to calculate the image aspect-ratio and have a hidden img and then manually, with js, calculate each marker pos in % based on parent div size.
The solution needs to work with all img sizes and aspect ratios. Users uploads image so I don't know what sizes will come.
I have also tried to position the image with full width and height and then used transform: scale to make it fit the parent div with lots of calculations in js.
But there has to be an easier way :-)
I'm working on my portfolio site and having trouble getting my divs to size together.
Here it is: I have one body div, inside there are two child divs.
The body div should have max-height 100%, max-width: 100% so that it doesn't exceed the browser window.
The left div contains an image which I want to scale to the parent body div (max-height 100%, max-width 70%).
The right div contains text about the image, it needs to scale to the height of the left div (there is also footer that sits at the bottom of this div).
This shouldn't be so hard, its almost working but right now my image container (left div) is not being contained to the body div.
html,
body {
height: 100%;
}
.Info {
float: left;
width: 25%;
padding-left: 15px;
padding-top: 10px;
/*margin-left: 78%;*/
border-left: 1px black solid;
/*position: absolute;*/
}
.InfoText p {
margin-top: -10px;
}
div img {
max-width: 100%;
max-height: 100%;
margin: 0 auto;
box-shadow: 5px 5px 15px #888888;
}
.ImageContainer {
float: left;
height: 100%;
max-width: 70%;
padding-right: 15px;
position: relative
}
section.ImageContainer img {
float: left;
object-fit: cover;
}
#SideQuote {
margin-top: 30px;
}
.StuffInBody {
position: relative;
padding-top: 15px;
float: left;
display: flex;
}
footer p {
position: absolute;
bottom: 0%;
margin: 0;
}
<div class="StuffInBody">
<div class="ImageContainer">
<img class="contained" src="images/TheGMODebate copy.jpg" alt="" />
</div>
<div class="Info">
<div class="InfoText">
<p>ILLUSTRATION</p>
<p>Title: <em>The GMO Debate</em>
</p>
<p>Media: Gouache</p>
<div id="SideQuote">
<p class="ClickToEnlarge">Full screen image click here.
</p>
</div>
<footer>
<p>© Brooke Weiland 2015</p>
</footer>
</div>
</div>
</div>
It's very hard to make a reliable interface using floats.
It changes default behaviour and put your element outside of the flux.
You should be able to do what you want using only flex.
The property box-sizing: border-box also makes miracles (margin and padding easier to manage).
Also, the object-fit property is not enough supported by browsers to be used now IMHO.
http://caniuse.com/#feat=object-fit
I am using for the first time this forum, so if I am missing some crucial elements let me know.
My goal: I am trying to create 4 clickable image (in a box layout) in which when the mouse goes over, I would like a slight animation to show an icon on the image AND i want at the same time, a descriptive text that would appear on its side of the image.
My situation: I have the images set up in the dimension I want, and the little animation.
My problem: I have tried many, many things, but can t managed to make the text appear on the side of the image when the mouse hovers on the image.
Here is the Html:
<div class="video-container">
<a href="" class="thumb-unit" style="background-image: url(/assets/img/video/thumb1.jpg)">
<div class="thumb-overlay">
<strong>PLAY</strong>
</a> </div>
(above structure 4 times)
And here is the css:
.videos
background: #e1f3f6
h3
margin: 0
text-align: center
font-family: "Quicksand", sans-serif
letter-spacing: 1em
color: #deb75c
padding-top: 70px
font-weight: 300
.video-container
max-width: 930px
margin: 0px auto
padding-top: 88px
padding-bottom: 70px
+clearfix
.thumb-unit
overflow: hidden
display: block
width: 50%
position: relative
padding-top: 20%
background: pink
float: left
background:
position: center center
repeat: no-repeat
size: cover
.thumb-overlay
+position(absolute, 100% 0px null 0px)
height: 100%
background: fade-out(#6693b0, 0.3)
+transition
text-align: center
strong
padding: 30px
display: block
Color: white
font-weight: 400
text-transform: uppercase
font-size: 17px
background:
image: url(/assets/img/icon/play.png)
position: center 70px
repeat: no-repeat
h4
border: 2px solid red
position: absolute!important
width: 300px!important
right: 300px!important
visibility: visible!important!important
&:hover .thumb-overlay
+position(absolute, 0% 0px null 0px)
as you can see, the code reflects my current situation.
If you have any insights, thank you very much!!!
i have slightly adjusted your code, not sure what else is on your page but hopefully this will make sense and you can add it into your code.
<div class="video-container">
<a href="#">
<div style="background-image:url('Thumb.jpg');">
<strong>PLAY</strong>
</div>
<div>
This is the display text you want to display.
</div>
</a>
</div>
and the css
.video-container
{
width:100px;
height:100px;
margin: 5px;
float:left;
}
.video-container > a > div:nth-child(1)
{
height:100px;
width:100px;
float:left;
}
.video-container > a > div:nth-child(2)
{
display:none;
height:100px;
width:100px;
margin:-100px 0px 0px 100px;
float:left;
}
.video-container > a:hover > div:nth-child(2)
{
display:block;
}
i think it works how you want, its hard as not sure what the other code on your page is etc. i have changed some dimensions to make it a bit easier to work with. any comments on this will be greatfull
I'm trying to create a "tile" (square block) with content centered perfectly in the middle of it.
Here's the HTML
<div class="tile-facebook">
<h5>Facebook</h5>
<div class="tile-notification">4</div>
<h4>notifications</h4>
</div>
And the CSS
.tile-facebook{
width:175px;
height:175px;
background: #3b5998 ;
text-align: center;
border-radius: 10px;
border-style: solid;
border-color: #ccc;
border-width:1px;
color: white;
}
.tile-notification{
font-size:80px;
font-weight:bold;
padding:10px 0px;
}
I've got the text in the middle of the block, however I want it to be directly in the middle with the same padding from the top and bottom. Thoughts?
You might not set the height , but use a pseudo or extra element to draw your square from any width of your boxe.
vertical padding at 100% + inline-block is a way to draw a square and add content in its middle.
<div class="tile-facebook">
<div class="wrapper">
<h5>
Facebook
</h5>
<div class=" tile-notification ">
4
</div>
<h4>
notifications
</h4>
</div>
</div>
.tile-facebook {
width:175px;
background: #3b5998;
text-align: center;
border-radius: 10px;
border-style: solid;
border-color: #ccc;
border-width:1px;
color: white;
}
.tile-notification {
font-size:80px;
font-weight:bold;
}
.tile-facebook .wrapper * {
margin:0;
}
.tile-facebook:before {
padding-top:100%;
content:'';
}
.tile-facebook:before,
.wrapper {
display:inline-block;
vertical-align:middle;
}
http://jsfiddle.net/cnq82/
some explanation about vertical % padding or margin : http://www.w3.org/TR/CSS2/box.html#padding-properties & http://www.w3.org/TR/CSS2/box.html#propdef-margin-top
So, (hope it makes it a bit more clear : ) )
If you give a vertical padding of 100% its height will be equal to width of parent.
If you want height to be taller of 20px you can do : padding:100% 0 20px 0 ; or padding:20px 0 100% 0;
If you want a box with a ration of 4:3 , just do padding-top:75%; or padding:50% 0 25% 0;.
pseudo or extra element can be floatting, or inline-block for vertical alignment.
You do not need to set a width in parent's CSS.
This fix tequires that the contents height never changes, and you need to add another <div>.
<div class="tile-facebook">
<div class="center">
<h5>Facebook</h5>
<div class="tile-notification">4</div>
<h4>notifications</h4>
</div>
</div>
And add the CSS:
.title-facebook {
position: relative;
}
.center {
position: absolute;
top: 50%;
margin-top: -[half height];
left:0;
width: 100%;
}
Where [half height] is half the height of the .center div.
Add margin: -30px; to your CSS here:
.tile-notification {
font-size:80px;
font-weight: bold;
padding: 10px 0px;
margin: -30px;
}
Like the example above. I've found some helpful script with the a small img which I do like however I don't know how to get the padding about the title so the line doesn't go straight through.
h3.line {
background-attachment: scroll;
background-clip: border-box;
background-color: transparent;
background-image: url(../images/line.jpg);
background-origin: padding-box;
background-position: center;
background-repeat: repeat-x;
background-size: auto auto;
display: block;
margin-bottom: 20px;
}
Which shows this.
Any suggestion or ideas?
You can have a 1px dot image which you can place as a background on the H3. Then have a span element in between which have a background on.
CSS:
h3 {
background: url(images/dot.png) left center repeat-x;
padding: 10px;
text-align: center;
}
h3 span { background: #fff; display: inline-block; padding: 10px 15px; }
HTML:
<h3><span>About</span></h3>
You can put a <span> for example in your <h3> and make it have the same background as your <h3> but without the line so the <span> effectively overlaps the <h3>.
You can say this to your span:
span {
display: block;
margin-left: auto;
margin-right: auto;
}
to make it center. You can add width and height to it too. line-height helps place your text to the middle vertically.
If you want to spare images than you can use text-decoration: line-through; to draw a line through your text.
Here is a solution using the CSS border property instead of an image.
the html:
<h2>
<span>This is a test</span>
<div></div>
</h2>
And here is the CSS:
h2 {
text-align:center;
background-color:#EFEFEF;
line-height:26px;
position:relative;
}
span {
background-color:#EFEFEF;
padding-right:5px;
padding-left:5px;
position:relative;
z-index:2;
}
h2 > div {
border-bottom:1px solid grey;
position:relative;
z-index:1;
top:-13px; /* half the line-height of the containing element */
}
A fiddle Demonstration
The <div> is placed inside the heading element, and positioned half-way up by settings its top position to one-half the height of the heading element, which is the headings line-height. z-index is used on the span and div so that the span gets a higher stack order than the div and obscures the (border) line where there is overlap.
I just stumbled upon another way of achieving this.
h1
{
position: relative;
padding: 0 26%;
}
h1:before,
h1:after
{
width: 25%;
background-color: rgba( 0, 0, 0, .5 );
content: '';
position: absolute;
top: 0;
bottom: 0;
}
Taken from: http://osvaldas.info/blog/background-independent-css-bars