How to keep divs from changing position - css

I am trying to create a sort of a window, formed by multiple divs. I'm basically putting div after div in my cshtml page, and trying to set their positions in the .css files.
After a considerable effort, I managed to set the divs in their beautiful positions. But as soon as I start to put text, or dom element or whatever inside one of those divs, it changes its position, and pushes other divs, creates a mess.
I somehow managed to keep some by using float, but it is really difficult. Isn't there an easy way to manage the inside elements of a div? Why does the inside elements cause the container div to travel to other places?
Here's the .html and .css code. The divs are empty and positioned correctly. You can simply write "qwe" inside div id="fiyat"> for example and see what I'm talking about.
.html code:
<div id="tablodetay">
<div id="secimler">
</div>
<div id="parcacerceve">
</div>
<div id="resim">
</div>
<div id="ebatsecimvefiyat">
<div id="ebatsecim">
</div>
<div id="fiyat">
</div>
</div>
<div id="urunozellik">
</div>
</div>
.css code:
div#tablodetay div#secimler
{
border:1px solid #000000;
margin:2px;
display:block;
width:706px;
height:100px;
display:block;
}
div#tablodetay div#parcacerceve
{
border:1px solid #000000;
margin:2px;
display:inline-block;
width:86px;
height:400px;
}
div#tablodetay div#resim
{
border:1px solid #000000;
margin:2px;
height:400px;
width:400px;
display:inline-block;
}
div#tablodetay div#ebatsecimvefiyat
{
margin:2px;
height:402px;
width:200px;
display:inline-block;
}
div#tablodetay div#ebatsecim
{
border:1px solid #000000;
height:248px;
width:200px;
margin-bottom:2px;
}
div#tablodetay div#ebatsecim form input
{
float:left;
border:1px solid #000000;
}
div#tablodetay div#fiyat
{
border:1px solid #000000;
height:148px;
width:200px;
}
div#tablodetay div#urunozellik
{
border:1px solid #000000;
height:120px;
width:706px;
margin-top:-2px;
margin-left:2px;
}

You need to define css attributes for each div. To prevent it from changing size or position you need
div ... {
position:absolute;
left: 50px;
top:50px;
width:100px;
height:50%;
}
position:absolute; ignores all other divs and puts your div on the spot you define.

width and height are overruled if the overflow isnt set to none or scroll
The browser will use your width and height as starting point, not end point.
You could also try to set max-width and max-height.

Related

vertical align middle not working

I want the text in front of the image to be in the middle of it!
My problem is the vertical align middle is not working...
what is wrong?
<div class="comments">
<div class="pull-left lh-fix">
<img class=foto src="/$foto" class="imgborder">
</div>
<div class="comment-text pull-left">
<span class="pull-left color strong">anna:</span> dododod
</div>
</div>
.pull-left { float: left; }
.lh-fix { line-height: 0 !important; }
.comments {
position:relative;
display:block;
overflow:auto;
padding-left:15px;
padding-top:8px;
padding-bottom:8px;
border:1px solid #000;
}
.comment-text {
margin-left: 8px;
color: #333;
vertical-align:middle; //not working?
line-height:normal;
width: 85%;
text-align:left;
}
.foto{
width:50px;
height:50px;
float:left;
}
https://jsfiddle.net/a0bhv4n1/
Vertical-align works on inline elements. You are applying it to the class .comment-text which is for a div element. A div is a block style element which of course means that it will take up the entire space that it is allowed to, thus you cannot center something that already takes up the whole space. Inline elements only take up the space they need to based on the content in them and you can simply add display:inline-block to .comment-text to allow vertical-align:middle to work. More information can be found at MDN's article on vertical-align

CSS drop down menu is not working as expected

when I make the div's(that contains the drop down menu)positioning to relative and the drop down menu div's positioning to absolute,it shows me only the last item on the drop down menu.if I set drop down menu container div to relative and leave the drop down menu div positioning,then it works.But that affects the rest of the page.So,how to set the positioning that would make the drop down works without affecting any other parts of the page.
HTML
<div id="top_head">
My Online Shop
<div id="nav">
<div class="test">Home</div>
<div class="test" id="product">Products
<div class="test1">shirt</div>
<div class="test1">Pant</div>
<div class="test1">inner</div>
<div class="test1">cap</div>
</div>
<div class="test">About</div>
<div class="test">contact Us</div>
</div>
</div>
CSS
body{
color:green;
}
#top_head{
width:100%;
height:100px;
font:48px Arial green;
border:1px dotted red;
}
#nav{
background-color:gray;
width:57%;
border-radius:5px;
font:28px Arial orange;
margin:0px -49px 5px 15px;
}
#nav a{
color:red;
text-decoration:none;
margin:0px 50px;
}
.test{
float:left;
}
.test:hover{
background-color:orange;
}
#product{
position:relative;
}
.test1{
position:absolute;
border:1px solid red;
visibility:hidden;
}
#product:hover .test1{
visibility:visible;
background-color:yellow;
}
I've tried with display property too. Same results.
If you have any idea where the problem lies, please help.
It looks like the issue here is that you're using position:absolute on all of the sub menu divs. This is essentially making them lay on top of each other (leaving the last one on top).
One solution for this is to wrap all of these elements in a container div and make that the thing that is hidden or shown:
Working Fiddle Demo
Your sub-menu becomes:
<div class="test1">
<div>shirt</div>
<div>Pant</div>
<div>inner</div>
<div>cap</div>
</div>
And the CSS is altered slightly:
.test1{
display:none;
}
.test1 div{
border:1px solid red;
}
#product:hover .test1{
position:absolute;
display: block;
background-color:yellow;
}

CSS float issues across several browsers

I have the following CSS setup for use on two different pages;
#content{
width:960px;
margin-top:0px;
height:auto;
font-family:arial;
font-size:1.em;
background-color:#f2f2f2;
}
#left-div {
width:600px;
padding-top:20px;
text-align:center;
line-height:.5em;
display:inline-block;
float:left;
}
#right-div {
width:300px;
margin-top:40px;
margin-right:20px;
display:inline-block;
text-align:center;
float:right;
background-color:#e0e0e0;
}
#isa-left {
width:440px;
margin-top:40px;
margin-left:30px;
margin-right:10px;
display:inline-block;
text-align:justify;
float:left;
}
#isa-right {
width:440px;
margin-top:40px;
margin-left:10px;
margin-right:30px;
display:inline-block;
text-align:center;
float:right;
}
On the page where I use left-div and right div like this;
<div id="content">
<div id="left-div"> Content </div>
<div id="right-div"> Content </div>
</div>
here is what happens. In FF, IE, Safari, and Chrome it looks just I expect with the two divs next to each other with a background color of #f2f2f2 from the content div.
On the second page where I use the isa-left and isa-right with the same setup as above what happens is that the inner divs are still showing where I expect them but now the background color from the content div is not showing.
After finding a post on here with the same problem I added this line overflow:auto; to the content div.
Now both pages in FF the content appears outside of the content div, 960 pixels to the right, with the background color showing. In IE, Safari, and Chrome both pages appear perfectly.
My question is what is causing the two inner divs to escape the content div in FF once I added overflow:auto;? Or is there a way to fix it so that the background color shows through on the second page without using overflow:auto;?
Any suggestion is appreciated.
Try this. I think it might be the solution to your problem.
http://jsfiddle.net/6dBdx/
-Code Reference -
CSS:
.wrapper {
width:400px;
margin-top:0px;
height:auto;
font-family:arial;
font-size:1.em;
background-color:#f2f2f2;
margin-bottom:15px;
}
.wrapper > div.box {
padding-top:20px;
text-align:center;
line-height:.5em;
border:thin solid #999;
/* Adding this for example purposes */
height:150px;
width:150px;
}
.pull-right {
float:right;
}
.pull-left {
float:left;
}
.clear-fix {
clear:both;
}
HTML
<label>Float Left Only</label>
<div class="wrapper">
<div class="pull-left box">One</div>
<div class="pull-left box">Two</div>
<div class="clear-fix"></div>
</div>
<label>Float Left & Right</label>
<div class="wrapper">
<div class="pull-left box">One</div>
<div class="pull-right box">Two</div>
<div class="clear-fix"></div>
</div>
Quick notes, don't forget to add a clear div after a float, so that elements show up correctly after floating an element. Also, if you want an element to line up next to each other, try using float:left as a rule of thumb, unless you want the elements to line up on the right in which case... float:right

Make Child Div Float Right of Parent

I'd like to make an inner div float right of the parent, regardless of the width of the parent. An example of what this looks like is here: http://jsfiddle.net/eqDyy/. That fiddle is just an example of what it should look like. It's done with the exact number of pixels given, but what I'm looking for is a solution that still lets that inner div appear to the right of its parent, even when the parent width changes.
The following is the code used in the fiddle:
HTML
<div id="one">
<div id="two"></div>
</div>
CSS
#one {
border:2px solid #ddd;
width:150px;
height:30px;
}
#two {
border:2px solid #ddd;
width:150px;
height:30px;
margin-left:150px;
margin-top:-2px;
}
Again, please remember that this is just to show what I'd like it to look like, but it isn't a satisfying implementation because it uses specific pixel amounts.
Since percentage-based margins are calculated against the containing block's width, you can set margin-left to 100%.
Example
#one {
border:2px solid #ddd;
width:150px;
height:30px;
}
#two {
border:2px solid #ddd;
width:50px;
height:30px;
margin-left:150px;
margin-top:-2px;
float:right;
}
I changed #two a little to show you the result.
http://jsfiddle.net/eqDyy/3/

Div's not filling container

I have a problem with css.
Let's assume the following structure
<div id='maincontainer'>
<div id='item'>
<div id='itemimage'> </div>
<div id='itemtext'> </div>
</div>
</div>
Now. For every item in the database it prints an item, with the corresponding image and text. Lets say i have 4 items in my database. It prints the first one fine, and the second, but the third and fourth get printed out of the container.
My maincontainer has a minimum height, which extends with the height of the itemdiv. But after two items, it stops extending.
Do you have any idea's.
#maincontainer {
font-size:11px;
font-family:Verdana;
margin-top:60px;
margin-left:auto;
margin-right:auto;
min-height:50px;
height:auto!important;
overflow:hidden;
width:650px;
position:relative;
color:#465258;
text-align:left;
border:1px solid black;
}
#item
{
height:170px;
width:650px;
position:relative;
clear:both;
}
#itemimage
{
float:left;
height:135px;
width:200px;
position:relative;
clear:both;
}
#item_text
{
color:#465258;
width:435px;
margin-top:-135px;
height:110px;
float:right;
position:relative;
padding-top:10px;
padding-bottom:10px;
clear:both;
}
Why do you need a height On the div? It should just stretch vertically to fit its contents. Unless you are using floats on the items and not containing them with float clearing methods.

Resources