I spent a little while trying to figure out how to achieve the following effect without using a table but couldn't figure it out: http://jsfiddle.net/sKFzA/
CSS :
.header{width:100%;font:25px Arial,Helvetica,sans-serif;}
.titleCol{width:99%;}
.dateCol{vertical-align:bottom;white-space:nowrap;}
.dateText{font-size:12px;}
HTML :
<table class="header">
<tr>
<td class="titleCol">This is the blog title</td>
<td class="dateCol"> <span> </span><span class="dateText">1/23/2012</span>
</td>
</tr>
</table>
To explain it, I have a blog title and a blog date. The title could be long and wrap. At the end of the last line, wrapped or not, I want the blog date to be aligned to the right.
So I have two questions. Is there any reason not to use a table for this? If so, how would you achieve it without assuming static font sizes?
CSS has properties that allow any element to behave like specific components of a table.
http://cssdeck.com/labs/rjiesryc
<header>
<h1>This is the blog title</h1>
<time datetime="2012-01-23">1/23/2012</time>
</header>
CSS
header {
display: table;
width: 100%;
}
header h1, header time {
display: table-cell;
}
header time {
/*vertical-align: bottom;*/
}
With the help of cimmanon and the others, I've gathered that:
The only reason's not to use a table here is because layout is not technically a table's intended purpose and also by not using a table you can separate your layout (CSS) from your markup (HTML). However, if I were to use a table, I am not aware of of any negative effects.
There doesn't seem to be a good solution to this exact layout without the concept of table, but my table solution can be achieved without using an HTML table by applying styles to display other elements as the table. So I replaced my table elements with divs. The span with the space before the date allows the smaller sized date to stay aligned to the title's baseline without having to hard-code line height's or font sizes. So if the font sizes change, I don't have to worry about updating any other magic numbers hard-coded around them.
http://jsfiddle.net/K35gT/
HTML
<div class="header">
<div class="titleCol">This is the blog title</div>
<div class="dateCol">
<span> </span><span class="dateText">1/23/2012</span>
</div>
</div>
Styles:
.header{display:table;width:100%;font:25px Arial,Helvetica,sans-serif;}
.titleCol{display:table-cell;width:99%;}
.dateCol{display:table-cell;vertical-align:bottom;white-space:nowrap;}
.dateText{font-size:12px;}
You do not need tables at all, simply block elements with the right styles.
If it was my website, I would do this:
<header>
<h1>This is the blog title</h1>
<time datetime="2012-01-23">1/23/2012</time>
</header>
Combined with this CSS:
header {position:relative; width:100%; font:25px Arial,Helvetica,sans-serif;}
header > h1 {margin:0px;}
header > time {display:block; font-size:12px; text-align:right;}
You can decide if you want to use HTML5 elements, or general elements and if you want to hook in class names or not. Here's the jsFiddle for above: http://jsfiddle.net/sKFzA/13/
Something like this? I hope i got you right.
HTML:
<div id="titleRow">This is the blog title</div>
<div id="dateText"><span id="spandate">1/23/2012</span></div>
CSS:
#titleRow{width:80%; height: 25px; font:25px Arial,Helvetica,sans-serif;
float:left;text-align: left;}
#dateText{width:20%; height: 25px; font-size:12px;float:left; text-align: right; position: relative;}
#spandate { position: absolute; bottom: 0; right: 0;}
See here: http://jsfiddle.net/sKFzA/31/
Related
I am in the process of making my own website, and I am making it out of pure HTML. I encountered in the making of the page, as I will describe below.
Here's my code for reference :-
<head>
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
<style>
.sideDiv {
border: 1px outset black;
background-color: white;
text-align: center;
width: 120;
height: 400;
}
</style>
<style>
.mainDiv {
border: 1px outset black;
background-color: white;
text-align: left;
width: 400;
height: 300;
}
</style>
<img src="AyushLogo.png" alt="logo" height="9.2%" width="9.2%" style="float:left">
<br>
<a><button>About Me</button></a>
<a><button>Games</button></a>
<a><button>My Blog</button></a> <br><br>
<hr>
</head>
<body>
<div class="sideDiv">
</div>
<div class="mainDiv">
<p>Hi,<br>My name is Ayush Bhatt.<br><br>I love to code and remake old games. You can view some of my games by clicking on the 'Games' button on the top bar.</p>
</div>
</body>
</html>
The output looks like this :-
I wanted the tag with the "mainDiv" properties to appear at the side of the one with the "sideDiv" properties, but it just doesn't want to.
PS : I want to use only HTML as long as possible
An important thing about <div> tags is that they are known as "block-level" elements, which in particular means that they always start on a new line and take up the full width available, regardless. With this in mind,
writing
<div class="sideDiv"></div>
<div class="mainDiv">
...
</div>
should result in a div with class sideDiv and width as defined in the class, and then a new div with class mainDiv started on a new line, as block-level elements do by default, though note that this is simultaneously also because the div with class sideDiv takes up the remaining width on the page as a block-level element (though its content width is as described in the class, it being a block-level element is a bit like it "reserving" the rest of the width even though its content only uses the amount defined), so the next element (block level or inline) can only start on at least the next line.
If you want to circumvent this behavior, there are many ways to do it. One is by using an external tool like bootstrap, as pointed out by another answer, but my favorite is to simply use flex box. This can be done for your code in this way
<div style="display: flex; flex-direction: row;">
<div class="sideDiv"></div>
<div class="mainDiv">
...
</div>
</div>
A method that directly overwrites the block-level property would be to set the style display: inline-block; for both divs, to prevent either from starting on a new line or taking up the whole available width by default. (Just one isn't enough, if you only set it on the first one, the second still starts on a new line by default, and if you only set it for the second one, the first still takes up all available width by default). However, this causes the element to be treated completely as an inline element besides the fact that block-level height and width can be applied, and can be strange/difficult to maneuver as a result. It is often easier to just use a flex box. Code for this would be
<div class="sideDiv" style="display: inline-block;"></div>
<div class="mainDiv" style="display: inline-block;">
...
</div>
However, note that <p> is also a block-level element, so directly substituting in your original code in the mainDiv div would still cause it to skip a line before displaying. Again, it is usually easier, more modern, and better looking to just use a flex box.
Edit: Added the detail about block-level elements taking up all available width, and fixed the incorrect initial method that changed the display property to overwrite the block-level property by setting display: inline;. This can work, but it will ignore the heights and widths of the <div>s.
try using bootstrap , it deals with layout perfectly , here is an example :
<div class="container">
<div class="row">
<div class="col-md-6">
this is the left section
</div>
<div class="col-md-6">
this is the right section
</div>
</div>
</div>
for more details check :
https://getbootstrap.com/docs/5.0/layout/grid/
NOTE : you will need to include bootstrap and jQuery libs , check for online tutorial to start using bootstrap
I have an article page that I am making small CSS changes, such as margin and font size, to. My code has to be able to be supported by Internet Explorer 8 and above. The problem is, I am using some CSS selectors that IE8 does not support. How do I write my CSS code without using the :not selector?
HTML for sample article page
<div class="entry">
<h3 class="social-title>Share This Article </h3>
<div class="social-content>
<table>
<td><img class="" src="twitter.png"><span class="">Twitter</span></td>
<td><img class="" src="facebook.png"><span class="">Twitter</span></td>
</table>
</div>
<!-- The article would start here -->
<p class="category_row"><h1 class="category-title>Lifestyle</h1></p>
<p style="margin: 0in 0in 0.0001pt; line-height: 13.5pt; vertical-align: baseline;"><img alt="" style="float: left;" src="example.jpg">Article goes starts here...</p>
<p style="margin: 0in 0in 0.0001pt; line-height: 13.5pt; vertical-align: baseline;">Second paragraph</p>
Third paragraph
</div>
CSS I am using
.entry p:not(.category_row) {
font-size: 14px;
line-height:22px;
}
img (margin: 10px)
So far example, if I wanted to add margin to the image that is in the article section, how would I write the CSS code so that it only affects the image in the article section and not the images in the <div class="social-content">? Without using :not?
Also how would I write CSS code to change the font-size of the article to a font size of 14px and line height of 22px? Without affecting everything else above (not in the article section) ?
Sorry if this is confusing, but I will clarify more if need be!
You will need to be more verbose if you want to support older browsers. The joy of the newer syntaxes is we are able to be more pithy, but if you have IE 8 in your supported list of browsers, you'll need to start with styling more general selectors and then overriding those styles in more precise selectors.
.entry p {
font-size: 14px;
line-height:22px;
}
.entry p.category_row {
font-size: XXpx;
line-height:XXpx;
}
I don't know where your article section begins from your markup. Figure out what is the most logical container for image would be, and then constrain your selector with it. Note article is an HTML5 element, so you would be remiss not to use it:
<article>
<img ... />
</article>
And article images would be styled with this simple selector: article img { ... }
If you want to use article with IE 8, be sure to include this: https://code.google.com/p/html5shiv/
Why don't you wrap the actual content in it's own div?
<p class="category-row">....</p>
<div class="post-content"><!--- maybe use article tag here -->
<p>First paragraph....</p>
<p>Second Paragraph</p>
</div>
Then you can just reference the p's like
.entry .post-content p {
....
}
Also, "category row" doesn't look like it should be a paragraph?
If it is a "row" a div or a span would be more appropriate.
If it contains nothing but the h1 you might as well scrap it and leave the h1 be there without a wrapper.
If you use the article tag (or any of the other new html5 semantic tags) include html5shiv, as Chris said in his answer.
I'm coding a quick site for a school project and I'm extremely rusty hangs head in shame. I can't seem to get the width to affect the tags or center the text in them.
I have no idea why but if you could explain to me what I have to do to get the links on the nav bar centered and for the image to sit atop the loading text and act as a title. The loading should be centered btw but calc() doesn't like me ;~;
http://jsfiddle.net/MasterWhipper/6Z6z6/
<div id="navBar">
<table id="navText">
<tr>
<td><div class="navTd">Home</div></td>
<td width="396px">Loading</td>
<td><div class="navTd">Donate</div></td>
<td><div class="navTd">About Us</div></td>
</tr>
</table>
<img src="http://i.imgur.com/Y7OE90F.png" class="titleIMG"/>
</div>
Note: would it be easier to use a list for what I'm trying to do?
the best idea it is to use the HTML5 nav tag, it has been thought for navigations menu as yours:
<nav>
Home |
Loading |
Donate |
About us
</nav>
It is a solution that fits with your problem and plus it is the current standard! You will not have problem with CSS.
Here you are more info about HTML5 nav tag:
http://html5doctor.com/nav-element/
first things first...you don't need <div class="navTd"> inside a <td>
Assign everything to <td> and that would do.
Then, instead of using calc (its still not cross-browser), use display:inline-block; to allocate width to the td
#navText > td {
display:inline-block;
z-index: 3;
text-align: center;
}
working demo
Instead of using table structure use ul, li tags for navigation... You can easily style them :)
<div id="navBar">
<ul>
<li>Home</li>
<li>Loading</li>
<li>Donate</li>
<li>About Us</li>
</ul>
<img src="http://i.imgur.com/Y7OE90F.png" class="titleIMG"/>
</div>
Once again, people are professing their hatred of table. Don't worry about it.
Here, I made it work with tables. Three things I changed:
Set the table to table-layout: fixed;
Put your navTd class on the td, not the div. Otherwise you're telling the div to take a certain size of the td, not the td to take a certain size of the table
Removed the px to 396px. The width attribute does not use CSS syntax. You may want to use CSS for the width though, just to be more HTML5-zealous-friendly.
http://jsfiddle.net/6Z6z6/3/
I've read that headers shouldn't be used as subtexts to other headers for HTML5 in order to have a nice outline.
for example:
<h1>Frustration</h1><br />
<h2>The life of developers</h2>
Rather, it could be written like this instead:
<h1>Frustration
<br />
<span style="font-size: 0.67em">The life of developers</span>
</h1>
The problem is that I have no control on the line-height of the subtext. It takes on the line-height of the H1 and that always seem a little too far.
I realize could do something like:
h1+.subtitle
to target the same thing, but I'd just like to know whether there is any way for the second option above to let me manipulate a paragraph with two different line-heights.
EDIT:
I'm using the latest version of Firefox.
As I continue to look for a solution, I'm beginning to wonder if this is a silly question to be asking, seeing as the browser has no reason to think the user would want separate line-heights within the same tag--especially when there are alternatives like using block elements with a negative margin-top.
You could do this:
<h1>
<span class="mainText">Frustration</span>
<span class="subText">The life of developers</span>
</h1>
h1 .mainText {
display: block; /* this prevent the need for the <br/> */
/* additional style for the main header text */
}
h1 .subText {
/* styling for the subtext */
}
You could also do this (which is easier I guess):
<h1>text
<div>subtext</div>
</h1>
h1 div {
font-size: 0.67em
}
The subtext will have a lower line height. See this jsfiddle for the latter one: http://jsfiddle.net/wLD35/
I really need help converting this simple table structure to css div's. Is there a simple application or something. CSS divs are driving me insane.
<table width="100%" border="0">
<tr>
<td colspan="3" id="Header"></td>
</tr>
<tr>
<td width="20%" id="Nav2"></td>
<td width="40%" id="ContentMiddleLeft"></td>
<td width="40%" id="ContentMiddleRight"></td>
</tr>
<tr>
<td colspan="3" id="BottomContent"></td>
</tr>
</table>
Test this:
EDIT: http://jsfiddle.net/DCbgg/
<style type="text/css" media="screen">
.container {
width: 100%;
}
.left20 {
width: 20%;
float: left;
background: red;
}
.left40 {
width: 40%;
float: left;
background: green;
}
.clear {
clear: both;
}
</style>
<div class="container">
<div class="left20">
Left
</div>
<div class="left20">
Left
</div>
<div class="left40">
Left
</div>
<div class="clear"></div>
<div class="left20">
Left
</div>
<div class="left20">
Left
</div>
<div class="left40">
Left
</div>
</div>
<div class="clear"></div>
First Things First
There are some very important things to remember when changing from table to div layouts that generally apply to all new learning experiences.
Don't get frustrated just because something's not working. Just take a break, look at something else, remember that it's something new and it won't always work the first time. It may take a number of different approaches and attempts before it finally works. You'll get the hang of it eventually.
Especially in this case, remember that divs are vastly different from tables, especially when using them as a major structural part of a site. The thought process behind each is completely different and can take a lot of getting used to for it to click. Because of this:
Not all designs transfer from table to div. Some things are only really easily and properly accomplished with tables, and others with divs. While this is not one of those cases in particular, be open to having to make some design changes when changing your site structure.
That being said, we can now answer the question properly.
An Answer
This is how I would set up the structure using divs:
<div id="wrap">
<div id="header"></div>
<div id="content">
<div id="nav2"></div>
<div id="content_right"></div>
<div style="clear: both"></div>
</div>
<div id="footer"></div>
</div>
And this is what the css would be:
#wrap {
width: 100%;
}
#header {
width: 100%;
height: /* some height value */;
}
#content {
width: 100%;
height: /* some height value */;
}
#nav2 {
width: 20%;
float: left;
}
#content_right {
width: 40%;
float: right;
}
#footer {
width: 100%;
height: /* some height value */;
}
Notes
As I was saying above, the thought process behind tables and divs are quite a bit different. With tables, you're used to using percentages (%) to define widths. This works and is not necessarily a bad thing. It gets the job done and is easy to do.
But when you're using divs, a more common approach is to have fixed widths defined by pixels (px). This allows for a more constant design that won't stretch across the page, and gives you more design freedom, knowing that the page will always be the same width.
For more information on fixed-width design, I would recommend looking at the 960 grid system. It is extremely easy to follow and leads to clean, good-looking designs that are easy to structure and style.
Most importantly, enjoy the new-found freedom that divs bring. They aren't locked in to anything and can literal do anything, be anywhere, and look like anything on a page. There isn't really a limit to what they can do. I've heard them called the only required part of a webpage (You really can design and create an enter page with just divs, text included).
Have fun on your journey!
CSS3 Style (using "display:table;"):
Check http://jsfiddle.net/reKfe/ (jsFiddle made from http://query7.com/exploring-the-power-of-css3)