Jump to content

Secrets of CSS- Responsive Layouts


tesseraltyme

Recommended Posts

After following the tutorials for CSS and HTML, there are still unanswered questions when it comes to putting it all together. From the tutorials, you learn that each CSS declaration follows this pattern (be careful for every keystroke and line feed)

selector1,

selector2,

selector3 {

style designator1:style1;

style designator2:style2;

style designator3:style3;

style designator4:style4;

}

You can also use a single line, although it is harder to read and remember what it means.

selector1, selector2,selector3 {style designator1:style1;style designator2:style2;style designator3:style3;style designator4:style4;}

This article will lend some insight into the inner thoughts of designers for Wordpress themes, specifically twentytwelve theme using the top navigation menu as the example.

CODE EXECUTION:

All code is executed sequentially. This means that if you declare a style higher up on the page, then you can undeclare it lower down. It also means that you can declare different styles in a top-down approach of increasingly narrower specificity. For instance, begin at the top and select all Object types for the most general style. Then select a subset of those types to single out for a different style. Continue to the most specific selector last.

SELECTORS

Selectors can be declared by any of three designator types (in increasing specificity)

Object Type (object)

Group Name (class)

Individual Name (id)

and combinations of designators using container-within-container approach.

Individual Name Object Type (id object) #this-id div{}

Group Name Object Type (class object) .this-class a{}

Individual Name Group Name Object Type (id class object) #this-id .this-class a{}

Other selectors follow a train of descendents and ancestors to arrive at the selection group.

.class1 .class2 .class3 object

(this part is a little fuzzy because sometimes you need the ancestors, and other times you don't)

 

Finally a special trick for navigation menus

.sub-menu a:hover > ul {display:hidden;} /*hide the hover feature for smartphones/

For hover mode on a sub menu item you wish to hide it until you hover over its parent. Later you will want to use 'display:inline-block;' or 'block;' for mobile devices since hover does not work on these devices.

OVERRIDE A STYLE

 

When you declare a style for a selector and then want to remove part of that style, you can simply declare it again and use the style 'none'.

Example: Begin by setting color, background color, font and other major styles that will permeate all elements regardless of screen size. Then repeat the selector for the smaller screen and change only those styles that are to be changed.

.main-navigation a{color:white;background:blue; font-weight:normal;font-size:12px;}

Minimal code to get the job done requires both a top-down sequence and an outer-to-inner selection strategy for selectors and styles.

RESPONSIVE LAYOUT BASED ON SCREEN SIZE

 

To get css to respond to screen size, use this command

@media screen and (min-width:600px){

/*code styles here for all screens larger than 600 pixels*/

}

PUTTING IT TOGETHER

 

Note: To follow this code example, create a child theme and copy the style.css file from twentytwelve into the child theme. This way you will retain your style with any theme updates.

 

twentytwelve theme uses top-down to designate navigation for smart phone styles first and then overrides these styles for screens larger than a minimum width. It begins with showing the Menu button for smartphones.

 

 

/* Navigation Menu above other navigation groups/.main-navigation {/*set styles for navigation container) margin-top: 24px; margin-top: 1.714285714rem; text-align: center;}.main-navigation li { /*set display for all navigation link items/ margin-top: 24px; margin-top: 1.714285714rem; font-size: 12px; font-size: 0.857142857rem; line-height: 1.42857143;}.main-navigation a { /*set text color for all links in the menu/ color: #5e5e5e;}.main-navigation a:hover,.main-navigation a:focus {/*set hover text color for all links in the menu/ color: #21759b;}.main-navigation ul.nav-menu,.main-navigation div.nav-menu > ul {/*hide menu for smartphones—overridden later/ display: none;}.main-navigation ul.nav-menu.toggled-on,.menu-toggle {/*display toggle button for smartphones—overridden later/ display: inline-block;}

/* Minimum width of 600 pixels. Now override styles for all screens larger than smartphones*/@media screen and (min-width: 600px) {

 

.main-navigation ul.nav-menu, .main-navigation div.nav-menu > ul {/*style for all top level links/ border-bottom: 1px solid #ededed; border-top: 1px solid #ededed; display: inline-block !important;/*this displays the top level navigation links/ text-align: left; width: 100%; } .main-navigation ul {/*new style for link containers/ margin: 0; text-indent: 0; } .main-navigation li a, .main-navigation li {/*display all links/ display: inline-block; text-decoration: none; } .main-navigation li a {/*re-style all links/ border-bottom: 0; color: #6a6a6a; line-height: 3.692307692; text-transform: uppercase; white-space: nowrap; } .main-navigation li a:hover, .main-navigation li a:focus { /*change text color for hover/ color: #000; } .main-navigation li {/*re-style margins and locations for navigation links/ margin: 0 40px 0 0; margin: 0 2.857142857rem 0 0; position: relative; } .main-navigation li ul {/*re-style sub-headings for hover/ margin: 0; padding: 0; position: absolute; top: 100%; z-index: 1; height: 1px; width: 1px; overflow: hidden; clip: rect(1px, 1px, 1px, 1px); } .main-navigation li ul ul {/*re-style sub-sub-headings for hover/ top: 0; left: 100%; } .main-navigation ul li:hover > ul, .main-navigation ul li:focus > ul, .main-navigation .focus > ul {/*display settings for hover elements/ border-left: 0; clip: inherit; overflow: inherit; height: inherit; width: inherit; } .main-navigation li ul li a {/*re-style sub-heading elements for normal screens/ background: #efefef; border-bottom: 1px solid #ededed; display: block; font-size: 11px; font-size: 0.785714286rem; line-height: 2.181818182; padding: 8px 10px; padding: 0.571428571rem 0.714285714rem; width: 180px; width: 12.85714286rem; white-space: normal; } .main-navigation li ul li a:hover, .main-navigation li ul li a:focus {/*re-style hover colors for sub menu items/ background: #e3e3e3;/*—example of more selective elements to override/ color: #444; } .main-navigation .current-menu-item > a, .main-navigation .current-menu-ancestor > a, .main-navigation .current_page_item > a, .main-navigation .current_page_ancestor > a {/*display style for primary

color: #636363;/*element link same as for current link /

font-weight: bold;/*so you know which primary link was selected/ } .menu-toggle { /*turn off the menu button for normal screens/ display: none; }

}

 

I hope this article helps you code better CSS.

Link to comment
Share on other sites

  • 2 weeks later...

You know, I started writing this thinking you needed help!! I got to the part where you said "it was still kind of fuzzy to you". I apologize!! Yet, I decided to continue on and post this. It took me a while to write! so I am posting it!! lol

 

I too hope this helps Someone!! Anyone!!

 

 

First, let me say this: KEEP IT SIMPLE!!!!

 

 

CSS Syntax selector { css-property: value; css-property: value;} You can group Selectors using a combinator, such as a comma which means "AND". An empty space means something all together different!! It implies descendancy. Check out this Video: YOUTUBE (See attached infographic) Okay, let's move along.

All code is executed sequentially. This means that if you declare a style higher up on the page, then you can undeclare it lower down. It also means that you can declare different styles in a top-down approach of increasingly narrower specificity. For instance, begin at the top and select all Object types for the most general style. Then select a subset of those types to single out for a different style. Continue to the most specific selector last.

Executed...... sequentially.... Not so sure I would say it that way. But, the web browser has a mechanism called the "Cascade". The Cascade is meant to decide which Style will be applied should there be conflict.. .... Conflict among Styles means there is more than one Style that targets the same element and has the same Media Type. Instead of the word Executed, the W3C CSS2.1 specs say,"Once a user agent has parsed a document and constructed a document tree, it must assign, for every element in the tree, a value to every property that applies to the target media type." (go to http://www.w3.org/TR/CSS2/cascade.html to read all about the Cascade.) What you are actually referring to is the Cascade of Styles. But, instead of "undeclaring a style", we can Override a Style. Yes, the order in which the Styles are specified on the External Style Sheet (or any type of style sheet) plays a role in determining which styles get applied, assuming there is conflict (or you are trying to override a style). But there are other factors that determine how the Styles Cascade, and which get applied should there be conflict. (Inline Styles override Internal & External Styles; Specificity: the most specific Selector will usually win; As will !important declarations. Author Styles usually override Browser Styles, etc) Object Types... this is not quite the correct terminology. I think you really mean to say Elements or Tags. (You must be a coder using the DOM!) And it is true, we typically specify more general styles initially, and later on override those styles with increasing Specificity.... to some degree. EXAMPLEFor instance, CSS Inheritance can really save a lot of time when writing CSS. Inheritance is another factor that plays into the Cascade of Styles. A perfect example is styling the <body> of the web page.......................... To do this, we use a Tag or Element Selector, which is General to some extent. body { font-family: Arial, Helvetica, sans-serif; } The CSS Property, font-family, is a property that can be inherited from Parent Elements, or Ancestors in the Document Tree. CSS Inheritance is the process in which certain CSS Properties can be passed down from Parent to Child, or Ancestor to Descendant. Thus, by setting the font-family on the body style, EVERY ELEMENT on the Page will Inherit that font-family. Soooo.... we only need to specify that font-family one time via the body style, UNLESS we want to override that font-family as we likely would when styling heading tags. Thus, my next style would be the following........ h1, h2, h3, h4, h5, h6 { font-family: Oswald, sans-serif;} This style is still quite general, but it will override the font-family that is passed down from the body, causing all heading tags to have Oswald as their font. Once we get the Layout made, we can make our Heading Styles much more specific thereby style h2 headings in different parts of the layout uniquely if we so desired. Each section of the Layout can be given an id, which we would style with an ID Selector. For instance, <div id="header"> </div> or <header id="header"> </header> #header { height: 100px;} Notice how the ID selector matches the id attribute. That is how we hook some css styles to the html markup. Now let's assume you have an h1 Heading for your onpage title, and an h2 Heading for the Tagline. We would get very specific and use the following Selectors........ #header h1 { ... }#header h2 { ... } Then, the h2 Headings in the #main-content section of the web page can be styled using the following Selector: #main-content h2 { ... } SELECTORSRemember to avoid the term "Objects". They are elements in the document tree. (The DOM is where Objects come into play.)**And there are MANY MANY different types of Selectors but Beginners need to first learn the basic ones:

  • [*]ID selectors[*]Class selectors[*]Tag or Element Selectors[*]Dynamic Pseudo Class Selectors (a:link, a:visited, a:hover, a:active, a:focus)[*]Descendant Selectors (
#header h2 is a descendant selector - meaning we are styling nested elements.)

 

 

.class1 .class2 .class3 object

(this part is a little fuzzy because sometimes you need the ancestors, and other times you don't)

 

Okay, there are times when you will use a Class selector without an Ancestor.

***Remember, class selectors can be applied to a given web page numerous times. ID selectors, on the other hand, must be unique and can be applied only one time on a given page.

 

Therefore, you will find times when you need to create a class for the purpose of applying it to a portion of an html element. This does not require the ancestor in the selector.

 

Perfect Example: I often create various styles to apply to some text or a few words in my paragraphs.

 

.bold-red {

font-weight: bold;

color: #ff0000;

}

 

This style can be applied over and over again on the same page, and it is Independent of any Ancestor. And.... I often apply that class to a portion of an html element, not the entire element.

 

<p>Lorem ipsum dolor sit amet, <span class="bold-red">consectetur adipiscing elit</span>. Nunc sit amet porttitor nibh. Ut mattis rutrum enim, sed fringilla metus semper nec.</p>

 

 

Another Class I like to create and use a lot:

 

.center {

text-align:center;

}

 

I don't like to center all of my text. That looks aweful. However, from time to time, I do like to center the text within a particular paragraph. In that case, the class .center must be applied to the <p> element and NOT to the text in that <p> element.

 

<p class="center"></p> will cause the text in this p tag to text-align center.

 

You can read more about Using Class Selectors.

 

phew... I need a nap now!!

 

Best wishes to all CSS Authors out there! And Aspiring Authors!!

post-171785-0-69812200-1406121531_thumb.jpg

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...