I like the h1 element because it specifies the contents are header style contents, but you're not supposed to put things like images or divs inside an h1, so is there an alternative to an h1 that I can put other markup in?
My current html looks like this:
<div class="section">
<h1>
<div style="float:left">header text</div>
<div style="float:right">text</div>
<div style="clear:both;float:none;"></div>
</h1>
<div>body contents</div>
</div>
I like the h1 because I can add a css style to any h1 with a div.section class, but I'm not suppoed to put divs in it...
You could always do
<h1>header text <span>text</span></h1>
Then you handle the css for clearing the floats and floating the second text in the css file.
You should use a semantic image replacement method: Which makes for the most elaborate design (images, colors ect.. the graphic is your oyster) as well as being completely semantic and accessible.
Otherwise as mentioned above, you can add any element that is an inline element: A, SPAN, ect... inside of your H1... but I would shy away from this if you are interested in semantics and being SEO friendly.:
<style>
h1{
background: url('../path/to/image/image_to_replace_header.jpg') no-repeat top left; // Sets the BG that will replace your header
text-indent: -9999px; // Moves the test off screen
overflow: hidden; // Hides the staggered text for good
width: 300px; // Sets width of block(header)
height: 100px; // Sets height of block(header)
}
</style>
<h1>My Awesome Site</h1>
Now your text is still technically there, but you have a pretty photo in its place. Sighted, non sighted, and robot friendly.
The method i personally prefer is to keep the <h1> tags intact and use <span> tags instead of divs inside them. You can style the spans to be display:block and then treat them like divs if need be. This way, the semantic meaning of your <h1> tags is kept, and needless <divs> are omitted. Read up on divitis.
This won't solve your problem if you need to include images inside your <h1> tags. You probably shouldn't be adding graphical styling with img tags anyways, but rather applying the image as a background to the the <h1> element, moving style-related graphics out of your markup into your CSS files.
Is there a reason you don't specify just:
<div style="float:right">text</div>
<h1>header text</h1>
<!-- <div style="clear:both"></div> only if really necessary -->
This will keep your markup semantic, still float text to the right and keep it out of the h1 tag which it is semantically not part of.
To answer your question directly: yes you can use another method. It keeps your CSS editing ability, as well as having a proper H1 element:
<div class="section">
<div id="Header">
<h1 style="float:left">header text<h1>
<div style="float:right">text</div>
</div>
</h1>
<div>body contents</div>
</div>
All the important text is in the H1 and you can still style it as you like.
You can use html5 structural elements :
<section>
<header>
<div>header text</div>
<div>text</div>
</header>
<article>body contents</article>
</section>
Just reverse the nesting order of some of your code:
<div class="section">
<div style="float:left"><h1>header text</h1></div>
<div style="float:right"><h1>text</h1></div>
<div style="clear:both;float:none;">body contents</div>
</div>
I'm not sure that the right-floated text was supposed to be h1, but you get the idea. Often these things are best solved by keeping block-elements on the outside and nesting the line-elements within them.
Headers have semantic meaning. Think of a magazine and why they use headers. If you want to place an image in a header for decoration purposes, use a background-image. I cannot think of a reason why you would need to put an image into a H1 for contextual purposes.
Related
One of the benefits of HTML 5 are semantic tags like <header>, <nav> and <main>.
Unfortunately, I haven't found a way to style them so the <nav> goes down the left of the page, not without a parent <div>, anyhow. It seems to me that such a <div> messes up the semantics, and I'm not sure it's technically valid HTML.
<header>My Page</header>
<div class="nav-main"> <!-- I don't like this -->
<nav>
Foo
Bar
</nav>
<main>
<h1>My Main Content</h1>
<p>Blah.</p>
</main>
</div>
<footer>ⓒ Not really copyright</footer>
The CSS:
div.nav-main {
display: flex;
}
nav {
width: 10em;
}
nav a {
display: block;
}
Is there a clean way to do it, without the <div>?
I don't think an old-school float: left is a solution, because I don't want the <main> to wrap around the <nav> if it's taller, and I don't want to add arbitrary heights or other hacks. I just want the <main> and <nav> to both be their natural heights and have consistent widths, and for the <footer> to be underneath both.
Otherwise, if somebody can cite assurance that wrapping <nav> and <main> in a <div> is okay, I'll just thank you and move along… although it still doesn't look very elegant to me.
So a <div> is a non-semantic element, so you technically have valid html here. But I understand not wanting divs everywhere. You could potentially put flex on the body, set the width of the header and footer to 100% so they would be on their own line. So it'd be
body
{
display:flex;
flex-flow: row wrap;
}
header, footer
{
width:100%
}
Hopes this helps!
You're trying too hard to achieve minimalist markup. What you have there is extremely simple compared to almost any website. If it works to have a couple div elements for layout, there's no issue.
The key is that relevant content is contained in your semantic elements, not that you only use semantic elements. If you're doing any sort of modern, flexible, responsive layout it can't be avoided.
I still haven't found 'official' (W3C) word about whether a <div> can be a legal parent for a <nav> or <main>, but I do trust Mozilla's web docs. They state that:
<nav> can have '[a]ny element that accepts flow content' as a parent, and
<main>'s parent can be any element '[w]here flow content is expected, but only if it is a hierarchically correct main element'.
So in this example, the <div> is fine.
here is my angular js markup:
<div layout="row">
<h1 ui-sref="home" style="cursor:pointer">Element A</h1>
<h1 flex="10"></h1>
<h1 ui-sref="simulator" style="cursor:pointer">Element B</h1>
</div>
however in the browser I see "Element A" and "Element B" very close to each other.
How can i make some space between them?
CSS is your friend. Simply apply either margin or padding to space out your html elements like h1's. This is what CSS is for - it is better to apply styling rather than rely on html elements like a to space things apart.
h1 {margin: 15px;}
Also - you should perhaps not have multiple h1's - semantically there should only be one h1 per semantic section (it used to be stated that you should only have 1 h1 per page - but with the advent of self contained HTML5 elements like sections - each one may have a h1 -h6) although I personally prefer to only have 1 h1 and treat all other headings as subservient to that.
also better to not have your styles inline - but in the hea or in an external style sheet.
h1{
margin: 15px;
cursor:pointer;
}
<div layout="row">
<h1 ui-sref="home" >Element A</h1>
<h1 flex="10"></h1>
<h1 ui-sref="simulator">Element B</h1>
</div>
let's say I have some markup like this:
<div id="content">
<h2>Post 1</h2>
<img src="dummy.jpg">
<iframe src="youtube.com/foobar"></iframe>
<p>Sample text.</p>
<p>Second paragraph.</p>
<h2>Post 2</h2>
<img src="dummy2.jpg">
<iframe src="youtube.com/foobar2"></iframe>
<p>Sample text2.</p>
<p>Second paragraph2.</p>
</div>
How could I style this, so that all the images and iframes are in a sort of "left column" with iframe under the image, whereas the paragraphs are on a right sided column?
float:left; for img and iframe obviously doesn't work, because the iframe floats left to the img.
Thanks for any input, by now I read too much about inline elements, paddings and floating to make it all work together.
As I want to implement this into a getsimple Site (with users who can't to much html markup) it should work without div-containers.. (also if I needed them I would actually doubt this content/design seperation that CSS always promises, but makes it so hard to implement ;))
If I work with floating, I am aware that I should clear this with the h2 headings as to get a new block for each post.
#content h2 {
clear:both;
}
I have a div container with a series of p tags. Each p tag will float to the left. I want two p tags per line, so think field/value.
Title: Some Title
Author: Some Author
<div id="container">
<p class="field">Title</p><p>Some Title</p>
<p class="field">Author</p><p>Some Author</p>
</div>
If I set the "field" class to clear: both, I get the desired functionality in most browsers except IE 7 (not worried < 7). However, in IE 7 if the containing div is wide enough, the clear: both seems to be ignored and I'll get something like this:
Title: Some title Author:
Some Author
A couple of thoughts:
I can monitor the width of the containing div so that only two p tags can sit on one line but that seems very brittle.
I can muddy up the markup by placing clearing divs after every two p tags. It would work but makes me feel dirty inside.
How can I overcome this issue?
Use this pattern (span is optional - for additional styling if needed). Lists make more semantic sense than re-purposing the wrong tags. This is a list. :)
<ul>
<li><label>Title</label><span>Some Title</span></li>
...
</ul>
CSS:
ul, li {
padding:0;
margin:0;
list-style-type:none
}
li {
clear:both
}
label {
float:left;
width:150px;
}
http://jsfiddle.net/QUL9v/1/
Using the p tags....
<div id="container">
<p class="field">Title</p><p class="field">Some Title</p><div class="clear"></div>
<p class="field">Author</p><p class="field">Some Author</p><div class="clear"></div>
</div>
with css:
.field {
float: left;
}
.clear {
clear: both;
}
This is just sticking to the use of the p tag. Personally, this is how I would accomplish it (http://jsfiddle.net/QUL9v/3/):
<div id="container">
<div class="field">Title</div>
<div class="field">Some Title</div>
<div class="clear"></div>
</div>
<div id="container">
<div class="field">Author</div>
<div class="field">Some Author</div>
<div class="clear"></div>
</div>
The only reason I'm recommending this is because since this is more of a layout issue, it feels more natural to me to use the div as opposed to p element. Also, it will ensure the position of the text, regardless of what you put inside the divs (anchors, forms, tables, etc).
Another thing you should pay attention to is I'm using the clear as the last sibling instead of the first (as in your examples). If you're clearing the front; then its possible that since your trailing elements are floated and inline, you're going to potentially run into errors down the road, especially with IE7. A lot of the times, the floating rule will get passed on to elements you never intended or thought it would be passed to. Clearing at the end ensures that this doesn't happen.
I am facing problem while aligning two text, one in center and other text in right.
I used a Div to align it:
<div style="text-align:center">
<h1> Sample Heading</h1>
<div style="float:right; text-align:center">
sample link
</div>
</div>
When I used this my heading comes left, its not a centrally align properly please tell is this the correct way or is there any other way to handle this scenario.
Thanks
If you want the item to not mess with the layout, try absolute:
<div id="header" style="position:relative;text-align:center;"><!-- define relative so child absolute's are based on this elements origin -->
<div id="sampleLink" style="position:absolute; top:0px; right:0px; >Link</div>
<h1 style="text-align:center;">Heading</h1>
</div>
You do not need to use div's to do this, you can style the elements themselves. The < a > tag by default does not understand text-align, as it is an inline element, so I have placed the link in a paragraph, which accepts text-align. I have placed the two lines in a < div > tag with a small width so it is easy to see what is going on.
<div style="width:400px;">
<h1 style="text-align:center"> Sample Heading</h1>
<p style="text-align:right">sample link </p>
</div>
It works fine to me.
But if you have some issues with positioning of h1, try make it block: h1 { display: block; }.
On other hand, if you want to display h1 and a at the same line, you just have to put right-aligned a before h1.
For anyone using pug
To quickly align a link to the right, this seems to work:
html
head
style.
rite {
font-family: monospace;
font-size: 10pt;
text-align: right;
}
and then ...
rite
p
a(href='/logout' align=right) logout
Note that this won't work:
rite a(href='/logout' align=right) logout
And this won't work:
rite
a(href='/logout' align=right) logout
As #superUntitled explained. Great tip from #superUntitled.