It is easy to absolutely position text relative to a div when the div is aligned with the left side of a browser window. The width of the browser window can change size and the text stays in the correct position. But I have a fixed width web page that is always centered in the browser window. I have a div that is centered on the web page regardless of the width of the browser window. This div contains an image. I need to absolutely position text on top of the image. My code is:
<div id="wrapper">
<div> <img style="max-width:100%; max-height:100%; display:block;" src="../../assets/logos/skitours_title_756w_100h.gif" alt="Website logo">
<div class="tour_title"> <h3>This is the title</h3> </div>
</div>
</div>
#wrapper {
width:756px;
height:auto;
margin: auto auto;
margin-bottom:5px;
}
.tour_title {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:20px;
font-weight:bold;
color:#696969;
display:block;
position:absolute;
width:500px;
top:40px;
left:20px;
}
That results in the image being centered on the web page regardless of the width of the browser window.
But the absolute positioning of the text (tour_title) is relative to the left side of the browser window, not the image. How do I correct the code so that the text is relative to the webpage regardless of the width of the browser window?
If I understand correctly, you want to make the text relative to the image.
If that's the case, you should:
Make the wrapper position: relative so the "absolute" text references it instead of the browser/webpage.
Make the text div text-align:center; and width:100%; for it to align at the center of the wrapper.
Make the top: whatever you wish.
Something like this:
#wrapper {
width:756px;
height:auto;
margin: auto auto;
margin-bottom:5px;
border-style:solid;
position:relative;
}
.tour_title {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:20px;
font-weight:bold;
color:#ff00ff;
position: absolute;
text-align:center;
width:100%;
top: 50%;
}
Related
Wondering if someone can help.
I'm a beginner learning website development and have been playing around with different styles for a particular project. The one I'm trying at the moment has a background colour, on top of this is a central wrapper of 60% width which contains all the website gumph. on the sides of the wrapper I have text turned 90/270 running down the wrapper with the website name.
When I go to resize the website the text jumps, how do I get the text to stay in it's original position? Also a secondary question, the text I have put as a % in CSS, however it doesn't seem to resize with the screen, even if I try em, here is the css:
*{
background-color:#085C08;
max-width:100%;
max-height:100%;
min-width:200px;
margin:0 auto;
}
.wrapper{
width: 60%;
background-color: #FDF3EC;
margin:0 auto;
margin-top:-1%;
margin-bottom:-1.5%;
}
.artwork-r{
transform:rotate(90deg);
white-space:nowrap;
transform-origin:bottom;
margin-top:-52.5%;
margin-left:78.65%;
position:absolute;
width:0%;
height:0%;
clear:both;
}
.artwork-r p{
padding:0%;
font-size:490%;
font-family:Verdana, Geneva, Tahoma, sans-serif;
color:#FDF3EC;
}
.artwork-l{
transform:rotate(270deg);
white-space:nowrap;
transform-origin:top left;
margin-left:15.1%;
margin-top:0.4%;
position:absolute;
width:0%;
height:0%;
clear:both;
}
.artwork-l p{
padding:0%;
font-size:490%;
font-family:Verdana, Geneva, Tahoma, sans-serif;
color:#FDF3EC;
}
when position the text, don't use margin-left or margin-top, those will change as the screen size changes. You should use top, bottom, left and/or right.
For the font size, em's and % are both based NOT on the screen size, but instead it's parent's font size. Without seeing your markup I am not 100% sure, but you will probably want the position of .artwork-l to be set as right: 100%;
I am new to website design and need help in centering a div that has 100% page height but 70% width, the width can be set and centered but the height is only as heigh as the content itself. For example like http://www.thelounge.fi/ however the scrolling part of this is on the left side while I would like it in the centre.
Thank You.
okey. In this case we have two solutions:
You can set the div display property to inline or inline-block. And then set the text-align property to center for the div parent, as you could see here https://jsfiddle.net/ivan0013/7q9Lp45q/
Or you can give a margin to the child, if it has acertain width and the display property set to block, which is the default, like here https://jsfiddle.net/ivan0013/wwu5ttxt/
Further explanation:
In the first solution, you change the div default value for the displayproperty. When you set displayto inline you are creating a line element, which does not take all the space to its side. Then you change the tex-align property for the parent, that means that all childs that are line elments will be centered.
For the second one, we use the block value for display. Using this, the element, in our case, the div, is taking all the width available. For that reason, we need to set a width, for instance 25%. Now, the div takes only the 25% of the parent. The last step is adding a margin, which is the distance to the parent's bounds. In the fiddle we set margin: 0 auto which means that div will have 0 for margin top and bottom, and will take an auto margin for each side.
A good reference: W3schools
if you dont want to set content of hello div in separate div with overflow scroll, and you want to keep your HTML as it is you can margin-top to hello with same hieght of navbar and set navbar left (100-70)/2=15
body {
/*background-color: #f0f0f2;*/
background-color:green;
background-attachment:fixed;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
overflow-y:scroll;
}
.hello {
width:70%;
background-color:white;
text-align:center;
margin:auto;
position:relative;
background-position:center;
/*margin-top:60px;*/
margin-bottom:20px;
height:100%;
margin-top:60px;
}
.navbar {
align:center;
background-color:gray;
background-attachment:fixed;
height:40px;
padding-top:20px;
box-shadow: inset 0 -3px 3px rgba(0,0,0,0.5), inset 0 3px 3px rgba(255, 255, 255,1);
position:fixed;
width:70%;
top:0;
left:15%;
}
.navbar2 {
align:center;
background-color:gray;
height:20px;
width:100%;
bottom:0;
}
<body>
<div class="hello">
<div class="navbar">NAVAGATION BAR
</div>
<p>test1</p>
<p>test2</p>
<p>test3</p>
<p>test4</p>
<p>test5</p>
<p>test</p>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
<p>test</p>
<p>test</p><p>test</p>
<p>test</p><p>test</p>
<p>test</p><p>test</p>
<p>test</p>
<div class="navbar2">Bottom Bar
</div>
</div>
</body>
<style type='text/css'>
body {
width:100%;
background:#097054;
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
}
#con {
margin-right:auto;
margin-left:auto;
width:80%;
}
#navbar {
background-color:#00F;
}
</style>
</head>
<body>
<div id='navbar'>
<div id='con'>
Hey
</div>
</div>
</body>
Whatever I try, the div navbar doesn't stick to the top of the page. When I try fixed or absolute positioning it fails to cover a little portion on the left. Either that, or it doesn't stick to top of the window at all.
#navbar {
background-color:#00F;
position:fixed;
top:0;
left:0;
width:100%;
}
When using position fixed also give it a width. I gave it width 100% so its the full width of the screen.
Okay so if you change the position to absolute it also works.
If you want to change the position to relative you also need to add this css
*{
margin:0;
padding:0;
}
this clears preset styles on elements. usually the browser does this.
I am trying to zoom a image which is inside a div.
When page is loaded i am showing 300*300 size image inside a 400*400 size div.
So, to show the image at the center of the div i am using the following css.
#img1{
width:300px;
height:300px;
position:absolute;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;
}
with the above css code i can able to show the image at the center of the div.
but when a user clicks on zoom button i am increasing the height and width of the image. If it becomes 600*600 size image, i have to show scroll bar so the user can scroll the div to show the full image.
For this i set overflow:auto to div.
But the problem is i can't see the full image when i scroll the div. This may be due to the position:absolute property of image. How can i fix this.
I created a fiddler also. There i am showing 2 divs before and after zooming image. Please check.
http://jsfiddle.net/codingsolver/L4qdL/1/
Can you try this;
HTML
<div class="outer">
<div class="inner">
<img id="img1" src="http://siliconangle.com/files/2012/04/HTML5_Wallpaper_1680x1050.png" />
</div>
</div>
<button class="zoomout" data-zoom="out">Zoom Out</button >
<button class="zoomin" data-zoom="in">Zoom In</button >
CSS
.outer{
height:400px;
width:400px;
overflow:auto;
border:1px solid black;
float:left;
margin:30px;
text-align:center;
}
.inner {
height:400px;
width:400px;
display:table-cell;
vertical-align:middle;
}
img {
width:300px;
height:300px;
}
http://jsfiddle.net/L4qdL/7/
I have a <div> containing an image and some text. The image is supposed to come at the left corner and the text is supposed to come in the center of the <div>. But the text is coming off a little off-center to the right due to the width of the image.
How do i fix it??
<header>
<img id="searchBoxProp" src="/resources/images/searchBoxProp.png">
<div class="title">RECIPE SEARCH</div>
</header>
header #searchBoxProp { margin: -16px 0 0 2px; width: 43px; float: left; }
header .title { text-align: center; margin-left: 0 auto; }
You could set the image as background of the <div class="title"> and then set text-align:center in order to align the text properly.
The HTML could be just:
<header>
<div class="title">RECIPE SEARCH</div>
</header>
And the CSS:
div.title {
background-image:url('/resources/images/searchBoxProp.png');
background-repeat:no-repeat;
text-align:center;
}
You will also need to set a fixed height (equal to the image), and finally set the width you wish.
Set header to position:relative, and #searchBoxProp to position:absolute. Absolute positioning takes the element out of the layout, so it won't affect the text postion. The relative positioning on header makes sure that #searchBoxProp is positioned relatively to header, instead of the browser window.
header {
position:relative;
}
#searchBoxProp {
position:absolute;
left:0px; /* set to your needs */
top:0px; /* set to your needs */
}
Best practice is to use a background image however if not you can use position absolue like this.
header{position:relative;}
header .title { position:absolute; width:100%; display:block; text-align:center; }