Center an overflowing image - html

I'm trying to make an image that overflows on it its parent div, but that's centered according to its parent.
Heres how I'd like it to look:
This is the code I currently have but obviously doesn't work,
.wrapper{
height:150px;
width:150px;
position:relative;
background:red;
margin:5em auto;
}
.image{
width:175%;
height:auto;
position:absolute;
}
body{
background:purple;
}
<div class="wrapper">
<img class="image" src="https://pngimg.com/uploads/goat/goat_PNG13151.png">
</div>
JSFiddle
Fiddle
I want to achieve this in pure css, no use of javascript.

You can center your image with the "negative translate" trick.
Here's a working example:
.wrapper {
height: 150px;
width: 150px;
position: relative;
background: red;
margin: 5em auto;
}
.image {
width: 175%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
body {
background: purple;
}
<div class="wrapper">
<img class="image" src="https://pngimg.com/uploads/goat/goat_PNG13151.png">
</div>

Based on this question.
.wrapper{
height:150px;
width:150px;
position:relative;
background:red;
margin:5em auto;
}
.image{
width:175%;
height:auto;
position: absolute;
top: -9999px;
bottom: -9999px;
left: -9999px;
right: -9999px;
margin: auto;
}
body{
background:purple;
}

Try this:
.image{
width:175%;
height:auto;
position: absolute;
top: -9999px;
bottom: -9999px;
left: -9999px;
right: -9999px;
margin: auto;
}
It should center your <img> no matter the size of the parent div

Oh looks like I'm late to this party, but I was going to suggest this technique - using the ::after pseudo element to draw your square underlay and don't actually make the image overflow the wrapper div.
https://codepen.io/hamzatayeb/pen/QMxJvw
<div class="wrapper">
<img class="image" src="http://pngimg.com/uploads/goat/goat_PNG13151.png">
</div>
Then this CSS -
.wrapper {
width: 262px;
height: 262px;
position: relative;
margin: 5em auto;
}
.wrapper::after {
content: "";
background: red;
position: absolute;
top: 16%; right: 16%; bottom: 16%; left: 16%;
z-index: -1;
}
.image {
padding-top: 25px;
width: 100%;
height: auto;
}
body {
background: purple;
}

Try this add this style only to your image
.image{
width:175%;
height:auto;
position:absolute;
top: -18px;
right: -65px;
}
JS Fiddle

Related

Placing text over absolute image

A similar question has been asked many times (how to place text over an image) but every solution says make a relative positioned container and place image and text inside.
But what if the container needs to be absolute??
I want the image to be absolute in order to span the full width of the page, without being limited by the wrapper's width: Wrapper has set width, the image container should ignore this and be full screen, and the text should float above the image.
Here is a fiddle in which the image isn't ignoring the wrapper's width
.splash_image {
position: relative;
left: 0;
top: 2%;
height: 600px;
overflow: hidden;
z-index: 1;
}
.splash_image img {
width: 100%;
position: absolute;
}
.splash_title {
color: red;
position: absolute;
}
.wrapper {
width: 50%;
}
<div class="wrapper">
<div class="splash_image">
<img src="http://fc07.deviantart.net/fs34/f/2008/290/6/4/Large_Tree_Stock_by_HauntingVisionsStock.jpg" alt="test image">
<div class="splash_title">Test title to go here on image</div>
</div>
</div>
You set relative positioning on the image container, so even though you've positioned the image absolutely, it's being positioned absolutely within a relative positioned container. The container should be positioned absolutely if I am understanding what you're looking for:
.splash_image{
position:absolute;
left:0;
top:2%;
height:600px;
width:100%;
overflow: hidden;
z-index: 1;
}
.splash_image img{
width:100%;
}
.splash_title{
color:red;
z-index: 88;
position:absolute;
top:0;
left:0;
}
Updated Fiddle
There are multiple ways to accomplish this. Here is a simple answer:
.splash_image {
position: absolute;
left: 0;
top: 2%;
height: 600px;
overflow: hidden;
z-index: 1;
}
.splash_image img {
width: 100%;
}
.splash_title {
color: red;
position: absolute;
z-index: 2;
}
.wrapper {
width: 50%;
}
<div class="splash_image">
<img src="http://fc07.deviantart.net/fs34/f/2008/290/6/4/Large_Tree_Stock_by_HauntingVisionsStock.jpg" alt="test image" />
</div>
<div class="wrapper">
<div class="splash_title">Test title to go here on image</div>
</div>
https://jsfiddle.net/jonnysynthetic/2vqgab7t/
However, you could also try setting the image as a background to the parent element as well. I wasn't sure of the scope of what this is in or a part of, so I wanted to give you the simple answer first.
.splash_image{
left: 0;
top: 2%;
overflow: hidden;
width: 100%;
z-index: 1;
height: 100%;
}
.splash_image img{
width:100%;
position:absolute;
}
.splash_title{
color: red;
z-index: 88;
position: absolute;
top: 50%;
left: 50px;
right: 0;
bottom: 0;
margin: auto;
}
.wrapper{
width: 50%;
height: 150px;
position: relative;
}
https://jsfiddle.net/2tpbx12x/5/
try this:
.splash_image img{
width:100%;
position:fixed;
}

Centre div over four divs within container on responsive site

This is the image I have:
How do I centre the black circle, I have tried a number of ways, best has been using absolute, but i cannot make it responsive.
Its on JSFIDDLE
And here is the code:
HTML
<div class="main">
<div class="center"></div>
<div class="leftTop"></div>
<div class="rightTop"></div>
<div class="leftBottom"></div>
<div class="rightBottom"></div>
</div>
CSS
.main {
position:relative;
width:100%;
height:100%;
}
.rightTop {
float:right;
background-color:red;
min-width:50%;
height:250px;
}
.leftTop {
float:left;
background-color:blue;
min-width:50%;
max-width:50%;
height:250px;
}
.rightBottom {
float:right;
background-color:yellow;
min-width:50%;
height:250px;
}
.leftBottom {
float:left;
background-color:orange;
min-width:50%;
max-width:50%;
height:250px;
}
.center {
position:absolute;
left: 50%;
top: 50%;
height:400px;
background-color:black;
width:400px;
border-radius:50%;
}
As I have said above, I have managed to centre it using LEFT, TOP but it is not responsive. Also it's not 50% as I would expect.
Any ideas what it is i am doing incorrectly ?
You could use positioning for this (getting rid of those inefficient and horrible float elements), in combination with the calc css3 property.
You may also be interested in using vw units, in which I have used to make the circle responsive to the width of the screen:
html,
body {
margin: 0;
padding: 0;
}
.wrap {
margin: 5vw;
height: 80vh;
width: 90vw;
display: inline-block;
position: relative;
}
.wrap div {
position: absolute;
height: 50%;
width: 50%;
}
.wrap .red {
background: tomato;
top: 0;
left: 0;
}
.wrap .yellow {
background: yellow;
top: 0;
left: 50%;
}
.wrap .green {
background: lime;
top: 50%;
left: 0;
}
.wrap .blue {
background: cornflowerblue;
top: 50%;
left: 50%;
}
.wrap .black {
background: black;
height: 20vw;
width: 20vw;
border-radius: 50%;
top: -webkit-calc(50% - 10vw);
top: calc(50% - 10vw);
left: -webkit-calc(50% - 10vw);
left: calc(50% - 10vw);
}
<div class="wrap">
<div class="red"></div>
<div class="yellow"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="black"></div>
</div>
just add margin-left:-200px; in
.center {
position:absolute;
left: 50%;
top: 50%;
height:400px;
background-color:black;
width:400px;
border-radius:50%;
margin-left:-200px;
}
here is the updated fiddle file
DEMO
Added:
top: 50%;, and left: 50%; to make it displayed relative to its parent: .main { position: relative
Added transform: translate(-50%, -50%) to center it. To center it on its own center point :D
You should be clearing the floats in your main container.
To do so add this to the main element:
<div class="main">
<div class="center"></div>
<div class="leftTop"></div>
<div class="rightTop"></div>
<div class="leftBottom"></div>
<div class="rightBottom"></div>
<div class="clearfix"></div>
</div>
<style>
/* Add this to your CSS */
.clearfix{
clear:both;
}
</style>
This will make the main container expand to the height of those floaters. After that you can use:
.center{
margin-top:-200px;
position:absolute;
left: 50%;
top: 50%;
height:400px;
background-color:black;
width:400px;
border-radius:50%;
}
**OR**
.center {
position:absolute;
left: 50%;
top: 50%;
height:400px;
background-color:black;
width:400px;
border-radius:50%;
transform:translate(-50%,-50%); /* This property doens't rely on pixels of the element, so the element can also be defined in percentages */
-webkit-transform:translate(-50%,-50%);
}
Add this css in your code:
.center {
background-color: #000000;
border-radius: 50%;
height: 400px;
left: 0;
margin: 0 auto;
position: absolute;
right: 0;
text-align: center;
top: 50%;
width: 400px;
}
See demo http://jsfiddle.net/JentiDabhi/gnhwork9/1/

How to resize to fit and center image in a position absolute div?

I need an image to be resized to fit in inside a div. This div must, necessarely, no matter what, be an position: absolute; div. Apart from the image have 100% from its greatest dimension, it should be centered in the other way.
I could resize to fit it, but can't center. I tried to make it inline and use vertical-align, but it didn't work.
Since code worth more than words, check my fiddle example.
This is the code from the jsfiddle:
CSS:
.relative {
width: 400px;
height: 400px;
position: relative;
<!-- Next is not important, only to display better -->
display: block;
background-color: green;
border: 3px solid yellow;
margin-bottom: 10px;
}
.absolute {
position: absolute;
top: 20px;
bottom: 20px;
left: 20px;
right: 20px;
background-color: red;
}
img {
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
}
HTML:
<div class="relative">
<div class="absolute">
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg"/>
</div>
</div>
<div class="relative">
<div class="absolute">
<img src="http://us.123rf.com/400wm/400/400/pashok/pashok1101/pashok110100126/8578310-vertical-shot-of-cute-red-cat.jpg"/>
</div>
</div>
you may put the image to background instead of an img tag.
<div class="absolute">
<img src="//upload.wikimedia.org/wikipedia/commons/5/52/Spacer.gif">
</div>
.absolute {
background-image: url(http://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
however, if you can set a fixed height for the div, you can use this:
.absolute { line-height:360px; }
.absolute img { vertical-align:middle; }
Only for semi-new browsers:
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Absolutely position all the things!
transform still needs browser prefixes I hear. -webkit- works for me.
http://jsfiddle.net/rudiedirkx/G9Z7U/1/
Maybe I did not understand the question…
.absolute {
position: absolute;
top: 20px;
bottom: 20px;
left: 20px;
right: 20px;
background-color: red;
line-height:350px; //new
}
img {
position:relative;
display:inline-block; // new
vertical-align:middle; // new
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
}

Vertical-align absolute positioned div

I'm fiddling with Jquery Cycle slideshow and trying to add a couple of buttons. I can't seem to align them veritcally without top: #px; tomfoolery; I'd love to just align it to middle of the div vertically.
CSS
#slidecontainer {
margin-left: auto;
margin-right: auto;
position: relative;
width: 800px;
height: 200px;
}
.slidecontrols {
top: 50px;
position: absolute;
z-index: 100;
width: 100%;
}
.slidecontrols a {
background-color: white;
}
.slidecontrols a.next {
position: absolute;
right: 0;
}
.slideshow {
width: 100%;
height: 100%;
}
.bannered {
height: 200px;
width: 800px;
}
HTML
<div id="slidecontainer">
<div class="slideshow" id="slideoptions">
<img src="http://i.imgur.com/ufZ0cxL.jpg" class="bannered" alt="" /></a>
<img src="http://i.imgur.com/RZTrFy4.jpg" class="bannered" alt="" /></a>
</div>
<div class="slidecontrols">
right
left
</div>
</div>
Here's a Fiddle. Adding vertical-align: middle; to .slidecontrols does absolutely nothing.
Here's another option if you don't want to guess or set any pixels:
.slidecontrols {
top: 50%;
position: absolute;
z-index: 100;
width: 100%;
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
}
Assuming the height of the controls will be 20px, you could use a top value of 50% and then a negative margin-top value of half the element's height. In this case, -10px.
Example Here
.slidecontrols {
top: 50%;
margin-top: -10px;
position: absolute;
z-index: 100;
width: 100%;
}
Alternatively, if you need a solution for dynamic heights:
Example Here
.slidecontrols {
position:absolute;
top:0; right:0;
bottom:0; left:0;
z-index: 100;
width: 100%;
height:100%;
display:table;
}
.slidecontrols a {
display:table-cell;
vertical-align:middle;
}
Make top : 50% to slidecontrols which will align the links exactly at the center.
.slidecontrols {
top: 50%;
position: absolute;
z-index: 100;
width: 100%;
}
Another posibility if you consider the buttons has 20px height,
top: calc(50% - 10px); // 10px is half of buttons height
This will align it exactly at the center
When you have position: absolute, the vertical align will not work.

How to make a transparent part of a div

Im trying to make part of a div transparent so the transparent part can show the background pattern ( a complicated one made with css).
So i have a view_main div and 2 other small divs , divs that will be transparent and show the background
#View_main{
margin-left:7%;
top: 15%;
height:100%;
width:70%;
background-color:white;
position:relative;
border:0;
z-index:1;
}
the left_space div
#left_space{
height:12%;
width:12%;
background-color:transparent;
margin: auto;
position: absolute;
top: 0; left: -100%; bottom: 0; right: 0;
}
the right_space div
#right_space{
height:12%;
width:12%;
background-color:red;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: -100%;
}
i have tried to make the left_space with z-index=2 and the view_main z-index=1 and still nothing ,
Here is a simple example, i im trying to show the background (in this case is green but in my code is a pattern ,or image) from the left_space div
I have also tried the opacity but still nothing!
does someone have any idea?
here it is a visual rapresentation
Here's code for creating blue shape using before and after pseudo-classes
body {
background-color: green;
}
.container {
margin: 50px auto;
height: 300px;
width: 210px;
background-color: blue;
position: relative;
}
.container:before, .container:after {
content: "";
height: 44%;
position: absolute;
background-color: blue;
z-index: -1;
width: 112%;
left: -6%;
}
.container:before {
top: 0;
}
.container:after {
bottom: 0;
}
DEMO
Use opacity property in the div that you want to make transparent and set its value from 0.1 to 1
Reference Link on w3cschools
From your above diagram and link to code provide by you
I modified your code to get that structure:
<!DOCTYPE html>
<html>
<style>
body{
background-color:green;
}
#View_main{
margin-left:7%;
top: 15%;
height:300px;
width:210px;
background-color:blue;
position:relative;
border:0;
}
#left_space{
height:12%;
width:12%;
background-color:green;
margin: auto;
position: absolute;
top: 0; left: -88%; bottom: 0; right: 0;
opacity:1;
}
</style>
<body>
<body><div id="View_main">
<div id="left_space"></div>
</div>
</body>
</body>
</html>
I can refer you,
That the #right_space you can give green color
http://jsfiddle.net/5BZdF/3/
Check this
You can use a transparent box with a large box-shadow drawn with :before or :after pseudo elements.
HTML:
<div id="View_main"></div>
CSS:
#View_main {
position:relative;
overflow: hidden;
height:300px;
width:210px;
}
#View_main:before {
box-shadow: 0 0 0 1000px blue;
position: absolute;
margin-top: -40px;
content: '';
height: 80px; /* Change width and height to increase or decrease transparent box area */
width: 20px;
opacity: 1;
top: 50%;
left: 0;
}
body{
background-color:green;
}
#View_main {
position:relative;
overflow: hidden;
margin-left:7%;
height:300px;
width:210px;
border:0;
top: 15%;
}
#View_main:before {
box-shadow: 0 0 0 1000px blue;
position: absolute;
margin-top: -40px;
content: '';
height: 80px;
width: 20px;
opacity: 1;
top: 50%;
left: 0;
}
<body>
<div id="View_main"></div>
</body>