I can't centre my div - html

I don't now what I'm doing wrong. I want the 'artikelen' div to be centered when it's opened. I've tried all kind of combinations but can't figure out what is going wrong.
this is the HTML
<nav class="cl-effect-1"><center>
<span data-hover="Artikelen"><font size="1px">Artikelen</font></span>
<span data-hover="Drukwerk"><font size="1px">Drukwerk</font></span>
<span data-hover="Jaarverslagen"><font size="1px">Jaarverslagen</font></span>
<span data-hover="Mailings"><font size="1px">Mailings</font></span>
<span data-hover="Onderwijs"><font size="1px">Onderwijs</font></span>
<span data-hover="Webteksten"><font size="1px">Webteksten</font></span>
</center></nav>
<div id="artikelen">
<div class="inhoud">
Artikelen
</div>
</div>
this is the CSS
#artikelen {
position:absolute;
display:none;
width:auto;
height:auto;
}
#artikelen:target {
display:block;
margin-left: auto;
margin-right: auto;
}
.inhoud {
background-color:black;
color: white;
width:200px;
height:150px;
margin-left: auto;
margin-right: auto;
}

See here: http://jsfiddle.net/h8GL3/
I removed the
poistion:absolute
property from #artikelen, and now the margin left and right works.

Related

Is there something wrong with my float:left in CSS

I'm trying to design a flag for a website i'm doing and the design is the parts at the bottom of the flag, they stack when i don't float them (expected) but when i do float them, they shrink to the text inside of the box, How can i fix this? Here's my code:
#flag1{
max-width:400px;
max-height:1000px;
margin:0 auto;
}
#mainflag{
background-color:#BA0500;
max-width:400px;
max-height:1000px;
position:relative;
height:auto;
margin: 0 auto;
text-align:center;
padding:5px;
color:white;
}
#flagdesign1{
background-color:#BA0500;
max-width:100px;
position:relative;
max-height:50px;
color:#BA0500;
margin-top:-21px;
margin-right:20px;
}
<div id="flag1">
<div id="mainflag">
<h1>Flag</h1>
</div>
<div id="flagdesign1">
<h1>A</h1>
</div>
<div id="flagdesign1">
<h1>A</h1>
</div>
</div>
In the end, it should look something like these
If I understand your question well, you could try using display: table;.
Also, an ID is supposed to be unique, you should use classes instead.
I also used span instead of h1 for the flag element, as IE might not permit using display: inline-block;, depending on the version you're supporting.
#flag1{
max-width:400px;
max-height:1000px;
margin:0 auto;
}
#mainflag{
background-color:#BA0500;
max-width:400px;
max-height:1000px;
position:relative;
height:auto;
margin: 0 auto;
text-align:center;
padding:5px;
color:white;
}
.bottomFlag {
display: table;
width: 100%;
}
.bottomFlagElem {
display: table-cell;
}
.bottomFlagElem.center {
text-align: center;
}
.bottomFlagElem.left {
text-align: left;
}
.bottomFlagElem.right {
text-align: right;
}
.bottomFlagElem span {
display: inline-block;
width: 100%;
background-color:#BA0500;
max-width:100px;
position:relative;
min-height:30px;
max-height:50px;
color:#BA0500;
margin: 0;
}
<div id="flag1">
<div id="mainflag">
<h1>Flag</h1>
</div>
<div class="bottomFlag">
<div class="bottomFlagElem left">
<span>A</span>
</div>
<div class="bottomFlagElem center">
<span>A</span>
</div>
<div class="bottomFlagElem right">
<span>A</span>
</div>
</div>
</div>

Centering title when next to toolbar

I'm trying to make a title bar with a nice centered title and a toolbar next to it. The issue I'm having is that as the toolbar grows, the title move further and further off center (and is never really centered to begin with). I've been monkeying with this for a while, tried a few searches, but can't seem to find an answer. Can someone with a bit more css experience throw me a bone please?
HTML
<div>
<span>
Section Title
</span>
<div class="toolbar">
<button>Add</button>
<button>Remove</button>
</div>
CSS
div { background:red;overflow:hidden; text-align: center; }
span a {
background:#222;
color:#fff;
display:inline-block;
margin:10px 10px 0 0;
padding:5px 10px
}
.toolbar {
float: right;
}
Here's a fiddle:
http://jsfiddle.net/scottvossen/cePe3/124/
You're going to want to use position: relative on the outer div and position: absolute on the inner div. You can learn more about positioning divs here.
FIDDLE. I also centered the text vertically.
HTML
<div id="background">
<div id="centeroutline">
<div id="centertext">
Section Title
</div>
</div>
<div class="toolbar">
<div id="buttons">
<button>Add</button>
<button>Remove</button>
</div>
</div>
</div>
CSS
#background {
position:relative;
background:red;
overflow:hidden;
text-align: center;
height:26px;
width:auto;
}
#buttons{
position:relative;
}
#centeroutline {
color:#fff;
display: table;
width:100%;
}
#centertext{
margin:10px 10px 0 0;
padding:5px 10px;
display: table-cell;
vertical-align: middle;
width:100%;
text-align:center;
}
.toolbar {
right:0;
top:0;
position:absolute;
}
You can add position: relative to the containing div. Then absolute position the toolbar in the corner.
div{
position: relative
}
.toolbar{
position: absolute;
top: 0;
right: 0;
}
FIDDLE

How to set the text absolutely (CSS) in the middle of a div despite a changing width div behind him?

The image shows what I want to accomplish:
Here is the code that I have:
http://jsfiddle.net/ZNtKj/202/
<div style="width: 200px" >
<div class="niveles-porcentaje">
<div class="alta" style="width: 40%"> <span class="porcentaje">40%</span></div>
</div>
And the style I am having trouble to fix:
div.niveles-porcentaje {
width:100%;
height:100%;
align-self:center;
text-align:center;
display:inline-table;
background-color:#D7D7D7;
}
div.alta {
display:inline-table; /*inside a table*/
line-height: 2em;
background-color: #06AC09;
height:100%;
float:left;
}
span.porcentaje{
text-align:center;
vertical-align:middle;
z-index:99;
}
The div will be inside a td.
You will have to make the text's parent full width. To do that, remove the back color and width definition from the .alta div, and create an inner absolute div to deal with the color fill, that won't interfeer with the text.
Also, remember to set the text span to display: block to be full width. Check here: http://jsfiddle.net/ZNtKj/209/
<div style="width: 200px" >
<div class="niveles-porcentaje">
<div class="alta">
<div class="fill" style="width: 40%"></div>
<span class="porcentaje">40%</span>
</div>
</div>
</div>
CSS:
div.niveles-porcentaje {
width:100%;
height:100%;
align-self:center;
text-align:center;
display:inline-table;
background-color:#D7D7D7;
}
div.alta {
display:inline-table; /*inside a table*/
line-height: 2em;
height:100%;
width: 100%;
position: relative;
}
div.alta .fill {
background-color: #06AC09;
height:100%;
position: absolute;
}
span.porcentaje{
text-align:center;
vertical-align:middle;
z-index:99;
position: relative;
display: block;
}
Here is the Fiddle with example. You can simply change the text div width to see the result.
div.niveles-porcentaje {
width:100%;
height:20px;
align-self:center;
text-align:center;
background-color:#D7D7D7;
position:relative;
}
div.alta {
line-height: 2em;
background-color: #06AC09;
height:20px;
float:left;
position:relative;
}
span.porcentaje{
position:absolute;
left:46%;
top:0;
}

Center text to parent element

I am trying to center the text to a parent element. Here is my code.
HTML
<div class="header"><!--header-->
<span class="child">
<h1 class="h1temp">
<div class="left">Hello</div>
<div class="left down">There</div>
<div class="left downer">James</div>
</h1>
</span>
</div><!--header-->
CSS
.header {
height: 200px;
background-color:#C0C0C0;
position:relative;
}
.child {
position:absolute;
margin:50% auto;
width:900px;
}
.h1temp {
margin:0 auto;
font-size: 20px;
}
The problem is that the text aligns with the browser, not the parent element. Here is what it currently looks like jsFiddle.
I assume you want to align h1temp.
margin: x auto only works if element has width`
.h1temp {
margin:0 auto;
font-size: 20px;
width: 500px;
}
Just write display:table; to .h1temp

How to have centered content with a left menu using css only?

I have two divs:
<div id="left_menu" > menu </div>
<div id="content" > centered </div>
Currently they have a css of
#content {
margin-left:auto;
margin-right:auto;
display:table;
}
So this would create a div with menu and a line below that a centered div with centered. What I want is a centered div#content with div#left_menu to the left of it. I DON'T want to center BOTH the divs together, only the div#content. This should be done with only divs and css and should work on all browsers.
So this could possibly look like
---> menu centered <--------
Just to clarify things:
I'm not centering/positioning the text, it's the divs that matter (text is there for marking the position in the example). I want both divs on the same line (like a span, but i want to use divs), the centered div should be centered in the middle of the page. The menu div should be right next to it, touching the left border of the centered div.
This solution should work for every screen size (e.g. if the screen is very large the two side gaps to the left and right of the menu and content should be very large, e.g. if the screen is too small for both the menu and content, there should be no gaps and the result should look like (the >< represent the cutoff) Notice how if the screen is too small, the menu div is fully displayed first with the centered div cutoff (as if it were just two divs floated left).
>menu cent<
Due to the number of incorrect answers being submitted:
1) Please verify your answers by creating your own .html file with your code
2) Refresh once on full screen and refresh once with browser resized to a smaller size such that the browser cannot hold both divs (e.g. the centered div is semi-cutoff)
3) Use inspect element tool(chrome) or equivalent tools to be sure that the two divs are touching, the centered div is indeed centered, etc
To further clarify what i want i've included a better example(NOT a solution though):
This does not work for every screen size:
http://jsfiddle.net/prt38/2/
Updated per requests in comments.
I really like using the vertical-align property when vertically-aligning elements.
HTML:
<div id="container">
<span id="alignment"></span><div id="wrapper">
<div id="sidebar">
</div><div id="main">
</div>
</div>
</div>
Notice how the closing and the succeeding are touching. For inline and inline-block elements to touch, there cannot be space between them in the markup.
CSS:
html, body {
height: 100%;
margin: 0;
padding: 0;
text-align: center; }
#container { white-space: nowrap; }
#wrapper {
white-space: nowrap;
text-align: left;
margin: 0 75px 0 0;
vertical-align: middle;
display: inline-block; }
#alignment {
height: 100%;
vertical-align: middle;
display: inline-block; }
#sidebar {
background: red;
width: 75px;
height: 200px;
vertical-align: middle;
display: inline-block; }
#main {
background: blue;
width: 300px;
height: 400px;
vertical-align: middle;
display: inline-block; }
Preview: http://jsfiddle.net/Wexcode/2Xrcm/8/
Your do it with simple overflow:hidden like this:
#left_menu{
float:left;
width:200px;
height:100px;
background:green;
}
#content {
margin-left:auto;
margin-right:auto;
display:table;
height:100px;
background:red;
overflow:hidden;
}
http://jsfiddle.net/hnXqg/
The solution for this is you have to create a wrapper class or id for a div like..
<div id="wrapper">
<div id="left_menu" > menu </div>
<div id="right">
<div id="content" > centered </div>
</div>
</div>
then the css is..
#wrapper{
margin:0px auto;
display:table;
width:90%;
}
#menu{
float:left;
width:300px;
margin:5px;
}
#right{
float:right;
display:block;
}
#content{
displat:table;
margin:0px auto;
}
I think this css should do the job, if I understood your question:
#left_menu{background:red;
width:100px;
height:100px;
float:left;
margin: auto 0;
z-index:2}
#content {
background:white;
width:100px;
height:100px;
margin: auto 0;
float:left;
position:absolute;
left:20%;
z-index:200;
padding-left:4%
}
And Html is below:
<div id="left_menu" >RED DIV</div>
<div id="content" >WHITE DIV</div>
I think this is what you are looking for. Adjust the sizes to suit your needs, obviously.
<style type="text/css">
.container {
margin: auto;
width: 500px;
}
.menu {
margin: 10px;
width: 180px;
}
.content {
margin: 10px;
width: 280px;
text-align: center;
}
.floatLeft {
float: left;
}
.clear {
clear: both;
}
</style>
<div class="container">
<div class="menu floatLeft">
Menu
</div>
<div class="content floatLeft">
Content
</div>
<div class="clear"></div>
</div>
Edited:
<style type="text/css">
.container {
margin: auto;
width: 500px;;
background: red;
}
.menu {
width: 50px;
margin-left: 50px;
background: green;
}
.content {
width: 300px;
margin: auto;
background: blue;
}
.floatLeft {
float: left;
}
.clear {
clear: both;
}
</style>
<div class="container">
<div class="menu floatLeft">
Menu
</div>
<div class="content">
Content
</div>
<div class="clear"></div>
</div>
<div align="center">
<span id="left_menu"> menu </span>
<span id="content"> centered </span>
</div>
html { text-align: center; }
div#content { display: inline; margin: 0 auto; text-align: left;width: 980px; }
something like this should work.