How can I align my 2 texts on the same row? - html

I'm trying to achieve something simple and the rules make it non-trivial. Here is my fiddle. It is non-trivial for me to align the elements in a natural way, I think the elements should be on the same row when I make a float:left.

Hx elements have default margins.
The "space" from the top cause by the margin-top property of the H1 element.
Consider using a reset.css file to reset those default values.
Another thing is the line-height property.
In conclusion:
margin-top:0;
line-height: 20px; (choose a value that fits to your needs)
http://jsfiddle.net/ynrmwgt9/4/

I would recommend to wrap your div in a container div. A little bit like bootstrap system.
See the fiddle, and ask me if you have questions about :)
I'm using something like this :
<div class='row'>
<div class='col'>Content 1</div>
<div class='col'>Content 2</div>
</div>
The stylesheet has to specify that rowfill 100% of the width, and your two columns, the half, then 50% each.
.row
{
width:100%;
}
.col
{
width:50%;
float:left;
text-align:center;
}
Text align center is just for make it beautiful
http://jsfiddle.net/ynrmwgt9/2/

Related

How to use margin/padding on text-align centered element

HTML
<div>Test</div>
CSS
div {
text-align: center;
width:200px;
margin-left:20px;
}
I don't want to have the text at center but pushed slightly right/left. Is this possible?
If it's a single line you should be able to do this:
text-indent:1em;
ref: https://developer.mozilla.org/en-US/docs/Web/CSS/text-indent
It is possible. Currently your margin-left property will affect the div and does not specifically target the inner text.
I would do something like the following. If you surround the text in a <p> tag you can offset it to the left or right by using padding.
div {
text-align: center;
width:200px;
background:blue;
}
div p{
padding-left:30px;
}
<div>
<p>Test</p>
</div>
If you use padding on one side, it will offset the text visually by whatever you set it to. What you have listed would work, as would using padding instead of a margin, but it depends on what the site looks like visually.

CSS Aligning Text Within Divs

I want to create a header with a few words aligned to the far left of the header div and a few words aligned to the far right. I initially did this by creating spans (.headerLeft and .headerRight) and adjusting the alignment for the portions I wanted aligned. However, this creates problems if I want a background-color for my header div. Is the best practice solution here to simply add a little inline CSS styling, or is there a better way?
My Code (example)
HTML
<div class="header">
<span class="headerLeft">Welcome to .</span>
<span class="headerRight">Login | Register</span>
</div>
CSS
.header {
width:100%
position:fixed;
top:0;
margin-top:0;
background-color:red;
text-color:#C14000;
}
.headerLeft {
float:left;
text-align:left;
}
.headerRight {
float:right;
text-align:right;
}
#header {
overflow: hidden;
}
This code will fix your problem. The height of #header will automatically take the height from the tallest element inside #header.
Another way would be to manually set the height for #header.
You don't need to style sth inline :)
You need to set the overflow attribute for the header class to force it to wrap around the inner spans. see http://jsfiddle.net/PsychegoPro/rnDT8/
You need to clear the floats in order for the div to have actual height.
This can be achieved by using clearfix. What is a clearfix?

How to divide a webpage horizontally?

I am learning HTML. I was reading the <div> tag in a book. I understand how it breaks the website into vertical pieces but I have also seen examples where people use <div> tag at the sidebar. How does the <div> tag work horizontally?
To have them so they are side by side, you need to use CSS. A div element by default has the display property set to block. So therefore elements after it will usually appear below.
To setup a couple columns lets say you have the following:
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
You can setup the columns by applying CSS rules to the classes:
<style type="text/css">
.container {
width:500px;
}
.left {
width:250px;
float:left;
}
.right {
width:250px;
float:right;
}
</style>
In short, im giving it a container to have a maximum width. It's not required, but generally done this way. Then I set both divs to be half the width of the container, and i put float left on it. So all elements will stick beside it. The float right is optional. However, if you make the container wider, the right div will stick on the right hand side.
It is considered a block element and has a width of 100%.
I'd suggest you try it in a text editor & your browser by adding a background-color and some content in it.

Inline div elements

I'm trying to put div elements right next to each other. The problem is, even if there is enough room for the two elements to be on the same line, the new div moves itself to the next line, I need the other div only to go to the next line if there isn't enough room.
Does anybody know how to do this?
Set the CSS display style to display:inline-block;.
This allows the element to keep it's block-like functionality, while also allowing it to be displayed inline. It's a half-way house between the two.
(But note that there are some compatibility issues with older versions of IE)
Divs are block level elements, so by default they will always occupy an entire line. The way to change this is to float the divs:
<div style="float: left"></div>
Use float's and margin's; that way when there's no space you can just put overflow:hidden to the container hide the rest of the div instead of making it go to the next line.
CSS
.container {
width:500px;
background:#DADADA;
overflow:hidden; /* also used as a clearfix */
}
.content {
float:left;
background:green;
width:350px;
}
.sidebar {
width:155px; /* Intentionaly has more width */
margin-left:350px; /* Width of the content */
background:lime;
}
HTML
<div class="container">
<div class="content">Content</div>
<div class="sidebar">Sidebar</div>
</div>
In this demo you can see: floats, margin+floats, display:inline-block.
Demo here: http://jsfiddle.net/kuroir/UupbG/1/
You need to use float CSS rule. Just use some class or identifier and set float to left or right.
Make sure that you have a fixed width to the divs

Inline-block elements don't show as expected

I'm writing a style sheet, and I want to display three elements horizontally (width=33%) within a container, with a relative layout.
Here's the code:
#container
{
margin:auto;
width:85%;
padding:0;
}
#element
{
display:inline-block;
width:33.3%;
margin-left:0;
margin-right:0;
border:0px;
text-align:center;
}
I don't understand why with three elements:
<div id="container">
<div id="element">hi</div>
<div id="element">every</div>
<div id="element">one</div>
</div>
The last element is shown below the first two, while I believed that they would be drawn on the same line. There are no margins,padding or borders.
If width is set to 32%, in a Full browser window, they are on the same line (it works), but when I reduce the browser-window width, the last element falls on a new line.
Does anybody know why this happens?
These are inline blocks, so they get laid out just like characters would (think of them as really big characters, basically). And in your case you have whitespace between them, so that whitespace becomes a single space on the line between the inline-blocks in the rendering; you can see this if you put borders on them. So the space taken up by all three together ends up being "99.9% of container width plus width of two spaces in the container's font". When you reduce to 32%, then you get line-breaking once two spaces add up to more than 4% of the container width (which is why it only happens at smaller container widths).
If you really want your inline-blocks flush up against each other, take out the spaces between them.
Make you element a class (thanx Jarrett) and add float:left style to that class.
.element
{
display:inline-block;
width:33.3%;
margin-left:0;
margin-right:0;
float:left;
border:0px;
text-align:center;
}
<div id="container">
<div class="element">hi</div>
<div class="element">every</div>
<div class="element">one</div>
</div>
I recommend to play arround with the containers width.
A tip that works for me is giving them a line. Below is my contribution:
http://jsfiddle.net/8dWhF/