Positioning buttons on a simple webpage using CSS - html

I'm trying to get my head around layout using CSS. As usual there seems to be more than one way to do things, but I have come up with a way of positioning things on my test webpage which works as I'd like, apart from getting some buttons in the correct place!
If you'll excuse my crude Paint layout diagram below, could you please look at how I've arranged the different parts, and then advise on how to position the buttons where they are shown?
The whole blue section is a wrapper to keep everything in, and has a
min and max width.
Nav bar - Just displayed as a block.
The main image is just max-width 100%, and the quote is positioned
inside this using absolute positioning relative to this image (so it
moves with the image)
Description 1 and 2 are floated left, both with a determined width
and margin (as a percentage) so they dont overlap and they dont
exceed the width of the screen.
My issue is with the buttons, shown in red. How can I get these where they are shown? If I float them as per the two descriptions, the buttons appear too low down (ie below the second description as this is further down the page).
I've tried an absolute position, but this seems a bit clunky as the only container I can refer it to is the entire wrapper, and it also seems a bit 'trial and error'y to get the position exactly right. Can I do an absolute position relative to the first description maybe? All suggestions/solutions welcome!
** Additional information **
Screenshot of the webpage layout after replicating the answer given:
Below is the HTML:
<div class="ib50">
<div id="desc1">
<h2>Welcome to Toms Properties</h2>
<p>There is a second leg to come at Old Trafford - on Wednesday 22 January - but who will the winners of tonight's tie face at Wembley?It will be Manchester City or West Ham, with the first leg of that one taking place at the Etihad tomorrow night.</p>
<p>Are we anticipating a Manchester derby final?</p>
</div>
<div id="buttons"><button>Ohai there!</button></div>
</div><div class="ib50">
<div id="desc2">
<h2>Site Navigation Tips</h2>
<p><strong>Charles in Golders Green, via text: </strong>I'm very much a Moyes supporter, but what I can't understand are his claims that we deserved to win or get something out of the Swansea and, to a lesser extent, the Spurs game. Regardless of new signings, now is the time Moyes must earn his money by rallying the side. Two results before Chelsea away in a fortnight are a must. </p>
</div>
</div>
And below is the CSS:
.ib50 {
display: inline-block;
width: 50%;
}

4/Description 1 and 2 are floated left, both with a determined width and margin (as a percentage) so they dont overlap and they dont exceed the width of the screen.
This is a very wrong way to go about it. Instead, try something like this:
<div class="ib50">
<div>Description 1</div>
<div id="buttons"><button>Ohai there!</button></div>
</div><div class="ib50">
<!-- Note that it is important to have no space between tags there -->
<div>Description 2</div>
</div>
Now apply this CSS:
.ib50 {
display:inline-block;
width:50%;
}
You should be able to figure out the rest from here :)

Related

Grid system html

I was just wondering if someone can give me a hand, i've tried for 3 hours to solve this issue. I need to have the interface like on the picture by using grids, or anything.
The closest thing i can get is when everything displayed correctly except the second bottom grid. It usually gets below the white line (thats the starting point).
Could someone give me a tip on how to get around this problem.
You have minimum two options:
one is to make the grid elements absolutely positioned and give them top, left, right and bottom values. The parent element (grid container) should have "position:relative;" (or can be fixed or absolute, but in your case relative will make more sense).
Another option is to write markup like this:
<div class="col-xs-6">
<div class="col-xs-12">
one
</div>
<div class="col-xs-12">
two
</div>
</div>
<div class="col-xs-6">
three
</div>
Basically you wrap the two divs on the left into one parent div so the layout will not break. Just make sure the height of inner divs are 50% of the height of the right box.

Vertical alignment in column content in Bootstrap 3

I am trying to horizontally align 3 columns (Bootstrap) that have variable heights depending on the size of the view port. As seen on the first picture, everything is centered and aligned on large screens.
When the viewport becomes smaller, the paragraph's height changes and the alignment is lost. I would like to keep the headings (blue), paragraphs, and buttons aligned.
My inital thought was to create three rows for the three different types of elements. Unfortunately I will not be able to do that since I want to keep the border in the middle column.
What would be a correct way of keeping alignment of these three columns regardless of the viewport size?
I've never really seen a perfect answer to this, thats not with javascript, I can't remember the exact code, however, last time i did it, i placed the text in a nested section, each boxes text section had the same class, and i assigned a min-height to that class that was long enough that it fitted the longest piece of text in, then place the button underneath that section.
If you put your code in a JSFiddle i can help you more, however,
it should looks something like this, layout wise,
<div class="four columns">
<img></img>
<div class="text"> this will have a min height
</div>
<input>button here</input>
</div>
I hope this helps, i know it doesn't seem very clear, if you have a live version or a JSfiddle i'm more than happy to help further.
as for the text, you're very limited, there is some very good jquery scripts, just look through http://www.unheap.com
and just have general play with text sizes
Assuming they are in an inline containe vertical-align:middle;

How do I make it so these div elements don't overlap?

I made a website with div's, classes and everything a website needs. But when I resize the window all the inner text moves over the NavArea and Buttons overlap. Like even on StackOverflow the top of the page has the search text box and when I resize the window it just moves overtop. What's the solution to this? Here is the code--
CSS: http://pastebin.com/2rqinFJw
HTML: http://pastebin.com/u7eXUkwC
Please test the code before you post an answer please, I have tried everything and need an indepth answer that I can understand. I have spent way too long but do not plan to give up!
Ok!
So firstly, a main problem with your programming (as I see it) is that you're using absolute positioning way too often - like honestly, they should nearly never be used for creating a website layout.
Also, this part of your code:
<div id="b1">
<ul>
<li>Home</li>
</ul>
</div>
<div id="b2">
<ul>
<li>Downloads</li>
</ul>
</div>
No offense, this isn't the correct layout for a navigation bar. Your <a> tags should go inside your <li> tags, not outside! Also, you split your navigation up into two - this is completely unnecessary, while also using position: absolute; on a navigation bar - no need at all.
Here's what it should (or could) be:
<div id="b1">
<ul>
<li>
Home
</li>
<li>
Downloads
</li>
</ul>
</div>
Note how I've placed the two in the same <ul> tag, and also put the <a> tags inside of the <li> tags. I've also rearranged your CSS to do the same thing.
Not sure what your <div class="imageArea"></div> does, but I just threw it in there - it appears you got no CSS code for it anyways.
What I did to the main layout, was I moved it into a <div class="container">, which I used the following CSS on:
width: 1000px;
margin: 0 auto;
This will create a container of sorts that fits in the middle of your screen. You can adjust this width if you want - I just find that 1000px suits my needs rather well.
Also, I've floated both elements to the left of this - this allows the two <div> tags - your navigation section and your textBody, to go next to each other. I made them with a width of 20% and 80%.
Since I need some spacing in the middle, I added padding-right: 10px; to your navigation. Since this makes a total width of 100% + 10px (which would push the textBody down), I added box-sizing: border;box; in order for the padding to be counted into the width (so that it makes a total width still 100%).
Here's the final HTML and CSS:
http://pastebin.com/ZC2TFir2 (HTML)
http://pastebin.com/wEwzRP66 (CSS)
I don't think many will give you an answer as detailed as this (I know I normally wouldn't) - but in all honesty, I believe this webpage could be designed a lot better (plus I'm bored and work hasn't started yet).
Positioning everything absolutely is not a good practice. Absolute positioning will allow elements to move on top of one another. Absolute positioned elements don't care about the layout of other elements.
If you want a nav bar to float to the left of the text, use the float property, like so:
#nav {
float: left;
}
See this Fiddle: http://jsfiddle.net/bex5b0by/
You could just put in
#nav {
position: relative ;}
And after position:relative put in margin-left: -200px or whatever you want it to be. (You can use margin-left/right/top/whatever you need OR left/top/right/etc. : -number px / number px;
That means you place your div in relation to the other divs. But for this to happen you have to have the divs in one another. (< BIG div>

Center text between floating paragraphs

I know that "how to center stuff" questions are kinda annoying and boring, but anyway ...
I'm trying to get one word ("Impressum") to appear perfectly centered in the footer of my wordpress page by putting something like this in the footer.php:
<p style="float:left">Impressum</p>
The problem is, on the left side of the same line is the copyright notice (floated to the left), on the right side is the theme author credit (floated to the right). I've tried different things to get the word centered (in relation to the width of the page!), but the floating to the left that I need to get my word in the same line as the other stuff screws with the position ...
Here's a fiddle:
http://jsfiddle.net/GinSan/2t7e3zeq/
Note that most of this is dynamically generated by the theme and I don't want to touch it. Please try to help me by only altering line 3 (i.e. without rebuilding the whole footer ...). Also, though I used a fixed width of 1100px for testing purposes, it should be responsive and stay in the center regardless of the width of the page (down to 640px).
Thanks!
Just put your "impress" paragraph at the and of the footer and replace float:left with text-align:center. Here is updated fiddle
in the jsfiddle this run, in your page you might position: relative for the first div
<div style="width:1100px">
<p style="float:left">© 2014 foo</p>
<p style="position: absolute;text-align: center;width: 100%;">Impressum</p>
<p style="float:right;text-align:right">Powered by Theme</a></p>
</div>

Why does Twitter use so many <div>s for its fixed position navigation bar?

I am trying to build up a website with a Navigation bar on top of the page. It should be fixed on top of the browser when we scroll the page (like facebook or twitter), but not scroll with the page(like google search??). see Fig like:
seems like we should set the css attribute position of this navigation bar like
#nav_bar {
postion:fixed;
}
but why all those websites use a whole bunch of div to do this? Does all these divs make any sence? Like twitter:
where topbar js-topbar is the outmost div which size is 1583*40px, but I didnt find the definition of its size. And then it goes to global-nav->global-nav-inner->container, finally...container, which is acutually hold the navgation items like a list, a search bar so on and so forth. something Weired is that the size of it is 865*0px. For more information, you can view source of the home page of twitter.
And my question is : but why all those websites use a whole bunch of div to do this? Does all these divs make any sence? Why is a div which height is 0px can hold those navigation items?
why the 'many' divs?
The general idea is the more wrapping elements you have the more flexibility you have with regards to what you can achieve in styling with css. Obviously there is a limit, as you should also try to keep your markup readable and semantic. I would say many important or segregated regions in a site would benefit from three wrapping elements:
<div class="positioner">
<div class="padder">
<div class="alignment">
Menu Here
</div>
</div>
</div>
Obviously with the more semantic HTML5 elements you can make this more readable:
<header class="positioner">
<div class="padding>
<nav class="alignment">
Menu Here
</nav>
</div>
</header>
The reason for keeping a seperate element for padding is so that you can set specific dimensions to your positioner (i.e. header) and not have that calculation messed up on certain browsers (with old box modles) by the addition of padding.
The reason for keeping alignment seperate is because it will give you greater flexibility on the alignment tricks you can use.
The reason for using the header element is because this content will act as a header imo.
The example you give above, each element will most definitely have it's reason for existing and they will most probably all be used to achieve the layout the designer wanted with regard to css. Some times extra wrapping divs are also used as placeholders for content that may be AJAXed, this is probably quite likely when dealing with the likes of Twitter.
You can of course get away with using only a single wrapping element, but you will be limiting what styling and positioning you can achieve later on down the line.
why the height 0px?
There is a trick often used with positioning absolute layers in a relative location (rather than an absolute location) - and I believe this is the reason why you are seeing this, but the trick in itself isn't the actual cause of the height:0px. The trick uses the following construction:
<div style="position: relative;">
<div style="position: absolute;">
The content here will float outside of the document flow,
but remain in the correct location within the document flow
- in all viable browsers.
</div>
</div>
If you inspect the above construction, using any browser debug method, you will notice that the position: absolute; layer has collapsed to have no height (in modern browsers). This is the default behaviour of position absolute outside of the old Internet Explorer world (with no other positioning or dimensions settings), because an absolutely position element is taken out of the document flow and by default doesn't calculate anything to do with it's children.
If you wish to override this behaviour you can simply use overflow:hidden; (as long as the height has NOT been specifically set to 0px by some other class or by JavaScript) - this will force the element to calculate the dimensions of it's children and wrap them.
First of all use position:absolute; if you don't want it move with you when scrolling. position:fixed; if you do.
Second of all when you build a website the first thing you're going to have to do is decide how the structure of your website is going to look like. So the menu at the top will be
<div id="Menu"> </div>
Now you may want to create a header under it
<div id="Header"> </div>
Under that you want to share content, since thats what website do.
<div id="Content"> </div>
Under that you may want a footer, that says 2012 Copyright etc.
<div id="Footer">2012 Copyright zoujyjs © </div>
Now you may want to center everything. Why not just put all these previous divs inside a wrapper div. Then all we have to do is center the wrapper div.
<div id="Wrapper">
<div id="Menu"> </div>
<div id="Header"> </div>
<div id="Content"> </div>
<div id="Footer"> </div>
</div>
You could also add stuff like a logo inside the header, etc.
I think you get the idea. But isn't it obvious you're going to get "divception" then?
Also: When no height is specified on a div, the div will automatically resize with the content within.
<div style="background-color:black;">
<!-- Nothing will be seen on your page, because the div is 0 height, 0 width by default -->
</div>
<div style="background-color:black;">
Height of the div will now be the same height as the height of this line. (15 px by default I believe
</div>