Child-element in inline-block div changes the parents siblings position - html

I have a couple of divs with some content (another div inside). I want the parent divs to be positioned in a nice line and I want the child div to be relative to its parent.
I've made a JSFiddle To illustrate. Right now the divs are all nicely placed, but if you remove the '.btn' div in the last one, everything gets messed up.
To see what I mean, change the HTML in the JSFiddle to:
<div id="expand1" class="expand">
<div id="btn1" class="btn">>></div>
</div>
<div id="expand2" class="expand">
<div id="btn2" class="btn">>></div>
</div>
<div id="expand3" class="expand"></div>
What's going on here? How can I get the desired result?

You should use floats for a better layout:
.expand{
border:1px solid black;
width:400px;
height:400px;
display:inline-block;
float: left;
}
.btn{
cursor:pointer;
position:relative;
top:150px;
left: 150px;
border: 1px solid black;
width:40px;
height:40px;
vertical-align:middle;
text-align:center;
line-height:40px;
}
<div id="expand1" class="expand">
<div id="btn1" class="btn">>></div>
</div>
<div id="expand2" class="expand">
<div id="btn2" class="btn">>></div>
</div>
<div id="expand3" class="expand"></div>

.expand {
border: 1px solid black;
width: 400px;
height: 400px;
display: inline-block;
vertical-align: top;
}
.btn{
margin: -20px 0 0 -20px;
cursor: pointer;
position: relative;
top: 50%;
left: 50%;
border: 1px solid black;
width: 40px;
height: 40px;
vertical-align: middle;
text-align: center;
line-height: 40px;
}
<div id="expand1" class="expand">
<div id="btn1" class="btn">>></div>
</div>
<div id="expand2" class="expand">
<div id="btn2" class="btn">>></div>
</div>
<div id="expand3" class="expand"></div>

You can simply play with the position property to get the desired result.
just add position:relative to your .expand class
.expand {
position:relative;
border: 1px solid black;
width: 400px;
height: 400px;
display: inline-block;
}
and update the position to absolute to your .btn class
.btn {
cursor: pointer;
position: absolute;
top: 150px;
left: 150px;
border: 1px solid black;
width: 40px;
height: 40px;
vertical-align: middle;
text-align: center;
line-height: 40px;
}

Related

How can I position two div elements side by side inside another one?

I would like div#alpha1 and div#alpha2 inside the div#alpha placed side by side.
CODE
#alpha {
position: relative;
padding-top: 4px;
margin-top: 8px;
margin-left: 2%;
margin-right: 2%;
width: 96%;
height: 100px;
border-top: 1px solid black;
border-bottom: 1px solid black;
}
}
#alpha1 {
position: relative;
width: 94px;
height: 94px;
border: 1px solid black;
margin-left: 2%;
}
#alpha2 {
position: relative;
margin-top: 0px;
height: 40px;
border-top: 1px;
border-bottom: 1px solid black;
margin-left: 94px;
}
<DIV id="alpha">
<DIV id="alpha1">
<IMG src="img/jenny.jpg" width="94px" height="94px">
</DIV>
<DIV id="alpha2">
<H1 id="patientname">Jenny Thomas</H1>
</DIV>
</DIV>
you can use flexbox for that by using display:flex in parent and then flex:1 in #alpha2 to make it grow according to screen size
Don't use HTML width/height tags, instead use CSS for styling it.
Note I did a few tweaks to your code.
#alpha {
padding-top: 4px;
margin: 8px 2% 0;
width: 96%;
height: 100px;
border: solid black;
border-width: 1px 0;
display: flex
}
#alpha1 {
width: 94px;
height: 94px;
border: 1px solid black;
margin: 0 2%;
}
#alpha2 {
flex: 1
}
#alpha2 h1 {
border-bottom: 1px solid black;
height: 40px
}
<div id="alpha">
<div id="alpha1">
<img src="//lorempixel.com/94/94" />
</div>
<div id="alpha2">
<h1 id="patientname">Jenny Thomas</h1>
</div>
</div>
The easiest/fastest solution is to assign display: flex to the container #alpha
http://codepen.io/anon/pen/mPgaJP
(I also erased some unneccesary settings in there)
You just needed to set the float property of your div. Here you are :-
#alpha{
position:relative;
padding-top:4px;
margin-top:8px;
margin-left:2%;
margin-right:2%;
width:96%;
height:100px;
border-top:1px solid black;
border-bottom:1px solid black;
float: none;
}
#alpha1{
position:relative;
width:94px;
height:94px;
border:1px solid black;
margin-left:2%;
margin-right: 0px;
float: left;
}
#alpha2{
position:relative;
margin-top:0px;
height:40px;
border-top:1px;
border-bottom:1px solid black;
margin-left:9%;
float: next;
}
<DIV id="alpha">
<DIV id="alpha1">
<IMG src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTvU-f_zys67Kv6hdqJcmSN5n_dfe2igiq9lLZYpcXAyVXEBNQ6" width="94" height="94" alt="IMAGE">
</DIV>
<DIV id="alpha2">
<H1 id="patientname">Jenny Thomas</H1>
</DIV>
</DIV>
I edited your margin in alpha2 for correct display of bottom line. It is displayed correct in browser. Here it is not. You can check it here. Mark the problem solved if it helps.

Numbers centered in a circle

.circle{
width: 20px;
height: 20px;
border: 1px solid #2d2d2d;
color: #2d2d2d;
border-radius: 50%;
cursor: pointer;
display: inline-block;
padding: 10px;
margin: 0 3px;
vertical-align: middle;}
.choice .circle{
position: relative;
top: 50%;
transform: translateY(-50%);
text-align: center;
}
<div class="choice">
<div class="circle" data-attr-val="2000" data-attr-price="59">2000</div>
<div class="circle" data-attr-val="20" data-attr-price="59">20</div>
</div>
In the example you can see my problem, 4 or more numbers looks bad, 1-3 numbers looks good.
How can I center 4 or more numbers?
Thanks for help :)
Have a look at this
HTML
<div class="choice">
<div class="circle" data-attr-val="2000" data-attr-price="59">2000</div>
<div class="circle" data-attr-val="20" data-attr-price="59">20</div>
CSS
.circle {
width:50px;
height:50px;
border:1px solid black;
border-radius:250px;
line-height:50px;
text-align:center;
display:inline-block;
}
Fiddle here
Remove the left and right padding, and increase the width instead by an equal amount – then you can get four digits centered in there via text-align. (More digits might still be problematic though.)
.circle{
width: 40px;
height: 20px;
border: 1px solid #2d2d2d;
color: #2d2d2d;
border-radius: 50%;
cursor: pointer;
display: inline-block;
padding: 10px 0;
margin: 0 3px;
vertical-align: middle;
text-align:center;
}
.choice .circle{
position: relative;
top: 50%;
transform: translateY(-50%);
text-align: center;
}
<div class="choice">
<div class="circle" data-attr-val="2000" data-attr-price="59">2000</div>
<div class="circle" data-attr-val="20" data-attr-price="59">20</div>
</div>
Here is a version using display: inline-table and a pseudo element.
These circle:before rule's properties will keep the numbers centered what ever size you set for the circle (though too small will look odd), even with two lines of numbers.
vertical-align: middle;
text-align: center;
I also used css attr() to set the numbers from your "data-attr-val" instead of adding them manually.
.circle {
display: inline-table;
width: 40px;
height: 40px;
border: 1px solid #2d2d2d;
color: #2d2d2d;
border-radius: 50%;
cursor: pointer;
}
.circle:before {
content: attr(data-attr-val);
display: table-cell;
vertical-align: middle;
text-align: center;
}
.circle.big {
width: 80px;
height: 80px;
}
.circle.big:before {
content: attr(data-attr-val);
}
<div class="choice">
<div class="circle" data-attr-val="2000" data-attr-price="59"></div>
<div class="circle" data-attr-val="20" data-attr-price="59"></div>
</div>
<br />2 lines, still centered<br /><br />
<div class="circle big" data-attr-val="10000 20000" data-attr-price="59"></div>

Vertically Align Div with Absolute Position

I am trying to vertically align a div (the "p" element in this example but it could be anything a div a link, an image, etc.) which needs to have its position property set to absolute. I followed this example: http://davidwalsh.name/table-cell-position-absolute
But I can get it to work. Here is the code:
<div style="width: 400px; height: 48px; background-color: #EEE; margin: 0 auto;">
<div style="display: table; position: relative; border: 1px solid blue; width: 100%; height: 100%;">
<div style="display: table-cell; border: 2px solid green;">
<div style="position:relative; overflow: auto; height: 100%;">
<p style="position:absolute; bottom:0; right:0; font-size: 18px; color: black; border: 1px solid orange;">VERTICALLY CENTER ME PLEASE!</p>
</div>
</div>
</div>
</div>
And the jsfiddle: http://jsfiddle.net/bqm7wudc/2/
Could someone suggest a solution please?
I actually sorted the problem that way:
<div style="display: table; position: relative; border: 1px solid red; width: 960; height: 48px; margin: 0 auto;">
<ul style="display: table; vertical-align: middle; border: 1px solid orange; padding: 0px; position:absolute; left: 700px; margin: 0; height: 100%;">
<li style="display: table-cell; vertical-align: middle; font-size: 20px; font-weight: 900; padding-right: 20px;">toto</li>
<li style="display: table-cell; vertical-align: middle; font-size: 20px; font-weight: 900; padding-right: 20px;">titi</li>
</ul>
</div>
The text is defined as the content of a list. The ul element is itself a table (positioned using the absolute mode), while the li elements are table cells. This seems to work.
Updated fiddle: http://jsfiddle.net/bqm7wudc/7/

Add vertical line between the vertical divs

I want to add a vertical line between the multiple divs so that it looks like the attached image:
I'm trying to achieve that by adding a div .border and setting its position absolute. However I want to add some margin between the border and make the border appear behind the boxes as in above image.
Here's the code I'm trying:
HTML:
<div class="wrap">
<div class="box">
<div class="border"></div>
<div class="figure"></div>
<div class="right"> right</div>
</div>
<div class="box">
<div class="border"></div>
<div class="figure"></div>
<div class="right"> right</div>
</div>
<div class="box">
<div class="border"></div>
<div class="figure"></div>
<div class="right"> right</div>
</div>
</div>
CSS:
.wrap{
position: relative;
overflow: hidden;
}
.box{
overflow: hidden;
margin-top: 50px;
}
.box:first-child{
margin-top: 0;
}
.figure{
width: 50px;
height: 50px;
background: yellow;
display: inline-block;
margin-right: 10px;
}
.right{
display: inline-block;
}
.border{
border-right: 3px solid red;
height: 100%;
left: 24px;
position: absolute;
top: 0;
width: 1px;
}
.box:last-child .border{
display: none;
}
JSFiddle:
http://jsfiddle.net/w5TY9/
Here you go.
WORKING DEMO
The HTML:
<div class="wrap">
<div class="box">
<div class="border"></div>
<div class="figure"></div>
<div class="right"> </div>
</div>
<div class="box">
<div class="border"></div>
<div class="figure"></div>
<div class="right"> </div>
</div>
<div class="box">
<div class="border"></div>
<div class="figure"></div>
<div class="right"> </div>
</div>
</div>
The CSS:
.wrap{
position: relative;
overflow: hidden;
}
.box{
overflow: hidden;
margin-top: 50px;
}
.box:first-child{
margin-top: 0;
}
.figure{
width: 50px;
height: 50px;
background: yellow;
display: inline-block;
margin-right: 10px;
}
.right{
display: inline-block;
}
.border {
border-right: 3px solid #FF0000;
height: 98%;
left: 24px;
position: absolute;
top: 0;
width: 1px;
z-index: -1;
}
.box:last-child .border{
display: none;
}
.figure {
background: none repeat scroll 0 0 #FFFF00;
border-bottom: 12px solid #FFFFFF;
border-top: 12px solid #FFFFFF;
display: inline-block;
height: 50px;
margin-right: 10px;
width: 50px;
}
The CSS Changes:
.border {
border-right: 3px solid #FF0000;
height: 98%;
left: 24px;
position: absolute;
top: 0;
width: 1px;
z-index: -1;
}
.figure {
background: none repeat scroll 0 0 #FFFF00;
border-bottom: 12px solid #FFFFFF;
border-top: 12px solid #FFFFFF;
display: inline-block;
height: 50px;
margin-right: 10px;
width: 50px;
}
Hope this helps.
.border{z-index: -1;} use this
And see link http://jsfiddle.net/bipin_kumar/w5TY9/2/
.figure{
width: 50px;
height: 50px;
background: yellow;
display: inline-block;
margin-right: 10px;
z-index:1;
border:3px solid white;
}
.border{
border-right: 3px solid red;
height: 100%;
left: 24px;
position: absolute;
top: 0;
width: 1px;
z-index:-1;
}
replace your classes with mine, you will get both effects
.border{
border-right: 3px solid red;
height: 100%;
left: 24px;
position: absolute;
top: 0;
width: 1px;
}
Class need to be added the following property and value
z-index: -1;
In your css you need to add the following two rules for the .border class:
z-index: -1;
margin-left: -1px
The first line puts the line behind the boxes. So in the vertical space without boxes the line shows up.
One small improvement for centering the border perfectly under the boxes:
Your border is 3px width so the border should be moved at least 1px to the left in order to stay centered. With margin-left: -1px you get the correct result. If you want the border to be completely perfect centered you should either use a border with of 4px and a margin-left of -1px or a border with of 2px and a margin-left of 1px;
see http://jsfiddle.net/w5TY9/1/
Add z-index=-1 to border class.
check this fiddle
What you want is very easy. The short version is like this:
<div style="background-color:yellow; height:30px;width:30px;"> </div>
<div style="background-color:red; height:30px; width:5px; margin-left:10px;"> </div>
In this way you have a square with background yellow and below that you have a red line
with 5px width or whatever you want.

2 Divs next to eachother

I'm trying to put a logo and a sidebar next to eachother, but it just won't work. The logo container needs to be centered at the top. And the sidebar needs the be at the top-left Can you help me? I already tried float, no succes. :(
code:
<body>
<center>
<div id="logo1">
<div id="logo2"></div>
</div>
</center>
<div id="sidebar1">
<a href="https://test.com/" target="blank">
<div id="test1"></div>
</a>
</div>
</body>
CSS:
#test1 {
display: inline-block;
position: absolute;
border: 1px solid;
margin-top: 15px;
margin-left: 22px;
background-image:url('Afbeeldingen/2.png');
height: 45px;
width: 45px;
}
#test1:hover {
display: inline-block;
position: absolute;
border: 1px solid;
margin-top: 15px;
margin-left: 22px;
background-image:url('Afbeeldingen/1.png');
height: 45px;
width: 45px;
}
#sidebar1 {
display: inline-block;
position: relative;
border: 1px solid;
margin-top: -10px;
margin-left: -15px;
background-image:url('Afbeeldingen/lol.png');
height: 1080px;
width: 118px;
}
#logo1 {
display: inline-block;
position: relative;
border: 1px solid;
margin-top: 10px;
height: 100px;
width: 700px;
}
Ok, This is what you have to do :
You need to remove the display:inline-block from #logo1
And instead of just writing margin-top:10px , you need to use margin:0px auto, or you could write margin:10px auto. By this, it will center your #logo1 div.
But to center a "div" , you need to have another container(div) that wrap within your div. So that it will know, from which side to which side that it will have to be "centered".
For that reason, you will need to create another div or container around your #logo1 div, and lets assume it is called "right" (see the code below).
And for this div/container to be just beside your sidebar, it will need to have a relative position same as your sidebar. Now, you can just float both of your #sidebar1 and also your #logo1 to the left.
Thus, you dont have to use that negative margin for your sidebar anymore (remove that). If you wanted to use the negative margin, you have to use the absolute position in this case. But you will then have to restructure your whole #logo1 div which will create a lot of works.
This is the full code for your reference :
HTML code :
<div id="container">
<div id="sidebar1">
<a href="https://test.com/" target="blank">
<div id="test1">This is sidebar</div>
</a>
</div>
<div id="right">
<div id="logo1">
<div id="logo2"><This is logo</div>
</div>
</div>
</div>
And use this CSS :
#container{
width:1000px;
height:1080px;
position:absolute;
border:1px solid #000;
}
#test1 {
display: inline-block;
position: relative;
border: 1px solid;
margin-top: 15px;
margin-left: 22px;
background-image:url('Afbeeldingen/2.png');
height: 45px;
width: 45px;
}
#test1:hover {
display: inline-block;
position: relative;
border: 1px solid;
margin-top: 15px;
margin-left: 22px;
background-image:url('Afbeeldingen/1.png');
height: 45px;
width: 45px;
}
#sidebar1 {
display: inline-block;
position:relative;
float:left;
border: 1px solid;
background-image:url('Afbeeldingen/lol.png');
height: 1080px;
width: 118px;
border:1px solid red;
}
#right{
position:relative;
float:left;
margin-top:0px;
width:870px;
height:100px;
}
#logo1 {
position:relative;
border: 1px solid;
margin: 0px auto;
height: 100px;
width: 700px;
}
Do you want this ?
#test1 {
border: 1px solid;
margin-top: 15px;
margin-left: 22px;
background-image:url('Afbeeldingen/2.png');
height: 45px;
width: 45px;
}
#test1:hover {
background-image:url('Afbeeldingen/1.png');
}
#sidebar1 {
position:absolute;
border: 1px solid;
background-image:url('Afbeeldingen/lol.png');
height: 1080px;
width: 118px;
}
#logo1 {
border: 1px solid;
margin-top: 10px;
height: 100px;
width: 700px;
}
<div id="sidebar1">
<a href="https://test.com/" target="blank">
<div id="test1"></div>
</a>
</div>
<div id="logo1">
<div id="logo2"></div>
</div>
I assume this is what you want? http://jsfiddle.net/Le6PH/
You should do:
Remove the negative margins (If you don't know what you are doing, don't use negative margins)
Remove the <center> tag (This tag is deprecated since EVER)
Remove the margin of your logo
Add a wrapper div around your whole structure
Add the following CSS to that div
CSS
.wrapper{
position:relative;
width:818px; /* sidebar width + logo width */
}
Change position:relative; to position:absolute for your logo & sidebar divs.
Add top:0; for both divs
Add right:0; for the sidebar div
EDIT:
With a centered logo, like this (http://jsfiddle.net/Le6PH/1/) you'll need to change 2 things:
Add a margin-left:118px; to the logo div
Change the width of the wrapper to width of logo + margin logo + width of sidebar.
Try floating your div, it should look like this..
<div class="row">
<div id="log"></div>
</div>
<div class="row">
<div id="sidebar"></div>
</div>
css
.row{
float: left;
width: 50%;
}