This question already has answers here:
Image overlay on responsive sized images bootstrap
(5 answers)
Closed 8 years ago.
I'm trying to have a full width <div> over an image, with text inside it, so that the text goes over the image itself. The grid and the image is responsive, and I can't get the <div> to be 100% of the grid.
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-6">
<img src="http://lorempixel.com/300/300/cats" class="img-responsive portfolio_frontpage" alt="">
<div class="portfolio_description">
<h2>Heading</h2>
<p>Some random tekst blablabla</p> <span class="read_more"></span>
</div>
</div>
<div class="col-md-6">
<img src="http://lorempixel.com/300/300/cats" class="img-responsive portfolio_frontpage" alt="">
<div class="portfolio_description">
<h2>Heading</h2>
<p>Some random tekst blablabla</p> <span class="read_more"></span>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
As you can see, I want the portfolio_desciption to be over the image, fill the width of the grid col-md-6 and have a white background.
Any suggestions on how my CSS should be composed?
tl;dr
Use position: absolute; with top: 0; on the <div> and don't forget to add position: relative; to their parent <div>s.
Solution
.portfolio_description {
position: absolute;
top: 0;
width: 100%;
background-color: white;
}
You also need a new class that you need to add to the parent elements of the <div>s that have the .portfolio_description class.
.portfolio-block {
position: relative;
}
Explanation
The MDN docs on position: relative; states:
The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static).
Also, the MDN docs on top states:
When position is set to absolute or fixed, the top property specifies the distance between the element's top edge and the top edge of its containing block.
So you need absolute positioning and because that is positioned relatively to the nearest non static ancestor, you need to make the parent elements relatively positioned, because that positioning is not static and will keep the element flow intact. Then just make sure the top property is set to 0 so there is no distance between the <div> and its containing block.
Demo
In this example I used a semi-transparent background to prove that it is over the image.
.portfolio_description {
position:absolute;
width:100%;
top:0;
background-color:rgba(255, 255, 255, 0.5);
}
.portfolio-block {
position: relative;
}
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-6 portfolio-block">
<img src="http://lorempixel.com/300/300/cats" class="img-responsive portfolio_frontpage" alt="">
<div class="portfolio_description">
<h2>Heading</h2>
<p>Some random tekst blablabla</p> <span class="read_more"></span>
</div>
</div>
<div class="col-md-6 portfolio-block">
<img src="http://lorempixel.com/300/300/cats" class="img-responsive portfolio_frontpage" alt="">
<div class="portfolio_description">
<h2>Heading</h2>
<p>Some random tekst blablabla</p> <span class="read_more"></span>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
Got it working with the following CSS:
.portfolio_description {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
text-align: center;
}
You'll have to use absolute positioning, something like this
.col-md-6 {
position: relative;
}
. portfolio_description{
position: absolute;
width: 100%;
bottom: 0px;
hieght: 60px;
}
You should use a background image instead of inline images, adjust your code like below then add your image as a background image in your CSS, this gives you a lot more flexibility. this way you can absolutely position everything inside your #portfolio1 div
<div class="col-md-6">
<div id="portfolio1">
<div class="portfolio_description">
<h2>Heading</h2>
<p>Some random tekst blablabla</p>
<span class="read_more"></span>
</div>
</div>
</div>
add the style=" padding-left: 0px ; padding-right: 0px;" in the col-md-6 or in whichever column div u are adding your image , the padding will fix the image entirely in the column without any before and after gap
Related
I am having a problem with my footer. I am trying to force it to the bottom, but it always looks ugly.
Here is the code:
<div id="footer">
<div class="container">
<div class="row">
<h3 class="footertext">About Us:</h3>
<br>
<div class="col-md-4">
<center>
<img src="http://oi60.tinypic.com/w8lycl.jpg" class="img-circle" alt="the-brains">
<br>
<h4 class="footertext">Programmer</h4>
<p class="footertext">You can thank all the crazy programming here to this guy.<br>
</center>
</div>
<div class="col-md-4">
<center>
<img src="http://oi60.tinypic.com/2z7enpc.jpg" class="img-circle" alt="...">
<br>
<h4 class="footertext">Artist</h4>
<p class="footertext">All the images here are hand drawn by this man.<br>
</center>
</div>
<div class="col-md-4">
<center>
<img src="http://oi61.tinypic.com/307n6ux.jpg" class="img-circle" alt="...">
<br>
<h4 class="footertext">Designer</h4>
<p class="footertext">This pretty site and the copy it holds are all thanks to this guy.<br>
</center>
</div>
</div>
<div class="row">
</div>
</div>
I tried to use
<div class="footer navbar-fixed-bottom">
but in that case my text ,which is located over the footer, get overlapped by footer
Here is css I used:
#footer {
padding-top: 100px
position: absolute;
bottom: 0;
width: 100%;
/* Sets the fixed height of the footer here */
height: 280px;
background:
/* color overlay */
linear-gradient(
rgba(240, 212, 0, 0.45),
rgba(0, 0, 0, 0.45)
),
/* image to overlay */
url(222.png);
}
.footertext {
color: #ffffff;
}
Here is what I mean by ugly. I did try to set html and body height as 100% in css
Please show us what you mean by "looks ugly" so we can help you, because we can't really understand your issue.
I'd say the problem could be in what the parent of #footer div.
I think you only have to change position: absolute; to position: relative; or position: fixed; for the div to stay at the bottom (depending on what behaviour you exactly expect).
Side note, don't use the tag <center> because it's now deprecated, use CSS instead (text-align: center).
You are missing a semi-colon after padding-top: 100px . You can also try position:fixed instead of position:absolute. An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled.
Its <div class="footer navbar fixed-bottom">
not <div class="footer navbar-fixed-bottom">
you mistypes a -
I am trying to make a design where some text of id="text-field" will overlap to id="image-field". I have no idea how to do this, Thank you for your help.
<!DOCTYPE html>
<html>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-6" id="text-field"> Some text </div>
<div class="col-sm-6" id="link-field"> Some link </div>
</div>
<div class="row">
<div class="col-sm-12" id="image-field">Background Image </div>
</div>
</div>
</body>
</html>
I made a small re-usable example it's a fairly common thing to do with css here
Here is a example
.box {
position: relative;
}
.box__image {} .box__text {
position: absolute;
bottom: 0;
}
<div class="box">
<div class="box__image">
<img src="http://placehold.it/350x150">
</div>
<div class="box__text">
Testing text
</div>
</div>
Just a small explanation position:relative is used to keep position:absolute elements from going out of their containing div
Using position properties you can ser postion of elements.
#image-field{
position: absolute;
top: 0px;
}
Research on postions property of css.You will get more idea
postion properties
Text over image. I can't seem to get my text over my picture. I tried with absolution and everything.
<div class="wrapper">
<div class="container">
<div class="row">
<div class="stages col col-6">
<img src="styles\images\stage1.jpg" alt="Stage1">
<p>Stage 1</p>
</div>
<div class="stages col col-6">
<img src="styles\images\stage2.jpg" alt="Stage2">
<p>Stage 2</p>
</div>
</div>
</div>
You are not applying it correctly.
To make it work, you need to make the parent div to have a position:relative; and the child div to have position:absolute;
Once you do the position:absolute;, you need to change the positioning by attributes such as top, bottom, left and right with numeric values.
Just in case, if your text is below the image, you can use z-index attribute to bring it up with a numeric value.
For Instance,
<div class="wrapper">
<div class="container">
<div class="row" style="position:relative;">
<div class="stages col col-6">
<img src="styles\images\stage1.jpg" alt="Stage1">
<p style="position:absolute;top: 0;left: 0;">Stage 1</p>
</div>
<div class="stages col col-6">
<img src="styles\images\stage2.jpg" alt="Stage2">
<p>Stage 2</p>
</div>
</div>
</div>
LIVE DEMO
Hope this helps.
It would be better to manage it through classes. Add image-container to the parent div of image, and text-over-img to p tag used for text.
.image-container {
position: relative;
}
.text-over-img {
position: absolute;
background-color: red;
padding: 5px;
border-radius: 5px;
top: 0px;
left: 5px;
margin-top: 5px;
}
DEMO
I have tried all the solutions I have found on the new trying to get this div to sit on top of the image.
We have a background image, and as you can see from the image below are four words Eversoft, TruSoft, Solarmax and Active Family.
These four words need to be moved up into the boxes on the picture, but I can get the CSS right, this is how my page is constructed
<div class="row">
<img src="~/Content/Images/Mobile-BackGround-new-2.png" class="img-responsive" style="position: relative" />
<div id="stainMasterLinksContainer">
<div id="stainMasterLinksTop">
<b>EverSoft</b> <small><sup>®</sup></small>
<b>TruSoft</b> <small><sup>®</sup></small>
</div>
<div id="stainMasterLinksBottom">
<b>SolarMax</b> <small><sup>®</sup></small>
<b>Active Family</b> <small><sup>™</sup></small>
</div>
</div>
And I'm currently using this CSS for the stainMasterLinksContainer
#stainMasterLinksContainer {
padding-top: -40%;
font-size: 15.5px;
position: relative;
z-index: 100;
}
Any help would be appreciated.
It CAN be done. Here's one way:
<img src="http://practicalaction.org/images/sea-of-ducks-300.jpg" style="position:absolute; left:0px; top:0px"/>
<div style="background-color:blue;position:relative;left:80px; top:80px; width:50px;"> Hello</div>
I am making a responsive site for a mobile. The HTML should not be changed but the css should handle the positioning of the elements so as to not effect the main site.
BACKGROUND INFORMATION
The desktop site has a navigation bar set at the bottom of the screen with a contact number below it whilst the site title and logo is placed at the top. For the mobile this is unfeasable so I've put the navigation bar at the top of the screen alongside the title and logo. The number has remained at the bottom as desired. Between the top header and the contact number at the bottom, I have placed the bulk content area. The content is being displayed correctly by using the height:calc(100% - 336px) property to set the content wrapper 100% - the total height of the top header and the contact number. The content wrapper is then set absolute to a position top: 176px to meet the bottom of the top header. The content inside the content wrapper does not fit inside the wrapper so overflow-y:scroll is used to ensure that the user can scroll through the content area.
PROBLEM
The content area within the wrapper is not scrolling.
CODE
CSS
.PageContentBox {
top: 176px!important;
height: calc(100% - 336px);
z-index: 12;
width: 100%;
overflow-y: scroll;
left: 0px!important;
}
#content {
padding: 0;
height: 100%;
overflow-y: scroll; /*This here for testing purposes*/
}
HTML
<div class="PageContentBox">
<div id="content">
<div id="pages">
<div class="page" id="page0">
<h1>HEADER</h1>
<div class="grid grid-pad" style="padding: 0 0 0 0!important">
<div class="row" id="r1">
<div class="col-5-12">
<div class="content">
<img class="megaServiceImage" src="../template/img/gallery/mega/test.jpg" alt="??????" />
</div>
</div>
<div class="col-7-12">
<div class="content">
<h2>Applications</h2>
<p class="MegaServicesText">
DUMMY TEXT
</p>
</div>
</div>
</div>
<div class="row" id="r2">
<div class="col-5-12">
<div class="content">
<img class="megaServiceImage" src="../template/img/gallery/mega/test.jpg" alt="??????" />
</div>
</div>
<div class="col-7-12">
<div class="content">
<h2>Performance</h2>
<p class="MegaServicesText">
DUMMY TEXT
</p>
</div>
</div>
</div>
<div class="row" id="r3">
<div class="col-5-12">
<div class="content">
<img class="megaServiceImage" src="../template/img/gallery/mega/test.jpg" alt="??????" />
</div>
</div>
<div class="col-7-12">
<div class="content">
<h2>Specifications</h2>
<p class="MegaServicesText">
DUMMY TEXT
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You need to set position on your .PageContentBox: top: and z-index don't work unless you define position.
Here is you updated Fiddle with the position set to absolute.
In your question you save already given the solution just use that only.
You have specified that absolute but not used it in css. Just use it and it will work no need to put the #content css.
css should be like this:
.PageContentBox {
position: absolute;
top: 176px!important;
height: calc(100% - 336px);
z-index: 12;
width: 100%;
overflow-y: scroll;
left: 0px!important;
}
See the example
I am getting desktop and mobile view like this and I think its fine. What you say?