negative margin and background - html

In the following mark up, I simply want to set the negative margin for the .text div so that it appears on the top of the .image div.
<div class="wrap">
<div class="image"><img src="imgage.jpg" /></div>
<div class="text">text with background</div>
</div>
.image {
overflow: hidden;
}
.text{
background: green;
padding: 20px;
margin-top: -60px;
color: #FFFFFF;
overflow: hidden;
}
I have set the background for the .text, but for some reason it does not appear on the image. Only the text appears. You can see the problem here: http://jsfiddle.net/ovkn4egc/
How I can fix it without using the absolute position.
Thanks.

You can simply add position:relative; to .text
DEMO
* {
margin: 0;
padding: 0;
}
.image {
overflow: hidden;
}
.image img {
display: block;
}
.text {
position:relative;
background: green;
padding: 20px;
margin-top: -60px;
color: #FFFFFF;
overflow: hidden;
}
<div class="wrap">
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
</div>
<div class="text">text with background</div>
</div>

Updated to :
<div class="wrap">
<div class="text">text with background</div>
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
</div>
</div>
*{
margin: 0;
padding: 0;
}
.image {
overflow: hidden;
}
.image img{
display: block;
}
.text{
background: green;
padding: 20px;
color: #FFFFFF;
overflow: hidden;
position:relative;
top: 0;
}
http://jsfiddle.net/ovkn4egc/4/

If you want the text to only span the width of the image only, there are a couple of changes more you need to make as below.
I believe this is the behaviour you are after:
DEMO
The HTML has to slightly change like below.
<div class="wrap">
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
<div class="text">text with background</div>
</div>
</div>
The CSS for the image div also has to change to such, where we set the position property to absolute:
.image {
overflow: hidden;
position: absolute;
}
Finally, the position property for the text, has to alter as such:
.text{
background: green;
padding: 20px;
margin-top: -60px;
color: #FFFFFF;
overflow: hidden;
position: relative;
}

Change you css to the below code
*{
margin: 0;
padding: 0;
}
.wrap{position:relative;}
.image {
overflow: hidden;
}
.image img{
display: block;
}
.text{
background: green;
padding: 20px;
top: 0px;
color: #FFFFFF;
overflow: hidden;
position: absolute;
width: 100%;
}
As well as your html to below one
<div class="wrap">
<div class="text">text with background</div>
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
</div>
</div>
I have written .wrap as relative because text div should be compare its position according to wrap div otherwise if you use only absolute for text div then it will be positioned according to body.

Related

How do I position a div with a colored background over a preceding image? [duplicate]

This question already has answers here:
How to overlay images
(11 answers)
Closed 3 years ago.
Stumped.
I have a basic card with an image and a text tag I want to float over the image.
The tag should have a background color behind the text and above the image.
The background of the text goes behind the image instead.
https://codepen.io/Mikeritteronline/pen/LwJYaR
html
<div class="card">
<div class="card__image">
<img src="https://placehold.it/1024x768" alt="">
</div>
<div class="card__tag">
<p>Image Tag</p>
</div>
</div>
css
img {
max-width:100%;
height: auto;
margin: auto;
}
.card {
max-width: 320px;
margin: auto;
}
.card__tag p{
background: blue;
color: lime;
padding: 0.25rem;
margin-top: -3rem;
}
You can add position to achieve what you want.
.card {
position: relative;
}
.card__tag p {
position: absolute;
}
I think its best if you position the div not the p like so:
(also generaly try to avoid positioning absolute if you do not have to)
img {
max-width:100%;
height: auto;
margin: auto;
}
.card {
max-width: 320px;
margin: auto;
}
.card__tag{
z-index: 1;
position:relative;
top: -3rem;
}
.card__tag p{
background: blue;
color: lime;
padding: 0.25rem;
}
<div class="card">
<div class="card__image">
<img src="//placehold.it/1024x768" alt="">
</div>
<div class="card__tag">
<p>Image Tag</p>
</div>
</div>
add
position: relative
to the p tag like
.card__tag p{
background: blue;
color: lime;
padding: 0.25rem;
margin-top: -3rem;
position: relative;
}
Add position:relative to .card and position:absolute to .card__tag
Here is the updated fiddle:
img {
max-width: 100%;
height: auto;
margin: auto;
}
.card {
max-width: 320px;
margin: auto;
position: relative;
}
.card__tag p {
background: blue;
color: lime;
padding: 0.25rem;
margin-top: -3rem;
}
.card__tag {
position: absolute;
}
<div class="card">
<div class="card__image">
<img src="//placehold.it/1024x768" alt="">
</div>
<div class="card__tag">
<p>Image Tag</p>
</div>
</div>

Positioning elements around element

I need to position elements on a side like in the image above and have onclick function on them to show the corresponding text. Any info on how to best achieve this to be responsive?
By responseive i mean that the dots and text should always stay on the same position relative to the size of the bottle.
What I did was to put everything inside the container div and then positioned the elements relative to that div and the bottle image absolute to the container div.
It kinda works when container div has fixed dimensions, but I guess there are better ways to do it.
EDIT: Added code! I suck at formatting, sorry.
<div class="bottle-one">
<div class="bottle-one-content">
<div class="bottle-one-image">
<div class="message">
<div class="message-hidden">
<div>
text
</div>
<div>
<img src="assets/images/icons/line_blue.svg" alt="">
</div>
</div>
<a href="#msg1" class="droplet droplet1 js-drop">
<img src="assets/images/icons/droplet.svg">
</a>
</div>
<div class="message">
<div class="message-hidden">
<div>
text
</div>
<div>
<img src="assets/images/icons/line_blue.svg" alt="">
</div>
<a href="#msg2" class="droplet droplet2 js-drop">
<img src="assets/images/icons/droplet.svg">
</a>
</div>
</div>
<div class="message">
<div class="message-hidden">
<div>
text
</div>
<div>
<img src="assets/images/icons/line_blue.svg" alt="">
</div>
<a href="#msg3" class="droplet droplet3 js-drop">
<img src="assets/images/icons/droplet.svg">
</a>
</div>
</div>
<div class="message">
<div class="message-hidden">
<div>
text
</div>
<div>
<img src="assets/images/icons/line_blue.svg" alt="">
</div>
<a href="#msg4" class="droplet droplet4 js-drop">
<img src="assets/images/icons/droplet.svg">
</a>
</div>
</div>
<img src="assets/images/bottle1.png" alt="" class="bottle-one-bottle">
</div>
</div>
.bottle-one {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 30%;
}
.bottle-one-image {
height: 100%;
position: relative;
width: 251px;
}
.message {
left: -340px;
top: 180px;
position: relative;
display: flex;
align-items: center;
justify-content: flex-end;
text-transform: uppercase;
font-size: .7rem;
color: #004197;
height: 30px;
width: 400px;
margin-bottom: 1rem;
}
.message-hidden {
display: flex;
}
.message-hidden div:nth-of-type(1) {
text-align: right;
font-family: 'BrandonGrotesqueWeb-Black', sans-serif;
letter-spacing: 2px;
border-right: 1px solid #004197;
width: 70%;
}
.message-hidden div:nth-of-type(2) {
width: 30%;
display: flex;
overflow:hidden;
}
I came up with this to help you with aligning your element to the left of a div.
By using a mix of [psuedo-elements] and floats, I think this gives you the desired effect you are looking for. Post your code and I'll be more then happy to help you with the other part.
html {
width: 550px;
border: none;;
}
body {
font-family: sans-serif;
color: rgba(0,0,0,.15);
height:200px;
}
body:after {
content: '';
display: block;
clear: both;
}
div {
position: relative;
}
div:after {
font-size: 200%;
position: absolute;
left: 0;
right: 0;
top: 0;
text-align: center;
}
.sibling {
border: 3px solid red;
height:200px;
}
.sibling.root:after {
content: 'Sibling';
color: green;
}
.float {
float: left;
border: 3px solid blue;
height: 90px;
width: 150px;
z-index: 1;
}
.float:after {
content: 'floated Element';
color: red;
}
.root {
overflow: hidden;
}
<div class="float">
</div>
<div class="sibling root">
</div>

How to align 3 images with width 100%

I will make a carousel and i try to align left 3 elements with width 100% each but it is not working.
Can someone tell me how to align 3,4 elements on left to get out of the screens witdh so that i can change the image with the transform css property ?
it worked when i delete from the top of the page but when i insert it,everything is ruined.
.carousel {
width: 100%;
margin: 0;
background-color: #ccc;
position: relative;
overflow-x: hidden;
}
.carousel--list {
margin: 0;
padding: 0;
}
.carousel--slide {
background: #000;
text-align: center;
color:#fff;
width:100%;
display: inline-block;
overflow: hidden;
height: 400px;
padding: 0;
}
.carousel--track{
width: auto;
-webkit-transition: transform 2s;
transition: transform 2s;
}
.carousel--buttons {
content: "";
clear: both;
display: table;
}
.slide--image {
width: 100%;
}
.red{
background: red;
}
.blue{
background: blue;
}
.yellow{
background: yellow;
}
/* transform .*/
<section class="carousel">
<div class="carousel--track">
<div class="carousel--list">
<div class="carousel--slide red" data-index="0">
<img class="slide--image" src="./images/carousel/slider-bg.png">
</div>
<div class="carousel--slide blue" data-index="1">
<img class="slide--image" src="./images/carousel/slider-bg.png">
</div>
<div class="carousel--slide yellow" data-index="2">
<img class="slide--image" src="./images/carousel/slider-bg.png">
</div>
</div>
</div>
<div class="carousel--buttons">
<button onclick="prevSlide()" class="btn--prev btn">Prev</button>
<button onclick="nextSlide()" class="btn--next btn">Next</button>
</div>
</section>
Thanks in advance !
You could use flexbox or bootstrap. With those you will be able to edit your code as a gride and place elements where you wan't.
Flexbox is a css engine. Maybe what you need !
Example for bootstrap :
And flexbox :
Apply white-space: nowrap to the parent .carousel--list class...It will wrap all the inner inline-block items in a single row.
Note: I have applied transform(-30px) to the .carousel--list to give you a visual
Stack Snippet
.carousel {
width: 100%;
margin: 0;
background-color: #ccc;
position: relative;
overflow-x: hidden;
}
.carousel--list {
margin: 0;
padding: 0;
white-space: nowrap;
transform: translateX(-30px);
}
.carousel--slide {
background: #000;
text-align: center;
color: #fff;
width: 100%;
display: inline-block;
overflow: hidden;
height: 150px;
padding: 0;
}
.carousel--track {
width: auto;
-webkit-transition: transform 2s;
transition: transform 2s;
}
.carousel--buttons {
content: "";
clear: both;
display: table;
}
.slide--image {
width: 100%;
}
.red {
background: red;
}
.blue {
background: blue;
}
.yellow {
background: yellow;
}
/* transform .*/
<section class="carousel">
<div class="carousel--track">
<div class="carousel--list">
<div class="carousel--slide red" data-index="0">
<img class="slide--image" src="http://via.placeholder.com/350x150/f00">
</div>
<div class="carousel--slide blue" data-index="1">
<img class="slide--image" src="http://via.placeholder.com/350x150/00f">
</div>
<div class="carousel--slide yellow" data-index="2">
<img class="slide--image" src="http://via.placeholder.com/350x150/f0f">
</div>
</div>
</div>
<div class="carousel--buttons">
<button onclick="prevSlide()" class="btn--prev btn">Prev</button>
<button onclick="nextSlide()" class="btn--next btn">Next</button>
</div>
</section>

Position div behind overlapping div

I've got the following setup http://jsfiddle.net/47x60k4w/529/.
HTML
<div class="header">
header
</div>
<div class="inner_block">
<div class="column">
<img src="xxx" />
</div>
<div class="column">
<img src="xxx" />
</div>
<div class="column">
<img src="xxx" />
</div>
</div>
<div class="footer">
footer
</div>
The inner_block should overlap the header class and the footer should be placed right behind the inner_block.
In my solution I just don't get the footer behind the inner_block without doing not responsible stuff like calling a margin-top with x.xem on it. I just found some links with z-index stuff which didn't worked for me because the inner_block lost his passed height and width from the nested block.
The result should look like this beautiful mockup.
Do you have any ideas?
Thanks in advance.
So I made the following changes to your code:
Remove the position: absolute for the inner-block.
As you are floating the contents of the inner-block you have clear the floats so that the parent container will not lose height.
.inner_block:after {
content: '';
display: block;
clear: both;
}
Whenever using floats, remember to clear it.
Added position: relative to the inner_block to position it over the header and footer.
Added display: block to the img so that you can remove the small space below it characteristic on inline elements (the default display).
Also tinkered a bit with the margins and widths to achieve the layout.
.header {
position: relative;
background-color: black;
width: 100%;
height: 50px;
}
.footer {
clear: both;
background-color: red;
width: 100%;
height: 50px;
}
.inner_block {
position: relative;
/*width: 100%;*/
border: solid 1px black;
padding: 5px;
margin-left: 2.5%;
margin-top: -2.5%;
margin-right: 2.5%;
margin-bottom: 2.5%;
background-color: white;
}
.inner_block:after {
content: '';
display: block;
clear: both;
}
.column {
max-width: 30%;
float: left;
margin-right: 2.5%;
}
.column:first-child{
margin-left: 2.5%;
}
.column:last-child{
margin-left: 0;
}
.column img {
max-width: 100%;
height: auto;
display: block;
}
<div class="header">
</div>
<div class="inner_block">
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg" />
</div>
</div>
<div class="footer">
test
</div>
Hope this gives you a head-start. Check it out and let me know your feedback on this. Thanks!
Alternate Solution:
So here is a solution using a flexbox which is easier to set up:
First remove the floating container and the clearfix.
Now Wrap the inner_block with another div
.inner_block_wrapper {
margin: -2.5% 2.5% 2.5% 2.5%;
background-color: white;
position: relative;
}
.inner_block {
border: solid 1px black;
background-color: white;
padding: 5px;
display: flex;
justify-content: center;
}
.column {
margin: 5px;
}
Using display: flex allows the images to take the available space along the row and justify-content: center aligns it along the center. Check this out!
.header {
position: relative;
background-color: black;
width: 100%;
height: 50px;
}
.footer {
clear: both;
background-color: red;
width: 100%;
height: 50px;
}
.inner_block_wrapper {
margin: -2.5% 2.5% 2.5% 2.5%;
background-color: white;
position: relative;
}
.inner_block {
border: solid 1px black;
background-color: white;
padding: 5px;
display: flex;
justify-content: center;
}
.column {
margin: 5px;
}
.column img {
max-width: 100%;
height: auto;
display: block;
}
<div class="header">
</div>
<div class="inner_block_wrapper">
<div class=" inner_block ">
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg " />
</div>
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg " />
</div>
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg " />
</div>
</div>
</div>
<div class="footer ">
test
</div>
You can even try something as below, your codes were fine just set your .footer margin-top equal to the height of .header and .inner_block using css calc() function.
.header{
position:relative;
background-color:black;
width:100%;
height:50px;
}
.footer{
background-color:red;
width:100%;
height:50px;
margin-top:calc(100% - 82%);
}
.inner_block{
position: absolute;
width:90%;
border:solid 1px black;
padding: 5px;
background-color:white;
margin:-2.5% calc(100% - 97%);
}
.column {
width:30%;
float:left;
margin:0 1.6%;
}
.column img {
max-width:100%;
height:auto;
}
<div class="header">
</div>
<div class="inner_block">
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg" />
</div>
</div>
<div class="footer">
test
</div>
is this what you were looking for ?
.header{
position:relative;
background-color:black;
width:100%;
height:50px;
}
.footer{
clear:both;
background-color:red;
width:100%;
height:50px;
}
.inner_block{
position: absolute;
width:100%;
border:solid 1px black;
padding: 5px;
margin-left: 2.5%;
margin-top:-2.5%;
background-color:white;
}
http://jsfiddle.net/8y4e8L08/
.header {
height: 200px;
width:800px;
background-color:#000;
margin:20px;
}
.header {
margin-bottom: -25px;
}
.inner_block {
width: 35%;
height: 150px;
margin: auto 200px;
background-color:#FFF;
border:1px solid #000;
margin-top: -45px;
}
.column{
max-width:20%;
float:left;
border: 2px soid #999;
margin:25px;
}
.column img{
max-width:100%;
height:auto;
}
.footer {
height: 100px;
margin-top: -25px;
margin:20px;
background-color:#F00;
width:800px;
}
.content {
position: relative;
z-index: 1;
}
<div class="header"></div>
<div class="inner_block">
<div class="column">
<img src="download.jpg"/>
</div>
<div class="column">
<img src="download.jpg"/>
</div>
<div class="column">
<img src="download.jpg"/>
</div>
</div>
<div class="footer">
</div>
Well just using the z-index won't always work. You also need to specify the 'position' property as well so as to define the z-index wrt some position of the frame.
Z-index is a property which defines the 'depth' or 'height' of an element. If your <header> has z-index of '100' and; <div> element defined inside the header, usually it would be shown above it but once you define the z-index:50; since 50<100, <div> element would be hidden behind it.
Example of z-index
1) http://www.w3schools.com/cssref/tryit.asp?filename=trycss_zindex
2) https://css-tricks.com/almanac/properties/z/z-index/
Hope it helps.

align the fixed position image on center for all devices

I have a fixed header and a image below the header.
The image has a property position: fixed.
Now for some devices the image is not in the center as It should be.
Here's the fiddle for it.
The left side has more spaces than the right hand side.
Also above the image, I want to add a p tag which is not showing up for some reason.
Please let me know what's wrong here.
<div class="content-wrapper new-class">
<div class="container">
<div class="row">
<div class="col-xs-12 ">
<div class="center-logo description">
<img src="http://i.imgur.com/ECTLTbK.png" alt="" class="img-responsive">
<P class="step1description" See price</p>
</div>
</div>
</div>
</div>
</div>
Checkout this solution:
body, html {
margin:0;
padding:0;
}
.content-wrapper {
position: relative;
overflow: hidden;
width: 100%;
background-color: #333230;
}
.content-wrapper .center-logo {
position: fixed;
width: 100%;
top: 125px;
left:0;
text-align: center;
margin: 5px 0 15px;
z-index: 1001;
background-color: #333230;
}
.content-wrapper .center-logo img {
margin: 0 auto;
}
.center-logo p.step1description {
position: relative;
top: -30px;
width: 220px;
text-align: center;
color: #fff;
font-size: 12px;
margin: 0 auto;
}
.description {
margin:30px 0 30px 0 !important;
}
.content-wrapper .content {
text-align: center;
width: 100%;
}
.new-class{
padding-bottom: 134px;
top: 134px;
}
<div class="content-wrapper new-class">
<div class="container">
<div class="row">
<div class="col-xs-12 ">
<div class="center-logo description">
<img src="http://i.imgur.com/ECTLTbK.png" alt="" class="img-responsive">
<p class="step1description"> See price</p>
</div>
</div>
</div>
</div>
</div>
You have some typos on html and css! - Here you can find a working fiddle: http://jsfiddle.net/sebastianbrosch/cxumhp9k/1/
Add left:0 on .content-wrapper .center-logo
.content-wrapper .center-logo { left:0;}