My websites layout gets messed up when I zoom in or out! :(

I’m not sure how much specificity issues relate to @blackopspros328 problem but I guess it is one thing that should be considered.

I used to use ids because I wanted to use getElementById without doing a lot of parentNode / childNode DOM walking. That need is pretty much moot now.

Technically, no ids need ever be used and multiple class values can be used when specificity is needed. I use them now mainly for my benefit, using them for generic elements that have a lot of descendants as a way to assist readability.

1 Like

I know the question was directed at Sam but I’d like to chip in with a few observations of my own.

As usual with CSS the answer is again “it depends…:slight_smile:

For many years I was a strong advocate of clean mark up and would go to any length to avoid adding classes or ids to my html if I could target them from a parent and use descendant selectors.

Lets take a case in point in your code:

#header div:last-of-type span {
    margin-left: 0.5em;
    color: #fcab15;
 }

The code above is now html dependent and will only work if the html is in exactly that structure. If your client adds a span (or widget/social icon) at the end of the line he not only has to address the code he is adding but the whole hierarchy of that structure in order to complete the task successfully. (It is also likely that your ids will outweigh any code that comes with the widget or third party plugin and break its display).

Now take the following css:

.info {
    margin-left: 0.5em;
    color: #fcab15;
 }
<span class="info">Info</span>

The css has now been immediately decoupled from the html and the client can add a span or do whatever without needing to go back and adjust every counted span in that section. Immediately the specificity is simple and the code is effective and easily maintained. The only drawback is the extra class in the html which has negligible impact on performance on the html even if you add thousands of them (which I don’t advocate).

So again we get into a situation where we are trying to say “one size fits all” but that is seldom the case. If I was working on my own site that no one else was going to manage I would use classes and ids sparingly but when I work for clients (I have created literally many hundreds of templates for clients over the last twenty years) the best practice is to try and make it idiot proof and keep it simple.

For css performance ID selectors are parsed quicker but only when you target the element with the id. Once you say something like #id span{} then there is virtually no difference in performance because css works from right to left. This means that using ids really doesn’t give you any benefit so you may as well make it simple and stick with classes and avoid any specificity issues.

The other part of the puzzle is overuse of descendant selectors because descendant selectors are slow especially when you get more than 2 deep and once again ties your css to the structure of the mark up and we were supposed to be separating concerns.

Lastly, it doesn’t really matter how you do it if you are the only one using the code and you know what you are doing. A good coder is free to use all the techniques at their disposal because they understand all the complexities of what they are doing. If you are building templates for others to use then I would advocate the simpler the better approach which in turn lends itself to more modular css rules and shorter code even though you may use more classes. To be honest I fought against this for years but time and time again I came un-stuck because my code was too specific to the html and clients would change the design half way through or constantly add and change things as the project developed. Using classes and avoiding deep descendant selectors made the sites easier to manage and easier to change.

For your code above I would look to compose something like the following.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Untitled Document</title>

<style>
.header {
    padding:0.75em 1em;
    background-color: #000;
    text-align: center;
    color:#fff;
 }
.sign-in{
    float: left;
    color: #fff;
    text-decoration: none;
}
.sign-in:hover  {color: #fcab15;}
.help{float: right;}
.free {display: inline-block; }
.free b {margin-left: 0.5em;}

</style>
</head>

<body>
<div class="header">
  <a class="sign-in" href="index.html"> Sign in/Register</a> 
  <span class="help">Need Help? : 02080207651</span>
  <span class="free">FREE  International shipping on<b>Selected Items</b></span>
</div>
</body>
</html>

However if it was for my own personal project then I would probably lose a couple of the classes and use similar methods to you but the above code is immediately more compact and more maintainable.

In the end it’s a matter of choice but I often find that simplest is the best even if it means adding an extra class. I am not averse to using the odd ID or an extra descendant selector but I am very careful in their use and restrain them to small controlled areas.

As usual opinions differ and that’s fine but I have certainly changed my mind on the code that I use now and will always look to simplify even if that means an extra class.:slight_smile:

6 Likes

Finally got the time to take a look at your code and honestly it made me feel depressed as my website has messy code, wrong use of tags and selectors while your code is precise, sleek and does the job. Im going to look at your code and compare it to mine to help me see where i am going wrong, thank you for your help.

This forum has really nice people!

2 Likes

This is just a quick question, is there anyway else to keep the layout of the website still without using media queries? Just curious.
Thanks

No reason to feel depressed. In fact you should be glad you can recognize where improvements could be made and are willing to make them. For me, I see that as a trait of professionals. By professional I don’t mean “makes money”, but someone that is not satisfied with what “works” and always does the best they can.

I would also go so far to wager that if most of us here were to look at our first attempts, if not something even a few months ago, we would see much we would do differently now.

6 Likes

Thank you, im going to keep trying until i can make a perfect website!

1 Like

Can you explain what you mean by “keep the layout of the website still”.

A URL to your test site that demonstrates the issue would be a big help.

Yes, that was something I mentioned in my post and I try to re-evaluate what I know and whether it works the way I think it does. I look at my old stuff and can see things I would never use today. It’s a continuous learning process as the landscape changes daily so we often have to work out the best route forward which may not be the same path we took yesterday.

As Ron said the question doesn’t really make sense in the real world as you can’t make a screen fit 320px and fit 2000px and still look the same? It just isn’t physically possible or advisable.

A site should make best use of the available space and does not need to look the same on all devices (in fact it would be bad to do so).

If you build a fluid site then media queries only come into play at widths where the design needs to change to make best use of the available space as mentioned before. It’s not rocket science and is in fact quite straight forward although for beginners there is a learning curve.

1 Like

Not wanting to dwell too much on the specificity subject, as I was concerned that @0_0Nathan had taken the topic off on a tangent and should probably have replied as a new topic. Though it is an important subject for a beginner to learn about.

I think @Ray.H put it well:-

I’m not against the use of IDs altogether, just so long as you understand what you are doing. But @0_0Nathan himself says he does not understand why sometimes his selectors don’t work as expected without using IDs. Though I would hazard a guess it may be due to previous use of IDs in the css.

That is not the issue the priority of using things like background images doesn’t work in classes but the instance I change it an id it works even if its the only code on the page.

The point is adding id’s overwrite content when needed classes don’t.

it’s hard to explain. its as if I use the id’s as main parts of the pages but overwrite some of the content concerned on the rest of the page leaving me no choice but to use id’s inside the parent element otherwise it has no effect on content within an ID

thats the easiest I can say it I am sorry I am not a genius and ruined the flow of this topic, I just thought it was important to the topic at hand as giving him advice and clarifying issues to avoid complications later on thats why I call it 2c comments as it has no use here but I feel it’s important to note.

That sounds very much as if your initial reaction to the idea of media queries was much the same as mine: “Help! Panic! I’ll never get the hang of that!”

The good news is that they’re not nearly as tricky as they look at first sight. I was putting off adapting my older sites because I thought media queries would be really hard to get to grips with, but once I got started, I discovered I’d been worrying about nothing and they’re really quite straightforward.

Courage!

3 Likes

Unfortunately that is incorrect and due to your misunderstanding on how specificity works. Whether a rule is applied to an element is always resolved by defined rules. Ids carry more weight than classes. You could concatenate 100 classes but they will not win over an ID. Without going off topic too much please read the following article. That will allow you to determine exactly which rules will be applied. (Unfortunately Sitepoint have removed my original article on Specificity which went into much more detail :().

2 Likes

If I understand you correctly, you are proving exactly my point.
Because you are using IDs on main elements, you then find it difficult to style any sub elements of those with different styles using just classes. So you then must use more IDs to override the highly specific selectors you already have.

And so it begins…

1 Like

The best solution to your misconceptions is for you to write a few pages without ANY IDs other than those required by JS or for fragment identifiers. But, if you must use an ID, do not assign any properties to it, add a class also, if necessary.

I posted a brief response in another thread a few years ago which attempts to address a similar concern by another member. The responses in this thread are far better and well referenced, but if you are still not convinced… well… it’ll only cost about 40 seconds to read it.
http://community.sitepoint.com/t/my-two-button-classes-are-clashing-and-i-dont-know-how-to-fix-it/44002/12

2 Likes

Hello guys! So i went away and implemented what i learnt and i completed my homepage!! And it actually stays still on my desktop screen but when i zoom in up to 175% or zoom out to 25% the content part of my website does not stay still. And i know i have to use media queries but i my code is soo much that i dont know what tags to place in the media query rules.
Could someone look at my code and help me please.
Thank you

<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>Bike King </title>

<link rel="stylesheet" href="screen.css" media="screen">

</head>
<body> 

<div id="wrapper"> <!--This contains the content of the page and i gave it an ID so i can format the entire content in the stylesheet-->
  
  <header class="header">
    <p class="inner"> <a href="index.html"> Sign in/Register</a> <span class="free">FREE International shipping on<b>Selected Items</b></span> <span>Need Help? : 01234 56789</span> </p>
  </header>
  
  <main>
  
	<!-- .content-header, contains the logo and searchbox--> 
    <div class="content-header">
      <h1><img src="image/logo.png" width="200" height="100" alt="Company Name Here"></h1>
      <form action="#">
        <input type="text" id="textbox" name="textbox" required placeholder="Search Site...">
        <input class="icon" type="image" value="Search" src="image/icon.png" alt="Search">
      </form>
    </div>
	
	<!-- .nav, contains the navigation -->
    <ul id="nav" class="nav">
      <li><a href="index.html" class="active">Home</a></li>
      <li><a href="male.html">Male</a></li>
      <li><a href="female.html">Female</a></li>
      <li><a href="unisex.html">Unisex</a></li>
      <li><a href="contact.html">Contact Us</a></li>
    </ul>
	
	<!-- .content,-->
    <div class="content">
		<p id="undernav"> <a class="star1" href="index.html">&#x2605;&#x2605;&#x2605;&#x2605;&#x2605; APPROVAL</a><span> <a class="star2" href="index.html"> &#x2611; WIDE RANGE OF PRODUCTS</a><span><span><a class="star3" href="index.html"> &#x2611; FINEST QUALITY</a></span><span><a class="star4" href="index.html">&#9993;SIGN UP TO OUR NEWSLETTER</a></span></p>
		<img id="offer" src="image/offer.png">
		<div class="overopacity">SAVE UP TO 20% ON BIKES</div>
		<h3>NEW OFFERS ON OUR PRODUCTS</h3>
		
		<img class="products1" src="image/biketyre1.jpg">
		<img class="products1" src="image/bike1.jpg">
		<img class="products1" src="image/seat1.jpg">
		<h4>
			
			<li>DISCOUNTED MALE BIKE TYRES</li>
			<li>NEW BMX BIKE AVAILABLE </li>
			<li>RED MALE BIKE SEAT TYPE</li>
			
		</h4>
		
		<img class="products2" src="image/biketyre2.jpg">
		<img class="products2" src="image/bike2.jpg">
		<img class="products2" src="image/seat2.jpg">
		
		
		<h5>
			
			<li>DISCOUNTED FEMALE BIKE TYRES</li>
			<li>235 BIKE TYPE AVAILABLE </li>
			<li>GREEN FEMALE SEAT TYPE</li>
			
		</h5>
		
		<img class="products3" src="image/biketyre3.jpg">
		<img class="products3" src="image/bike3.jpg">
		<img class="products3" src="image/seat3.jpg">
		
		
		<h6>
		
			<li>DISCOUNTED UNISEX BIKE TYRES</li>
			<li>SCOOB BIKE TYPE AVAILABLE </li>
			<li>DUNLOP UNISEX SEAT TYPE</li>
		
		</h6>
		
    </div>
  </main>
  
</div>

</body>

<footer class="footer">

    <div class="inner">
      <p class="copyright"><b><i>Copyright &copy; Bike King<i></b></p> <p class="footermedia"><b>FOLLOW US</b> ON : <img class="media1" src="image/media1.jpg">
	  <img class="media1" src="image/media2.jpg">
	  <img class="media1" src="image/media3.jpg"></p></p>
    </div>
	
	
  </footer>


</html>
html, body {
  height: 100%;
}

html,
body,
p,
p1,
p2,
form,
h1,
h2,
h3,
h4,
h5,
h6,
ul,
ol {
  margin: 0;
  padding: 0;
}

body {
  background-color: #f0f0f0;
  font: 100%/160% arial, helvetica, sans-serif;
  padding: 0em 0em 0em;
  max-width: 980px;
  margin: 0 auto;
}

.header, .footer {
  position: fixed;
  right: 0;
  left: 0;
  top: 0;
  background:#bcbcbc;
  color:black;
  min-height: 2em;
  z-index: 99; /* keeps the selectors above everything else */
}

.footer {
  text-align: left;
  top: auto;
  bottom: 0em;
  font-size: 0.8em;
}

.footer p {
  line-height: 1.5em;
}

.inner {
  max-width: 980px;
  margin: auto;
  padding: 0 10px;
}

.header .inner {
  padding: 0.5em 0;
  text-align: center;
  color: black;
  display: flex; /* modern browsers only */
  justify-content: space-between;
}

.header a,
.header span {
  padding: 0 0px;
}

.header a {
  color: black;
  text-decoration: none;
}

.header a:hover {
  color:white;
  list-style-type: none;
}

.free b {
  margin-left: 0.5em;
  color:white;
  font-weight: normal;
  display: inline-block;
}

/* keep everything else inside the wrapper */
#wrapper {
  background: white;
  min-height: 100%;
  max-width: 980px;
  padding: 3em 0; /* push content so its visible otherwise it will be hidden under the fixed header and footer */
  margin: auto;
}

.content-header {	
  padding: 0 0 0em;
  background:;
  width:100%;
  text-align: center;
  overflow: hidden;
}

.content-header h1 {
  float: left;
  position: relative;
  font-size: 100%;
  color: #fff;
  right:0.5em;
}

.content-header h1 img {
  display: block;
  max-width: 100%;
  margin:0em 0.49em;
  
}

.content-header form {
  float: right;
  padding: 1em;
}

.content-header form input[type="text"] {
  line-height: 2em;
  font-size: 1em;
  padding: 0px 53px 0px;
  margin: 5px 0;
  
}

.content-header form input[type="image"] {
	display:inline-block;
	position:relative;
	float:right;
	right:0px;
	top:5px;
 
}

.nav {
	list-style-type: none;
	background:#2FAC63;
	overflow: hidden;
	clear: both;
	text-align: center;
	display: flex;
}

.nav li {
  flex: 1 0 0;
  display: inline-block;
}


.nav a {
  display: block;
  color: white;
  text-decoration: none;
  font-size: 17px;
  line-height: 40px;
}

.nav a:hover {
  background-color:#f7f7f7;
  color: black;
}

.nav a.active {
  background-color:#9af495;
  color: white;
  font-weight:bold;
}

.content p {
  margin: 0 0 1em;
}



.star1, .star2, .star3, .star4{
	max-width: 980px;
	margin: auto;
	text-align: center;
	display: flex; /* Used in modern browsers only, to seperate elements into layout */
	justify-content: space-between;
	font-size:11px;
	font-family:Arial;
	display:inline-block;
}

#undernav{
	background-color:#474E58;
	min-width:100%;
	padding:0em 1em;
	position:relative;
	right:16px;
	
}

.content {
		padding: 0em 1em;
}

.content a{
  padding: 0em 0em 0em 6.6em;
  
}

.content a{
  color: white;
  text-decoration: none;
  display: inline-block;
}

.content a:hover {
  color:#2CAF5F;
  list-style-type: none;

}

#offer{
	padding:0em 1em;
	max-width:980px;
	opacity:0.7;
	position:relative;
	top:-16px;
	right:32px;
}

.overopacity{
	width:50%;
	position:relative;
	z-index:1;
	top:-190px;
	left:250px;
	border:0px solid red;
	text-align:center;
	font-family:Helvetica;
	font-size:3em;
	line-height: 1em;
	color:black;
	font-weight:bold;
	text-shadow: 2px 2px white;
}

h3{
	padding:0em 1em;
	width:100.9%;
	text-align:center;
	position:relative;
	background-color:#474E58;
	color:white;
	bottom:120px;
	right:16px;
	font-family:Arial;
	font-weight:italic;
	font-size: 12px;
	
}

.products1{
	padding:0em 0em;
	position:relative;
	bottom:6em;
	border:5px solid #e3e8ef;
	left:0.4em;
}

h4 li{
	list-style-type: none;
	padding:0em 6.8em;
	max-width: 980px;
	display: flex; /* Used in modern browsers only, to seperate elements into layout */
	justify-content: space-between;
	font-size:11px;
	font-family:Arial;
	display:inline-block;
	position:relative;
	bottom:0em;
	color:black;
}

h4{
	width:100%;
	background-color:#e3e8ef;
	position:fixed;
	font-family:calibri;
	display:inline-block;
	position:relative;
	bottom:7em;
}

.products2{
	padding:0em 0em;
	position:relative;
	bottom:6em;
	border:5px solid #e3e8ef;
	left:0.4em;
}

.products3{
	padding:0em 0em;
	position:relative;
	bottom:6.5em;
	border:5px solid #e3e8ef;
	left:0.4em;
}

h5 li{
	list-style-type: none;
	padding:0em 6.7em;
	max-width: 980px;
	display: flex; /* Used in modern browsers only, to seperate elements into layout */
	justify-content: space-between;
	font-size:11px;
	font-family:Arial;
	display:inline-block;
	position:relative;
	bottom:0em;
	color:black;
}

h5{
	width:100%;
	background-color:#e3e8ef;
	position:fixed;
	font-family:calibri;
	display:inline-block;
	position:relative;
	bottom:8em;
}

h6 li{
	list-style-type: none;
	padding:0em 6.5em;
	max-width: 980px;
	display: flex; /* Used in modern browsers only, to seperate elements into layout */
	justify-content: space-between;
	font-size:11px;
	font-family:Arial;
	display:inline-block;
	position:relative;
	bottom:0em;
	color:black;
}

h6{
	width:100%;
	background-color:#e3e8ef;
	position:fixed;
	font-family:calibri;
	display:inline-block;
	position:relative;
	bottom:11em;
}

footer{
	font-size:10px;
}

.copyright{
	text-align:center;
}



@media screen and (max-width: 900px) {

  .header,
  .footer {
    position: static;
  }
  #wrapper {
    padding: 0;
  }
  
}



@media screen and (max-width: 560px) {
	
	
  .content-header h1 {
    float: none;
    margin: auto;
    display: table;
  }
  .content-header form {
    float: none;
  }
  
  .nav {
    position: fixed;
    left: -320px;
    top: 0;
    bottom: 0;
    overflow: auto;
    z-index: 1000;
    width: 320px;
    display: block;
    padding: 0;
    text-align: center;
    transition: all 0.5s ease;
  }
  .nav li {
    display: block;
  }
  .nav li a {
    line-height: normal;
    padding: 6px 10px;
    border-bottom: 1px solid #f8ad0f;
    display: block;
  }
  .nav:target {
    left: 0;
    box-shadow: 0 0 10px 10px rgba(0, 0, 0, 0.5);
  }
  .nav a.active {
    border: none;
    border-bottom: 1px solid #f8ad0f;
    background: #000;
  }

  .header,
  .header .inner {
    display: block;
  }
  .header a,
  .header span {
    display: block;
  }
  
}



Thanks again for helping me!

Before anything else, validate the html.

1 Like
<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>Bike King </title>

<link rel="stylesheet" href="screen.css" media="screen">

</head>
<body> 

<div id="wrapper"> <!--This contains the content of the page and i gave it an ID so i can format the entire content in the stylesheet-->
  
  <header class="header">
    <p class="inner"> <a href="index.html"> Sign in/Register</a> <span class="free">FREE International shipping on<b>Selected Items</b></span> <span>Need Help? : 01234 56789</span> </p>
  </header>
  
  <main>
  
	<!-- .content-header, contains the logo and searchbox--> 
    <div class="content-header">
      <h1><img src="image/logo.png" width="200" height="100" alt="Company Name Here"></h1>
      <form action="#">
        <input type="text" id="textbox" name="textbox" required placeholder="Search Site...">
        <input class="icon" type="image" src="image/icon.png" alt="Search">
      </form>
    </div>
	
	<!-- .nav, contains the navigation -->
    <ul id="nav" class="nav">
      <li><a href="index.html" class="active">Home</a></li>
      <li><a href="male.html">Male</a></li>
      <li><a href="female.html">Female</a></li>
      <li><a href="unisex.html">Unisex</a></li>
      <li><a href="contact.html">Contact Us</a></li>
    </ul>
	
	<!-- .content,-->
    <div class="content">
		<p id="undernav"> <a class="star1" href="index.html">&#x2605;&#x2605;&#x2605;&#x2605;&#x2605; APPROVAL</a><span> <a class="star2" href="index.html"> &#x2611; WIDE RANGE OF PRODUCTS</a></span><span><a class="star3" href="index.html"> &#x2611; FINEST QUALITY</a></span><span><a class="star4" href="index.html">&#9993;SIGN UP TO OUR NEWSLETTER</a></span></p>
		<img id="offer" src="image/offer.png" alt="Special offer">
		<div class="overopacity">SAVE UP TO 20% ON BIKES</div>
		<h3>NEW OFFERS ON OUR PRODUCTS</h3>
		
		<img class="products1" src="image/biketyre1.jpg" alt="biketyre1">
		<img class="products1" src="image/bike1.jpg" alt="bike1">
		<img class="products1" src="image/seat1.jpg" alt="seat1">
		<h4>
			
			<li>DISCOUNTED MALE BIKE TYRES</li>
			<li>NEW BMX BIKE AVAILABLE </li>
			<li>RED MALE BIKE SEAT TYPE</li>
			
		</h4>
		
		<img class="products2" src="image/biketyre2.jpg" alt="biketyre2">
		<img class="products2" src="image/bike2.jpg" alt="bike2">
		<img class="products2" src="image/seat2.jpg" alt="seat2">
		
		
		<h5>
			
			<li>DISCOUNTED FEMALE BIKE TYRES</li>
			<li>235 BIKE TYPE AVAILABLE </li>
			<li>GREEN FEMALE SEAT TYPE</li>
			
		</h5>
		
		<img class="products3" src="image/biketyre3.jpg" alt="biketyre3">
		<img class="products3" src="image/bike3.jpg" alt="bike3">
		<img class="products3" src="image/seat3.jpg" alt="seat3">
		
		
		<h6>
		
			<li>DISCOUNTED UNISEX BIKE TYRES</li>
			<li>SCOOB BIKE TYPE AVAILABLE </li>
			<li>DUNLOP UNISEX SEAT TYPE</li>
		
		</h6>
		
    </div>
  </main>
  


<footer class="footer">

    <div class="inner">
      <p class="copyright"><b><i>Copyright &copy; Bike King</i></b></p> 
	  <p class="footermedia"><b>FOLLOW US</b> ON : <img class="media1" src="image/media1.jpg" alt="instagram">
	  <img class="media1" src="image/media2.jpg" alt="facebook">
	  <img class="media1" src="image/media3.jpg" alt="twitter"></p></p>
    </div>
	
	
  </footer>
  
</div>
</body>



</html>

Here is the validated code

I would like to know the dimensions of the images you are using. I’m assuming by “stay still” you mean that content doesn’t shift but I’m having trouble understanding exactly what you mean and being able use my own images of the same height and width could help some.

1 Like

For the logo on the top left corner which says bike king: 230x116px
For the image under the text “Save up to 20% on bikes”: 980x 250px

The other 9 images of products: 300x150px

And by “stay still” i mean that the content doesent move just like how the rest of the webpage including the header, footer, navigation stays in view.

1 Like

Hi there blackopspros328,

you have not validated your HTML code. :unhappy:

Unfortunately, you have also seen fit to totally ignore this link…

Placeholder Attribute Is Not A Label!

…which was given in post #31 :unhappy:

coothead