To Put it simple, I would like a header with two elements floating to each side and vertically centered:
I started out with doing this with non-floating elements and managed to make this example.
But once I add the float:left or float:right the vertical centering is lost (I understand why, because it's not part of the flow anymore)
I wonder what is the best method to achieve this. Complete CSS redesign is happily accepted.
Thanks in Advance!
Vertical centering can be painful, especially when you are not dealing with inline elements. In this case, I would recommend taking advantage of display:table-cell.
HTML
<div id="wrapper">
<div class="cell">
<div class="content">
Content Goes here
</div>
</div>
<div class="cell">
<div class="content2">
<div class="redbox">
</div>
</div>
</div>
</div>
CSS
#wrapper {
color: white;
display: table;
border: 1px solid darkblue;
background: blue;
width: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
height: 200px;
}
.content {
float: left;
}
.content2{
float: right;
}
.redbox {
border: 2px solid darkred;
background: red;
height: 75px;
width: 75px;
}
Example: http://jsfiddle.net/YBAfF/
Add text-align:right to parent div, it makes child elements to align right side. Now add float:left to #text
#parent {
border: 1px solid black;
display: block;
line-height: 400px;
height: 400px; text-align:right
}
#text {
display: inline-block;
border: 1px dashed black;
height: 100%; text-align:left; float:left
}
#logo {
border: 1px dashed black;
height: 90%;
line-height: 90%;
vertical-align: middle;
display: inline-block;
}
#logo img {
border: 1px dashed red;
height: 100%;
}
DEMO
Here's a sample jsfiddle and the same code below. When you set the height of an element, you can set the same line-height to nested elements and they'll expand to the height. Vertically centering the content.
HTML
<div id="wrapper">
<div id="left">left</div>
<div id="right">right</div>
</div>
CSS
#wrapper{
margin:0 auto;
width 960px;
background: #eee;
height:50px;
}
#left{
float:left;
background:#ccc;
line-height:50px;
}
#right{
float:right;
background:#ddd;
line-height:50px;
}
You should add a wrapper around the elements you want to center and float them inside the wrapper. Something like that:
HTML
<div class="center">
<p class="left">Some text goes here</p>
<img src="/path/toimage" alt="My image" class="right">
</div>
CSS
.center {
margin:0 auto;
width: 400px;
}
.right {
float: right;
}
.right {
float: left;
}
Of course, this is a very simple example. You can change the values and CSS according to your needs.
Related
I'm a real beginner in CSS...
Do you know how could I do this sort of alignment ? I try a lot a things but I don't get what I need... So I just draw it if you have any code idea...
<div id="sys-wrap">
<img src="image.png">
<p>Long message texte</p>
</div>
#sys-wrap {}
#sys-wrap p {
border: 1px solid #ffffff;
float:left;
margin: 15px;
width: 691px;
}
#sys-wrap img {
border: 1px solid #ffffff;
float: left;
margin: 15px 15px 15px 100px;
vertical-align: top;
}
Thanks!
(source: imgsafe.org)
I made a fiddle. Does this work for you?
https://jsfiddle.net/hnf8jou4/3/
HTML:
<div>
<div id="left">
<img src="http://lorempixel.com/400/200" id="inner">
</div>
<div id="right"></div>
</div>
CSS:
#left {
width:25%;
height:200px;
float: left;
background-color:#f00;
}
#right {
width:75%;
height:200px;
background-color:#0f0;
}
#inner {
display:block;
width:100px;
margin-left: auto;
margin-right: 0;
background-color: #00f;
}
It is really unclear what exactly you are trying to achieve. But since i saw the drawing u made i suppose this is what u need. U need to have a div for wrap and add float: right to the right div. The other things are just playing with height in % and paddings.
https://jsfiddle.net/hnf8jou4/4/
For #sys-wrap img inside of your CSS file, add a float: right; and a margin of 15px
Your new CSS should look similar to this:
#sys-wrap img {
width: 50px;
height: 50px;
border: 1px solid #ffffff;
float: right;
margin: 15px;
}
Also, give it's parent container a set width and height:
#sys-wrap {
width: 150px;
height: 150px;
background: red; // just to make it pretty
}
And just as a little fyi, vertical-align does not work, unless the specified element has a display of table-cell. :-)
Here's a fiddle: https://jsfiddle.net/d2ae3ub3/
I'm trying to center these 3 floated divs on the same line. Here is a link to jsfiddle:
https://jsfiddle.net/dtps4fw8/2/
any suggestions?
HTML:
<div class="content">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
CSS:
.box {
width: 30%;
height: 200px;
float: left;
background: gray;
border: black solid 2px;
box-sizing: border;
margin: 5px;
}
See this fiddle
To make the 3 divs centered, first of all, remove the floatproperty and then to apply the floated effect, use display:inline-block. inline-block display gives a textual characteristics to the div. A text-align:center for the parent div would center these inline-block elements inside the parent.
Update your CSS as follows
.box {
width: 30%;
height: 200px;
display: inline-block;
background: gray;
border: black solid 2px;
box-sizing: border;
margin: 5px;
}
.content {
text-align: center;
}
First the float:left; is not relevant in your case, just like Lal said, instead of float:left; its should be display:inline-block; and you can also add a relative positioning position:relative;
I use flexbox. Very minimal and responsive.
.content {
width:100%;
display: flex;
flex-direction:row;
flex-wrap:wrap;}
.box {
height: 200px;
flex:1;
background: gray;
border: black solid 2px;
box-sizing: border;
margin: 5px;}
I have some html as given in code below.
I am using display:table and display:table-cell for laying out the divs in a table-like manner without using html table. This works fine except that the widths of left and right cells are much bigger than the width of their contents.
A demo for this question is at following URL: http://js.do/sun21170/87371
Question: What CSS I can use to make the left and right cells automatically resize to their contents? I do not want to specify the widths of these cells.
I tried to solve this problem by setting the center div width to 100% which did what I was after but then the margins of left and right divs are not respected. So may be there is a better solution that I am missing!
HTML Markup
<script></script>
<style>
#rgcmd {
width:100%;
display:table;
position:relative;
border:solid 1px green;
}
.leftCell {
margin-right: 5px;
height:100%;
display:table-cell;
vertical-align:middle;
border-right:solid 1px red;
}
.rightCell {
margin-left:
5px;height:100%;
display:table-cell;
vertical-align:middle;
border-left:solid 1px red;
text-align:right;
}
</style>
<div id="rgcmd">
<div class="leftCell">Go Left</div>
<div style="display:table-cell; vertical-align:top;">
<span id="status">Your status is offline</span>
</div>
<div class="rightCell">Go Right</div>
</div>
Add white-space: nowrap to the left and right cells and width: 100% to the center cell:
#rgcmd {
width: 100%;
display: table;
position: relative;
border: solid 1px green;
}
.leftCell {
padding-right: 5px;
height: 100%;
display: table-cell;
vertical-align: middle;
border-right: solid 1px red;
white-space: nowrap;
}
.rightCell {
padding-left: 5px;
height: 100%;
display: table-cell;
vertical-align: middle;
border-left: solid 1px red;
text-align: right;
white-space: nowrap;
}
.centerCell {
width: 100%;
display: table-cell;
vertical-align: top;
}
<div id="rgcmd">
<div class="leftCell">Go Left
</div>
<div class="centerCell">
<span id="status">Your status is offline</span>
</div>
<div class="rightCell">Go Right
</div>
</div>
The {margin:0 auto} can't make all the content center-placed in my case.
div.whole {
width: 620px;
overflow: auto;
border: 2px solid red;
}
div.left,
div.right {
margin: 0 auto;
float: left;
width: 300px;
height: 200px;
border: 2px solid black;
}
li {
list-style: none;
margin: 0 0;
padding 0 0 0 0;
display: inline-block;
border-bottom: 1px dashed black;
width: 100px;
}
<div class="whole">
<div class="left">
<img src="images/pic.png" width="120" height="120" />
</div>
<div class="right">
<ul>
<li>x1</li>
<li>y1</li>
</ul>
<ul>
<li>x2</li>
<li>y2</li>
</ul>
<ul>
<li>x3</li>
<li>y3</li>
</ul>
<ul>
<li>x4</li>
<li>y4</li>
</ul>
</div>
</div>
The displayed effect is as the following.
How to put the left content--img photo and the right content in the center of div.left and div.right ?
How to put the left content--img photo and the right content in the center of div.left and div.right ?With the help of shareeditflag, display: inline-block;vertical-align: middle; added into my css,the css file was changed into following:
div.whole{
width:620px;
overflow:auto;
border:2px solid red;
text-align: center;}
div.left,div.right{
display: block;
margin: 0 auto;
height: 100%;
float:left;
width:300px;
height:200px;
border:2px solid black;
vertical-align: middle;}
li{
list-style:none;
display: inline-block;
vertical-align: middle;
padding 0 0 0 0;
display:inline-block;
border-bottom: 1px dashed black;
width:100px;}
<div class="whole">
<div class="left">
<img src="images/pic.png" width="120" height="120"/>
</div>
<div class="right">
<ul>
<li>x1</li><li>y1</li>
</ul>
<ul>
<li>x2</li><li>y2</li>
</ul>
<ul>
<li>x3</li><li>y3</li>
</ul><ul>
<li>x4</li><li>y4</li>
</ul>
</div>
</div>
The displayed effect was changed into the following.
ALL the content were made center horizontally,
How to make the content vertically?
Centering the image in the Div you could use padding: to align. on .div-left
Centering the text content should be as simple as a text-align:centeron .div-right
there's a way to make anything centered. You don't need to know the height or width of any component, except the parent's. This involves a couple of things:
heres a fiddle example: https://jsfiddle.net/ahmadabdul3/eed1n1kj/1/
make sure the parent has an explicit height. I say this because html and body are sometimes forgotten. set their heights to 100%. The next thing is, every parent until your centered component should have some sort of height.
This is required for the centered-helper class that you will see.
The second thing is, the parent of the centered content should have text-align: center so that it can be centered horizontally.
Finally, the centered content should be set to vertical-align: middle along with the centered-helper should have height 100% so that it can help center your content vertically.
html:
<div class='parent'>
<div class='centered'>
centered
</div>
<div class='centered-helper'>
</div>
</div>
css:
.parent {
width: 400px;
height: 400px;
border: 1px solid black;
text-align: center;
}
.centered {
display: inline-block;
vertical-align: middle;
}
.centered-helper {
display: inline-block;
vertical-align: middle;
height: 100%;
}
You can use margin for center align.
<head>
<style>
div {
margin: auto;
width: 60%;
border: 3px solid #73AD21;
padding: 10px;
}
</style>
</head>
The text-align property will apply to images also.
So you could do this:
.left, .right {
text-align: center;
}
And this will center the image too.
jsFiddle: https://jsfiddle.net/sukritchhabra/uxve3n53/
Short way for margin is (THe first is for top and bottom the second is for left and right.
Margin:0 auto;
make sure that you give this the a child of a div .
Is there a possibility for the div (#contentwrapper) to take all the remaining width while floating side by side for the next example:
#maincontainer {
width:1000px;
height: 100%;
display:inline-block;
}
#leftcolumn {
float:left;
width: 100px;
height:20px;
background: blue;
}
#contentwrapper {
float:right;
width:900px;
height: 20px;
background-color: red;
}
<div id="maincontainer">
<div id="leftcolumn"></div>
<div id="contentwrapper"></div>
</div>
JsFiddle
Try using flexbox. It is better than using tables. Make sure to include the vendor prefixes in it.
https://jsfiddle.net/qa6cds9c/
<div id="maincontainer">
<div id="leftcolumn"></div>
<div id="contentwrapper"></div>
</div>
#maincontainer {
border: 1px solid red;
display: flex;
}
#leftcolumn {
border: 1px solid blue;
height: 400px;
width: 100px;
}
#contentwrapper {
border: 1px solid green;
height: 400px;
flex: 1;
}
It's really simple. Using good ol' CSS:
float-left only the left element
add margin-left to the right column to compensate the left's one width:
#leftcolumn {
float:left;
width: 100px;
background: blue;
}
#contentwrapper {
margin-left: 100px; /* same as #leftcolumn width */
background: red;
}
<div id="maincontainer">
<div id="leftcolumn">left</div>
<div id="contentwrapper">right<br>contentwrapper</div>
</div>
You could use a table instead of The divs. Or make the divs behave as a table using CSS.
When you have two table cells in a row and set the width for one then the other will fill the remaining space.
Not sure if this is considered best practice though.