Could someone please have a look at my jsfiddle and see if you can make the red div vertically align in the middle and get the red div to be centred as well. You will have to make the div that contains the red div a certain height
jsFiddle
<div class="container">
<div class="row1">
<div>
<div style="height:200px; width:725px; background-color:red; margin:0px auto">A</div>
</div>
<div></div>
</div>
<div class="row2">
<div>B</div>
<div>C</div>
</div>
</div>
html, body {
height:100%; margin:0px; padding:0px
}
.container {
width: 100%;
height: 100%;
display:table;
position: relative;
}
.row1 {
display:table-row;
max-height: 425px;
background: pink;
}
.row1 div {
display:table-cell;
width:100%;
}
.row2 {
display:table-row;
height: 100%;
}
.row2 div {
width: 100%;
height: 100%;
float:left;
background: green;
}
.row2 div + div {
background: aqua;
width: 50%;
height: 100%;
position: absolute;
top:0;
right:0;
}
#media (max-width: 1024px) {
.row1 {
width: 100%;
}
.row1 div + div {
display: none;
}
.row2 div + div {
width:50%
}
.row2 div {
width: 50%;
}
.row2 div + div {
position: static;
}
}
Just a small change makes your table-cell rule apply only to the immediate children of what you're calling table-row: .row1 > div instead of .row1 div. See the update: http://jsfiddle.net/Xgr3k/11/
This fixes your problem except now your aqua colored div is popping up whenever your width > 1024. That's happening because of your media query and setting that div to position: static. Which I'm not sure you want to do. But, basically, be careful about how your rules cascade down your DOM. Good luck :)
Related
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>
I have one parent div and two child divs. My aim is to center one div and align the other div to the right and make both responsive.
I can't seem to find the conventional way to do align the second one to the right within this type of setup:
<div id="container">
<div id="middle">Centered to middle</div>
<div id="right">I want to be to the right</div>
</div>
CSS:
#container {
position: relative;
width: 100%;
}
#middle {
margin: 0 auto;
position: relative;
width: 100%;
max-width:150px;
height:300px;
}
#right {
max-width:150px;
width:100%;
}
Here is a jsFiddle. How do we crack this puzzle?
Make it inline-block, otherwise the second div would always be on the second line
Reduce their width in percentage, making it 33%
Remove max-width
#container {
position: relative;
width: 100%;
text-align:middle;
}
div{
display:inline-block;
}
#middle {
background: #ddd;
margin: 0 auto;
position: relative;
width: 100px;
margin-left:40%;
height:300px;
}
#right {
background:yellow;
width:33%;
float:right;
}
<div id="container">
<div id="middle">Centered to middle</div>
<div id="right">I want to to be to the right</div>
</div>
Added a pseudo element as left column, so the layout becomes to [left] + [middle] + [right] and set table for the parent and table-cell for the children.
JSFiddle Demo
#container {
display: table;
table-layout: fixed;
width: 100%;
}
#container:before, #middle, #right {
display: table-cell;
}
#container:before {
content:"";
}
#middle {
background: aqua;
width: 50%;
}
#right {
background: yellow;
}
<div id="container">
<div id="middle">Centered to middle</div>
<div id="right">I want to to be to the right</div>
</div>
There is another approach by using flexbox, can be found here.
I would like to set the 2-columns divs with the same height than container (without using px of course)
HTML
<body>
<div id="container">
<div id="hdr-lay">
Header
</div>
<div id="left-column">
Grid Layout left
</div>
<div id="right-column">
Grid Layout right
</div>
</div>
</body>
CSS
#hdr-lay {
_background-color: red;
}
#container {
background-color: gray;
height:100%;
width:100%;
}
#left-column {
float: left;
background-color: red;
border: 1px;
width: 70%;
}
#right-column {
float: left;
width: 30%;
background-color: blue;
display: block;
}
http://jsfiddle.net/g3gxv4j2/
Perhaps it would be easier to do itwith no ?
I would like to set the 2-columns divs with the same height than
container
Since your container have height:100%, I assume you want the same for your child div's
Give 100% height to your html and body
html,body{
height:100%
}
You've set height:100% for your container. This will only extend its height to 100% of its content(which themselves are not getting 100% height). Let your left and right columns inherit height from their parent container.
#right-column {
float: left;
width: 30%;
background-color: blue;
display: block;
height:inherit;
}
#left-column {
float: left;
background-color: red;
border: 1px;
width: 70%;
height:inherit;
}
Here's the fiddle
Cheers!
This might be better :
#container {
display:flex;
flex-direction:row;
}
#left-column {
width: 30%;
background-color: blue;
}
#right-column {
background-color: red;
width: 70 %;
}
Is there a possibility to put an image on the middle of a page?
BUT: On the right side and the left side of the image, there are 2 areas that can grow according to the screen resolution.
These areas are "1 pixel repeat-x" images.
Please note: the image on the right side and the left side aren't the same picture!
Below a picture with a sketch that (I hope) will explain my problem:
You can do it like this
Set the image in .container where for now I have added sample text.
CSS
.rightArea,
.leftArea {
position: absolute;
width: 50%;
background: yellow;
top:0;
left:0;
height: 100%;
}
.rightArea {
background: red;
right: 0;
left: auto;
}
Here is one way of building this layout using CSS table cells.
Start with this HTML:
<div class="wrapper">
<div class="left"></div>
<div class="center">
<img src="http://placekitten.com/400/200">
</div>
<div class="right"></div>
</div>
and apply the following CSS:
.wrapper {
display: table;
width: 100%;
border: 1px solid blue;
}
.left, .center, .right {
display: table-cell;
border: 1px dotted blue;
vertical-align: top;
}
.center {
width: 1%;
}
.left, .right {
width: 50%;
}
.left {
background-image: url(http://placekitten.com/4/100);
background-repeat: repeat-x;
background-position: left center;
}
.right {
background-image: url(http://placekitten.com/10/100);
background-repeat: repeat-x;
background-position: left center;
}
.center img {
display: block;
}
The .wrapper has a width of 100%, so it fills the width of the page.
The child elements .left, .center and .right are table cells.
.center is forced to shrink-to-fit the image by setting the width to some small value, for example, 1%.
The .left and .right elements are set to the same width, 50%, which forces them to take up the remaining space equally.
You can apply background images as needed to any of the child elements.
See demo at: http://jsfiddle.net/audetwebdesign/pG2v3/
Note: Most modern browsers support CSS table cells.
You can relay on a single container and pseudo-elements.DEMO
display:table/table-cell -properties will make this easy to manage: (update test with your image name/path)
HTML
<div>
<img src="middle.jpg" />
</div>
CSS
img {
display:block;/* avoid gap underneath*/
margin:auto;/*optionnal*/
}
div {
display:table;
width:100%;
background:#7E858F;
}
div:before, div:after {
content:' ';
display:table-cell;
width:50%;/* will shrink to leave room for image */
background-repeat:repeat-x;
}
div:before {
background-image:url(left.jpg);/* slice of 1px */
}
div:after {
background-image:url(right.jpg);/* slice of 1px */
}
DEMO
To freely grow sided placed divs, keeping a content in the middle, use a wrapper display: table; element, and set the inner divs to display:table-cell, so the center element will adjust according to the width of the right and left divs:
http://jsfiddle.net/4uHm8/
HTML:
<div class='wrapper'>
<div class='left'></div>
<div class='center'>test</div>
<div class='right'></div>
</div>
CSS:
.wrapper {
display: table;
width: 100%;
height: 200px;
}
.wrapper > div {
display: table-cell;
}
.left {
width: 30px;
background: red;
}
.right {
width: 50px;
background: blue;
}
EDIT:
In the same line of thought, if you set only the central div's width, the left and right divs will adjust themselves equally using the remaining width... even zero.
http://jsfiddle.net/4uHm8/1/
.wrapper {
display: table;
width: 100%;
height: 200px;
}
.wrapper > div {
display: table-cell;
}
.center {
background: blue;
width: 100px;
}
.left, .right {
background: red;
}
HTML
<div class="main">
<div class="left"> </div>
<div class="image">
<img src="http://www.ricoh.com/r_dc/r/r8/img/sample_10.jpg" height="150" width="120" alt="image" />
</div>
<div class="right"> </div>
</div>
CSS
.main {
width: 100%;
margin: 0 auto;
position: relative;
}
.left {
float: left;
width: 50%;
background: #FF0000;
}
.right {
float: right;
width: 50%;
background: #FFFF00;
}
.image {
position: absolute;
}
jQuery
$(document).ready(function () {
$(".image").css('right', ($(".main").width() / 2) - ($(".image").width() / 2));
$(window).resize(function() {
$(".image").css('right', ($(".main").width() / 2) - ($(".image").width() / 2));
});
});
Example
http://jsfiddle.net/wphn9/
I have this code:
.wrapper{
position: relative;
height:315px;
}
.wrapper.image{
position: absolute;
bottom: 0px;
height: 259px;
}
.wrapper.text{
}
.left{
display: inline;
float:left;
}
<div class="wrapper">
<div class="image left"><img src="img.jpg"></div>
<div class="text left">Text</div>
</div>
I want the image positioned at the bottom of the wrapper div, but then my text doesn't recognize the image div as a element and the text div is placed on top of the image div.
I could put in position absolute for the text as well, but seems like too much hard-coding.
Is there another way to make sure these two divs recognize (a better word for this?) each other?
I want this design:
http://i.imgur.com/iMjTg9z.jpg
In this example I'm showing 2 rows.
A fiddle:
This way the content can decide the height and it's responsive
edit:
here is another fiddle that uses inline-block - but you'll have to really understand positioning to have it be useful http://jsfiddle.net/sheriffderek/62pU3/
/* vertical center needs, (more that one thing... display: inline-block and vertical-align: middle - on all things involved... keep in mind they are aligning to each other --- not their parent */
(original)
HTML
<div class="block">
<div class="image-w">
<img src="http://placehold.it/600x300" alt="" />
</div>
<div class="text-w">
<p>text text text</p>
</div>
</div> <!-- .block -->
CSS
.block {
width: 100%;
border: 1px solid red;
/* this should be a clear fix - or floated instead - because now that the divs inside are floated, it no longer understands how they work unless clear fixed, or (floated itself ) */
overflow: hidden; /* temp replacement for clear fix */
/* float: left; */
}
.image-w img { /* image fills wrapper | decide size with wrapper */
display: block;
width: 100%;
height: auto;
}
.block .image-w {
float: left;
}
.block .text-w {
float: left;
}
/* #media rule "break-point" */
#media (min-width: 40em) {
.block {
padding-top: 2em; /* arbitrary space */
}
.block .image-w {
max-width: 15em;
margin-right: 1em;
}
/* i would usually use nth-of-type(even) {} */
.oposite-block .image-w {
float: right;
}
.block .text-w {
float: none;
max-width: 50em;
}
} /* end breakpoint */
You can wrap your image and text inside one div and apply position: absolute.
Fiddle
.wrapper {
position: relative;
height:315px;
border: 1px solid red;
}
.content {
position: absolute;
bottom: 0px;
}
.left {
display: inline-block;
}
Check this Demo jsFiddle
HTML
<div class="wrapper">
<div class="content">
<div class="image">
<img src="http://podcast.iu.edu/upload/IUSEUITS/images/300x300.gif" />
</div>
<div class="text">Text</div>
</div>
</div>
CSS
.wrapper {
position: relative;
height:50px;
border: 1px solid #f15a27;
}
.content {
position: relative;
}
img{
width:100px;
height:50px;
}
.image {
display: inline-block;
float:left;
}
.text {
margin:16px 10px 0 0;
float:right;
}