Center div inside parent div - html

I'm trying to center a div inside a parent div based on the dimensions of the parent div. I have tried using:
display: inline-block;
because I have seen other questions where this was used to center the div but I am not having luck.
BOX1 should be centered insdie of test
<div class="tab-pane" id = "test">
<div id="Box2">
<h1> Graph Text </h1>
</div>
<div id="BOX1">
</div>
</div>
#test {
width:700px;
height: 500px;
background: grey;
position:relative;
}
#BOX1 {
display: inline-block;
width: 500px;
height: 300px;
background: lightgrey;
position:absolute;
z-index:1;
}
#Box2{
width: 250px;
height: 50px;
background: lightblue;
position:absolute;
left: 125px;
z-index:2;
}
h1 {
font: 25px Helvetica, sans-serif;
text-align: center;
}
http://jsfiddle.net/bahanson/xvL2qvx0/5/

try this :demo
#test {
width:700px;
height: 500px;
background: grey;
position:relative;
}
#BOX1 {
margin:0 auto;
width: 500px;
height: 300px;
background: lightgrey;
position:relative;
z-index:1;
}
#Box2{
width: 250px;
height: 50px;
background: lightblue;
position:absolute;
left: 125px;
z-index:2;
}
h1 {
font: 25px Helvetica, sans-serif;
text-align: center;
}
<div id="test" class="tab-pane">
<div id="BOX1">
<div id="Box2">
<h1> Graph Text </h1>
</div>
</div>
</div>

Adding this to the box 1 css does what you want and will keep the child centered if the parent width changes.
left: 50%;
margin-left: -250px;
http://jsfiddle.net/xvL2qvx0/6/
If you don't need IE8 support you can just use:
left: calc(50% - 250px);

You should read up on normal flow and CSS positioning.
http://webdesign.about.com/od/cssglossary/g/bldefnormalflow.htm
But basically, a div will always position relative to the parent div.
If you add margin: 0 auto; to a div, it should horizontally position it within the parent div

#BOX1 {
display: inline-block;
margin-left:100px;
width: 500px;
height: 300px;
background: lightgrey;
position:absolute;
z-index:1;
}
use margin-left command to adjust it to the centre....

Seen as though you are using absolute positioning you can simply give it a top,right,left and bottom of 0 and use margin:auto to centre it both horizontally and vertically.
This benefits from be able to use relative (percentage) sizing if you want and there's no maths involved. Furthermore, if you later change the dimensions (maybe via a media-query for mobile devices) you don't need to recalculate messy margins or offsets - just change the size and it will be centred.
#BOX1 {
display: block;
width: 500px; /* it will still work if you change the size */
height: 300px; /* the dimensions could be percentages if you like */
background: lightgrey;
position:absolute;
z-index:1;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
}
http://jsfiddle.net/xvL2qvx0/7/
#test {
width:700px;
height: 500px;
background: grey;
position:relative;
}
#BOX1 {
width: 500px;
height: 300px;
background: lightgrey;
position:absolute;
z-index:1;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
}
#Box2{
width: 250px;
height: 50px;
background: lightblue;
position:absolute;
left: 125px;
z-index:2;
}
h1 {
font: 25px Helvetica, sans-serif;
text-align: center;
}
<div class="tab-pane" id = "test">
<div id="Box2">
<h1> Graph Text </h1>
</div>
<div id="BOX1">
</div>
</div>

Related

keep a button fixed, but inside body

body{
max-width:1366px;
}
.gotop{
position:fixed;
right:9px;
bottom:7px;
cursor:pointer;
width:25px;
}
gotop is a button to scroll page on top and it must not be scrollable, i.e. must be fixed.
Problem is on monitors greater than 1366 px. The button is far right from the body.
How to keep it fixed, but inside body?
One possible solution is to omit top, right, bottom, left values for the fixed button. This way it will be sticked to the container:
.container {
position: relative;
max-width: 800px;
height: 200vh; /* for scrolling demo */
margin: 0 auto;
border: 1px solid black;
}
.button-wrapper {
position: absolute;
right: 35px; /* button width plus margin */
top: 30%; /* or whatever you need */
}
.button {
position: fixed;
width: 25px;
height: 25px;
cursor: pointer;
background: black;
}
<div class="container">
<div class="button-wrapper">
<div class="button"></div>
</div>
</div>
Try This
body{
max-width:1366px;
background:#f1f1f1;
}
.gotop{
position:absolute;
right:25px;
bottom:25px;
cursor:pointer;
}
<body>
<button class='gotop'>TOP</button>
</body>
I wouldn't recommend using max-width on the body... you should put it on a div that wraps everything in the page instead.
Then place your button at the bottom of wrapper with the following CSS applied. Tweak the values to get a better position if you need it.
.wrapper{
position: relative;
height:200vh;
width: 100%;
max-width:400px;
background: #000;
}
.holder{
position: absolute;
top:92.5%;
right:0;
background: #ccc;
}
.button{
height:30px;
width: 70px;
position: fixed;
margin-left:-70px; /* minus width */
bottom:10%;
}
<div class="wrapper">
<div class="holder">
<button class="button">Test</button>
</div>
</div>
What you asking is rather an old way of doing things but it can be achieved.
Set the width of body.
Set fixed element to center.
Offset center by width of body and fixed element.
html,
body {
position:relative;
height: 100%;
max-width: 200px;
margin: 0 auto;
border:1px solid #111;
}
.gotop {
position: fixed;
left:50%;
bottom: 7px;
cursor: pointer;
width:40px;
background:#eee;
margin-left:60px;/*half width of body minus width of gotop*/
}
<div class="gotop">TOP</div>

div height fill rest of the white space

I'm trying to make my orange div to get all of the white space in height. Im using 1920x1080 monitor. When i open bottom code in my page i have white space under red, blue and green div's. I wanna orange div to move my red, blue, green div's down and fill that white space under them.
The idea is site automatically to fill browser window without scrollbars.
I try to write 100% instead of 700px, but when my attribute is 100%, orange div disappear.
Can someone tell me why that is happening, where is my mistake, how can i prevent it.
Also is it there another way to give equal space to my red, blue and green div's? I calculate that 100% of page divided by 3 is 33.3333 in period. That's why i set my width to be 33.33% but it didn't fill page completely.
.wrapper{
position: relative;
}
.pink{
background-color: pink;
height: 100px; width: 100%;
position: relative;
}
.orange{
background-color: orange;
height: 700px; width: 100%;
position: relative;
margin: 0px 0px 0px 0px;
}
.red{
background-color: red;
height: 300px; width: 33.33%;
position: relative;
float: left;
}
.blue{
background-color: blue;
height: 300px; width: 33.33%;
position: relative;
float: left;
}
.green{
background-color: green;
height: 300px; width: 33.33%;
position: relative;
float: left;
}
<div class="wrapper">
<div class="pink"></div>
<div class="orange"></div>
<div class="red"></div><div class="blue"></div><div class="green"></div>
</div>
Give height:100% to parent div, body and html
body, html{
height:100%;
}
.wrapper{
position: relative;
height:100%;
}
.orange{
background-color: orange;
height: 100%; width: 100%;
position: relative;
margin: 0px 0px 0px 0px;
}
Please check this fiddle.
Is this what you mean?
I've made use of
display: table
display: table-row
display: table-cell
The orange div will now fill the remaining height of the window.
Fiddle
EDIT: I updated the fiddle. tidied the code a bit.
Include this in your style:
body{
margin:0;
padding:0;
}
Wrap orange and pink inside a separate div from last three and use display:flex; on that div.
You can make three div eualwidth by using display:flex to the parent div and flex:1 to the children divs. You don't necessarily have to use width:33.33%;
html,body{
width:100%;
height:100%;
margin:0;
padding:0;
}
.wrapper{
position: relative;
margin:0;
padding:0;
display:flex;
min-height:100vh;
flex-direction:column;
}
.pink{
background-color: pink;
height: 50px;
width: 100%;
position: relative;
flex-shrink:0;
}
.orange{
background-color: orange;
height:100%;
width: 100%;
position: relative;
margin: 0px 0px 0px 0px;
flex-shrink:0;
flex-grow:1;
}
.wrapper2{
position: relative;
margin:0;
padding:0;
flex-shrink:0;
width:100%;
height:100px;
display:flex;
flex-direction:row;
justify-content: space-between;
}
.red{
background-color: red;
height:100%;
position: relative;
float: left;
flex: 1;
}
.blue{
background-color: blue;
height:100%;
flex: 1; position: relative;
float: left;
}
.green{
background-color: green;
height:100%;
flex: 1; position: relative;
float: left;
}
<div class="wrapper">
<div class="pink"></div>
<div class="orange"></div>
<div class="wrapper2">
<div class="red"></div>
<div class="blue"></div>
<div class="green"></div>
</div>
</div>

Helping with centering things

I wants to make a structure of: top, middle and bottom.
I wants that the top will be 30px from the top, bottom 30px from the bottom and that the text in the middle will be exactly the same distance from bottom to top (this is my main problem).
And of course that my goal that it will be adapted to all the resolutions.
Thanks in advance :)
You could use absolute positioning.
You can play around with the Codepen link here
HTML:
<div id="top">this is top</div>
<div id="middle">
<span class="full_center">middle is here</span>
</div>
<div id="bottom">bottom down below</div>
CSS:
The trick lies in the .full_center class
html, body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
#top {
background-color: #ccc;
height: 30px;
position: relative;
top: 30px;
}
#middle {
background-color: aliceblue;
min-height: calc(100vh - 60px);
height: auto;
}
#bottom {
height: 30px;
background-color: #ccc;
position: relative;
bottom: 30px;
}
.full_center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
width: 100%;
}
Try Using display:flex;. It's the closest I could draw from your question. Here I used background-color:red; to show the 30px distance from top and bottom.
*{
margin:0;
padding:0;
box-sizing:border-box;
}
.outer{
width:100%;
height:100vh;
background-color:red;
padding:30px 0;
}
.inner{
height:100%;
width:100%;
background-color:green;
display:flex;
align-items:center;
}
p{
width:60%;
margin:0 auto;
text-align:center;
color:white;
font-size:2em;
}
<div class="outer">
<div class="inner">
<p>This is text<br>
This is text<br>
This is text<br>
This is text<br></p>
</div>
</div>

Image Overlapping DIVS on either side

I'm really stuck on this and would appreciate any direction.
I need to code the following design using CMS and html but I have no idea how to get the center image to overlap the divs on the right and left of the image. I have been reading about relative position and z-indexes but everything that I have tried has failed. Generally when I line up three dives across I will use the float property and it works perfectly but it turns out z-indexes can only be used with positioned elements. If someone could get me started in the right direction I will probably be able to figure it out.
See the design I am trying to code here: https://cdn.shopify.com/s/files/1/0211/8026/files/Example.png?9982
This is the base framework but I don't know where to go from here...
.row-container {
width: 100%;
height: 300px;
margin: auto;
text-align: center;
position: relative;
}
.box1 {
height: 216px;
width: 288px;
float: left ; /* <-- This does not work */
border: 1px solid blue;
}
.image {
height: 250px;
width: 350px;
float: left ; /* <-- This does not work */
border: 1px solid grey;
}
.box2 {
height: 216px;
width: 288px;
float: left; /* <-- This does not work */
border: 1px solid red;
}
<div class="row-container">
<div class="box1"></div>
<div class="image">-- Should I use a div for the image?</div>
<div class="box2"></div>
</div>
Try this it would have worked a bit more better if position:absolute is used but since you wanted float there will be re sizing problems Fiddle
Zoom out to get the effect
.row-container {
width: 100%;
height: 300px;
margin: auto;
text-align: center;
position: relative;
}
.box1 {
position: relative;
z-index: -1;
background: green;
height: 216px;
width: 288px;
float: left;
}
.image {
margin-left: -80px;
background: red;
float: left;
height: 250px;
width: 200px;
}
.image img {
width: 300px;
}
.box2 {
position: relative;
z-index: -1;
float: left;
background: blue;
height: 216px;
width: 288px;
}
<div class="row-container">
<div class="box1"></div>
<div class="image">
<img src="http://placekitten.com/300/301" />
</div>
<div class="box2"></div>
</div>
You can do it without floats using position: (colors added for emphasis)
fiddle
.row-container {
width:900px;
height:300px;
margin:auto;
text-align: center;
border:2px solid black;
background-color:blue;
position:relative;
}
.box1 {
height:216px;
width: 288px;
left:0px;
position:absolute;
z-index:10;
}
.image {
height:250px;
width: 350px;
position:absolute;
top:20px;
left:275px;
z-index:100;
background-color:red;
}
.box2 {
height:216px;
width: 288px;
right:0px;
position:absolute;
z-index:10;
}
div{
background-color:green;
}
You can use z-index on position: relative, so add that to your inner elements and set the z-index.
To create the overlap you can use a negative margin-left on the second and third elements.

Center h1 over image in a div

How to center (vertically,horizontally) properly over an image in a ?
<div class="category-info">
<div class="image">
<h1>Title</h1>
<img src="image.jpg" />
</div>
</div>
CSS
.category-info {
text-align: center;
height: 200px;
width: 770px;
overflow: auto;
margin-bottom: 20px;
}
The image is 770px width and 200px height. I don't what to do next with . I tried several things, without success.
Here you go: http://jsfiddle.net/QjLuP/4/
The CSS:
.image{
position: relative;
background: green; /* IE */
}
.image h1{
z-index: 2;
position: absolute;
top: 50%;
left: 0;
right: 0;
font-size: 20px;
width: 100%;
height: 26px;
padding: 0;
margin: 0;
margin-top: -13px; /* 1/2 height */
text-align: center;
background: red;
background: rgba(170, 0, 0, 0.8); /* CSS3 */
}
.image img{
z-index: 1;
width: 100%;
height: 100%;
position: absolute;
top:0;
right:0;
bottom:0;
left:0;
background:green
}
I threw a position relative on the .image class and set the width and height on the image element (that way it doesn't resize when it loads). I changed the table back to the h1 and added your line-height of 200px. That is the only downside, you'll still have to manually set the line-height of the h1.
HTML:
<div class="category-info">
<div class="image">
<h1>Title</h1>
<img src="http://placekitten.com/770/200" />
</div>
</div>
CSS:
.category-info {
text-align: center;
margin-bottom: 20px;
}
.image{
position:relative;
}
.image img{
width:770px;
height:200px;
}
.image h1{
position:absolute;
width:100%;
color:white;
line-height:200px;
margin:0;
}
JSFiddle: http://jsfiddle.net/5wGwL/2/
Have you tried this?
h1 { text-align:center; }
html
<h1 style="background-image:url(your php source.img)">Title</h1>
css :
h1 {
height: 200px;
width: 770px;
margin-bottom: 20px;
text-align:center;
vertical-align:middle;
line-height:200px;
background:transparent no-repeat scroll 50% 50%;
}
and nothing else