I have an image within an image div container. I vertical and horizontal align it. But when i do so using transform - i lose the bottom border of one of my images, and am not quite sure why.
html:
<div class="imageContainer">
<img class="myImage" src='x'>
</div>
css:
.imageContainer {
width: 20px;
max-height: 25px;
height: 25px;
position: relative;
background-color: lightblue;
}
.myImage {
width: auto;
height: auto; /* Set to 9.2px in fiddle example to force the problem. */
max-height: 25px;
max-width: 20px;
border: 1px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
update:
The reason for this is that the auto sized image land on a decimal value. (fx 9.2px) which is rounded down. So as I understand it, it wraps my 9.2 height element in a border and then rounds it down to 9px which makes the bottom border vanish. (even with overflow: visible)
Anyway to force auto values not to land on a decimal value? or something along those lines.
fiddle example : http://jsfiddle.net/dLLan/24/ (the problem only happens in firefox, so make sure to run the fiddle in firefox.)
Since you updated your question, here is what I did:
1. Added a new div (#imgholder) to hold the myImg div.
2. Added the customized width and height of 20px and 25px on #imgholder.
3. Changed width and height of #myImage to height: auto; & width: 100%; to make it fit comfy on #imgholder.
#imageContainer {
float: left;
position: relative;
background-color: lightblue;
}
#imgholder {
width: 20px;
height: 25px;
}
#myImage {
height: auto;
width: 100%;
border: 1px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div id="imageContainer">
<div id="imgholder">
<img id="myImage" src="http://s7.postimg.org/k8e5116ff/806c278b5a0f77b98f4dcc469f2d0b08.jpg" />
</div>
</div>
Related
I have a background image, but I need to place a div that its bottom edge should go below the image. What's the easiest way to do this?
Please see the attached image. The white part is the background image and the blue part is my div over the background.
You can create a relative positioned wrapper and then set absolute positioning with bottom: -10%; or bottom: -20px; for a div over a div with image:
.image-with-block-wrapper {
position: relative;
}
.image {
height: 200px;
border: 1px solid #111;
background: url('https://www.gravatar.com/avatar/f42a832da648291bf80206eda08e3332?s=328&d=identicon&r=PG&f=1');
}
.div-over-bg {
border: 1px solid #111;
position: absolute;
height: 50px;
bottom: -10%;
left: 50%;
transform: translateX(-50%);
background: green;
width: 100px;
margin: 0 auto;
}
<html>
<head></head>
<body>
<div class='image-with-block-wrapper'>
<div class='image'></div>
<div class='div-over-bg'></div>
</div>
</body>
</html>
Edit:
In the case of using percents for bottom it will be related with the wrapper height, but you can use bottom: 0;
and transform: translate(-50%, 15%); in order to set the upper block vertical position as relative to the block itself.
So I've created a container with a background image and placed a div inside.
I've given the .block margin: auto; to center it and added position: relative; so I can move it, because it has position: relative; I can add top: 100px; to move it down from the top by 100px
.container {
background-image: url('https://via.placeholder.com/150');
width: 100%;
background-position: cover;
height: 300px;
position: relative;
}
.container .block {
height: 300px;
background-color: blue;
width: 500px;
position: relative;
margin: auto;
top: 100px;
}
<div class="container">
<div class="block">
</div>
</div>
Extra info by #I_Can_Help
In the case of using percents for bottom it will be related with the wrapper height, but you can use bottom: 0;
and transform: translate(-50%, 15%); in order to set the upper block vertical position as relative to the block itself.
I'm using absolute and relative positioning for my .box, but as you can see the text doesn't seem like it's top: 100%.
Can someone explain why this is?
Like why is the text (some text is going here) spilling into the child element when top: 100% was specified?
.box {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: flex-start;
height: 200px;
margin: 0 auto;
width: 90%;
border: 1px solid red;
}
.child {
position: relative;
width: 100px;
height: 100px;
border: 1px solid orange;
}
.childschild {
position: absolute;
top: 100%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="box">
<div class="child">
<img src="dog1.jpg" alt="Picture of a dog" width="250" height="250">
<div class="childschild">
some text is going here
</div>
</div>
<div class="child">dfd</div>
<div class="child">dfd</div>
</div>
.. why is the text ('some text is going here' text) spilling into the child element when 100% was specified?
Because you've applied the transform property to the element:
.childschild { transform: translate(-50%, -50%); }
This tells the element to shift backward 50% of its width along the x-axis and 50% of its height along the y-axis.
So first you're telling it to be top: 100%. Then you're telling it to backtrack 50% of its height, which puts it back over the .child element. You can remove the transform and it will work as intended.
Try this instead: transform: translateX(-50%); (demo)
A more complete explanation with illustrations can be found here:
Element will not stay centered, especially when re-sizing screen
The problem is because in your transform you are applying -50% to Y-axis , which means will subtract that 50% from top:100%, So it will be has it you stated just top:50% with no transform
Either you only apply the transform to X-axis removing the Y-axis, as stated by #Michael_B, or you can use this code below to vertically/horizontally center the text just below the box:
position: absolute;
top: 100%;
right: 0;
left: 0;
margin: auto
Note: I changed your img to background-img (optional)
.box {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
height: 200px;
margin: 0 auto;
width: 90%;
border: 1px solid red;
}
.child {
width: 100px;
height: 100px;
border: 1px solid orange;
}
.box > div:first-of-type {
background: url("//dummyimage.com/100x100");
position: relative
}
.box > div:first-of-type div {
position: absolute;
top: 100%;
right: 0;
left: 0;
margin: auto;
width: 50px;
text-align:center;
background:red
}
<div class="box">
<div class="child">
<div>some text is going here</div>
</div>
<div class="child">dfd</div>
<div class="child">dfd</div>
</div>
If i understood you correctly, this could help you..
Try using margin-top: -10px;, it will reduce top's space by 10px. You can also try marign-left,margin-right and margin-bottom.
This is my first post on this website and i will try to make my question as clear as possible. If it isn't clear i will try and explain as best as i can.
I'm making a responsive design with 2 images, later on i will add some more.
one of the images is the a part of the head which needs to be always on the bottom. The other part needs to be always on top. This needs to be responsive.
I did some research and found that the best way to do this is with %. I will post some code of my tries.
The code below this is just a technique that can be used to achieve something like i want.
.wrapper {
border: 2px solid #000;
position: absolute;
bottom: 0;
width: 90%;
margin: 0;
}
.outer {
position: relative;
width: 40%;
height: 120px;
margin: 0 auto;
border: 2px solid #c00;
overflow: hidden;
}
.inner {
position: absolute;
bottom: 0;
margin: 0 25%;
background-color: #00c;
}
.inner-onder {
position: absolute;
text-align: center;
top: 0;
background-color: #00c;
margin: 0 25%;
}
img {
width: 50%;
height: auto
}
<div class="wrapper">
<div class="outer">
<img class="inner " src="http://img.india-forums.com/images/600x0/57963-still-image-of-pooja-gaur.jpg" />
</div>
<div class="outer">
<img class="inner-onder " src="http://img.india-forums.com/images/600x0/57963-still-image-of-pooja-gaur.jpg" />
</div>
</div>
I have used relative width and an absolute position/horizontal transform of the parent container to give you the look you want.
Note: I took care of the gap created below the images in their containers by giving the containers a line-height: 0;
.container {
width: 50%;
border: 1px solid red;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
.head, .body {
text-align: center;
line-height: 0;
}
img {
width: 100%;
}
/* in the case of the images I was working with I had to add the styles below because the head image was enlarged after being sliced from the body image. If you don't resize the head when you split the picture you won't need the extra styling */
.head img {
width: 38%;
transform: translateX(-14%)
}
<div class="container">
<div class="head"><img src="http://c7ee2562.ngrok.io/portfolio/img/head.png" alt="" /></div>
<div class="body"><img src="http://c7ee2562.ngrok.io/portfolio/img/thinkingn.png" alt="" /></div>
</div>
I am trying to build a timeline to display a series of events, there may be many events added to the timeline so its container is set to autoflow, for visual purposes I have the code here: http://codepen.io/tomevans1664/pen/QyLObL. This all works fine in chrome, but in IE the vertical timeline bar is not bound by the overflow.
HTML:
<div class="center">
<div class="timeline">
<div class="line">
</div>
<div class="content"></div>
</div>
</div>
CSS:
body{
margin: 0;
padding: 0;
}
.center{
margin-left: auto;
margin-right: auto;
width: 800px;
height: 500px;
border: 1px solid #dddddd;
overflow-y: auto;
overflow-x: none;
}
.line{
position: absolute;
top: 0;
left: 50%;
height: 100%;
width: 5px;
background: #d7e4ed;
}
.content{
height: 1000px;
}
.timeline{
position: relative;
width: 100%;
min-height: 50px;
}
IE Screenshot:
http://imgur.com/0pUVtOi
So it appears, for IE, that to overflow absolutely positioned content, the containing div that has the overflow attribute must also use absolute positioning, although this does break the auto margin.
To get .center centered again i used:
left: 50%;
transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
I am trying to create a div that is covers the browser window diagonally. See example here:
This is my CSS:
.shape {
height: 100%;
width: 150%;
transform: rotate(25deg);
}
This is my actual result:
I've tried a bunch of different things using transformOrigin and setting top and left of the div, but nothing seems to work to have this div centered diagonally across the browser.
You need to add these: transform-origin: center;
Also when width is more than 100% you need to move content its centered before rotate. Like Position: absolute; left: -25%;
body {
padding: 0px;
margin: 0px;
}
.frame {
width: 100vw;
height: 100vh;
background: #EFEFEF;
}
.rotated {
position: absolute;
left: -25%;
width: 150%;
height: 100%;
border: 2px solid blue;
transform: rotate(25deg);
transform-origin: center;
}
<div class='frame'>
<div class='rotated'></div>
</div>