4 Divs side by side (Menu Bar) - css

I need to have these divs side by side for a menu bar but up until now it keeps stacking up on each other. I have tried doing margin-right/left/top/bottom, padding, etc... but can't get it to work, any suggestions?
<div id="Menu" >
<div id="M_1"><a id="M_1_L" href="P4.html">Given</a></div>
<div id="M_4"><a id="M_4_L" href="P2.html">Received</a></div>
<div id="M_3"><a id="M_3_L" href="P3.html">Bucket List</a></div>
<div id="M_2"><a id="M_2_L" href="P1.html">Traditions</a></div>
</div>
The CSS is:
#Menu
{
width: 50%; height: 40px; background-color: blue; margin-left: auto;
margin-right: auto; margin-top: 20px; border-radius: 20px;
}
#M_1
{
text-align: center; width: 20%; background-color: black;
}
#M_2
{
text-align: center; width: 20%; background-color: black;
}
#M_3
{
text-align: center; width: 20%; background-color: black;
}
#M_4
{
text-align: center; width: 20%; background-color: black;
}

add these two for each one
#M_1
{
display: inline-block;
float: left;
}
if that doesn't fit them all in, try changing 1 or 2 or all of them to 19% width instead

You need to float: left them or set them to display: inline-block.

You can tidy your CSS by merging selectors.
#menu{
display:table;width: 50%; height: 40px; background-color: blue;
margin:20px auto; border-radius: 20px;
}
#menu > div{
text-align: center; background-color: black;display:table-cell
}

This best thing to do would be:
<ul>
<li>given</li><li>
received</li>
</ul>
Ul {
Width:50%;
Height:40px;
Background: blue;
Margin: 0 auto;
}
Ul li {
Display:inline-block;
Width:20%;
Background:#000;
Text-align: center;
}
Having the closing tag and open tag next to each other stops inline-block from leaving a gap between the elements.

Related

Image not Vertical Aligned

.topnavigatiebar {
width: 1500px;
height: 100px;
background-color: #fafafa;
position: fixed;
top: 0;
overflow: hidden;
z-index: 9;
white-space: nowrap;
}
.topnavigatiebar img {
float: left;
}
.topnavigatiebar #nav {
width: 80%;
height: 50%;
margin-top: 25px;
margin-left: 20px;
float: left;
display: block;
text-decoration: none;
text-align: center;
}
.topnavigatiebar #nav #sector {
width: auto;
height: 75%;
float: left;
margin: 0 15px 0 15px;
padding: 5px 10px 5px 10px;
border-radius: 6px;
}
.topnavigatiebar #nav #sector:hover {
background-color: lightgrey;
transition: 0.25s;
}
.topnavigatiebar #nav .active {
background-color: #b7a483;
}
.topnavigatiebar #nav:visited {
color: black;
}
.topnavigatiebar #nav #sector a {
font-size: 20px;
vertical-align: center;
line-height: 40px;
text-decoration: none;
color: inherit;
}
#logo {
display: inline-block;
margin-top: auto;
height: 100%;
vertical-align: middle;
}
#logo img {
background: #3A6F9A;
vertical-align: middle;
max-height: 100px;
max-width: 100px;
border-radius: 45px;
}
.right {
float: right;
}
<div class="topnavigatiebar">
<img src="https://imgur.com/trhdmMX">
<div id="nav">
<div id="sector">Home</div>
<div id="sector" class="active">KlantenInformatie</div>
<div id="sector">Artikelen</div>
<div id="sector">Instellingen</div>
<div class="right">
<div id="sector">Afmelden</div>
</div>
</div>
<div id="logo"><img src="../Includes/Pictures/ProfielLogo.png"></div>
</div>
So i couldn't find a specific answer to my question, but something in my html is going wrong.
i try to vertical align a image to the center of a navigation bar. the image is for profile pictures, so you can see who is logged in (the login config isn't there yet, but that is for later).
Can anyone look at my code and tell me what i am doing wrong?
It just might be very simple and me stupid enough to forget, but i just can't figure out?
i already tried these methods, but they didn't solve my problem:
How to vertically align an image inside a div?
facebook photo/ image vertical align?
vertical-align image in div
EDIT: here are 2 links for the pictures:
https://imgur.com/trhdmMX
Try adding margin-top: 20% in the #logo img in CSS.
You can also do the same by adding display: block; margin-top: 25%; in #logo img in CSS as well.

A div that follows another appears to alter the first div's top margin

I have an HTML code that can be simplified as:
<body>
<div class='header'>
</div>
<div class='main'>
<nav class='menu'>
<a>a</a><a>b</a>
</nav>
<div class='content'>Here be dragons!</div>
</div>
</body>
It uses the following CSS:
html, body
{
margin: 0;
padding: 0;
background-color: #7effad;
}
.header
{
background-color: black;
position: relative;
height: 20px;
}
.main
{
position: relative;
}
.main .menu
{
background-color: white;
}
.main .menu a,
.main .menu a:visited,
.main .menu a:active
{
display: block;
float: left;
background-color: blue;
border-radius: 2px;
margin: 5px;
}
.main .menu:after
{
display: block;
content: "";
clear: both;
height: 0;
line-height: 0;
visibility: hidden;
}
.main .content
{
position: relative;
padding-top: 12px;
padding-bottom: 10px;
padding-left: 15px;
padding-right: 15px;
margin-left: 21px;
margin-right: 21px;
margin-top: 15px;
margin-bottom: 0;
min-height: 1500px;
background-color: #d4b074;
}
As you can see here this generates some sort of a margin between the header and the menu. However, if i remove the content tag all together this margin magically disappears.
I don't understand how it can alter the appearance, since it is a normal block that follows the menu.
Edit: The problem only occurs in Firefox, Chromium does everything just fine.
The problem goes away if i set margin-top for .main .content to 0; however if margin-bottom of .main .menu is non-zero the problem returns.

Setting last cell right aligned in a row

I'm setting my div display as table-cell like this.
HTML
<div id="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="lastchild">4</div>
</div>
CSS
.child{
background: red;
width: 20px;
display: table-cell;
}
.lastchild {
background: blue;
width: 20px;
display: table-cell;
margin-right: 0;
}
.parent {
width:200px;
}
http://jsfiddle.net/Lx1p3y93/
I wonder if there's any way I can set the rightmost cell right aligned?
Change CSS to:
.lastchild {
background: blue;
width: 20px;
display: table-cell;
margin-right: 0;
float:right;
}
.child {
background: none repeat scroll 0 0 red;
display: table-cell;
float: left;
width: 20px;
}
Remove table-cell property and use float property. Update your CSS like below.
.child{
background: red;
width: 20px;
float:left;
}
.lastchild {
background: blue;
width: 20px;
margin-right: 0;
float:right;
}
FIDDLE DEMO
How about pseudo elements? demo
.child{
background: red;
width: 20px;
display: table-cell;
float:left;
}
.child:last-child {
background: blue;
width: 20px;
display: table-cell;
margin-right: 0;
float:right;
}
.parent {
width:200px;
}
For this purpose you can use this piece of code into your .lastchild class:
.lastchild {
background: blue;
width: 20px;
display: table-cell;
margin-right: 0;
float: left;
}
This will align the .lastchild item to the most right of the .parent element

divs floated to left and right + another div therebetween

This is my mockup:
I want to set divs as in the picture. DIV 1 and DIV 2 contain dynamically generated content and the width is different every time.
Div 1 is floated to the left side and Div 2 is floated to the right side.
My question is: how to position Div 3 to fit it between div 1 and 3?
My code is:
HTML
<div class="dia">
<div class="left">sassssss</div>
<div class="center">dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd</div>
<div class="right">asdasdfgdsgdf</div>
</div>
CSS
.dia {
background: #282828;
padding: 10px;
height: 106px;
border-radius: 5px;
}
.dia .center {
margin-left: 20px;
}
.dia .left, .dia .right {
overflow: hidden;
}
.dia .left {
float: left;
margin-right: 10px;
}
.dia .right {
float: right;
margin-left: 10px;
background: rgb(214,214,214);
}
and I want to fit center div which now is too wide and moves right div lower than I want.
SOLUTION:
HTML
<div class="dia">
<div class="left">sassssss</div>
<div class="center">ddddddddddddddddddddd dddddddddddddddddddddddddd ddddddddddddddddddddddddddddddddd dddddddddddd</div>
<div class="right">asdasdfgdsgdf</div>
</div>
CSS
.dia {
background: #282828;
padding: 10px;
height: 106px;
border-radius: 5px;
width: inherit;
}
.dia .left, .dia .right {
overflow: hidden;
height: 106px;
}
.dia .left {
float: left;
margin-right: 10px;
}
.dia .right {
float: right;
margin-left: 10px;
}
.dia .center {
height: inherit;
float: left;
}
JQuery
$('.dia .left a:not(:first-child)').css('width',$('.dia a').outerWidth(true)+'px');
$('.dia .center').css('width',$('.dia').width()-$('.dia .left').outerWidth(true)-$('.dia .right').outerWidth(true)+'px');
And Fiddle
Simply float the central div.
For your purpose you case use:
.dia{
float:left//Float container in order to contain nested elements.
}
.dia div{
float:left;
}
Here's a fiddle you can fiddle with
Well I did manage to do this in this fiddle, but it feels a little like a workaround, since you have to be careful with center content. Then again, it works, so you can use it as a placeholder or something until you or someone else comes up with better solution.
CSS:
.dia {
background: #282828;
padding: 10px;
height: 106px;
border-radius: 5px;
}
.dia .center {
overflow:auto;
height: 100px;
background-color: #aaa;
margin-left: 20px;
}
.dia .left {
float: left;
height: 100px;
background-color: #ddd;
}
.dia .right {
position:absolute;
/*.dia padding + border*/
right: 15px;
/*.dia padding + border + (.dia height - this height)/2*/
top: 18px;
height: 100px;
background: rgb(214,214,214);
}
try this.
<!DOCTYPE html>
<html>
<head>
<style>
#main
{
width: 100%;
height: 300px;
border: 1px solid black;
display: -webkit-flex; /* Safari */
display: flex;
}
#panelLeft, #panelRight
{
-webkit-flex: 1; /* Safari 6.1+ */
-ms-flex: 1; /* IE 10 */
flex: 1;
}
#panelCenter
{
-webkit-flex: 3; /* Safari 6.1+ */
-ms-flex: 3; /* IE 10 */
flex: 3;
}
</style>
</head>
<body>
<div id="main">
<div id="panelLeft" style="background-color:coral;">RED</div>
<div id="panelCenter" style="background-color:lightblue;">BLUE</div>
<div id="panelRight" style="background-color:lightgreen;">Green div with more content.</div>
</div>
</body>
</html>
How about using a table instead?
<table class="dia">
<tbody>
<tr>
<td class="left">sassssss</td>
<td class="center">dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
<td class="right">asdasdfgdsgdf</td>
</tr>
</tbody>
</table>
Then the CSS would be:
.dia {
background: #282828;
padding: 10px;
height: 106px;
border-radius: 5px;
width: 100%;
table-layout: fixed;
}
.dia .center {
margin-left: 20px;
word-wrap: break-word;
}
.dia .left, .dia .right {
overflow: hidden;
}
.dia .left {
float: left;
margin-right: 10px;
}
.dia .right {
float: right;
margin-left: 10px;
background: rgb(214,214,214);
}
http://jsfiddle.net/YvnnW/13/

Display: Table-cell doesn't center the div

I am trying to center an image within a div and I used the display:table & display: table-cell method. It is centered but it is off by a few pixels from the top. What can I do to correct this?
CSS
html, body {
width: 100%;
height: 100%;
}
body,html {
margin: 0;
padding: 0;
color:#ffffff;
text-align: center;
}
#wrapper{
text-align: left;
width: 940px;
margin: 0 auto;
margin-top: 22px;
background-color: #ffffff;
overflow: hidden;
}
header {
width: 908px;
height: 76px;
display: table;
border-style:solid;
border-top-width: 16px;
border-bottom-width: 16px;
border-top-color: #ffffff;
background-color: #cccccc;
}
#headerdiv { display: table-cell; vertical-align: middle; margin: 0; height: 28px; }
HTML
<div id="wrapper">
<header>
<div id="headerdiv"><img src="logotest.gif" height="28" width="180" alt="test"></div>
</header>
</div>
Add
img {
vertical-align:middle;
}
jsFiddle example

Resources