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.
Related
I have simething like this:
<div class="container">
<div class="left">
(an image)
</div>
<div class="right">
(some divs and other text)
</div>
</div>
and I want toalign the 2 divs side by side
.container {
width: 100%;
}
.left, .right {
float: left;
width: 50%;
}
What do I need to do, because this doesn't work
No have problem with your code... Divs are aligned side by side...
.container {
width: 100%;
}
.left,
.right {
float: left;
width: 50%;
border: 1px solid red;
box-sizing: border-box;
}
<div class="container">
<div class="left">
(an image)
</div>
<div class="right">
(some divs and other text)
</div>
</div>
If you want to do full-height divs with half screen spreading. this will work.
.Left {
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 50%;
background-color:black;
}
.Right {
position: absolute;
left: 50%;
top: 0;
bottom: 0;
right: 0;
background-color:blue;
}
Here's an working Example.
maybe you have given padding, margin, border to child div
it should work
try this
decrease some width of child div
50% to 49% like this or
.container {
width: 100%;
margin:0;
padding:0;
border:0;
}
.left, .right {
float: left;
width: 50%;
margin:0;
padding:0;
border:0;
}
Both divs are inside another div :
#container {
width: 100%;
}
#container > .first {
display:inline-block;
float:left;
width:100px;
}
#container > .second {
display:inline-block;
float:right;
margin: 0 auto;
width:100px;
}
<div id='container'>
<div class='first'>Left</div>
<div class='second'>Right</div>
</div>
The second div is aligned right not center though. How do I get it centered without using tranforms? I also need it so it is in one line, no stacking.
Unfortunately there is no simple method using floats, inline-block or even flexbox which will center the 'middle' div whilst it has a sibling that takes up flow space inside the parent.
In the snippet below the red line is the center point. As you can see the left div is taking up some space and so the middle div is not centered.
Sidenote: You can't use float and display:inline block at the same time. They are mutually exclusive.
#container {
text-align: center;
position: relative;
}
#container:after {
content: '';
position: absolute;
left: 50%;
height: 200%;
width: 1px;
background: #f00;
}
#container > .first {
float: left;
width: 100px;
background: #bada55;
}
#container > .second {
display: inline-block;
width: 100px;
background: #c0feee;
}
<div id='container'>
<div class='first'>Left</div>
<div class='second'>Center</div>
</div>
Solution:
You would have to remove one of the elements from the document flow using position:absolute and then position that element accordingly.
#container {
text-align: center;
position: relative;
}
#container:after {
content: '';
position: absolute;
left: 50%;
height: 200%;
width: 1px;
background: #f00;
}
#container > .first {
position: absolute;
top: 0;
left: 0;
width: 100px;
background: #bada55;
}
#container > .second {
display: inline-block;
width: 100px;
background: #c0feee;
}
<div id='container'>
<div class='first'>Left</div>
<div class='second'>Center</div>
</div>
<div style="width:100%;">
<div style="display:inline-block;float:left;width:100px;">Div 1</div>
<div style="display:block;width: 100px;margin: 0 auto;">
Div 2
</div>
</div>
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 Know there are several questions about this topic, however I think they depend a bit on another CSS properties given before.
I have a nested <div id="tituloParametros>" and I need its text/contain to be centred on vertical and horizontal position.
This is my markup:
<div id="outer">
<div id="parametros">
<div id="tituloParametros">Ingresa los puntos conocidos x,f(x)</div>
</div>
<div id="resultados">
<div id="graficos">
<div id="bars"></div>
<div id="fx"></div>
<div id="pinchetabla">Tabla inĂștil</div>
</div>
<div id="loquerealmenteimporta"></div>
</div>
</div>
And this is the applied CSS:
#outer{
padding-left: 15px;
padding-top: 15px;
width: 1350px;
height: 640px;
}
#parametros {
float:left;
width: 20%;
height: 100%;
}
#tituloParametros {
height: 9%;
width: 100%;
background-color: red;
text-align:center;
vertical-align:middle
}
#resultados {
float:right;
width: 80%;
height: 100%;
}
#graficos {
height: 75%;
width: 100%;
}
#bars {
float: left;
height: 100%;
width: 30%;
}
#fx {
float: left;
height: 100%;
width: 30%;
}
#pinchetabla {
float: left;
height: 100%;
width: 40%;
}
#loquerealmenteimporta {
height: 25%;
width: 100%;
}
I thought that:
text-align:center;
vertical-align:middle
both will make it but it didn't. Adding display: table-cell; doesn't solve it neither, it actually crops the background to the text limits.
This is how it looks like
You're right - the table/table-cell approach doesn't work here.
As an alternative, you could resort to the absolute positioning method. An element will be vertically centered when the top value is 50% subtracted by half the element's height. In this instance, it shouldn't be a problem because the height is already set with the % unit. 100% - 50% - 9%*.5 = 45.5% If this weren't the case, you could use calc() or negative margins to subtract the px unit from the % unit. In this case, it's worth noting that the child element is absolutely positioned relative to the parent element.
Updated CSS -- UPDATED EXAMPLE HERE
#parametros {
float:left;
width: 20%;
height: 100%;
outline : 1px solid black;
position:relative;
}
#tituloParametros {
background-color: red;
width: 100%;
height: 9%;
text-align:center;
position:absolute;
top:45.5%
}
The element #tituloParametros is now centered within the parent element. If you want to center the text within it, you could wrap the text with a span element and then use the table/table-cell vertical centering approach:
UPDATED EXAMPLE HERE
#tituloParametros {
/* other styling.. */
display:table;
}
#tituloParametros > span {
display:table-cell;
vertical-align:middle;
}
Here is my fix for this!::::
HTML:
<div id="parametros">
<div id="tituloParametros"><p>Ingresa los puntos conocidos x,f(x)</p></div>
</div>
CSS:
#tituloParametros {
height: 70px;
width: 100%;
background-color: red;
text-align:center;
vertical-align:middle
}
#tituloParametros p{
line-height: 70px;
}
<html>
<head>
<title>Universal vertical center with CSS</title>
<style>
.greenBorder {border: 1px solid green;} /* just borders to see it */
</style>
</head>
<body>
<div class="greenBorder" style="display: table; height: 400px; #position: relative; overflow: hidden;">
<div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
<div class="greenBorder" style=" #position: relative; #top: -50%">
any text<br>
any height<br>
any content, for example generated from DB<br>
everything is vertically centered
</div>
</div>
</div>
</body>
</html>
Here is the demo
http://www.jakpsatweb.cz/css/priklady/vertical-align-final-solution-en.html
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 :)