HTML overlapping div elements - html

I'm making a website that has a series of offset cards, one with image and one with text and I want to alternate the offset for each line. The only issue is I can't quite figure out how to (while keeping both in the wrapper) make the image smaller than the wrapper in height so that the text card can be at the top and overlap the top and side, like this:
Sorry for the image layout, it was supposed to be landscape. Anyway, the way I have it now, the parent and child are both at the top of the wrapper. I want it so that the text (child) is at the top and the image is slightly shorter so that I get the overlap/overlay effect from the text box on the top as well as the right. I also need to make sure, for responsiveness, that they stay inside the wraper. How should I fix that?
#wrapper{
background-color:green;
}
#parent{
width: 500px;
height:400px;
border: 4px solid blue;
background-size:cover;
}
#child{
width:300px;
height:200px;
border: 3px solid red;
position:relative;
top:0%;
left:80%;
}
<div id="container">
<div id="wrapper">
<div id="parent">
<div id="child">
This is my overlapping Text
</div>
</div>
</div>
</div>

I'm not sure this is exactly what you're going for, but if you add padding to the top of the wrapper, you can offset the child element.
#wrapper {
background-color: green;
padding-top: 50px;
}
#parent {
width: 500px;
height: 400px;
border: 4px solid blue;
background-size: cover;
}
#child {
width: 300px;
height: 200px;
border: 3px solid red;
position: relative;
top: -50px;
left: 80%;
}
<div id="container">
<div id="wrapper">
<div id="parent">
<div id="child">
This is my overlapping Text
</div>
</div>
</div>
</div>
Here's the fiddle:
https://jsfiddle.net/16f8mj39/

I have added z-index to position the text-box in front of image box.Check my code.
#container{ width:100%;position:relative; }
#wrapper{ width:100%;border:1px solid black;position:relative;overflow:auto;height:400px; }
#text_box{ position:absolute;border:1px solid red;width:50%;height:250px;top:10px;right:10px;z-index:1;background-color:red; }
#image_box{ position:absolute;border:1px solid blue; width:70%;height:300px;bottom:10px;left:10px;z-index:0;background-color:blue; }
<div id="container">
<div id="wrapper">
<p>Wrapper</p>
<div id="text_box">
<p>Text Box</p>
</div>
<div id="image_box">
<p>Image Box</p>
</div>
</div>
</div>

Related

Image + Text in DIV and keep aspect ratio

I have the following requirements:
I need a div with an image + text inside.
The div should keep its aspect ratio and all images inside of the divs should have the same height (to do this, I use: Maintain the aspect ratio of a div with CSS).
The image inside of the div should be stretched in order to completely fill the parent div.
I prepared a jsfiddle
I have two main issues:
As you can see, the left div is higher, because it has text inside. This collides with the padding-bottom: 150%-approach which is used to preserve the aspect ratio of the divs when their size is changed. The goal, however, is that all divs have the same size no matter if/how much text they have inside.
Moreover, I would like to change the opacity of the background image when I hover the div. However, the text-opacity should not change. I think this can only be achieved via JS. How?
Add text as position:absolute
.inner-text{
position:absolute;
width:100%;
height:100%;
text-align:center;
z-index:1;
}
.event:hover .inner-text{
background:rgba(255,255,255,0.5)
}
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/test.jpg)">
<div class="inner-text">
Text inner
</div>
</div>
</div>
Update fiddle code: https://jsfiddle.net/f5uxz4mx/8/
.bit-3 {
width: 33.33333%;
float: left;
margin-left: 20px;
}
.event {
margin: 0 auto;
position: relative;
border: 2px solid #000000;
padding-bottom: 150%;
background-size: cover;
}
.inner-text{
position:absolute;
width:100%;
height:100%;
text-align:center;
z-index:1;
}
.event:hover .inner-text{
background:rgba(255,255,255,0.5)
}
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/test.jpg)">
<div class="inner-text">
Text inner
</div>
</div>
</div>
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/20170715_mareados.jpg)">
<div class="inner-text">
Text inner
</div>
</div>
</div>
try adding background-size: contain;height: 0;
.bit-3 {
width: 33.33333%;
float: left;
max-resolution: res;-left: 20px;
}
.event {
margin: 0 auto;
position: relative;
border: 2px solid #000000;
padding-bottom: 150%;
background-size: cover;
}
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/test.jpg);background-size: contain;height: 0;">asdf text
</div>
</div>
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/20170715_mareados.jpg)">
</div>
</div>

Position div over other content

I am using bootstrap and the page width is NOT fixed.
I would like to display a contact form div (grey box below) like this:
So the grey contact form is sort of floating over the blue and white divs.
Thanks!
Here's what I have been trying to do: http://jsfiddle.net/w69j4xam/
<div class="header">
Header
</div>
<div class="content">
<div class="bluediv">
Some text here
</div>
<div class="whitediv">
Some more text here
</div>
<div class="contactform">
Contact Form<br/><br/><br/><br/>
</div>
</div>
body{
padding: 20px;
}
.header{
height: 50px;
background-color: green;
}
.content{}
.bluediv{
height: 150px;
background-color: #AFEEEE;
}
.whitediv{
height: 180px;
background-color: #FFF;
}
.contactform{
background-color: grey;
width: 100px;
position: absolute;
}
In terms of your jfiddle example, all you need to add is a right and a top.
.contactform{
right:50px;
top:100px;
background-color: grey;
width: 100px;
position: absolute;
}
http://jsfiddle.net/w69j4xam/2/
Position the outer div however you want, then position the inner divs using absolute. They'll all stack up.
<style type="text/css">
.inner {
position: absolute;
}
</style>
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
<div class="inner">4</div>
</div>

HTML: div floats are not horizontally aligned

I have two divs that I've placed inside another div:
<div id="outer_div">
<div id="left_div">
<!-- Buttons and text field -->
</div>
<div id="right_div">
<!-- Buttons and drop list -->
</div>
</div>
My style sheet includes the following:
#left_div {
float:left;
}
#right_div {
float:right;
}
Now, when I do this, I expect that the left and right div will be on the left and right of the screen, respectively, and that they will be aligned horizontally as well. The first holds true, however, the second hope is not true. If I put left_div above right_div, the right_div buttons and drop list are on a line below the left_div. If I put right_div above left_div, the divs are almost in line, but the right div is slightly elevated, so that it overlaps a div that is above it.
Try setting a width to your outer_div
#outer_div {
width: 100%; //or whatever width you would like it to be
}
#left_div, #right_div {
width: 50%;
}
here is a sample that works fine for me.
<div id="outer_div">
<div id="left_div">
<div class="button"></div>
<div class="button"></div>
</div>
<div id="right_div">
<div class="button"></div>
<div class="button"></div>
</div>
</div>
and css
#outer_div
{
overflow: hidden;
width: 450px;
background: yellow;
}
#left_div {
float:left;
width:200px;
height: 200px;
border: thin red solid;
}
#right_div {
float:right;
width:200px;
height: 200px;
border: thin blue solid;
}
.button {
width: 75px;
height: 25px;
margin: 5px;
border: thin green solid;
}
and the fiddle: http://jsfiddle.net/cMhpK/

Create div with two divs inside that need to stay centered

I'm making a web site responsive, and on the home page I should insert two "containers" that should be centered and aligned. (containers in this case are two divs with inside images and text)
I wish they would behave in this way
and when the page is "restricted", the two divs should position itself in this way
I tried like this, but it is not exactly what I would get
<div style="">
<div style="width: 300px;float: left;">
div 1
</div>
<div style="width: 300px;float: left;">
div 2
</div>
</div>
I'd try to use display: inline-block property. In this way you don't have to apply 'overflow' for parent and it's pretty easy to make blocks centered.
HTML:
<div class="wrapper">
<div class="box">Div 1</div>
<div class="box">Div 2</div>
</div>
CSS:
.wrapper {
text-align: center;
/* Just decoration */
border: 1px solid blue;
padding: 20px;
}
.wrapper .box {
display: inline-block;
width: 300px;
height: 50px;
/* Just decoration */
border: 1px solid green;
}
Take a look at the fiddle http://jsfiddle.net/caprella/y4BQ3/
I put something quick together for you. You will have to use media queries to find the size of the page when you want the style to switch. Mess around with my example and you should be able to figure something out to your liking.
<div id="box">
<div class="innerBox">
div 1
</div>
<div class="innerBox">
div 2
</div>
<div class="clear"></div>
</div>
And the CSS...
#box {
width:88%;
background:red;
padding:20px 6%;
}
.clear{clear:both}
.innerBox {
width:41%;
float:left;
background:blue;
display:block;
}
.innerBox:first-child {
margin-right:18%;
}
#media screen and (max-width: 400px) {
#box .innerBox {
float:none;
width:100%;
margin:20px 0 0 0;
}
#box .innerBox:first-child {
margin-top:0;
}
}
}
JsFIddle link: http://jsfiddle.net/x3JLX/
Check out this Fiddle. There's only a few simple changes to your existing code, which I included below.
http://jsfiddle.net/ArKKG/
<div style="overflow:auto; height: 100% text-align: center;">
<div style="width: 300px; height: 50px;float: left;">
div 1
</div>
<div style="width: 300px;height: 50px;float: left;">
div 2
</div>
</div>
And some CSS to make them visible, and keep the borders separated.
div{
border: 1px solid black;
margin: 4px;
}

More CSS woes: how to stack divs inside another div, and have them all behave?

Fiddle here: http://jsfiddle.net/csaltyj/mbnWm/
<style type="text/css">
#container {
border: 2px solid #000;
position: absolute;
width: 20em;
}
.box {
background: transparent;
position: absolute;
border: 1px solid #0f0;
}
</style>
<div id="container">
<div class="box">
<p>X</p>
<p>Taller than the other two.</p>
</div>
<div class="box">
<p> Y</p>
</div>
<div class="box">
<p> Z</p>
</div>
</div>
This is not working. They overlap fine, but there are issues. The goal is to:
Get the #container to properly wrap around the children divs.
Have the .box divs fill (width & height) the parent #container (so the green borders should reach all the way out to the thick black border).
This must be possible, I'm just having a hard time with positioning. (that and floats seem to be the toughest parts of CSS)
Thanks for any help.
The problem you have here is that anything that is position: absolute; is removed from flow. Thus, #container can never contain the .boxes. In this cause you will need to set height and width on #container and make sure the .boxes can never expand beyond it. You requested they fill the #container, so I've done that here: http://jsfiddle.net/X3EJ6/
Note though that because width and height are set to 100% the borders will not work correctly. You will need to set explicit values or use box-sizing and set it to border-box (this is not support in IE < 8).
<style type="text/css">
#container {
border: 2px solid #000;
position: absolute;
width: 20em;
height: 10ex;
overflow: hidden;
}
.box {
background: transparent;
position: absolute;
border: 1px solid #0f0;
width: 100%;
height: 100%;
}
</style>
<div id="container">
<div class="box">
<p>X</p>
<p>Taller than the other two.</p>
</div>
<div class="box">
<p> Y</p>
</div>
<div class="box">
<p> Z</p>
</div>
</div>
How to make children auto fit parent's width only with CSS?