How do I get a better grasp of layout using only CSS - html

h1, p {
font-family: futura; sans-serif;
margin: 20px 20px 0px 20px;
}
.section {
background-color: tomato;
padding: 50px;
margin: 0 auto;
}
.container {
background-color: white;
padding: 5px;
margin: 10px 20px;
text-align: center;
display: inline-block;
height: 250px;
}
<div class="section">
<div class="container">
<h1>This is a title</h1>
<p>This is the body copy for the section in which we read more about the aforementioned subject matter within the title.</p>
</div>
<div class="container">
<h1>This is another title</h1>
<p>This is the body copy for the section in which we read more about the aforementioned subject matter within the title.</p>
</div>
<div class="container">
<h1>This is a third title</h1>
<p>This is the body copy for the section in which we read more about the aforementioned subject matter within the title.</p>
</div>
</div>
I am trying to improve my ability to organize content with CSS. It's something I'm really struggling with and I've read a lot of different articles and watched all the videos but it's not clicking just yet. I would like to understand how to do so in a way that incorporates responsive design ideals in addition to not relying on flex box. Flexbox is the next step but I want to master this stage first.
I practiced adding styles to a simple three-card section of content. I was able to get to the point where I have the boxes lined up horizontally. Now there are a couple of issues.
The cards won't align on the same row unless I give a max width, which I'm not sure is best practice for responsive design.
The .container content won't center within the .section class. I've done 'margin: 0 auto;' but it won't affect it.
I've tried adding a relative position property to the .container class as well but that didn't seem to matter.
When I size the viewport to mobile view, the text spills over the bottom of the white container. How do I get the height of the container to adjust to the content within?

Try to use css flex features. Example:
h1, p {
font-family: futura; sans-serif;
margin: 20px 20px 0px 20px;
}
.section {
background-color: tomato;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.container {
width:80%;
background-color: white;
text-align: center;
padding: 5px;
margin: 10px 20px;
}
<div class="section">
<div class="container">
<h1>This is a title</h1>
<p>This is the body copy for the section in which we read more about the aforementioned subject matter within the title.</p>
</div>
<div class="container">
<h1>This is another title</h1>
<p>This is the body copy for the section in which we read more about the aforementioned subject matter within the title.</p>
</div>
<div class="container">
<h1>This is a third title</h1>
<p>This is the body copy for the section in which we read more about the aforementioned subject matter within the title.</p>
</div>
</div>

Your query states that you want to learn style techniques prior to Flexbox, so I dug up an antique prototype that I built in 2011 following this tutorial by Christopher Schmitt http://christopher.org/css-floats-to-display-columns-in-any-order/, and I posted it as a Pen for you to see.
https://codepen.io/cwebba1/pen/agxMzQ
<div><p>Check it out Here</p></div>
I also put it here.
Christopher Schmitt's original tutorial is still up and he has a link to a book that he wrote. You can see from his tutorial that he uses floats, inline-blocks, and percentage widths for layout, and negative margins to control the order of content display on the page. I am in the midst of redesigning my webpage and I still use this pattern.
Also, when I first started learning CSS I found a small inexpensive book, CSS in Easy Steps. It will lead you through the Cascade and styling elements quickly.
As to your projet, I recomend using persentages to set widths, and then changing percentages and adding inline-block to wider media-query layouts.

Related

Make a div responsive like a svg (text scaling down)

So I've been looking for days and can't find a way to do that.
I made a WordPress site, and my homepage is displaying the most recent posts I've made.
Thing is, the content of a post is written with some specific layout, and it is meant to stay that way. The layout cannot change! I need the text to stay in the exact same place, even if it becomes way smaller and less readable when looking at the site on a smartphone.
Of course, the site is responsive. Only thing I don't want to be responsive is the content of a post.
I've tried a lot of things, but I can't find a way to make it... This page shows exactly what I'm looking for, except that I don't want the post to be re-sizable by the user. I just need it to fit the parent div.
.responsive-wrapper {
border: 3px solid black;
}
.post-content {
position: relative;
width: 100%;
height: 100%;
padding: 10px;
}
.txt-top-right {
display: block;
float: right;
margin-left: auto;
padding-right: 20px;
}
.txt-under-top-right {
clear: both;
}
.txt-center {
text-align: center;
}
.post-content img {
display: block;
max-width: 200px;
float: right;
padding-right: 20px;
}
<div class="responsive-wrapper">
<div class="post-content">
<p class="txt-top-right"><em>Div right-top corner</em></p>
<p class="txt-under-top-right">
Div following the one on right-top corner
</p>
<div class="txt-center">
<h2>h2 in the middle of the page</h2>
<h1>h1 in the middle of the page</h1>
<h3>h3 in the middle of the page</h3>
</div>
<img src="http://cs.pes.edu/wp-content/uploads/2016/06/default.jpg" />
<p>
paragraph<br> on the left side<br> of the page<br> describing<br> a lot of things<br> on a lot of lines
</p>
<p>
Adress<br> City<br> Country
</p>
</div>
</div>
I understand HTML/CSS/JS, but I am struggling with JavaScript... I can insert some <script> tags in my html header with functions in it, but I have hard times understanding what these functions do.
I'd like the layout to stay the exact same way, doesn't matter which device the user is on. For instance, I want the .txt-top-right paragraph to take this amount of space. On a smartphone, the font size shouldn't re-size and fill up more than this amount of space of the container...
This is an example of what is happening right now when re-sizing the window. The layout changes completely !
Thank you for reading, hope it was clear enough :)

html/css how to calculate margin-top to be 100vh - block height

A html section (content + top margin) should take up whole viewport.
There is no other margins except top. Section is not going to have much content in it
This could be achieved by setting something like
height:60vh
margin-top:40vh
But the problem is, section height may change due to viewport width change or content amount
So i need some solution for margin to be calculated automatically. Something like margin-top: 100vh-height.
Context
there is bunch of sections directly in body that are located vertically, some of them should be like i described ( a horizontal line that takes 1/3 - 2/3 of vp height, some of them take 100vh and don't have a vertical margins.
body have a
display:flex;
flex-direction:column;
on it
Notes
I know that it is possible to do by
1) Wrapping everything in unnecessary level of divs
2) Using js
But I want to avoid those solutions and keep html as minimalistic as possible. So I need a way to calculate margin or other changes to css to achieve this (suggestions about changing approach to layout in general (but only css) are welcome)
clarifying image
body { display:flex; flex-direction:column;}
body > section { min-height:vh; margin:0; background-color: #e13652;}
.sectionHorizontal { min-height:10em; margin-top:60vh; }
<section class="sectionHorizontal">
<p> horizontal section with some content, it should take part of a viewport and the rest should be filled by margin </p>
</section>
<section>
<p> another section. but this one takes 100% of viewport</p>
</section>
<section class="sectionHorizontal">
<p> another horizontal section, that have more content and is higher than the first one</p>
</section>
More notes
I'm asking not how to make a section 100vh but how to make section + empty space before it =100vh. Or how to move stuff inside it
I wanted to keep example as simple as possible, but actually section has background and itself may contain not only one but several different elements that have their own positioning
I'm still not sure if I understand your problem correctly or not, but have a look at the following. If you can clarify further, that'd be helpful.
section {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
display: flex;
flex-direction: column;
height: 100vh;
}
section.horizontal {
justify-content: flex-end;
}
<section class="horizontal">
<p> horizontal section with some content, it should take part of a viewport and the rest should be filled by margin </p>
</section>
<section>
<p> another section. but this one takes 100% of viewport</p>
</section>
<section class="horizontal">
<p> another horizontal section, that have more content and is higher than the first one</p>
</section>
Assuming your example code this would be the only way possible without changing the html.
* {
margin: 0;
padding: 0;
}
body {
display:flex;
flex-direction:column;
}
section {
margin-top: 100vh;
height: 0;
}
section p {
background: #ccc;
transform: translateY(-100%);
}
section:not(.sectionHorizontal) p {
height: 100vh;
}
<section class="sectionHorizontal">
<p> horizontal section with some content, it should take part of a viewport and the rest should be filled by margin </p>
</section>
<section>
<p> another section. but this one takes 100% of viewport</p>
</section>
<section class="sectionHorizontal">
<p> another horizontal section, that have more content and is higher than the first one</p>
</section>

Right-aligning a div under centered text

I have my main content div, and I want to create a title that is centered in this parent div, and then have a subtitle underneath that is right-aligned to the edge of the centered text. I've been having a real difficult time getting this to work, and all the examples I can find on this site and others seem to get messed up when the top text is centered (as opposed to, say, left-aligned to the parent div).
It should look like this (where App Title is centered in a larger div):
The closest I can find is this question (Right-align two divs) but it seems that float: left is very important to making this work, which doesn't seem to work for my case. Other solutions seem to involve display: table but I was struggling to get the effect I want.
The title div needs to be variable width and I don't need to support ancient browsers. Thanks for any help!
Here are 2 ways, one using flex and the other using inline-block. Basically create a container for the titles, center that container, then align the subtitle to the right.
* {margin:0;}
h1 {
font-size: 3em;
}
.flex {
display: flex;
justify-content: center;
}
.flex h2 {
text-align: right;
}
.ib {
text-align: center;
}
.ib header {
display: inline-block;
}
.ib h2 {
text-align: right;
}
<section class="flex">
<header>
<h1>app title</h1>
<h2>subtitle</h2>
</header>
</section>
<section class="ib">
<header>
<h1>app title</h1>
<h2>subtitle</h2>
</header>
</section>

Section below another doesn't go directly underneath the one above

I have these three sections that have three sections underneath them and I would like for the ones below to follow the ones directly above them. For example in this codepen the second section below does not go directly underneath the second section above, because the first section above is a little longer.
codepen.io/anon/pen/apEQRE
Is there a way to fix that?
Not easily without changing your HTML structure. You could float the elements to the left rather than displaying them as inline-block, however, the element on the bottom left would have the same problem. You could display them as flex, but that would cause problems with the way the text is formatted.
What I would recommend is diving the sections into three defined columns:
<div class="column">
<section>
<h2>Title</h2>
<p>Text</p>
<p>Text</p>
</section>
<section>
<h2>Title</h2>
<p>Text</p>
</section>
</div>
Then you would need to slightly change your CSS to cover this:
.column {
float: left;
width: 33.33%;
}
section {
text-align: center;
background-color: white;
width: 90%;
padding: 15px;
border: 1px solid #007d87;
margin-bottom: 5px;
opacity: 0.8;
display: inline-block;
text-align: left;
vertical-align: top;
}
I've created a new pen showcasing this.
Note that your example has identical sections, but in practical application, you'll need to re-order the sections so that they read left to right, rather than top to bottom.
Hope this helps!

2 div columns: fixed and liquid. Fixed one must be removable. Liquid one must be the first in code

Consider the following 2 cols html structure:
<div id="container">
<div class="left">some text</div>
<div class="right">some text</div>
</div>
CSS:
#container { overflow: hidden; }
.left { float: left; width: 200px; background: red; }
.right { overflow: hidden; background: green; }
The same code in jsFiddle - http://jsfiddle.net/vny2H/
So we have 2 columns. The left column width is fixed, the width of the right one is liquid.
If we remove the left column from html, the right column stretches to 100% of parent #container width.
The question is: can we change the order of the left and right columns?
(I need it for SEO)
<div id="container">
<div class="right"></div>
<div class="left"></div>
</div>
Thanks.
Added
There's one interesting method to reach what I want, but fixed column becomes not removable. The method is based on negative margin. http://jsfiddle.net/YsZNG/
HTML
<div id="container">
<div id="mainCol">
<div class="inner">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div><!-- .inner end -->
</div><!-- .mainCol end -->
<div id="sideCol">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div><!-- .sideCol end -->
</div><!-- #container end -->
CSS
#container { overflow: hidden; width: 100%; }
#mainCol { float: right; width: 100%; margin: 0 0 0 -200px; }
#mainCol .inner { margin: 0 0 0 200px; background: #F63; }
#sideCol { float: left; width: 200px; background: #FCF; }
So we have 2 ways:
Using "float" for the fixed column and "overflow: hidden" for the liquid. Fixed column becomes removable. But liquid one goes second in code.
Using negative margin. Liquid column goes first in code. But fixed one is not removable.
Is there a third way, when fixed column is removable and liquid one is the first in code?
Added
Half-decision has been suggested by #lnrbob. The main idea - using table-like divs. http://jsfiddle.net/UmbBF/1/
HTML
<div id="container">
<div class="right">some text</div>
<div class="left">some text</div>
</div>
СSS
#container { display: table; width: 100%; }
.right { display: table-cell; background: green; }
.left { display: table-cell; width: 200px; background: red; }
This method is suitable, when a fixed column is placed to the right in a site. But if we need it to the left - it seems to be impossible to do this.
Consider the semantics of the content you are marking up before anything else, that will almost always lead to a solution that has both decent markup and is search engine friendly.
For instance, is .right the main content of the page, and .left some supplementary information or navigation? In that case, mark it up as such and the search engines will do a good job of interpreting it the way you want them to. HTML5 provides many elements for just this purpose:
<div id="container">
<nav>
<ul>
<li>Home</li>
<li>etc.</li>
</ul>
</nav>
<article>
<h1>My nice, juicy content</h1>
<p>Cool stuff, huh?!</p>
<article>
</div>
Or for supplementary content you might consider <aside> or simply <div role="supplementary">.
Google will happily scrape that and recognise the difference between the navigation and the actual content, the idea that source order is important no longer applies to SEO in the same way it did a few years ago.
Because your elements have same height you can do this:
#container { overflow: hidden; position:relative; }
.left { float: left; width: 200px; height: 200px; background: red; position:absolute; top:0; left:0; }
.right { overflow: hidden; height: 200px; background: green; margin-left:200px;}
Fiddle page: http://jsfiddle.net/Ptm3R/9/
I still think that this is a rather pointless endeavour, because the only reason to try is for dubious SEO benefits. But, I've been dragged back to this question so many times that I'm going to bring something to the table.
If I was forced on pain of death to come up with a pure CSS solution, this is it - but I don't recommend it:
http://jsfiddle.net/RbWgr/
The magic is transform: scaleX(-1);. That's applied to .container to flip the visual order, and then also to the child divs so that the content of each div is not flipped.
It won't work in IE7, because I'm using display: table-cell.
It's not so hot in IE8 - any text looks horrible, as is usual with filters. But, it does work.
Extra div wrappers were required to make it work in Opera - and the text doesn't look perfect.
It works fantastically in other modern browsers (IE9, Chrome, Safari, Firefox), but applying transforms to a parent of "every element" might have unforeseen consequences.
To be honest, I'm not sure why you're boiling it down to having to use only two id's ( [#left / #right] OR [#mainCol / #sideCol] )...
Would it not be far easier to use the mainCol/sideCol solution you had in JSFiddle at http://jsfiddle.net/YsZNG/ and introduce a third class that could be applied to the main div in the absence of the sideCol programmatically.
As in http://jsfiddle.net/biznuge/aAE3q/4/
Sorry. I may well have missed the point of all this, but I've had previous gut wrenching agony with trying to work in fluid/fixed mixture sites so just thought I'd share my own feelings on the matter...
UPDATE
I provided a second answer to this now that I think works. Sorry to double answer but it seemed sufficiently different from my initial response that I thought it would stand on its own two feet.
Depends on your browser support requirements. For IE8 and above (and all modern browsers) you could use display: to set a table layout (still using your <div />'s of course.)
Here is an example -I've only added javascript so you can toggle whether the element is hidden or not easily :)
How about putting the left col inside the right one at the bottom?
http://jsfiddle.net/vny2H/32/
http://jsfiddle.net/biznuge/aAE3q/12/
this seems to satisfy the brief I think. Works in FF anyway, but I'm unsure how other browsers might react to the table type display attributes.
UPDATE
Have tested this in FireFox(4), IE(9), Opera(11), Safari(5)[Win] and Chrome(12) and the layout seems to be robust across all browsers.
Rather surprising really...
UPDATE FOLLOWING CLARIFICATION
thanks to #thirtydot for that
http://jsfiddle.net/biznuge/aAE3q/19/
Works ONLY in Firefox 4 as far as I can tell, after some brief checking... But it's a start...