This scenario is somewhat different than the traditional margin:0 auto.
I try to center a div above another div, while both of them are in the same div (no need to look like they are in the same div, because blue box will be bigger than black box on top)!
I have create a fiddle of what I got so far, and I did align the top div into sort center, but its center in a sense of top left corner of each div. I want to center in a sense that, the blue box is RIGHT above the blackbox, but the bluebox's middle line is aligned with the middleline of the blackbox. So something like this
|______|
|__|
top box blue, bottom box black
But I just can't think of a way to do that.
http://jsfiddle.net/adamchenwei/nay8fe5q/
HTML
<div class="blockcontainer">
<div class="blockcenterbox">
<div class="blocktop">abc</div>
</div>
<div class="blockbottom"></div>
</div>
CSS
.blockcontainer {
margin:0 auto;
width:25px;
background-color:#00CC66;
}
.blocktop {
width:100px;
background-color:#6699FF;
height:50px;
}
.blockcenterbox {
width: .1px;
height: 5px;
background-color: yellow;
margin: 0 auto;
position: relative;
float: none;
}
.blockbottom {
width:25px;
height:25px;
background-color:black;
}
Is this what you're trying to achieve?
.blue {
width: 200px;
height: 100px;
background-color: blue;
}
.black {
width: 150px;
height: 80px;
background-color: black;
margin: 0 auto;
}
.blockcontainer {
margin: 0 auto;
width: 200px;
}
<div class="blockcontainer">
<div class="blue"></div>
<div class="black"> </div>
</div>
Try this working link at plnkr:
http://plnkr.co/edit/OaQBWxlIfa2fVvanKKEl?p=preview
Hope it helps!!!
HTML
<div class="blockcontainer">
<div class="blockcenterbox">
<div class="blocktop">abc</div>
</div>
<div class="blockbottom"></div>
</div>
CSS
.blockcontainer {
width:200px;
height:200px;
background-color:#00CC66;
overflow:hidden;
}
.blocktop {
width:100px;
background-color:#6699FF;
height:50px;
}
.blockcenterbox {
width: 100px;
height: 50px;
background-color: yellow;
margin: 0 auto;
}
.blockbottom {
width:25px;
height:25px;
background-color:black;
margin: 0 auto;
}
Related
I've got the bx-slider that has overflow:hidden on its wrapper. My layout dictates, that one element should break out of the box and lay above the top edge.
Does not sound hard at all - thats what overflow-y and x is for. I thought.
But no matter what I do, once I set one of the values to hidden, the other (x or y) is hidden too.
I've made a testcase which simulates the slider. I want only the grey element to be displayed in full size out of the wrapper box. The next elements should not be displayed until they flow into the viewport.
.wrapper {
margin: 50px auto;
width:400px;
overflow-x:hidden;
}
.sliderContainer {
width:1400px;
}
.sliderElement {
width:400px;
height:200px;
background-color:red;
display:inline-block;
margin-right:50px;
}
.breakoutElement {
width:50px;
height:50px;
background-color:grey;
margin: -25px auto 0 auto;
}
<div class="wrapper">
<div class="sliderContainer">
<div class="sliderElement">
<div class="breakoutElement"></div>
</div>
<div class="sliderElement"></div>
<div class="sliderElement"></div>
</div>
</div>
The reason for this behavior is according to the W3C spec:
[...] some combinations with ‘visible’ are not possible: if one is specified as ‘visible’ and the other is ‘scroll’ or ‘auto’, then ‘visible’ is set to ‘auto’.
You simply can add a top margin to your slide elements and adjust their size accordingly:
.sliderElement {
...
height: 175px;
margin-top: 25px;
...
}
If you want your slide to be exactly 200px, simply adjust the wrappers height.
.wrapper {
margin: 50px auto;
width: 400px;
overflow-x: hidden;
}
.sliderContainer {
width: 1400px;
}
.sliderElement {
width: 400px;
height: 175px;
background-color: red;
display: inline-block;
margin-right: 50px;
margin-top: 25px;
}
.breakoutElement {
width: 50px;
height: 50px;
background-color: grey;
margin: -25px auto 0 auto;
}
<div class="wrapper">
<div class="sliderContainer">
<div class="sliderElement">
<div class="breakoutElement"></div>
</div>
<div class="sliderElement"></div>
<div class="sliderElement"></div>
</div>
</div>
See this post for more information about the overflow problem.
Instead of margin add padding to wrapper.
Try this
.wrapper {
margin: auto auto;
padding:50px 0px;
width:400px;
display:block;
overflow-x:hidden;
}
.sliderContainer {
width:1400px;
}
.sliderElement {
width:400px;
height:200px;
background-color:red;
display:inline-block;
margin-right:50px;
}
.breakoutElement {
width:50px;
height:50px;
background-color:grey;
margin: -25px auto 0 auto;
}
<div class="wrapper">
<div class="sliderContainer">
<div class="sliderElement">
<div class="breakoutElement"></div>
</div>
<div class="sliderElement"></div>
<div class="sliderElement"></div>
</div>
</div>
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>
Suppose there is a div, say "parent-div".
The parent div has a background color. What if the child div, "child-div", needs to be set with a transparent background,such that it is set with the background image of the grandparent div, with class name "wrapper"?
I know that a child div can inherit css properties from parent div, but how do I set the background to transparent, making the whole picture appear like the parent-div has a hole in it?
.wrapper{
background-image: url('http://media.istockphoto.com/photos/medium-golden-brown-wood-texture-background-picture-id513694258?k=6&m=513694258&s=170667a&w=0&h=xETakP7VjpAtRj9e6rJRYNqw_AJLZ9ovLlC4ebR5BOQ=');
}
.parent-div{
width: 100px;
height: 100px;
background: #ff0000;
padding: 10px;
margin: auto;
}
.child-div{
width: 60%;
height: 60%;
margin: auto;
background: transparent;
border: 1px solid;
}
<div class="wrapper">
<div class="parent-div">
<div class="child-div">
</div>
</div>
</div>
Don't apply background on .parent-div.
Instead use a large value of box-shadow on .child-div and add overflow: hidden on .parent-div to hide unwanted shadow effect.
Following css will do the work:
.parent-div {
overflow: hidden;
}
.child-div {
box-shadow: 0 0 0 500px #f00;
}
.wrapper {
background-image: url('http://media.istockphoto.com/photos/medium-golden-brown-wood-texture-background-picture-id513694258?k=6&m=513694258&s=170667a&w=0&h=xETakP7VjpAtRj9e6rJRYNqw_AJLZ9ovLlC4ebR5BOQ=');
}
.parent-div {
overflow: hidden;
width: 100px;
height: 100px;
padding: 10px;
margin: auto;
}
.child-div {
box-shadow: 0 0 0 500px #f00;
border: 1px solid;
width: 60%;
height: 60%;
margin: auto;
}
<div class="wrapper">
<div class="parent-div">
<div class="child-div">
</div>
</div>
</div>
Check this Fiddle
based on:
.parent{
width:300px;
height:300px;
position:relative;
border:1px solid red;
}
.parent:after{
content:'';
background:url('http://www.dummyimage.com/300x300/000/fff&text=parent+image');
width:300px;
height:300px;
position:absolute;
top:0;
left:0;
opacity:0.5;
}
.child{
background:yellow;
position:relative;
z-index:1;
}
ref
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>
css
.container{
height: 250px;
padding 10px 0;
}
.col-left{
display: inline-block;
background-image:url("support.png");
height:235px;
width:300px;
}
.col-right{
display: inline-block;
width:600px;
}
html
<div class="container">
<div class="col-left"></div>
<div class="col-right">
<h1>this is my title</h1>
<p>to reach their Potential</p>
</div>
</div>
Question:
I want the img at the left and the texts at the right
to show on the same line.
vertically line up(texts appear in the middle position of img)
how could I do this?
I guess this is what you want. Live Demo
.container {
height: 250px;
padding 10px 0;
}
.col-left {
display: inline-block;
background-image:url("http://www.lois-systems.co.uk/wp-content/uploads/2012/08/support.png");
height:235px;
width:300px;
vertical-align:middle;
}
.col-right {
display: inline-block;
width:600px;
vertical-align:middle;
}
.col-right h1, .col-right p {
margin:0;
}
As far as I understand your question I found that Float will solve your problem. Float works same as inline-block. You can learn more about this from this url http://www.vanseodesign.com/css/inline-blocks/
So, as far as my knowledge concert I will go for following solution, Hope this will help you.
CSS :
.container {width:100%;height: 250px;padding 10px 0;}
.col-left {float:left;background-image:url("support.png");height:235px; width:30%;}
.co-right {float:left;width:70%}
JSFiddle : http://jsfiddle.net/ugQCU/
Here is the code you want to achieve try this i've just added a div for displaying vertical align use this and let me know
Html code is
<div class="container">
<div class="col-left"></div>
<div class="col-right">
<div class="col-right-wrap">
<h1>this is my title</h1>
<p>to reach their Potential</p>
</div>
</div>
</div>
and css
.container{
height: 250px;
padding 10px 0;
}
.col-left {
float:left; /*give a direction where you want */
display: inline-block;
background-image:url("images/support.png");
height:235px;
width:300px;
}
.col-right {
float:left; /*give a direction where you want */
display: inline-block;
width:600px;
}
/* align however you want adjust this below code according to you */
.col-right-wrap{
display:table-cell;
vertical-align:middle;
}
I think something like this should be what you want:
HTML:
<div class="container">
<div class="col-left">
<img src="http://placehold.it/300x235" />
</div>
<div class="col-right">
<h1>this is my title</h1>
<p>to reach their Potential</p>
</div>
</div>
CSS:
.container {
width:100%;
height: 250px;
padding 10px 0;
}
.col-left {
float:left;
background-image:url("support.png");
height:235px;
width:300px;
}
.col-right {
float:left;
width: 600px;
padding-top: 50px;
}
LIVE DEMO
Do note that for the placement of the text I used padding, which may not be an ideal solution.
HTML code the same and CSS:
.container {
position: relative;
height: 235px;
padding 10px 0;
}
.col-left {
position: absolute;
background: #AAA;
height:235px;
width:300px;
}
.col-right {
display: table-cell;
vertical-align: middle;
height: 235px;
padding-left: 310px;
}
Demo:
http://jsfiddle.net/zWNCP/2/