Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected css behaviors

Tags:

html

css

I encountered a couple of problems with my layout I can't explain.

Layout HTML

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>AuctionProject</title>

    <script async type="application/dart" src="auctionproject.dart"></script>
    <script async src="packages/browser/dart.js"></script>

    <link rel="stylesheet" href="auctionproject.css">
  </head>
  <body>    
    <div id="menu_bar">
      <div id="logo"></div>
      <div class="menu_tab" id="123">Tab123</div>
      <div class="menu_tab" id="upcomming">Tab456</div>
    </div>
    <div id="content_box">
      <div id="side_menu">
        <div id="open_menu_shadow"></div>
        <div id="menu_content" class="hidden">
          <div id="account" class="side_label hidden">
            <p class="tiny">You are currently logged in as:</p>
            <p id="current_user">Some User</p>
            <p class="tiny">This is not you?
              <span class="important" id="log_out" role="button" tabindex="0" style="cursor: pointer;">Log Out</span>
            </p>
          </div>
          <div id="login_box" class="side_label">
            <input type="text" name="login" placeholder="Login">
            <input type="text" name="password" placeholder="Password">
            <button type="button">Sign In</button>
            <p class="tiny">Don't have an account? <span class="important" id="register" role="button" tabindex="0" style="cursor: pointer;">Register</span></p>
          </div>
          <hr>
          <div class="side_label">Add Category</div>
          <div class="side_label">Super category 1</div>
          <hr>
          <div class="side_label">Popular #tags:
            <ol id="trendingTags">
              <li><div class="label">#Tag123</div></li>
              <li><div class="label">#Tag456</div></li>
            </ol>
          </div>
        </div>
        <div id="open_menu"><p>&lt &lt &lt &lt &lt &lt &lt &lt &lt &lt</p></div>
      </div>
      <div id="smart_box">

      </div>
      <div id="auction_list">
        <div class="auction">
          <img src=""></img>
          <div class="title">This is a sample title</div>
          <div class="teaser">This is a sample teaser </div>
        </div>
        <div class="auction">
          <img src=""></img>
          <div class="title">This is a sample auction title</div>
          <div class="teaser">This is sample teaser </div>
        </div>        
      </div>
    </div>
  </body>
</html>

Layout CSS

body {
  background-color: #FFF;
  font-family: 'Open Sans', sans-serif;
  font-size: 14px;
  font-weight: normal;
  line-height: 1.2em;
  margin: 0px;
  position: absolute;
  height: 100%;
  width: 100%;
}

h1, p {
  color: #333;
}

#menu_bar {
  width: 100%;
  height: 60px;
  padding-bottom: 2px;
  border-bottom: 1px solid #aaa;
  background-color: #F8F8F8;
}

#logo {
  height: 60px;
  width: 200px;
  margin-right: 40px;
  background-color: blue;
  display: inline-block;
}

.menu_tab {
  margin-left: 25px;
  padding: 4px 4px 0px 4px;
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  border-right: 1px solid #aaa;
  font-size: 1.2em;
  display: inline-block;
  bottom: 0;
  background-color: #ccc;
}

#menu_tab_selected {
  margin-left: 20px;
  padding: 4px 4px 0px 4px;
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  border-right: 1px solid #aaa;
  font-size: 1.2em;
  display: inline-block;
  bottom: 0;
  background-color: #fff;
}

#content_box {
  boarder: 2px solid red;
  width: 100%;
  height: 100%;
}

#side_menu{
  boarder: 1px solid #aaa;
  height: 100%;
  position: absolute;
  display: inline-block;
  background-color: #F8F8FF;
  float: left;
}

#open_menu {
  border: 1px solid #aaa;
  border-top: 0px solid #aaa;
  display:inline-block;
  position: absolute;
  top: 0;
  right: 0;
  width: 15px;
  height: 100%;
  background-color: #eee;
  z-index: 3;
}

#open_menu_shadow {
  display: inline-block;
  position: absolute;
  top: 0;
  right: 0;
  width: 15px;
  height: 100%;
  box-shadow: -7px 0px 5px #aaa;
  position: absolute;
  z-index: 1;
}

#open_menu p {
  text-align: center;
  vertical-align: bottom;
}

#menu_content {
  width: 200px;
  height: 100%;
  display: inline-block;
  float: right;
}

#menu_content hr {
    border: 0;
    border-bottom: 1px dashed #ccc;
    background: #999;
    margin-top: 20px;
    margin-bottom: 20px;
}

.hidden {
  display: none;
}

.tiny {
  font-size: 0.7em;
}

.important {
  color: red;
}

#login_box input {
  width: 120px;
  margin-bottom: 10px;
}

#current_user {
  margin: 4px;
}

.side_label {
  border: 1px solid #aaa;
  border-top-left-radius: 4px;
  border-bottom-left-radius:4px;
  margin-top: 15px;
  margin-bottom: 15px;
  margin-left: 10px;
  padding: 10px;
  padding-right: 35px;
  box-shadow: -7px 7px 5px #aaa;
  position: relative;
  background-color: #fff;
  z-index: 2;
}

.label {
  margin: 2px;
  padding-left: 5px;
  padding-right: 5px;
  border-radius: 5px;
  font-size: 0.8em;
  font-weight: bold;
  display: inline-block;
  background-color: #ccc;
}

#auction_list {
  margin-left: 200px;
  padding: 20px;
  position: absolute;
  display: inline-block;
  overflow: auto;
  width: calc(100% - 250px);
}

.auction {
  padding: 10px;
  margin-bottom: 10px;
  boarder: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  border-top: 1px solid #aaa;
}

.auction img {
  width: 60px;
  width: 60px;
}

.auction .title {
  font-size: 1.2em;
  text-decoration: underline;
}

#smart_box {
  margin: 10px;
  width: 300px;
  height: 200px;
  float: right;
  border: 1px solid #aaa;
}

My first problem has to do with the display:none property The menu you can see on the left (id="side_menu") has a div inside with id="menu_content", and the bar on the right (id="open_menu"). To toggle the menu I created the class .hidden containing the display:none property. I expected that when I add this class to the "menu_content" div it will disappear. Unfortunately nothing happens layout menu

I think my other problem has to do with the inline-block property. I would like the main content list id="auction_list" to fill up the space as shown below. layout content Which means it should be relative to the menu, so I don't have to change the left margin when toggling the menu. Also I do not know how to keep the posts (class="auction") from moving under the div on the right (id="smart_box")

I would be very grateful for any help.

Disclaimer: Please don't assume much prior knowledge. I'm an html novice.

like image 484
Lukasz Avatar asked May 03 '15 12:05

Lukasz


2 Answers

1) in #side_menu, you wrote boarder instead of border.

#side_menu {
  boarder: 1px solid #aaa;
  height: 100%;
  position: absolute;
  display: inline-block;
  background-color: #F8F8FF;
  float: left;
}

2) There's a concept with CSS that gives more or less "weight" to the CSS rules. When you write:

#menu_content {
  display: inline-block;
}

.hidden {
  display: none;
}

Maybe you think that the class="hidden" will overwrite id="menu_content". That's what should normally happen because the .hidden is written in your CSS file after the #menu_content. But it doesn't because a CSS rules with #id have more weight than .class.

If you want to make things work, you have two options:

  • write display: none !important in your .hidden class. The !importantgives the highest rules priority but is not a very good practise.
  • simply write:

    #menu_content.hidden { display: none; }

This last option will gain weight because it's more specific: you say you apply the CSS rule display to 1) a class="hidden" element 2) and that element contains an attribute id="menu_content". More specificity means more weight.

#id is more specific than .class, which is more specific than <element>.

To learn more about CSS weight, see MDN or CSS-Tricks

3) For your last problem, here's a little suggestion, maybe not perfect, but easy to understand.

Start by editing HTML: put you smart box inside the auction list, at the beginning.

<div id="auction_list">
  <div id="smart_box"></div>
  <div class="auction">(blabla)</div>
  <div class="auction">(blabla)</div>
</div>

It's not the proper way (semantically, I mean) to solve your problem, but now, let's edit CSS… For #auction_list, remove position, display and width.

#auction_list {
  margin-left: 200px;
  padding: 20px;
  overflow: auto;
}

For .auction, just add inline-block.

.auction {
  padding: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid #aaa;
  border-top: 1px solid #aaa;
  display: inline-block;
}

But note that the width of the .auction elements will very depending on their content(that's how inline-block works if you don't specify any width). It means you'll maybe encounter 2 auctions by “line” in the browser rendering. The improvment of this structure will, I think, depend on what you will do with it + design choices.

The idea behind this proposal is: don't touch at the #auction_list positioning, better work on the elements inside it.

Hope it helped. :)

like image 132
meduz' Avatar answered Oct 23 '22 15:10

meduz'


to hide/show the navigation bar you'll need some Javascript or jQuery. Here is an example which don't require a lot of modification to your code, especially your HTML..

1 - Include jQuery to your project, in my example I've used 2.1.3.

2 - This is the jQuery Part to hide/show the sidebar.

$(document).ready(function () {
    $("#open_menu").click(function () {
        $("#side_menu").toggleClass('open');
        //toggles full class on auction_list element (see snippet for live example)
        $("#auction_list").toggleClass('full');
    });
});

2a - Each time you click on your #open_menu element, the code above is being executed and toggles a class called open on the #side_menu .

3 - To make the whole thing work, we also need to do some changes in your CSS

#side_menu {
    boarder: 1px solid #aaa;
    height: 100%;
    position: absolute;
    display: inline-block;
    background-color: #F8F8FF;
    float: left;
    /* the lines below were added */
    width: 14px;
    transition: width 0.8s ease-in;
    -webkit-transition: width 0.8s ease-in;
    -moz-transition: width 0.8s ease-in;
}
/* this is new */
#side_menu.open {
    width: 200px;
}

3a - We set the width of the whole #side_menu element to 14px, so only your #open_menu element is visible. Some extra: Add some fancy transitions (for the attribute width)

3b - A new CSS rule for the open state, just set the width of your #side_menu element to 200px.

SNIPPET

$(document).ready(function() {
  $("#open_menu").click(function() {
    $("#side_menu").toggleClass('open');
    $("#auction_list").toggleClass('full');
  });
});
body {
  background-color: #FFF;
  font-family: 'Open Sans', sans-serif;
  font-size: 14px;
  font-weight: normal;
  line-height: 1.2em;
  margin: 0px;
  position: absolute;
  height: 100%;
  width: 100%;
}
h1,
p {
  color: #333;
}
#menu_bar {
  width: 100%;
  height: 60px;
  padding-bottom: 2px;
  border-bottom: 1px solid #aaa;
  background-color: #F8F8F8;
}
#logo {
  height: 60px;
  width: 200px;
  margin-right: 40px;
  background-color: blue;
  display: inline-block;
}
.menu_tab {
  margin-left: 25px;
  padding: 4px 4px 0px 4px;
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  border-right: 1px solid #aaa;
  font-size: 1.2em;
  display: inline-block;
  bottom: 0;
  background-color: #ccc;
}
#menu_tab_selected {
  margin-left: 20px;
  padding: 4px 4px 0px 4px;
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  border-right: 1px solid #aaa;
  font-size: 1.2em;
  display: inline-block;
  bottom: 0;
  background-color: #fff;
}
#content_box {
  boarder: 2px solid red;
  width: 100%;
  height: 100%;
}
#side_menu {
  boarder: 1px solid #aaa;
  height: 100%;
  position: absolute;
  display: inline-block;
  background-color: #F8F8FF;
  float: left;
  width: 14px;
  transition: width 0.8s ease-in;
  -webkit-transition: width 0.8s ease-in;
  -moz-transition: width 0.8s ease-in;
}
#side_menu.open {
  width: 200px;
}
#open_menu {
  border: 1px solid #aaa;
  border-top: 0px solid #aaa;
  display: inline-block;
  position: absolute;
  top: 0;
  right: 0;
  width: 15px;
  height: 100%;
  background-color: #eee;
  z-index: 3;
}
#open_menu_shadow {
  display: inline-block;
  position: absolute;
  top: 0;
  right: 0;
  width: 15px;
  height: 100%;
  box-shadow: -7px 0px 5px #aaa;
  position: absolute;
  z-index: 1;
}
#open_menu p {
  text-align: center;
  vertical-align: bottom;
}
#menu_content {
  width: 200px;
  height: 100%;
  display: inline-block;
  float: right;
}
#menu_content hr {
  border: 0;
  border-bottom: 1px dashed #ccc;
  background: #999;
  margin-top: 20px;
  margin-bottom: 20px;
}
.hidden {
  display: none;
}
.tiny {
  font-size: 0.7em;
}
.important {
  color: red;
}
#login_box input {
  width: 120px;
  margin-bottom: 10px;
}
#current_user {
  margin: 4px;
}
.side_label {
  border: 1px solid #aaa;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  margin-top: 15px;
  margin-bottom: 15px;
  margin-left: 10px;
  padding: 10px;
  padding-right: 35px;
  box-shadow: -7px 7px 5px #aaa;
  position: relative;
  background-color: #fff;
  z-index: 2;
}
.label {
  margin: 2px;
  padding-left: 5px;
  padding-right: 5px;
  border-radius: 5px;
  font-size: 0.8em;
  font-weight: bold;
  display: inline-block;
  background-color: #ccc;
}
#auction_list {
  margin-left: 0;
  padding: 20px;
  position: absolute;
  display: inline-block;
  overflow: auto;
  width: calc(100% - 250px);
  transition: margin 0.8s ease-in;
}
#auction_list.full {
  margin-left: 200px;
}
.auction {
  padding: 10px;
  margin-bottom: 10px;
  boarder: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  border-top: 1px solid #aaa;
}
.auction img {
  width: 60px;
  width: 60px;
}
.auction .title {
  font-size: 1.2em;
  text-decoration: underline;
}
#smart_box {
  margin: 10px;
  width: 300px;
  height: 200px;
  float: right;
  border: 1px solid #aaa;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menu_bar">
  <div id="logo"></div>
  <div class="menu_tab" id="123">Tab123</div>
  <div class="menu_tab" id="upcomming">Tab456</div>
</div>
<div id="content_box">
  <div id="side_menu">
    <div id="open_menu_shadow"></div>
    <div id="menu_content" class="hidden">
      <div id="account" class="side_label hidden">
        <p class="tiny">You are currently logged in as:</p>
        <p id="current_user">Some User</p>
        <p class="tiny">This is not you? <span class="important" id="log_out" role="button" tabindex="0" style="cursor: pointer;">Log Out</span>

        </p>
      </div>
      <div id="login_box" class="side_label">
        <input type="text" name="login" placeholder="Login">
        <input type="text" name="password" placeholder="Password">
        <button type="button">Sign In</button>
        <p class="tiny">Don't have an account? <span class="important" id="register" role="button" tabindex="0" style="cursor: pointer;">Register</span>
        </p>
      </div>
      <hr>
      <div class="side_label">Add Category</div>
      <div class="side_label">Super category 1</div>
      <hr>
      <div class="side_label">Popular #tags:
        <ol id="trendingTags">
          <li>
            <div class="label">#Tag123</div>
          </li>
          <li>
            <div class="label">#Tag456</div>
          </li>
        </ol>
      </div>
    </div>
    <div id="open_menu">
      <p>&lt &lt &lt &lt &lt &lt &lt &lt &lt &lt</p>
    </div>
  </div>
  <div id="smart_box"></div>
  <div id="auction_list">
    <div class="auction">
      <img src=""></img>
      <div class="title">This is a sample title</div>
      <div class="teaser">This is a sample teaser</div>
    </div>
    <div class="auction">
      <img src=""></img>
      <div class="title">This is a sample auction title</div>
      <div class="teaser">This is sample teaser</div>
    </div>
  </div>
</div>
like image 34
Ramiz Wachtler Avatar answered Oct 23 '22 14:10

Ramiz Wachtler