First I want to say I am quite a newbie with CSS.
Without changing a lot of my code I would like to have text over an image.
My current code is:
#container {
width: 1010px;
margin-left: auto;
margin-right: auto;
}
#left-box {
width: 681px;
float: left;
margin-right: 29px;
}
#data {
width: 206px;
height: 140px;
background-color: #fff;
float: left;
margin-right: 10px;
margin-left: 11px;
margin-bottom: 20px;
}
<div id="container">
<div id="left-box">
<div id="data">
<img src="img/img1.jpg" />
<<<< here I want put some text, which should be over the image >>>>
<h2 class="h2-data">Some text</h2>
<p class="stats">text</p>
</div>
</div>
</div>
I tried adding some code which I found from Google, but the text is appearing at the top of the website when I use absolute as position. And with relative the text is under the image, but not OVER the image.
Anyone who can help me?
Use position: relative for the block enclosing the image and position: absolute for the text after assigning a class to it.
#container {
width: 1010px;
margin-left: auto;
margin-right: auto;
}
#left-box {
width: 681px;
float: left;
margin-right: 29px;
}
#data {
width: 206px;
height: 140px;
background-color: #fff;
float: left;
margin-right: 10px;
margin-left: 11px;
margin-bottom: 20px;
position: relative;
}
.text {
top: 0;
left: 100;
position: absolute;
}
<div id="container">
<div id="left-box">
<div id="data">
<img src="http://placehold.it/200x140"></img>
<div class="text">
<<<< here I want put some text, which should be over the image>>>></div>
<h2 class="h2-data">Some text</h2>
<p class="stats">text</p>
</div>
</div>
</div>
maybe so
#container {
width: 1010px;
margin-left: auto;
margin-right: auto;
}
#left-box {
width: 681px;
float: left;
margin-right: 29px;
}
#data {
width: 206px;
height: 140px;
background-color: #fff;
float: left;
margin-right: 10px;
margin-left: 11px;
margin-bottom: 20px;
position: relative;
}
#data img{
position: absolute; top: 0; left: 0;
}
#data .text{
position: relative;
}
<div id="container">
<div id="left-box">
<div id="data">
<img src="http://placehold.it/200x140" />
<div class="text">
<<<< here I want put some text, which should be over the image >>>>
<h2 class="h2-data">Some text</h2>
<p class="stats">text</p>
</div>
</div>
</div>
</div>
Related
I am trying to achieve similiar effect to this:
The header and its subheader are both vertically aligned to the image.
I've written following code:
http://jsfiddle.net/u4zxn4eb/
It works but is it the best way to achieve this?
<div class="wrapper">
<h1>
<img src="https://s3.amazonaws.com/uifaces/faces/twitter/sachagreif/128.jpg" alt="">
<div class="vertical_align">
Your company's name
<p>Description</p>
</div>
</h1>
</div>
CSS:
body {
background: lightgrey;
}
.wrapper {
background: grey;
width: 100%;
}
h1 {
font-size: 18px;
margin: 0;
position: relative;
top: 35px;
}
.vertical_align {
display: inline-block;
vertical-align: middle;
}
img {
display: inline-block;
vertical-align: middle;
margin-right: 10px;
}
p {
margin: 0;
}
EDIT
As #chrisbedoya suggested I shouldn't put div inside h1 tag.
Deleted.
Instead of using h1 tag to position the whole, it's better to apply these rules separately:
.vertical_align a,
.vertical_align p {
position: relative;
top: 40px;
}
Well, define "best". If your method is standards compliant and works in all browsers then use it. If you're interested, here's how I'd do it:
<style>
#header {
height: 100px;
margin: 0px;
padding: 0px;
width: 500px;
}
#rectangle {
background-color: #f0f0f0;
height: 80px;
margin: 0px;
padding: 0px;
position: absolute;
left: 0px;
top: 0px;
width: 500px;
z-index: 1;
}
#square {
background-color: #ff0000;
height: 80px;
margin: 0px;
padding: 0px;
position: absolute;
left: 20px;
top: 20px;
width: 80px;
z-index: 2;
}
#text {
/* background-color: #00ff00; */
margin: 0px;
padding: 0px;
position: absolute;
left: 120px;
top: 41px;
width: 360px;
z-index: 2;
}
h1 {
font-family: Arial;
font-size: 20px;
line-height: 20px;
margin: 0px;
padding: 0px;
}
p {
font-family: Arial;
font-size: 12px;
line-height: 12px;
margin: 5px 0px 0px 0px;
padding: 0px;
}
</style>
<div id="header">
<div id="rectangle"></div>
<div id="square"></div>
<div id="text">
<h1>Company Name</h1>
<p>Company Tagline</p>
</div>
</div>
Well, this question is way too subjective and better suited for CodeReview. But I thought I'd give you my input.
First, let's clean up the HTML. Get some HTML5 in there as well.
<div class="wrapper">
<header>
<a href="#" class="logo-link">
<img src="https://s3.amazonaws.com/uifaces/faces/twitter/sachagreif/128.jpg" alt="">
</a>
<div class="vertical_align">
<h1>Your company's name</h1>
<h2>Description</h2>
</div>
</header>
</div>
Then make the CSS nice and tidy. We don't need much:
.logo-link {
display: inline-block;
position: relative;
top: 24px;
margin: 0 24px;
}
.logo-link img {
display: block;
}
.vertical_align {
display: inline-block;
vertical-align: top;
}
Here's an example.
I would use a position: relative property to align the image slightly below the header like you have in your image there.
I'm facing with a strange problem I can't pinpoint. I have a 3 column layout where the first 2 columns have position-fixed, so that only the third column scrolls.
The first element of each column have a margin-top of 20px (for the first and third column it's the H1 element, for the second column it's the div). For some reason, the third column does not line up with the first 2.
<body>
<div class="wrapper">
<div class="container2">
<div class="sidebar">
<h1>Sidebar</h1>
</div>
<div class="menu">
<div class="mediablock">
Media here</div>
</div>
<div class="content">
<h1>Content goes here</h1>
</div>
</div>
</div>
</body>
I have a simple version of it at jsfiddle, demonstrating the problem.
http://jsfiddle.net/59ez7zmy/
I can only assume that the position-fixed has got something to do with it, but I can't seem to figure it out.
When I use the Chrome developer toolbar, there is a (approx) 20px gap (although not defined by any margin) between the top of the page and the divs, and the elements inside the position: fixed columns have a 20px margin relative to the container2 div (as expected). The third column however, has a 20px margin to the top of the screen rather than the .container2 div.
Anyone knows what I'm missing here?
Specify top: 0 for .sidebar and .menu (fiddle).
.sidebar {
position: fixed;
width: 100px;
height: 100%;
color: rgb(97, 68, 50);
text-align: right;
top: 0;
}
.menu {
position: fixed;
width: 250px;
margin-left: 110px;
height: 100%;
color: rgb(97, 68, 50);
top: 0;
}
See the documentation for top
One solution is to add position: fixed on .content class:
h1 {
font-size: 1em;
margin-top: 20px;
}
.sidebar {
position: fixed;
width: 100px;
height: 100%;
color: rgb(97, 68, 50);
text-align: right;
}
.menu {
position: fixed;
width: 250px;
margin-left: 110px;
height: 100%;
color: rgb(97, 68, 50);
}
.content {
margin-left: 370px;
width: 150px;
position: fixed;
}
.mediablock {
margin-top: 20px;
}
.wrapper {
position: relative;
text-align: center;
}
.container2 {
width: 550;
margin-left: auto;
margin-right: auto;
}
<body>
<div class="wrapper">
<div class="container2">
<div class="sidebar">
<h1>Sidebar</h1>
</div>
<div class="menu">
<div class="mediablock">
Media here</div>
</div>
<div class="content">
<h1>Content goes here</h1>
</div>
</div>
</div>
</body>
Also if you don't want to use position: fixed you can use display: inline-block also in .content class:
h1 {
font-size: 1em;
margin-top: 20px;
}
.sidebar {
position: fixed;
width: 100px;
height: 100%;
color: rgb(97, 68, 50);
text-align: right;
}
.menu {
position: fixed;
width: 250px;
margin-left: 110px;
height: 100%;
color: rgb(97, 68, 50);
}
.content {
width: 150px;
position: relative;
display: inline-block;
}
.mediablock {
margin-top: 20px;
}
.wrapper {
position: relative;
text-align: center;
}
.container2 {
width: 550;
margin-left: auto;
margin-right: auto;
}
<body>
<div class="wrapper">
<div class="container2">
<div class="sidebar">
<h1>Sidebar</h1>
</div>
<div class="menu">
<div class="mediablock">Media here</div>
</div>
<div class="content">
<h1>Content goes here</h1>
</div>
</div>
</div>
</body>
Also you have some errors in your css i fix them.
I have an image of a web-browser that I am trying to position a text url over using position:absolute. The problem is that when I adjust the width of the page the text doesn't move the image in the fluid layout.
Device screen with text url example (what i'm trying to achieve): http://matthewhartman.github.io/base/
My code: http://jsfiddle.net/VwkwT/1/
HTML:
<div id="primaryContainer" class="primaryContainer clearfix">
<header id="Header">
<img id="Logo" src="img/logo.png" class="image" />
</header>
<section id="HeroDiv" class="centre">
<img id="Hero" src="img/browser.png" class="image" />
<div id="url">www.website.com</div>
</section>
<div id="box1" class="clearfix">
</div>
<div id="box2" class="clearfix">
</div>
<div id="box3" class="clearfix">
</div>
<div id="box4" class="clearfix">
</div>
</div>
CSS:
* {
margin:0px;
padding:0;
}
.primaryContainer {
height: auto;
margin-left: auto;
margin-right: auto;
min-height: 100%;
width: 100%;
background-color: rgb(252, 252, 252);
}
#box1 {
float: left;
height: 1190.75px;
margin-left: 0px;
margin-top: 153px;
clear: both;
width: 100%;
background-color: rgb(244, 244, 244);
}
#box2 {
float: left;
height: 759.40625px;
margin-left: 0%;
margin-top: -90.5px;
clear: none;
width: 100%;
margin-right: 0%;
background-color: rgb(242, 242, 242);
}
#Logo {
float: left;
width: 190px;
height: auto;
margin-left: 13.893926%;
margin-top: 41.75px;
margin-bottom: 45.75px;
clear: none;
}
#box3 {
float: left;
height: 733.828125px;
margin-left: -100%;
margin-top: 668.90625px;
clear: none;
width: 100%;
margin-right: 0%;
background-color: rgb(184, 218, 242);
}
#box4 {
float: left;
height: 733.828125px;
margin-left: 0%;
margin-top: 0px;
clear: both;
width: 100%;
margin-right: 0%;
background-color: rgb(244, 244, 244);
}
#HeroDiv {
position:relative;
height: 392px;
}
#url {
position:absolute;
font-size:11px;
color:#8B8B8B;
font-family: 'Candara', Calibri, Segoe, Segoe UI, Optima, Arial, sans-serif;
}
#Hero {
position:relative;
width: 809px;
height: auto;
display: block;
margin-left: auto;
margin-right: auto;
clear: both;
}
Any help is appreciated!
I have this HTML:
<div style="position: relative;">
<img width="100%" src="images/some.jpg">
<div class="header">
<h1>my header</h1>
Some more<br>text
</div>
</div>
This is the CSS:
.header {
color: #000000;
font-size: 52px;
left: 70%;
position: absolute;
text-align: right;
top: 0;
}
h1 {
font-size: 52px;
margin-bottom: 0;
}
Now, when I resize the page, there is 1 problem:
At some point, the text is not above the image to 100% any longer, but why?
Sooo you want it like this?
HTML
<div style="position: relative;">
<div class="header">
<h1>my header</h1>
Some more<br>text
<img width="100%" src="/image/some-image.jpg">
</div>
</div>
CSS
.header {
color: #000000;
font-size: 52px;
left: 70%;
width: 100%;
position: absolute;
text-align: right;
top: 0;
}
h1 {
font-size: 52px;
margin-bottom: 0;
}
DEMO: http://jsfiddle.net/cT7Sy/
This question already has answers here:
css background color with floating elements
(5 answers)
Closed 6 years ago.
I have an issue with my floated child divs not growing with my parent div. Is there a way to fix this? I need the wrapper to be 100% of the content in either div. Here is the html;
<body>
<div id="wrapper">
<div id="leftpane">
<div id="lefthead">
<div id="leftheadfiller">
</div>
<div id="leftheadlogo">
</div>
<div id="leftheaddivider">
</div>
</div>
<div id="leftcontent">
</div>
</div>
<div id="rightpane">
<div id="righthead">
<div id="rightheadfiller">
</div>
<div id="rightheadlogo">
</div>
<div id="rightheaddivider">
</div>
</div>
<div id="navigation">
Properties Careers About Blog Advertise Contact
</div>
<div id="rightcontent">
</div>
</div>
<div id="close">
</div>
</div>
</body>
And here is the css;
html,body {
background-image:url('images/background.gif');
background-repeat: repeat-y;
background-position: center;
background-attachment: fixed;
height: 100%;
width: 100%;
margin: 0px;
}
#wrapper {
background-color: aqua;
height: 100%;
width: 866px;
margin-left: auto;
margin-right: auto;
}
#leftpane {
background-image: url('images/darkbackground.gif');
width: 326px;
height: 100%;
float: left;
}
#lefthead {
height: 132px;
width: 100%;
}
#leftheadfiller {
height: 75px;
width: 100%;
}
#leftheadlogo {
background-image: url('images/index_07.gif');
width: 71px;
height: 56px;
float: right;
}
#leftheaddivider {
height: 1px;
width: 100%;
background-image: url('images/lightbackground.gif');
float: right;
}
#lefttcontent {
height: 100%;
background-color: fuchsia;
}
#rightpane {
background-image: url('images/lightbackground.gif');
width: 540px;
height: 100%;
float: right;
}
#righthead {
height: 132px;
width: 100%;
}
#rightheadfiller {
height: 75px;
width: 100%;
}
#rightheadlogo {
background-image:url('images/index_09.gif');
width: 109px;
height: 56px;
float: right;
}
#rightheaddivider {
height: 1px;
width: 100%;
background-image: url('images/darkbackground.gif');
float: right;
}
#navigation {
margin-top: 2px;
font-family: Arial, Helvetica, sans-serif;
color: #A3A3A3;
font-size: 14px;
word-spacing: 44px;
text-align: center;
width: 100%;
height: 18px;
}
#rightcontent {
padding-left: 6px;
background-color: fuchsia;
}
#close {
clear: both;
}
Is there a way to fix this? I have this set up on a temporary folder on my local server at:
http://68.113.27.229/test
The two divs that I need to force the wrapper to grow are Leftcontent and Rightcontent.
Thanks!
Add overflow: hidden to the parent div element.
If you don't want the overflowing content to be hidden, then try this:
Insert this as the last div inside the wrapper div.
<div style="clear: both"></div>