How do i vertical align the white box and green box inside red box. I want it look like in the picture below. Thanks you. guys are the best. here is http://jsfiddle.net/hamdlink/JxDn5/
<style>
*{
padding:0;
margin:0;
}
.box{
width:600px;
height:600px;
position: absolute;
margin:auto;
left:0;
right:0;
bottom:0;
top:0;
background: red;
}
.box2{
margin:0 auto;
background: green;
width:100px;
height:100px;
}
.box3{
margin:0 auto;
background: white;
width:100px;
height:100px;
}
.whole-background{
position: absolute;
width:100%;
height:100%;
background: rgba(0,0,0,.5);
}
</style>
<div class="whole-background"></div>
<div class="box">
<div class="box2"></div>
<div class="box3"></div>
</div>
The red box should be used as a table-cell.
The 2 little boxes should be displayed in inline-block;
Horizontal alignment: text-align: center;
Vertical alignment: vertical-align: middle;
Result: http://jsfiddle.net/JxDn5/2/
*{
padding:0;
margin:0;
}
.box{
width:100px;
height:600px;
margin:auto;
background: red;
display: table-cell;
vertical-align: middle;
text-align: center;
padding: 0 250px;
}
.box2{
margin:0 auto;
background: green;
width:100px;
height:100px;
display: inline-block;
vertical-align: middle;
}
.box3{
margin:0 auto;
background: white;
width:100px;
height:100px;
display: inline-block;
vertical-align: middle;
}
.whole-background{
position: absolute;
width:100%;
height:100%;
background: rgba(0,0,0,.5);
}
Related
i want to ask you. I'm trying to create a border using pseudo element (after). I want to place a border in the middle of the div box that I have created, without setting (top, bottom) and (left, right). Can it be automated in the middle?
My Codepen
<div class="box">
</div>
.box{
height:500px;
width:300px;
background-color:red;
position:relative;
&::after{
content:'';
position:absolute;
border:2px solid #fff;
height:90%;
width:90%;
}
}
I've used percentages for the top and left and than correect the positioning with transform: translate(-50%, -50%); so its in the middle
.box{
height:500px;
width:300px;
background-color:red;
position:relative;
}
.box::after{
content:'';
position:absolute;
border:2px solid #fff;
height:90%;
width:90%;
left: 50%;
transform: translate(-50%, -50%);
top: 50%;
}
<div class="box"></div>
.box{
height:500px;
width:300px;
background-color:red;
position:relative;
}
.box:after {
content: '';
position: absolute;
border: 2px solid #fff;
top: 3%;
left: 4%;
height: 93%;
width: 90%;
}
<div class="box">
</div>
Yes it can:
.box {
width: 500px;
height: 500px;
position: relative;
background-color: blue;
padding: 10px;
}
.box:after {
content: '';
position: absolute;
display: block;
width: 95%;
height: 95%;
border: 1px solid #FFF;
}
<div class="box">
</div>
Allthough I don't completely understand why you wouldn't want to use top, right, bottom, left values. With transform, you can't automate it ALWAYS. You'll have to set it to half the height of the box. With a top value, you could just set it to 50%.
There are couple of ways....!!
.box{
height:500px;
width:300px;
background-color:red;
position:relative;
&::after{
content:'';
position:absolute;
border:2px solid blue;
height:90%;
width:90%;
right: 50%;
bottom: 50%;
transform: translate(50%,50%);
}
}
Another way...without using positions.
.box{
height:500px;
width:300px;
background-color:red;
display: flex;
&::after{
content:'';
border:2px solid blue;
height:90%;
width:90%;
margin: auto;
}
}
You can use flexbox on the .box element to center its contents. Just make sure no other position property is set on the ::after pseudo-element:
.box {
height: 500px;
width: 300px;
background-color: red;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
.box::after {
content: '';
position: absolute;
border: 2px solid #fff;
height: 90%;
width: 90%;
}
I'm developing ionic 2 app.
I'm looking to make profile picture in circle but it's not works fine.
I want it to be in the middle with border circle and white color.
html code
<div class="splash-info">
<div class="wrapper">
<img src="{{photo}}">
</div>
css code
.wrapper{
width:128px;
height:128px;
position: relative;
margin: 25px auto;
overflow: hidden;
border-radius:50%;
border: 5px solid white;
}
.wrapper img{
position: relative;
width:100%;
height:auto;
transform: translate(-50%,-50%);
}
.splash-info {
position: relative;
z-index: 2;
margin-top: -64px;
margin-bottom: 30px;
text-align: center;
font-size:15px;
font-weight:bold;
}
Just remove the transform: translate(-50%,-50%); from the img css. I also removed the negative margin you had on splash-info just for the purposes of this demo(so that the image wouldn't be cut off at the top).
.wrapper{
width:128px;
height:128px;
position: relative;
margin: 25px auto;
overflow: hidden;
border-radius:50%;
border: 5px solid white;
}
.wrapper img{
position: relative;
width:100%;
height:auto;
}
.splash-info {
background:#000;
position: relative;
z-index: 2;
margin-bottom: 30px;
text-align: center;
font-size:15px;
font-weight:bold;
}
<div class="splash-info">
<div class="wrapper">
<img src="http://placehold.it/128x128">
</div>
</div>
Solution, image position absolute inside parent: https://jsfiddle.net/209de4hu/
.wrapper{
width:128px;
height:128px;
position: relative;
margin: 25px auto;
overflow: hidden;
border-radius:50%;
border: 5px solid white;
}
.wrapper img{
right:50%;
position:absolute;
}
.splash-info {
position: relative;
z-index: 2;
text-align: center;
font-size:15px;
font-weight:bold;
}
<div class="splash-info">
<div class="wrapper">
<img src="http://www.ricoh-imaging.co.jp/common/img/header-logo2_en.jpg">
</div>
</div>
I want to make a div which has two divs inside itself, and also I want to position them ALL in center using the margin:auto property. But it's not working properly. I've searched the web for this problem but I couldn't find a good answer. What have I missed?
<style>
body{
padding: 0px;
margin:0px;
}
.top{
padding: 0px;
margin:0px;
width:100%;
height:50%;
background-color: lightblue;
}
.top div{
margin:auto;
padding:0px;
box-shadow:2px 2px 2px black;
width:200px;
height:200px;
background-color: red;
border-radius:20px;
}
.top div div{
margin:auto;
padding:0px;
box-shadow:2px 2px 2px black;
width:100px;
height:100px;
background-color: red;
border-radius:20px;
}
</style>
to whom asked for html:
<body>
<div class="top">
<div>
<div>
</div>
</div>
</div>
</body>
Please update you following class. Please add positions to .top div {position:relative;} and update the .top div div class.
Html
<div class="top">
<div><div></div></div>
</div>
Css
body{
padding: 0px;
margin:0px;
}
.top{
padding: 0px;
margin:0px;
width:100%;
height:50%;
background-color: lightblue;
}
.top div{
margin:auto;
padding:0px;
box-shadow:2px 2px 2px black;
width:200px;
height:200px;
background-color: red;
border-radius:20px;
position:relative;
}
.top div div{
margin:auto;
padding:0px;
box-shadow:2px 2px 2px black;
width:100px;
height:100px;
background-color: red;
border-radius:20px;
vertical-align: middle;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
Demo here
I have three div boxes
box1 - menu, box3 - info, and box2 - slide box which I want to be back of box1 and box3, (As the photo shows http://s16.postimg.org/u9mwuhmcl/111.png)
My attempt:
.box1 {
width:980px;
height:100px;
background-color:#CCCCCC;
}
.box2 {
width:980px;
height:100px;
background-color:#000;
position:relative;
z-index:1 ;
}
.box3 {
width:980px;
height:100px;
background-color:#CCCCCC;
}
<div class="box1">Menu</div>
<div class="box2">Slider</div>
<div class="box3">Info text</div>
Here is one options using position relative on the top and bottom boxes.
JSfiddle Demo
* {
color: white;
}
.box1,
.box3 {
margin: auto;
}
.box2 {
height: 100px;
background-color: #000;
position: relative;
z-index: -1;
}
.box1 {
width: 980px;
height: 100px;
background-color: #CCCCCC;
position: relative;
top: 1em;
}
.box3 {
width: 980px;
height: 100px;
background-color: #CCCCCC;
position: relative;
top: -1em;
}
<div class="box1">Menu</div>
<div class="box2">Slider</div>
<div class="box3">Info text</div>
2nd option with position relative on the second box.
JSfiddle Demo 2
* {
color: white;
}
.box1,
.box3 {
margin: auto;
}
.box2 {
height: 100px;
background-color: #000;
position: relative;
top: -1em;
z-index: -1;
}
.box1 {
width: 980px;
height: 100px;
background-color: #CCCCCC;
position: relative;
}
.box3 {
width: 980px;
height: 100px;
background-color: #CCCCCC;
position: relative;
margin-top: -2em;
}
<div class="box1">Menu</div>
<div class="box2">Slider</div>
<div class="box3">Info text</div>
Try
.box1, .box3 {
position: relative;
z-index: 0;
}
.box2 {
z-index: 1;
}
for box 1 and box 3.
Note: please set top,width,height according to your need.
.box1 {
width:500px;
height:50px;
background-color:#CCCCCC;
position:absolute;
left:0px;
right:0px;
top:0px;
}
.box3 {
width:500px;
height:50px;
background-color:#CCCCCC;
position:absolute;
left:0px;
right:0px;
top:100px;
}
For box 3
.box3 {
width:980px;
height:100px;
background-color:#CCCCCC;
margin-top:40px;
}
You better need a wrapper to simply the code.
No need to play with z-index in your case because position: relative; will give your block priority to the default position: static;
.wrapper{
width: 980px;
}
.box1, .box3 {
height:100px;
margin: 0 20px;
background-color:#CCCCCC;
box-sizing: padding-box;
position: relative;
}
.box2 {
height:100px;
margin: -20px 0;
background-color:#000;
}
<div class="wrapper">
<div class="box1">Menu</div>
<div class="box2">Slider</div>
<div class="box3">Info text</div>
</div>
use this-
.box1 {
width:90%;
height:100px;
background-color:#CCCCCC;
margin:0 auto;
position: relative;
z-index: 2;
bottom: -10px;
}
.box2 {
width:100%;
height:100px;
background-color:red;
position:relative;
z-index:1 ;
margin:0 auto;
padding:10px;
}
.box3 {
background-color: #cccccc;
height: 100px;
margin: 0 auto;
position: relative;
top: -10px;
width:90%;
z-index: 4;
<div class="box1">Menu</div>
<div class="box2">Slider</div>
<div class="box3">Info text</div>
I hope it will helps you.
I'm trying to center divs using these code but it's just centering vertical.
I want them horizontal.
HTML
<div class='test'>
<div class='test1'>
<img class='gamespic' src='img/{$row['pictures']}'></div>
</div>
CSS
.test{
width: 100%;
text-align: center;
}
.test1 {
width: 220px;
height: 300px;
border: solid 2px #000;
content: '';
display: inline-block;
vertical-align: middle;
margin-right: 0.5em;
margin-top: 0.5em;
margin-bottom: 0.5em;
position:relative;
}
change your CSS like this, I have also added full page coverage just in case (but you can remove it)
.test {
width: 100%;
height:100%;
text-align: center;
top:0;
left:0;
right:0;
bottom:0;
background:#fc0;
position:absolute;
}
.test1 {
width: 220px;
height: 300px;
border: solid 2px #000;
content:'';
display: inline-block;
vertical-align: middle;
margin-right: 0.5em;
margin-top: 0.5em;
margin-bottom: 0.5em;
position:absolute;
top:50%;
left:50%;
margin-top:-150px;
margin-left:-110px;
}
See bootply here