center center css on dynamic heights - html

I want to put the play button (first img) centered to the second images. I can do it with position absolute and margin but how can that be dynamic? What if I'm in a loop and the height of the second images is not always the same?
<img src="http://maxcdn.clubcooee.com/img/icons/play-button2.png"/>
<img src="http://www.howtorecordpodcasts.com/wp-content/uploads/2012/10/YouTube-Background-Pop-4.jpg"/>

There are two solutions to do this
1) Put that image between paragraph tags and give style to that paragraph "text-align:center;", just like below
<p style="text-align:center"><img src="#url to your image"/></p>
2) Give style to that img tags margin:0px auto , just like below
<img src="#url to your image" style="margin:0px auto;"/>

you will need to add a parent for the images and position the play image absolute and set it top left 50% and set negative margin of half the width of the play image so that it align it vertically and horizontally
div {
margin: 30px;
position: relative;
border: 1px solid red;
}
img.main {
width: 100%;
height: auto;
vertical-align: middle;
}
img.play {
position: absolute;
top: 50%;
left: 50%;
margin: -63px 0 0 -63px;
}
.second .main {
height: 400px;
}
<p>with smaller height</p>
<div>
<img class="play" src="http://maxcdn.clubcooee.com/img/icons/play-button2.png" />
<img class="main" src="http://www.howtorecordpodcasts.com/wp-content/uploads/2012/10/YouTube-Background-Pop-4.jpg" />
</div>
<p>with larger height</p>
<div class="second">
<img class="play" src="http://maxcdn.clubcooee.com/img/icons/play-button2.png" />
<img class="main" src="http://www.howtorecordpodcasts.com/wp-content/uploads/2012/10/YouTube-Background-Pop-4.jpg" />
</div>

Related

Scrollable text over image HTML/CSS

Currently I can not find a solution that is responsive and scrollable to put text on an image. I need the height to be flexible and the width at 100%. I tried to use position:relative; and css background images with no luck. When I use position: relative; there is a space at the top of the image and the only way to delete it is negative margins which I think is not sustainable it there are multiple posts. css backgrounds does not show the full image unless you set dimensions and when is responsive you cant set dimensions. I dont think I can use position absolute because it would not scroll. so I dont not know what to use.
I have this HTML code here:
<div class="post">
<span><a>Posted By Adam</a></span>
<img width="100%" src="uimg/adam-levine-600.jpg">
</div>
Use position: absolute; and add a spacer for the nav:
http://jsfiddle.net/ryanpcmcquen/p3bes5xq/
.nav {
height: 100px;
width: 100%;
position: fixed;
background-color: #21A7F0;
text-align: center;
z-index: 10;
color: #ffffff;
}
.spacer {
height: 105px;
}
.post span {
position: absolute;
color: #ffffff;
text-shadow: 1px 1px 2px #000000;
}
.post img {
width: 100%;
}
<div class="nav">nav bar</div>
<div class="spacer"></div>
<div class="post"> <span><a>Posted By Adam</a></span>
<img src="//fillmurray.com/880/450" />
</div>
<div class="post"> <span><a>Posted By Adam</a></span>
<img src="//fillmurray.com/880/260" />
</div>
<div class="post"> <span><a>Posted By Adam</a></span>
<img src="//fillmurray.com/880/194" />
</div>

CSS: Positioning an IMG tag within a div with overflow

I am building a responsive website that contains a complex collapsible grid - which requires parts of large images to be displayed within smaller fixed width divs - which will adjust in size based on media queries - overflows are hidden to give a masked effect.
I need to position the images to -
Left Top
Center Top
Left Top
Of course left top is standard browser behaviour - but I need best practice cross-device/browser solutions for center/top and left/top.
So for example - the following code structure -
<div style="width:100px; height:50px; overflow:hidden;">
<img src="http://i.guim.co.uk/static/w-620/h--/q-95/sys-images/Guardian/Pix/pictures/2014/9/24/1411574454561/03085543-87de-47ab-a4eb-58e7e39d022e-620x372.jpeg" style="width:620px; height:320px;">
</div>
How would I position the image tag Center-top or left-top
You can use absolute positioning with the properties top, bottom, left, right.
div {
width: 200px;
height: 100px;
overflow: hidden;
position: relative;
margin: 10px;
display: inline-block;
}
img[data-position] {
position: absolute;
border: 1px dotted red;
}
img[data-position*=top] { top: 0; }
img[data-position*=bottom] { bottom: 0; }
img[data-position*=left] { left: 0; }
img[data-position*=right] { right: 0; }
<div>
<img src="http://i.stack.imgur.com/SyxjC.jpg" alt=""
data-position="left top" width="620" height="320">
</div>
<div>
<img src="http://i.stack.imgur.com/SyxjC.jpg" alt=""
data-position="right top" width="620" height="320">
</div>
<div>
<img src="http://i.stack.imgur.com/SyxjC.jpg" alt=""
data-position="left bottom" width="620" height="320">
</div>
<div>
<img src="http://i.stack.imgur.com/SyxjC.jpg" alt=""
data-position="right bottom" width="620" height="320">
</div>

Display 2 images per row side by side, centered horizontally and vertically

Let's say that I would like to display 4 images in my webpage like this:
img1 img2
img3 img4
I want every image to take 50% of the width of the webpage, but if its width is less than 50%, to be centered to this space (so I need it to be centered to the 50% of the left width).
Similarly, I want the image with the less height to be centered relatively to the height of the image on its side.
Example:
What I've tried till now:
HTML:
<div class="imgGroup">
<div class="groupImg">
<img src="images/speed.png" alt="Wind Speed Graph">
</div>
<div class="groupImg">
<div class="strongCenter">
<img src="images/wind_now.png" alt="Wind Direction Now">
</div>
</div>
</div>
CSS:
.groupImg img {
display: inline-block;
margin-left: auto;
margin-right: auto;
position: relative;
float: left;
max-width: 50%;
border-radius: 10px;
}
I've grouped the images 2-2 in imgGroup with the thought that I have to work with the height of the div in order to center the images inside.
HTML->
<div class="imgGroup">
<div class="groupImg">
<img src="http://i.imgur.com/WZUUq4g.jpg" width="" height="150" alt="Wind Speed Graph" />
</div>
<div class="groupImg">
<div class="strongCenter">
<img src="http://bit.ly/1apzyBy" width="" height="100" alt="Wind Direction Now" />
</div>
</div>
</div>
CSS->
.imgGroup {
background:Silver;
display:table;
width:100%;
}
.groupImg{
display:table-cell;
text-align:center;
width:50%;
vertical-align:middle;
}
.groupImg img {
background: White;
width: 100%;
border-radius: 10px;
display: block;
}
.strongCenter img{
width: 50% !important;
display: inline-block;
}

Position image center of div whetever image size

I have in a problem with centering an image.Hence I have many div and in every div have two div that is divided into two.In those two div I will show two image.Each of these two div is 300 pixel.Problem is images may be small or 300 pixel and I have to show images center if small and if image is 300 then automatically sit on full div. Can anyone help me to do this?
Here is the code..
<div style="margin-top:10px;height: 300px;width: 600px;background: whitesmoke">
<div style="float:left;width: 300px;height: 300px;">
<img class="" src="images/productImages/medium/<?php echo $product1['product_image']?>" alt="image_01" width="" height="" />
</div>
<div style="float:right;width: 300px;height: 300px;">
<?php if(!empty($product2)) { ?>
<img class="" src="images/productImages/medium/<?php echo $product2['product_image']?>" alt="image_02" width="" height="" />
<?php } ?>
</div>
</div>
you have to position the parent with "relative" and than you can handle the IMGs with position "absolute" ;)
take a look at this fiddle
.CenterMe {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
You should use display: table-cell; for the parent element and than use max-height and max-width properties for your img tag.. This way you can align the images vertically as well as horizontally.
Demo
div {
width: 300px;
height: 300px;
display: table-cell;
border: 1px solid #f00;
text-align: center;
vertical-align: middle;
}
div img {
max-height: 100%;
max-width: 100%;
}
But if you are looking to align images only horizontally and not vertically, than you won't need to do much, just declare text-align: center; on the parent element.
Use image as background and set position to center:
<div style="margin-top:10px;height: 300px;width: 600px;background: whitesmoke">
<div style="float:left; width: 300px; height: 300px; background-image: url('images/productImages/medium/<?php echo $product2['product_image']?>'; background-position: center center; background-repeat: no-repeat;">
</div>
<!-- etc. -->
</div>
just add text-align:center to your image container:
<div style="float:left;width: 300px;height: 300px; text-align:center">
<img class="" src="images/productImages/medium/<?php echo $product1['product_image']?>" alt="image_01" width="" height="" />
</div>
okay, considering your images and condition are both dynamic, modify your inline css to :
<div style="margin-top:10px;height: 300px;width: 600px;background: whitesmoke;text-align:center">
added : text-align:center
Fiddle

How to bottom align two elements in a DIV element?

I am currently doing this with a table with 2 bottom-aligned cells. I am okay with the table solution, but just wondering if this is possible with (just css and html, no javascript).
Requirement:
* The sizes of the text and image are unknown, but the combined width of the two will not exceed the width of the containing element. (e.g. if i later want to change the image or the text, i do not want to dive into the ccs file)
* Image is aligned to the left, and the text (actually, a horizontal list) is aligned to the right.
Edit: In response to Kos,
the sizes of the text and images are dynamic, be it height or width, BUT the combined width of the two elements will not exceed the width of the containing element.
the image and text should be bottom aligned
the containing element should fit tightly the tallest element.
HTML
<div class="wrapper">
<img src="" class="image" />
<p class="text">Hello world!</p>
</div>
CSS
.wrapper {
position: relative;
width: 500px;
}
.image {
position: absolute;
display: block;
left:0;
bottom: 0;
}
.text {
position: absolute;
right:0;
bottom: 0;
}
EDIT: I added the appropriate HTML code.
EDIT 2: In case the height of the wrapper is unknown (only restriction is that .image has always to be higher than .text)
CSS
.wrapper {
position: relative;
width: 500px;
}
.image {
vertical-align: bottom;
}
.text {
position: absolute;
right: 0;
bottom: 0;
}
HTML
<div class="wrapper">
<img class="image" src="" />
<p class="text">
Hello world!
</p>
</div>
This should work I think:
HTML
<div class="outer">
<img src="" title="" />
<div class="text">Some text </div>
</div>
CSS
.outer {display: inline-block; width: 350px; }
.outer img {float: left;}
.outer .text {float: right; }
<div style="width:400px; overflow:visible; position:relative;">
<img src="#" alt ="#" style="float:left;"/>
<p style="position:absolute; bottom:0; float:right;">Lorem Ipsum</p>
</div>