make an element centered to an image on resizing using css - html

I have to make a div follow an image and sit on its center vertically and horizontally when responsive. I simply have no idea or don't think whether it is possible only by css. Any help is appreciated

.imageWrapper {
height:200px;
width:200px;
position:relative;
margin:50px auto 0px auto;
}
.imageWrapper > div:first-child {
position:absolute;
z-index:1;
top:0px;
left:0px;
right:0px;
bottom:0px;
overflow:hidden;
}
.imageWrapper > div:first-child img{
height:200px;
width:100%;
object-fit:cover;
position:relative
}
.imageWrapper > div:last-child {
position:relative;
z-index:2;
text-align:center;
line-height:200px;
height:200px;
width:100%;
}
<div class="imageWrapper">
<div><img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/Billede_084.jpg"></div>
<div><p>bla bla</p></div>
</div>
make a wrapping div, make the image absolute as a background and place the text in front of the image.

Well you can make good use of an old trick to center element using position property.
as usual an example is better than an explanation.
.html
<div class="parent">
<div class="child"></div>
</div>
.css
.parent {
position: relative;
width: 100%;
height: 200px;
background-color: lightblue;
}
.parent .child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100px;
height: 100px;
background-color: grey;
}

Related

Float:right causing anchor to stretch with line-height

I want to position my anchor to middle (vertical) inside a fixed div.
I can't use display:table/table-cell because of other content inside my div, so I thought line-height would be the best alternative.
My problem is that the anchor will stretch out when I put line-height with it, but only if it's floated.
HTML:
<div class="fixed">
<a class="btn" href="#">btn1</a>
</div>
CSS:
.fixed
{
height: 100px;
width: 100%;
background-color:grey;
position:fixed;
}
.btn
{
padding: 3px 9px;
background-color:red;
color:white;
line-height:100px;
float:right;
}
JSfiddle: https://jsfiddle.net/828zrzrk/
add display:block;
change padding value.
.fixed
{
height: 100px;
width: 100%;
background-color:grey;
position:fixed;
top:0;left:0;
}
.btn
{
display:block;
padding: 0 9px;
background-color:red;
color:white;
line-height:100px;
float:right;
}
<div class="fixed">
<a class="btn" href="#">btn1</a>
</div>
is this what you need?
```
.btn {
**
width: WIDTH;
height: HEIGHT;
position: absolute;
left: 50%;
top: 50%;
margin-top: -WIDTH/2;
margin-left: -HEIGHT/2;
}
```
Demo

Why i can't make absolute div height 100%

I have problem, I have one fixed container, inside them I have absolute div, I want to set .absolute div height:100%; to be full height of container div(500px).
Here is what I tried to solve my problem, this need because I want to create mobile menu with toggle container, and its important for me to be height 100% of mobile phone screen.
https://jsfiddle.net/d1bh9ncs/
HTML
<div class="container">
<div class="fixed">
<div class="absolute">
</div>
</div>
</div>
CSS
.container{
width:100%;
height:500px;
background-color:#ddd;
}
.fixed{
position:fixed;
width:100%;
height:50px;
background-color:red;
top:8px;
left:8px;
right:15px;
}
.absolute{
position:absolute;
height:100%;
width:100%;
background-color:green;
top:51px;
left:0px;
}
The parent div .fixed is absolutely positioned and has a height 50px. So applying height: 100%on it's child will inherit the relative height(i.e 50px).
Use height: 100vh; on .absolute. I have used calculated height height: calc(100vh - 51px) to avoid scrollbar due to top: 51px.
Note: vh is 1/100th of the height of the viewport(visible webpage height).
Updated Fiddle
.absolute {
position: absolute;
height: calc(100vh - 51px);
width: 100%;
background-color: green;
top: 51px;
left: 0px;
}
you are using Fixed div as an Parent div of Absolute div, Absolute div can have 100% of Fixed div it can't extend to its parent's height if you add height value in Percentage.If you want it to extend as parent height you must have to add height in px (pixels)
.container{
width:100%;
height:500px;
background-color:#ddd;
}
.fixed{
position:fixed;
width:100%;
height: 101px;
background-color:red;
top:8px;
left:8px;
right:15px;
}
.absolute{
position:absolute;
height: 117px;
width:100%;
background-color:green;
top: 0px !important;
left:0px;
z-index: 99999999;
top: 50px;
}
Try to give height in vh. Put .absolute height = 100vh
.absolute
{
position:absolute;
height:100vh;
width:100%;
background-color:green;
top:51px;
left:0px;
}
https://jsfiddle.net/bj9wcdLs/
A more modern way of doing this is to use vh and vw (view height and width). Which rather than being a percentage of its parent (like %) is a percentage of the full page.
In the example below I've done some calc's to help it work out what sizes we really want things.
example = function() {
var abSel = document.querySelector(".absolute");
abSel.classList.toggle('hidden');
}
body {
margin: 0;
}
.container {
width: 100vw;
height: 100vh;
background-color: #ddd;
}
.fixed {
position: fixed;
width: calc(100vw - 16px);
height: 50px;
line-height: 50px;
text-align: center;
background-color: red;
top: 8px;
left: 8px;
}
.absolute {
position: absolute;
border-top: 1px solid #ddd;
height: calc(100vh - 59px);
width: calc(100vw - 16px);
background-color: green;
top: 50px;
}
.hidden {
display: none;
}
<div class="container">
<div class="fixed">
<button onclick="example()">Example</button>
<div class="absolute hidden"></div>
</div>
</div>
Hope this helps.
Just like sticksu said.
Change your code
.fixed{
position:fixed;
width:100%;
height:100%; //must be 100%
background-color:red;
top:8px;
left:8px;
right:15px;
}

unable to position div at bottom of another div

Am trying to position div in bottom of another div but unable to do that
<div class="div">
<img src="dual.png"/>
<h1>Boot From a CD or USB Drive on Any PC</h1>
<div>footers</div>
</div>
CSS
.div{
float:left;
height:300px;
width:22%;
margin-top:15px;
margin-left:15px;
background-color: #e4f2ff
}
.div img{
width:100%;
height:180px;
}
.div h1{
font-size:20px
}
.div div{
background:red;
float: bottom;
position: absolute;
bottom: 0;
}
here is
jsfiddle
trying to make design like this http://i.stack.imgur.com/CdGKY.png
Can someone help me how to position div at bottom of parent div..
thanks in advance
Display:table could help you : http://jsfiddle.net/j1raqvc8/4/
.div {
float:left;
height:300px;
width:22%;
margin-top:15px;
margin-left:15px;
background-color: #e4f2ff;
display:table;
}
.div img {
width:100%;
height:180px;
}
.div h1 {
font-size:20px;
margin:0
}
.div div {
background:red;
display:table-footer-group;
}
<div class="div">
<img src="dual.png" />
<h1>Boot From a CD or USB Drive on Any PC</h1>
<div>footers</div>
</div>
But it will allow the box to grow taller than 300px if content needs more height http://jsfiddle.net/j1raqvc8/5/ .
Remove position:absolute solve your problem.
Note: also try to make your class name more clear.
Delete
position: absolute;
bottom: 0;
float: bottom;
From Css selector
.div div{
So it would be like:
.div div{
background:red;
}
This also depends how you want it to flow. But since you had absolute in there I went with that.
Add position: relative; to the parent.
Take your float: bottom; (no such thing) away from the footer div.
.div {
float: left;
height: 300px;
width: 22%;
margin-top: 15px;
margin-left: 15px;
background-color: #e4f2ff;
position: relative;
}
.div img {
width: 100%;
height: 180px;
}
.div h1 {
font-size: 20px
}
.div div {
background: red;
position: absolute;
bottom: 0;
}
<div class="div">
<img src="dual.png" />
<h1>Boot From a CD or USB Drive on Any PC</h1>
<div>footers</div>
</div>
you got several failures in your code. as a suggestion never name a div class div, furthermore there is no float: bottom.
you need to add a position: relative to the parent element to use position: absolute in a child element.
.top{
float:left;
height:300px;
width:22%;
margin-top:15px;
margin-left:15px;
background-color: #e4f2ff;
position: relative;
display: block;
}
.div img{
width:100%;
height:180px;
}
.div h1{
font-size:20px
}
.footer {
background:red;
position: absolute;
bottom: 0;
left: 0;
}
<div class="top">
<img src="dual.png"/>
<h1>Boot From a CD or USB Drive on Any PC</h1>
<div class="footer">footers</div>
</div>

Not able to center the image inside a div

I have an image inside a div, I want to center the image vertically and horizontally, tried different approaches like making container display as table and image as table-row etc.
Here it's not aligning vertically.
Here is my code:
<div class="container">
<img src='images/img1.jpg' class="imgclass">
</div>
CSS:
.container {
width: 70%;
min-height:400px;
overflow: hidden;
position: relative;
text-align: center;
}
.container img {
max-height: 100%;
}
You can use this absolute method:
JSFiddle
.container {
width: 70%;
min-height:400px;
overflow: hidden;
position: relative;
}
.container img {
max-height: 100%;
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
}
This way, you don't need to change the structure of your HTML and it will always be in the center, no matter what height or width you give the element, or even if you re-size the page.
You also don't have to know the size of the image.
Browser Compatibility:
Chrome, Firefox, Safari, Mobile Safari, IE8-10.
An easier solution would be to place the image as a background image inside the div
.container
{
background:url('images/img1.jpg') no-repeat center center;
width: 70%;
min-height:400px;
}
You can also use the table inside the div and vertical and horizontal align the content of the table to center
<div class="container">
<table>
<tr>
<td style="vertical-align:center; text-align:center;">
<img src='images/img1.jpg' class="imgclass">
</td>
</div>
You can try absolute centering. Using this method, you don't have to alter your html or know the size of the image.
.container {
position:relative;
width: 70%;
min-height:400px;
overflow: hidden;
text-align: center;
}
.container img {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
margin:auto;
max-height: 100%;
}
JSFiddle
If you know the dimension of the image, then following css will work for you:
img {
width:250px;
height:250px;
margin:0 auto;
display:block;
margin-top:calc(50% - 125px);
margin-top:-moz-calc(50% - 125px); /*for mozilla
margin-top: -webkit-calc(50% - 125px); /*for webkit browsers.
}
You don't need any special style for container. This is the list of compatible browsers: http://caniuse.com/calc
Demo:http://jsfiddle.net/lotusgodkk/GCu2D/143/
If you only need ie9+ you can use transforms:
.container {
width: 70%;
min-height:400px;
overflow: hidden;
position: relative;
text-align: center;
}
.container img {
max-height: 100%;
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-o-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
If you know the exact size of the image you can use negative margins instead of translate. The margins will be half the size of the image.
Take another div inside of .container and place image inside of it. Then making .container div's css rule to display:table and display:table-cell; vertical-align:middle for inner div will do the trick.
HTML
<div class="container">
<div class="img-cont">
<img src='http://fc08.deviantart.net/images3/i/2004/09/1/0/flower_eyes.jpg' class="imgclass">
</div>
</div>
CSS
.container {
width: 70%;
min-height:400px;
overflow: hidden;
position: relative;
text-align: center;
display:table;
background:#ccc;
}
.img-cont{
display:table-cell;
vertical-align:middle;
}
.container img {
max-height: 100%;
}
Here is a fiddle http://jsfiddle.net/v22rG/

Center a div horizontally and vertically and keep centered when resizing the parent [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 9 years ago.
I want to center a div horizontally and vertically at all times.
I can reduce/increase the width of the window and the div will respond by always remaining in the center of the window
.cent
{
height:50px;
width:50px;
background-color:black;
margin:auto;
}
Here is a JSFiddle Example of what I have currently.
But I want to keep the div centered vertically as well so if I reduce/increase the height of the window the the div will respond by staying in the middle of the window.
In regards to the example, I want to keep the blackbox vertically centered on window resize in the same way it always stays in the center horizontally.
You can do this with CSS tables:
JsFiddle
Markup
<div class="container">
<div class="cent"></div>
</div>
(Relevant) CSS
html,body
{
height: 100%;
}
body
{
display: table;
margin: 0 auto;
}
.container
{
height: 100%;
display: table-cell;
vertical-align: middle;
}
.cent
{
height: 50px;
width: 50px;
background-color: black;
}
try this
position: absolute;
top: 50%;
left: 50%;
margin-left: -25px;
margin-top: -25px;
Demo link is here
.cent
{
height:50px;
width:50px;
background-color:black;
margin:auto;
position:absolute;
left:50%;
top:50%;
margin-left:-25px;
margin-top:-25px;
}
Test this
.cent {
width: 50px;
height: 50px;
background-color: black;
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
max-width:100%;
max-height:100%;
overflow:auto;
}
here you have a example: http://jsbin.com/iquviq/30/edit
Check this
.cent
{
height:50px;
width:50px;
background-color:black;
margin:auto;
margin-top:50%;
}
try this
http://jsfiddle.net/tVuS6/4/
.cent
{
height:50px;
width:50px;
background-color:black;
margin-left: -150px;
margin-top: -150px;
margin:auto;
position:absolute;
top:50%;
left:50%;
}
.cent
{
height:50px;
width:50px;
background-color:black;
position:absolute;
left:50%;
top:50%;
margin: 0 auto;
}
Usually margin: 0 auto; does the horizontal alignment and vertical-align: middle for vertical alignment. I tried it but not working.
Try the below css
.cent {
height: 50px;
width: 50px;
background: #222;
position: absolute;
margin: -50px 0 0 -50px;
left: 50%;
top: 50%;
}
Check it in JSFiddle.
To know more about the trick check Dead Center an Element.