Here is my fiddle:
JSFiddle
You can find the sources here in a JSFiddle.
CSS
.container {
width: 100%;
height: 350px;
float: right;
}
.box {
margin-top: -350px;
float: right;
width: 100%;
}
.box:first-child {
margin-top: 0;
}
img {
width: 50%;
height: 350px;
float:right;
}
.top-section {
width: 50%;
background-color: red;
height: 175px;
}
.bottom-section {
width: 50%;
background-color: blue;
height: 175px;
}
HTML
<div class="container">
<div class="box">
<img src="http://st.hzcdn.com/simgs/7d0137b70f7848d3_3-3892/modern-futons.jpg">
<div class="top-section">
some text.......
</div>
<div class="bottom-section">
some other text.....
</div>
</div>
<div class="box">
<img src="http://i.ebayimg.com/00/s/MzAwWDMwMA==/z/JLYAAOSwxH1UBi1~/$_35.JPG?set_id=2">
<div class="top-section">
some text.......
</div>
<div class="bottom-section">
some other text.....
</div>
</div>
</div>
I have two divs stacked on top of each other (div.box) using margin-top. each div.box is divided into 3 section: a section for a picture, one for some text and another for some other text.
I'm trying to make div.top-section and div.bottom-section the same height and my problem is this:
I want my pictures to be responsive using width:100%;height:auto but I want at the same time, whenever a user resize the window, div.top-section and div.bottom-section don't loose their height relevant to the picture. I mean I want div.top-section and div.bottom-section, each have 1/2 image height and using % unit and not pixel.
How can I achieve this? Thanks in advance.
You can do this with absolute positioning. You can position the container your holding this all in to relative and then posiiton all of your inner content to absolute. Then give your container a padding-bottom of a percentage and play with this percentage until you get your desired height. Then give your text section absolute positioning and a height of 50% and your image a height of 100% and width of 50% and position this to the right.
This can be acheived one of 2 ways. You can use just the image and position that absolutely like so:
html:
<div class="container">
<div class="text-1">
Text 1
</div>
<div class="text-2">
Text 2
</div>
<img src="http://st.hzcdn.com/simgs/7d0137b70f7848d3_3-3892/modern-futons.jpg"/>
</div>
Css:
.container{
position: relative;
padding-bottom: 60%;
}
.text-1{
position: absolute;
left: 0;
top: 0;
height: 50%;
width: 50%;
background: red;
}
.text-2{
position: absolute;
left: 0;
top: 50%;
height: 50%;
width: 50%;
background: blue;
}
.container img{
position: absolute;
top: 0;
right: 0;
width: 50%;
height: 100%;
}
Here is a working fiddle of that method Fiddle
Next if you don't want to lose the aspect ratio of the image. Say you have a tall image and a short image and you want to use this multiple times on your page. You can put another div in the container and position this to the right and place your image as the background of this div like so:
Html:
<div class="container">
<div class="text-1">
Text 1
</div>
<div class="text-2">
Text 2
</div>
<div class="image-section" style="background:url('http://st.hzcdn.com/simgs/7d0137b70f7848d3_3-3892/modern-futons.jpg')"></div>
</div>
Css:
.container{
position: relative;
padding-bottom: 60%;
}
.text-1{
position: absolute;
left: 0;
top: 0;
height: 50%;
width: 50%;
background: red;
}
.text-2{
position: absolute;
left: 0;
top: 50%;
height: 50%;
width: 50%;
background: blue;
}
.image-section{
position: absolute;
right: 0;
top: 0;
background-size:cover !Important;
background-position:center !Important;
backgroung-repeat:no-repeat !Important;
height: 100%;
width: 50%;
}
Here is a working fiddle of that Fiddle
Related
I am trying to add a face overlay to an image, where I want the face to always be within the inner div.
The image needs to fit over a 16/9 ratio backdrop and always fit within the outer container, I have this working where the outside div is greater height than a 16/9 ratio, however whenever it drops below 16/9 it chops the bottom and top of the head off.
I need this to work with IOS so aspect-ratio does not work on safari, hence me using the old padding-top trick for 16/9.
Can anyone help me?
What I want is something like this...
But what i'm getting is this...
.outside {
position: relative;
width: 500px;
height: 200px;
background: red;
overflow: hidden;
}
.inside {
height: 0;
overflow: hidden;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding-top: 56.25%;
background: blue;
}
.img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: contain;
}
<h1>Works when height is bigger</h1>
<div class="outside" style="height:400px!important">
<div class="inside" style="padding-top: 56.25%;">
<img src="https://solvemoji-test.s3.eu-west-2.amazonaws.com/unnamed.jpg" class="img">
</div>
</div>
<h1>Does not work when height is smaller</h1>
<div class="outside">
<div class="inside" style="padding-top: 56.25%;">
<img src="https://solvemoji-test.s3.eu-west-2.amazonaws.com/unnamed.jpg" class="img">
</div>
</div>
Assuming you know the height of outer (as you do in the question) you can use the CSS min function to decide what the width and height of the inner element should be to ensure it has a 16/9 ratio.
This snippet removes the img element and replaces it with a background image to the inner element. The height of outer is passed as a CSS variable rather than as a height setting so that calculations can be done on it.
The results are:
.outside {
position: relative;
width: 500px;
height: calc(var(--h) * 1px);
background: red;
overflow: hidden;
}
.inside {
height: 0;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding-bottom: 56.25%;
background-color: blue;
--minw: calc(var(--h) * 16px / 9);
width: min(100%, var(--minw));
background-image: url(https://solvemoji-test.s3.eu-west-2.amazonaws.com/unnamed.jpg);
background-size: auto min(100%, calc(var(--h) * 1px));
background-position: center;
background-repeat: no-repeat no-repeat;
}
<h1>Height is bigger</h1>
<div class="outside" style="--h: 400">
<div class="inside">
</div>
</div>
<h1>Height is smaller</h1>
<div class="outside" style="--h: 200;">
<div class="inside">
</div>
</div>
Note that if the height of outer is not known in advance then I think you will have to resort to JS to set --h at run time.
You might not need absolute positioned image inside the absolute positioned div, for instance
.outside {
position: relative;
width: 500px;
height: 200px;
background: red;
}
.inside {
position: absolute;
top: 0;
right: 72px;
bottom: 0;
left: 72px;
display: flex;
justify-content: center;
background: blue;
}
.img {
max-height: 100%;
object-fit: contain;
}
<div class="outside">
<div class="inside">
<img class="img" src="https://solvemoji-test.s3.eu-west-2.amazonaws.com/unnamed.jpg" />
</div>
</div>
I want a child div to stick to the bottom of parent div on the border. Something like the div below. But I want that red div should always be on the center of the border vertically, meaning the child div should stick to the bottom border of parent div with 50% inside the parent and 50% outside. So if the height of parent div changes the red still stays on the center vertically.
The way I did was to give bottom: -20px, but if I change the height of the parent then the div won't stay in the center vertically to the bottom border.
Here's the code I came up with.
.parent {
position: absolute;
background-color:black;
width: 200px;
height: 200px
}
.another-parent {
position: relative;
float:right;
background-color:black;
width: 200px;
height: 70px
}
.child {
position: absolute;
background-color: red;
width: 50px;
height: 50px;
bottom: -20px;
}
<div class="parent">
<div class="child">
</div>
</div>
<div class="another-parent">
<div class="child">
</div>
</div>
You can change the height and the child div will stay in place. And the parental diva needs to be given a position: relative.
.parent {
position: relative;
background-color:black;
width: 200px;
height: 200px
}
.child {
position: absolute;
background-color: red;
width: 50px;
height: 50px;
bottom: -25px;
right: 75px;
}
<div class="parent">
<div class="child">
</div>
</div>
Use translateY(50%) combined with bottom:0. This will work for any height including child and parent element:
.parent {
position: absolute;
background-color:black;
width: 200px;
height: 200px
}
.another-parent {
position: relative;
float:right;
background-color:black;
width: 200px;
height: 70px
}
.child {
position: absolute;
background-color: red;
width: 50px;
height: 50px;
bottom: 0;
transform:translateY(50%);
}
<div class="parent">
<div class="child">
</div>
</div>
<div class="another-parent">
<div class="child">
</div>
</div>
Use percentages for perfect and responsive output.
.parent {
position: absolute;
background-color:black;
width: 200px;
height: 200px
}
.another-parent {
position: relative;
float:right;
background-color:black;
width: 200px;
height: 70px
}
.child {
position: absolute;
background-color: red;
width: 50px;
bottom: -20px;
left: 35%;
}
<div class="parent">
<div class="child">
</div>
</div>
<div class="another-parent">
<div class="child">
</div>
</div>
Let me know if it works for you.
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;
}
I am a CSS beginner.
I want a half transparent centered div with the main content. Below it should be a fixed div containing the table of contents.
Below is my attempt on this. This works with a certain browser size. But when the size of the browser window changes, the table of content moves.
I want the table of contents to stay at a fixed distance to the main div.
jsFiddle link
With this window size everything looks ok:
Decreasing the window size moves toc under content div:
html
<html>
<head>
<title>Testpage</title>
<link rel='stylesheet' href='css/testpage.css'>
</head>
<body>
<div id="contenttable">
<h1>Contents</h1>
Content 01<br>
</div>
<div id="content">
some text
</div>
</body>
</html>
css:
#content{
height: 1000px;
width: 320px;
position: relative;
top: 50px;
left: 50%;
margin-left: -160px;
background-color: cyan;
}
#contenttable{
padding: 12px;
width:100%;
height:200px;
position: fixed;
background-color: yellow;
top: 125px;
left: 6%;
}
#contenttable a{
position: relative;
top: 0px;
left: 66%;
}
#contenttable h1{
position: relative;
top: 0px;
left: 66%;
}
You can use an inner div absolutely positioned inside the fixed TOC, and set its position.
Use CSS3 Calc to elaborate the right position for your main content.
Use opacity for transparency, and avoid setting the height of the main content div for automatic overflow handing.
Demo: http://jsfiddle.net/vMAQz/1/
CSS
#contenttable {
padding: 12px;
width:100%;
height:200px;
position: fixed;
background-color: yellow;
top: 125px;
}
#innerContent {
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 100px;
padding: 30px;
}
#content {
padding: 10px;
opacity: 0.8;
width: 320px;
position: relative;
top: 50px;
left: calc(100% - 480px);
background-color: cyan;
}
HTML
<div id="contenttable">
<div id="innerContent">
<h1>Contents</h1>
Content 01
<br/>
</div>
</div>
<div id="content">
some text
</div>
all you need to do is change the width of the content div
#content{
height: 1000px;
width: 30%;
position: relative;
top: 50px;
left: 50%;
margin-left: -160px;
background-color: cyan;
}
i have div area which is devided in to 4 equal parts, like the one atached.
now i need another div to be placed at the bottom area as an overlay to the above div. Imagine it like a text scroll area on the bottom side of the TV and the TV screen is constructed by 4 divs.
I am able to create the 5 divs. now the issue is that the 5th div(scroll area) is not going above the bottom edge of the 2 lower divs (3 and 4). I also had put z-index also but failed
can anybody share a sample for styling this.
You can solve it this way:
HTML:
<div class="area"></div>
<div class="area"></div>
<div class="area"></div>
<div class="area"></div>
<div class="overlay"></div>
CSS:
.area{
float: left;
width: 49%;
height: 300px;
border: 1px solid black;
}
.overlay{
width: 200px;
height: 100px;
background-color: blue;
clear: both;
position: absolute;
bottom: 30px;
margin: -100px;
left: 50%;
}
Please note that I have used hard coded example values. The actual values depends on which context the markup is in.
Without your code it's hard to figure what's not working.
If I understand what you want this is what I would have done:
<div class="container">
<div class="block1"></div>
<div class="block2"></div>
<div class="block3"></div>
<div class="block4"></div>
<div class="overlay"></div>
</div>
css:
.container {
position: relative;
width: 600px; /* use the size you want */
height: 400px;
}
.container div {
position: absolute;
width: 50%;
height: 50%;
}
.container .block1 { top: 0; left: 0; background: pink; }
.container .block2 { top: 50%; left: 0; background: red; }
.container .block3 { top: 0; left: 50%; background: green; }
.container .block4 { top: 50%; left: 50%; background: blue; }
.container .overlay {
position: absolute;
width: 80%;
height: 100px;
left: 10%;
bottom: 30px; /* distance from the bottom */
z-index: 1;
background: yellow;
}