Why did all div appear under each other? [closed] - html

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am not asking this because it's a problem for me, actually, that's exactly how I wanted to display the divs, but I didn't know they would appear below each other. Why is that? I only gave them width and height, I didn't position them.
I thought they would appear on each other at the same position
<div class="D1">
</div>
<div class="D2">
</div>
<div class="D3">
</div>
<div class="D4">
</div>
.D1,.D2,.D3,.D4{
border:1px solid;
border-color:red;
width:500px;
height:200px;
}
/* OR
div{
border:1px solid;
border-color:red;
width:500px;
height:200px;
}
*/
Sorry for this probably dumb question, but I'm just curious :D

That's the expected behavior.
div by default are block elements which means that they always start on a new line and take up the full width available.
If you want elements to be on the same line and to only take up as much width as necessary, you must use inline elements, such as span.
Find here a complete reference

By default, the flow of the page will display your divs elements (which are blocks) one below another as you have seen.
If you want to override this behaviour you could set a
position: absolute;
property to your divs so they can be placed wherever you want regardless of the position of other elements. For example you may want to set all your divs at the top left corner by doing:
div {
position: absolute;
top: 0;
left: 0;
}

By default display property is block for div.Change it to inline to display in one line.

If you use float: left; in css, the problem will be solved. Because div element is a block level element.

Related

Which position should I use? "Relative? static etc." [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I haven't used float for the divs in CSS rather I have used position. I used relative to position all the divs but all of them gets jumbled up in other screen resolutions. What am I doing wrong please clarify since I am new to HTML. Thanks in advance.
You shouldn't really use the position property unless you want something specific out of it. Block and inline elements do most of the work when it comes to position. With that said we still need the position:property in many cases. The most used kind of positions are relative and absolute, and I can help you understand these.
position: absolute; allows you to assign a specific position example:
div {
position: absolute;
top: 50px;
left: 50px;
}
What the previous code does is place the selected div element 50 pixels away from the top border and 50 pixels away from the left border. The tricky part is that you need to specify what your borders are going to be.
For this we use position: relative;. Example:
.parent {
position: relative;
}
.child{
position: absolute;
top: 50px;
left: 50px;
}
What the previous code does is set the parent element to be the reference to it's child element. So the position: absolute;child will be positioned 50 pixels away from it's .parenttop and left border. Hope this helps.
Here's w3schools article about positioning : W3 Positioning

How to Implement this UI with CSS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working on Wordpress template, and got this little problem which is:
I can't position the date label above the image thumbnail, as you can see here:
http://cl.ly/image/3X1j3h3j0E0X
If I make it position: absolute its position would be changed while the windows is changing.
How could I implement the right CSS for that.
This is the CodePen example, try to resize the window:
`http://codepen.io/msabdullah/pen/rplgC`
You need to use a combination of position: relative and left right top bottom to position it correctly. It should be positioned relative to the parent element.
<div class="parent">
<div class="child">
</div>
<div>
If you had the above html structure then you would need to make sure both had positioning, and then position the child relative to the parent:
.parent {
position: relative;
}
.child {
position: relative:
top: 0;
right: 0;
}
This is just an example, but it should be something similar.
Try to give position relative to parent element to maintain the uniformity.
check the DEMO.
CSS like this: Where div is parent element have position:relative.
img, p{display:inline-block;}
div{position:relative;}
p{position:absolute; right:0; top:0;}

HTML5 positioning [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
is possible to make a specific element about 5px longer that it would automatically be? because my article element has a video element which sticking out of the given space a bit, while I could just set the height manually I thought it would be better to have it set itself automatically (like it normally does) only adding about 5px. Any Help?
I'm not sure I understand the question, but have you tried giving it a padding-bottom of 5px?
Try using a dynamic width to set the width of your contained element.
Eg.
article {
width:300px;
}
contained-element{
width:95%;
}
Just give padding-bottom:5px to your <article> elements, & make height:auto for both elements, then <article> element will expand as per height with 5px at bottom.
Am not very much sure what are you asking about but if it's regarding fitting your video in a div for that you can create a wrapper class such as
.wrapper-div {
position: relative;
padding-bottom: 20%;
height: 0;
}
then have the child element inside it
.element-to-load{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
I assume you want to the div to expand if the video is larger than the article.
To do this you just need to:
1) Give the Outer Div a height:auto - and a min-height:
2) Give the Inner Div a margin-bottom of 5px
Check out this Fiddle Demo
Does this answer your question?
Just set the CSS for that element be (use the actual pixel width and height):
height: 25px;
width: 50px;
and the height and the width will be forced to be that height and width.
Although if possible try to use 'em' so that it is automatically responsive.

How can I best port a frame-based "HTML of yesteryear" application to modern CSS(3)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have an old HTML-based webapp that I am modernizing, and I wanted to see if there was anything better than my off-the-cuff CSS use to find an equivalent for the frames.
To simplify slightly, there are three roughly equal frames side by side. All three of them scroll vertically. The best approach I can think of now is to float everything left, with appropriate width and margin-left, and use overflow-y (and, where horizontal scrolling makes sense, overflow-x) settings for the DIVs in question.
Is "float everything left and set width and margin-left" optimal, and if not, what would be a more idiomatic way to replace the frames?
Thanks,
Replacing frames is tricky if the different frames use different style sheets, JS and what not.
If that is not an issue in your case and you can just copy the relavent part of the HTML from the three frames to one new HTML page with three vertical DIV's of some sort then I would suggest that you use something the overflow technique that you described. Position them absolutely with a width of 33% and a height of 100%.
You could use floats, but that might result in all kinds of annoying issues. In yoru case I'd go for position: absolute
<div class="c c1">
loooooong content
</div>
<div class="c c2">
loooooong content
</div>
<div class="c c3">
loooooong content
</div>
.c {
position: absolute;
width: 33%;
height: 100%;
top: 0;
overflow-y: scroll;
}
.c1 {
left: 0;
}
.c2 {
left: 33%;
}
.c3 {
left: 66%;
width: 34%;
}
here's a jsfiddle: http://jsfiddle.net/9CAYb/

z index issue navmenu will not overlap [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to place some links between my top area and bottom area so that they overlap both of them by an equal amount. I have the navmenu div set to a larger z-index than all the other div's but I can't get it to overlap anything. site is at http://www.joekellywebdesign.com/churchsample1/index.html
stylesheet is at http://www.joekellywebdesign.com/churchsample1/css/styles.css
Thanks in advance for the help.
Many ways to do it.
You can simply specify a negative margin for your navmenu
#navmenu {
margin: -10px 0;
}
Since you have specified the position as relative, which means the location of the div will depend on previous div. Its top would be the top plus the height of the previous div.
You can either change the position into absolute, or adjust the margin or padding values to display content inside the div in your way.
z-index will only be effective when elements are overlapping. In your case, all divs are in relative position. None of them is overlapping.
You could for instance do the following:
<div id="navmenu">
<div class="inner"><h1>Test text</h1></div>
</div>
and than in CSS:
#navmenu .inner {
padding-bottom: 15px;
margin-top: -15px;
position: relative;
z-index: 200;
background-color: #F00;
}