SASS nesting best practise? - css

Is it mandatory to nest all child element to it's parent? Please take a look at my example code. I saw some articles, they warned to nest child elements only to 4 levels. But here I wrapped all childs to it's parent. Is it ok to code sass like this format?
<div class="col-md-3 left-side">
<div class="profile-info">
<img src="img/user.jpg" alt="">
<div class="info">
<p>Header</p>
<span>2 minutes ago</span>
</div>
</div>
<div class="ads">
<h4>Advertisements</h4>
<img src="img/ad1.jpg" alt="">
<p>Grab your book !!!</p>
<img src="img/ad2.jpg" alt="">
<p>Hurry up. Limited offers !!!</p>
<img src="img/ad3.jpg" alt="">
<p>Grab your book !!!</p>
<img src="img/ad4.jpg" alt="">
<p>Hurry up. Limited offers !!!</p>
</div>
</div>
.left-sidebar{
height: auto;
background-color: #fff;
.ads{
img{
width:100%;
}
h4{
margin-top:45px;
margin-top: 45px;
font-weight: 600;
}
p{
margin-top: 5px;
font-weight: 500;
margin-bottom: 22px;
}
}
.profile-info{
#include basic_style;
padding-top: 31px;
.info{
padding-top: 28px;
padding-left: 16px;
display: inline-block;
#media only screen and (max-width: 1200px){
padding-top: 20px;
padding-left: 0px;
text-align: center;
display: block;
}
}
img{
width: 100px;
height: 100px;
border-radius: 50%;
float: left;
#media only screen and (max-width: 440px){
width: 85px;
height: 85px;
}
#media only screen and (max-width: 1200px){
display: block;
margin: 0 auto;
float: none;
}
}
p{
#include post_title;
}
span{
#include sub_header;
}
}
}

The problem of this code is that you can't use .ads or .profile-info blocks in right sidebar or somewhere else. Your code is context depended.
To improve situation you can read about BEM (block element modificator).
In your case at first you should remove .left-sidebar selector. Second, tad selectors are not good, so add class names to your inner spans, images and paragraphs.
Your code will look like:
.left-sidebar {
height: auto;
background-color: #fff;
}
.ads {
.img {
width:100%;
}
.h4 { // .h4 is just an example, write some more meaningful name
margin-top:45px;
margin-top: 45px;
font-weight: 600;
}
.p {
margin-top: 5px;
font-weight: 500;
margin-bottom: 22px;
}
}
// styles for .profile-info
But this scss generates unnecessary second level selectors like .ads .img {}. You can follow the BEM methodology to write only first level selectors.
Scss:
.ads {
&__img {
width:100%;
}
&__h4 {
margin-top:45px;
margin-top: 45px;
font-weight: 600;
}
&__p {
margin-top: 5px;
font-weight: 500;
margin-bottom: 22px;
}
}
Css output:
.ads__img {
width: 100%;
}
.ads__h4 {
margin-top: 45px;
margin-top: 45px;
font-weight: 600;
}
.ads__p {
margin-top: 5px;
font-weight: 500;
margin-bottom: 22px;
}
Summary
Don't nest all child element to it's parent. Write more reusable and context-independent code.

Related

Moving a tag to the top of a todo bar

The spent text with the teal background is meant to be a tag, and I want the tag to appear above the todo bar...kind of like this:
Like a small rectangle on top of a big one. So the tag would be on the top left corner of the todo bar. How would I achieve this? I've tried doing margin to the tag, but that did not work out at all.
CSS for the tag (style.css)
.tag {
color: white;
font-size: 15px;
font-weight: 400;
letter-spacing: 2px;
text-transform: uppercase;
background: #36d1dc;
padding: 3px;
border-radius: 5px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
}
React JS code for the tag part (Todo.js)
<li className={`todo-item${todo.completed ? "completed" : ""}`}>
{isSpent && <p className="tag">Spent</p>}
{isReceived && <p className="tag">Received</p>} ${text}
</li>
In case anyone needs the whole of the todo.css file: https://pastecode.io/s/s5XZ9e3DRW
If you need anymore information, or if my question was poorly phrased, please tell me. Any help is very much appreciated. Thank you!
I think if yow will separate the tag and the navbar to two different div tags and put them on main div something like:
<div id="wrapper">
<div id="top-left">top left div</div>
<div id="down">down side div</div>
</div>
and the css will be something like (using grid on the main div):
#wrapper {
display: grid;
}
#top-left {
background: green;
width: 250px;
float:left;
margin-right: 20px;
}
#down {
background: blue;
float:left;
width: 500px;
}
the result is:
I would go with something like this, where input:focus could be a class set on on .container, for example, if the input has any values.
I couldn't understand why you used li and p in your original code, because you need to override so much stuff to make it look nice.
Using "rem" over a fixed pixel value is also preferred if you want to create a responsive site, where you just override the font-size in the body to make everything scale.
.container {
position: relative;
display: flex;
}
body,
input {
padding: 1rem;
}
.container.selected > .todo-item,
input:focus ~ .todo-item {
transform: translateY(-1rem);
}
.todo-item {
position: absolute;
left: 1rem;
transform: translateY(1rem);
transition: transform 400ms;
}
.tag {
color: white;
font-size: 15px;
font-weight: 400;
letter-spacing: 2px;
text-transform: uppercase;
background: #36d1dc;
padding: 3px;
border-radius: 5px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
}
<div class="container">
<input type="number">
<div class="todo-item"><span class="tag">Spent</span></div>
<div style="padding-top: 1rem"><-- select this input</div>
</div>
<div class="selected container" style="padding-top: 2rem">
<input type="number">
<div class="todo-item"><span class="tag">Spent</span></div>
</div>
body {
background-color: #48AEE0;
}
.container {
height: 200px;
width: 500px;
display: flex;
flex-direction: column;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.tag {
color: white;
font-size: 15px;
font-weight: 400;
letter-spacing: 2px;
text-transform: uppercase;
background: #36d1dc;
padding: 3px;
width: 80px;
text-align: center;
border-radius: 5px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
}
.other {
margin: 0;
display: inline-flex;
flex-direction: column;
}
input {
height: 30px;
width: 200px;
border: white;
margin: 0;
}
<div class="container">
<div class="tag">spent</div>
<div class="others">
<input type="text">
</div>
</div>

Positioning elements inside DIV

I have the following HTML:
<div class="Section__item">
<div class="Section__item__title">Title</div>
<div>
<img
class="Section__item__image"
width="120px"
src="/static/images/test.jpeg"
>
<i class="Section__item__icon icon-right-nav-workflow"/>
</div>
<div class="Section__item__text">This is a descritption</div>
</div>
And this is my style using scss:
.Section {
&__item{
border: #EEF3F7 solid 1px;
padding: 10px;
height: 150px;
margin-bottom: 15px;
box-shadow: 3px 3px #EEF3F7;
&:hover {
background-color: #E3F4FE;
cursor: pointer;
}
&__title {
text-align: left;
color: black;
font-size: 16px;
font-weight: 900;
}
&__image {
padding-top: 5px;
float: left;
}
&__icon {
float: right;
font-size: 40px;
}
&__text {
float: left;
}
}
}
The result is the following:
And what I need to get is the following:
I need the text to be under the image and where you see a "red" line in the right the text can't go further, if text is bigger then wrap text.
Also if you see right icon has to be positioned exactly on the same top level as the image.
Any clue?
There's loads of ways to do this (flexbox, grid, tables, absolute positioning). The oldschool way would be a clearfix but really you should avoid floats altogether. The simplest solution to what you have so far is to remove ALL of the float's; make the div that holds the image and the icon position:relative; and set the icon to position:absolute; top:0; right:0;.
.Section__item {
border: #EEF3F7 solid 1px;
padding: 10px;
min-height: 150px; /* changed to min-height so that it expands if there's loads of text */
margin-bottom: 15px;
box-shadow: 3px 3px #EEF3F7;
width:400px;
}
.Section__item:hover {
background-color: #E3F4FE;
cursor: pointer;
}
.Section__item__title {
color: black;
font-size: 16px;
font-weight: 900;
}
.Section__item__imagewrap {
position: relative;
}
.Section__item__image {
margin-top: 5px;
}
.Section__item__icon {
font-size: 40px;
line-height: 40px;
position: absolute;
top: 0;
right: 0;
}
.Section__item__text {}
<div class="Section__item">
<div class="Section__item__title">Title</div>
<div class="Section__item__imagewrap">
<img class="Section__item__image" width="120px" src="https://placeimg.com/320/240/any">
<i class="Section__item__icon icon-right-nav-workflow">i</i>
</div>
<div class="Section__item__text">This is a description. If the text is long it will wrap and the section__item's height will increase to fit the content.</div>
</div>
Uh... don't use float? Or rather, only use float on the one thing you want to break out of normal flow, which is the icon.
PS: <i> is not an autoclosing tag, so writing <i /> is incorrect even if browsers will likely ignore your mistake. Also, putting padding on an image doesn't seem right, I switched to margin-top in this code.
.Section__item {
display: inline-block; /* so it doesn't take full width of the snippet */
border: #EEF3F7 solid 1px;
padding: 10px;
height: 150px;
margin-bottom: 15px;
box-shadow: 3px 3px #EEF3F7;
}
.Section__item:hover {
background-color: #E3F4FE;
cursor: pointer;
}
.Section__item__title {
text-align: left;
color: black;
font-size: 16px;
font-weight: 900;
}
.Section__item__image {
margin-top: 5px;
vertical-align: top;
}
.Section__item__icon {
font-size: 40px;
float: right;
}
<div class="Section__item">
<div class="Section__item__title">Title</div>
<div>
<img class="Section__item__image" width="120" height="120">
<i class="Section__item__icon icon-right-nav-workflow">Icon</i>
</div>
<div class="Section__item__text">This is a descritption</div>
</div>

text-align/margin-left/etc. not working for my animated header

I have an animated header that is basically one stagnant word and preceeding it is a carousel of many words (in the same position) that fade in and out (thanks to animate.css). I followed a tutorial on youtube to get the basic webpage so it may look familiar or simple.
However, the preceeding words are stuck in complicated div's and headers to attempt to get multiple animations (enter and exit for each).
The problem is that when a long word comes onto the screen, it overlaps on the second word.
(Picture included)
To explain briefly, the button and text are all in a "display:table;" div and the words were normally just one cohesive sentence and worked fine. However, I've had to split them up and the only way to prevent them from being above/below one another (VERY annoying) was to force them to float left and right. However, when I delete the floats now (after much 'debugging') it doesnt seem to actually change the design, which greatly worries me. I have included my code. Please help! My idea right now is to make the text right aligned and relative to the second word, but everything I try seems to do absolutely nothing. If there are any other solutions I am all ears.
<section class="intro">
<div class="inner">
<div class="content">
<section style="clear: both">
<div style="position:absolute" z-index="1">
<h1 z-index="inherit"display="inline-block" class="os-animation" data-os-animation="fadeInDown" data-os-animation-delay="0s">
<div text-align="right" z-index="inherit" class="os-animation" data-os-animation="fadeOutDown" data-os-animation-delay="1s">Loving</div></h1>
</div>
<div style="position:absolute" margin-left="15%" z-index="2">
<h1 z-index="inherit" display="inline-block" class="os-animation" data-os-animation="fadeInDown" data-os-animation-delay="1s">
<div text-align="right" z-index="inherit" class="os-animation" data-os-animation="fadeOutDown" data-os-animation-delay="2s">Enjoying</div></h1>
</div>
<div style="position:absolute" z-index="3">
<h1 z-index="inherit" display="inline-block" class="os-animation" data-os-animation="fadeInDown" data-os-animation-delay="2s">
<div text-align="right" z-index="inherit" class="os-animation" data-os-animation="fadeOutDown" data-os-animation-delay="3s">Exploring</div></h1>
</div>
<h1 style="float: right">Irelnd</h1>
</section>
<section style="clear:both" class="os-animation" data-os-animation="fadeInUp" data-os-animation-delay="5s">
Get Started
</section>
</div>
</div>
</section>
css:
#import url('https://fonts.googleapis.com/css?family=Yantramanav:100');
#import url('https://fonts.googleapis.com/css?family=Montserrat:400');
html,body{
margin:0;
padding:0;
height:100%;
width:100%;
}
#carousel {
background-color: green;
text-align: center;
vertical-align: middle;
float: left;
}
#Dubai{
background-color: red;
}
.intro{
height:100%;
width:100%;
margin:auto;
background: url(https://static.pexels.com/photos/470641/pexels-photo-470641.jpeg) no-repeat ;
display: table;
top:0;
background-size:cover;
}
.intro .inner{
display: table-cell;
vertical-align:middle;
width: 100%;
}
.content{
max-width:480px;
margin: 0 auto;
text-align: center;
padding-bottom: 28%;
padding-left: 4%;
}
.content h1 {
font-family: 'Yantramanav', sans-serif;
font-size: 600%;
font-weight: 100;
color: #E1efe9;
line-height: 55%;
}
.btn {
font-family: 'Montserrat', sans-serif;
font-size: 135%;
font-weight: 400;
color: #3c4f1f;
text-transform: uppercase;
text-decoration: none;
border: solid #3c4f1f;
padding:10px 20px;
border-radius: 9px;
transition: all 0.3s;
}
.btn:hover{
color: #a0afa8;
border: solid #a0afa8;
}
p{
font-size: 150%;
line-height: 120%;
font-family: sans-serif;
}
/*--- Media Queries --*/
#media screen and (max-width: 900px) {
.content{
padding-bottom: 30%;
max-width: 320px;
}
.content h1{
font-size: 450%;
}
.btn{
font-size: 130%;
padding: 9px 15px;
}
}
#media screen and (max-width: 768px) {
.content{
padding-bottom: 40%;
max-width: 210px;
}
.content h1{
font-size: 300%;
}
.btn{
font-size: 110%;
padding: 9px 15px;
}
p{
font-size: 120%;
line-height: 100%;
}
}
#media screen and (max-width: 480px) {
.content{
padding-bottom: 50%;
max-width: 173px;
}
.content h1{
font-size: 250%;
}
.btn{
font-size: 90%;
padding: 4px 12px;
}
p{
font-size: 100%;
line-height: 100%;
}
}

Align Text Inside A Inline-Block Div? CSS

I am trying to align a group of text, including p and h5 html elements. I am using inline-block display type to get the text to be on the same line for the titles. If the aligning with this display type is not possible it would be helpful if you could provide a alternate way the 3 titles on the same line. Here is my code:
.container {
position: relative;
width: 100%;
max-width: 960px;
margin: 0 auto;
padding: 0 20px;
box-sizing: border-box;
}
.column,
.columns {
width: 100%;
float: left;
box-sizing: border-box;
}
/* For devices larger than 400px */
#media (min-width: 400px) {
.container {
width: 85%;
padding: 0;
}
}
/* For devices larger than 550px */
#media (min-width: 550px) {
.container {
width: 80%;
}
.column,
.columns {
margin-left: 4%;
}
.column:first-child,
.columns:first-child {
margin-left: 0;
}
.one-third.column {
width: 30.6666666667%;
}
#importantpeople {
text-align: center;
}
#manager-1 {
font-weight: 500;
text-align: left;
margin-left: -2px;
display: inline-block;
text-align: left;
}
#manager-2 {
font-weight: 500;
text-align: center;
display: inline-block;
text-align: center;
}
#manager-3 {
font-weight: 500;
text-align: right;
margin-right: 10px;
display: inline-block;
text-align: right;
}
<div class="container">
<div class="row">
<div id="seven columns" id="seven-cols">
<h4 id="aboutus">About Us</h4>
<p>TheVersionArts is a private design studio. We were founded in the winter of 2014. We connect clients to the designers they need. Our goal is to serve high quality design at an affordable price through the internet. We strive to impress our clients.
If you want to be apart of this movement then sign up now!</p>
</div>
</div>
</div>
<div class="container">
<div class="row" id="importantpeople">
<h4 id="managers-head">Our Managers</h4>
<div class="one-third.column" id="screamer">
</div>
<div class="one-third.column" id="kinzu">
</div>
<div class="one-third.column" id="swezii">
</div>
<h5 id="manager-1">Screamer</h5>
<h5 id="manager-2">KINZU</h5>
<h5 id="manager-3">Swezii</h5>
<p>Just a guy chilling on his computer creating works of art for people</p>
<p>I am a guy who loves to get the things in my head onto paper. I have some great ideas that will blow your minds! Get ready!</p>
<p>I love Web, App and other designing. It is my goal to get rid of bad design in my city.</p>
</div>
</div>
So my question is how to I align #screamer to the left, #kinzu to the center and #swezii to the right when it is on the same line?
Use float: left for the #manager-1 and float: right for the #manager-3.
This pulls the Screamer to the left and the Swezii to the right while KINZU is in the center.
#manager-1 {
font-weight: 500;
float: left;
margin-left: -2px;
display: inline-block;
text-align: left;
}
#manager-2 {
font-weight: 500;
text-align: center;
display: inline-block;
text-align: center;
}
#manager-3 {
font-weight: 500;
float: right;
margin-right: 10px;
display: inline-block;
text-align: right;
}
Change the Margins to 20%, and invert the direction you have them set as, make sure you use % not px for the margins.
#manager-1 {
font-weight: 500;
text-align: left;
margin-right: 20%;
display: inline-block;
text-align: left;
}
#manager-2 {
font-weight: 500;
text-align: center;
display: inline-block;
text-align: center;
}
#manager-3 {
font-weight: 500;
text-align: right;
margin-left: 20%;
display: inline-block;
text-align: right;
}

CSS Layout shifts to Right on iPhone/iPad?

Super weird: For some reason, my site's front page layout (CSS) shifts to the right on a mobile device when it's supposed to be centered? See: http://www.stylerepublicmagazine.com
Does anyone know why this is? I've seen this error on other forums, but no one seems to have a solid fix for it.
Here's the main portion of the stylesheet for my template:
#wrapper {
position:absolute;
width:100%;
margin: 0, auto;
margin-top:60px;
}
#socialmedia {
float:right;
}
#topbanner {
margin-left:180px;
width:990px;
}
#magnavigation {
position:absolute;
margin-top:150px;
margin-left:150px;
}
#featureslides {
position:absolute;
margin-top:240px;
margin-left:190px;
width:1000px;
}
div.img
{
padding-top:40px;
margin: 0px;
height: 150px;
width: 150px;
float: left;
text-align: left;
vertical-align:top;
padding-right:62px;
}
div.imglast
{
padding-top:40px;
margin: 0px;
height: 150px;
width: 150px;
float: left;
text-align: left;
vertical-align:top;
}
div.img img
{
display: inline;
margin: 3px;
}
div.articlename {
padding-top:5px;
font-family:'Oswald', sans-serif;
font-size:1.4em;
}
div.desc
{
padding-top:5px;
text-align: left;
font-family:helvetica;
font-size:1em;
font-weight: normal;
width: 140px;
margin: 0px;
padding-bottom:100px;
}
#morefeatures {
margin-top:180px;
float:left;
width:685px;
padding-right:15px;
padding-bottom:20px;
}
#adverts {
width:300px;
float:right;
margin-top:180px;
}
.FrontHeading {
font-family: 'Oswald', sans-serif;
font-size:30px;
padding-bottom:5px;
}
Thanks,
B
You're declaring a lot of margin-left properties which causes the elements to shift to the right.
Before and after removing the margins on the left.
As some others pointed out, you're simply using too many position: absolute properties in your CSS and basically, you've tuned your layout for one resolution (1440 wide). For example, on my resolution of 1920x1080, your layout appears on the left.
You can fix this by removing all position: absolute properties and using substitutes. For example, for the main column, you should be using margin: 0 auto, which will center it.
I've created an example of a layout you can use, to get an idea of the various types of positioning you'll want to use for your layout. I essentially duplicated the layout (more or less) using different properties that should scale across resolutions and devices.
The Fiddle
HTML
<div id='wrapper'>
<div id='banner'>
Your logo
<div id='social'>FACEBOOK | TWITTER</div>
</div>
<div id='slides'><img src='http://placekitten.com/500/200'/></div>
<div class='news'>News item 1</div>
<div class='news'>News item 2</div>
<div class='news'>News item 3</div>
<div class='news'>News item 4</div>
<div class='news last'>News item 5</div>
<div class='blog'><div class='entryimg'><img src='http://placekitten.com/50/50'/></div> Blog entry</div>
<div class='blog'><div class='entryimg'><img src='http://placekitten.com/50/50'/></div> Blog entry</div>
<div class='blog'><div class='entryimg'><img src='http://placekitten.com/50/50'/></div> Blog entry</div>
<div class='blog'><div class='entryimg'><img src='http://placekitten.com/50/50'/></div> Blog entry</div>
<div style='clear: both'></div>
</div>
CSS
#wrapper {
width: 500px;
margin: 0 auto;
font: 18px sans-serif;
}
#banner {
background: #8888ff;
padding: 20px;
margin-bottom: 5px;
}
#social {
float: right;
margin-top: -10px;
font-size: 50%;
}
#slides {
margin-bottom: 5px;
}
.news {
background: #88ff88;
display: inline-block;
*display: inline; /* IE8- hack */
zoom: 1; /* IE8- hack */
margin-right: 10px;
width: 78px;
text-align: center;
padding: 5px;
}
.news.last {
margin-right: 0;
}
.blog {
margin-top: 8px;
clear: both;
}
.blog .entryimg {
float: left;
margin-right: 10px;
}
Result
Too much position absolute for the CSS I think.
Change these few CSS for content to center.
#wrapper {
width: 100%;
margin: 0 auto;
margin-top: 60px;
}
#topbanner {
margin-left: 180px;
width: 990px;
margin: 0 auto;
}
#magnavigation {
margin-top: 150px;
margin-left: 150px;
margin: 0 auto;
}
#featureslides {
margin-top: 240px;
margin-left: 190px;
width: 1000px;
margin: 0 auto;
}
I suggest you to reconstruct your section as it's quite a mess and hard to control from what I saw.

Resources