karim karim - 2 months ago 10
CSS Question

how to align a sidebar to the right with css?

Am trying to make a simple sidebar, i made this code till now , and i didn't figure out how to make it float to the right , am so confused!! any help ? thanks :)

here is a link to jsfiddle :
https://jsfiddle.net/y7bx0v1s/

._sidebar{
position: absolute;
z-index: 2;
top: 3rem;
bottom: 0;
left: 0;
margin-top: 1px;
overflow-x: hidden;
overflow-y: scroll;
background: #f9f9f9;
background-image: initial;
background-position-x: initial;
background-position-y: initial;
background-size: initial;
background-repeat-x: initial;
background-repeat-y: initial;
background-attachment: initial;
background-origin: initial;
background-clip: content-box;
background-color: rgb(249, 249, 249);
background-clip: content-box;
-webkit-overflow-scrolling: touch;
-ms-overflow-style: none;


}
section {
outline: 0;
width: 300px;
outline-color: initial;
outline-style: initial;
outline-width: 0px;


}
aside, section,div {
display:block;
}
body {
height: 50%;
margin: 0;
overflow: auto;
font: normal 1em/1.7 "Open Sans", Helvetica, Arial, sans-serif;
font-style: normal;
font-variant: normal;
font-weight: normal;
font-stretch: normal;
font-size: 1em;
line-height: 1.7;
font-family: 'Open Sans', Helvetica, Arial, sans-serif;
color: #333;
word-wrap: break-word;
overflow-wrap: break-word;
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}

._sidebar::-webkit-scrollbar {
-webkit-appearance: none;
background-color: rgb(255, 255, 255);

width: 10px;
}
._sidebar::-webkit-scrollbar-button {
display: none;
}
._sidebar::-webkit-scrollbar-thumb {

min-height: 2rem;
background: #d2d2d2;
background-image: initial;
background-position-x: initial;
background-position-y: initial;
background-size: initial;
background-repeat-x: initial;
background-repeat-y: initial;
background-attachment: initial;
background-origin: initial;
background-clip: padding-box;
background-color: rgb(210, 210, 210);
background-clip: padding-box;
border: 3px solid #fff;
border-top-color: rgb(255, 255, 255);
border-top-style: solid;
border-top-width: 3px;
border-right-color: rgb(255, 255, 255);
border-right-style: solid;
border-right-width: 3px;
border-bottom-color: rgb(255, 255, 255);
border-bottom-style: solid;
border-bottom-width: 3px;
border-left-color: rgb(255, 255, 255);
border-left-style: solid;
border-left-width: 2px;
border-radius: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
}
._list {
margin: 0;
padding: 0;
list-style: none;
width: 18rem;
box-shadow: inset -1px 0 #e3e3e3;

}


<section class="_sidebar">
<div role="navigation" class="_list">

<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>
<a href="#">link1</a><br>

</div></section>

Answer

this code is kind of awkward, but if you just want to move this to the right, simply change left:0 to right:0

  ._sidebar {
    position: absolute;
    z-index: 2;
    top: 3rem;
    bottom: 0;
    right: 0;
    margin-top: 1px;
    overflow-x: hidden;
    overflow-y: scroll;
    background: #f9f9f9;
    background-image: initial;
    background-position-x: initial;
    background-position-y: initial;
    background-size: initial;
    background-repeat-x: initial;
    background-repeat-y: initial;
    background-attachment: initial;
    background-origin: initial;
    background-clip: content-box;
    background-color: rgb(249, 249, 249);
    background-clip: content-box;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: none;
  }
  
  section {
    outline: 0;
    width: 300px;
    outline-color: initial;
    outline-style: initial;
    outline-width: 0px;
  }
  
  aside,
  section,
  div {
    display: block;
  }
  
  body {
    height: 50%;
    margin: 0;
    overflow: auto;
    font: normal 1em/1.7 "Open Sans", Helvetica, Arial, sans-serif;
    font-style: normal;
    font-variant: normal;
    font-weight: normal;
    font-stretch: normal;
    font-size: 1em;
    line-height: 1.7;
    font-family: 'Open Sans', Helvetica, Arial, sans-serif;
    color: #333;
    word-wrap: break-word;
    overflow-wrap: break-word;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
  }
  
  ._sidebar::-webkit-scrollbar {
    -webkit-appearance: none;
    background-color: rgb(255, 255, 255);
    width: 10px;
  }
  
  ._sidebar::-webkit-scrollbar-button {
    display: none;
  }
  
  ._sidebar::-webkit-scrollbar-thumb {
    min-height: 2rem;
    background: #d2d2d2;
    background-image: initial;
    background-position-x: initial;
    background-position-y: initial;
    background-size: initial;
    background-repeat-x: initial;
    background-repeat-y: initial;
    background-attachment: initial;
    background-origin: initial;
    background-clip: padding-box;
    background-color: rgb(210, 210, 210);
    background-clip: padding-box;
    border: 3px solid #fff;
    border-top-color: rgb(255, 255, 255);
    border-top-style: solid;
    border-top-width: 3px;
    border-right-color: rgb(255, 255, 255);
    border-right-style: solid;
    border-right-width: 3px;
    border-bottom-color: rgb(255, 255, 255);
    border-bottom-style: solid;
    border-bottom-width: 3px;
    border-left-color: rgb(255, 255, 255);
    border-left-style: solid;
    border-left-width: 2px;
    border-radius: 5px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
  }
  
  ._list {
    margin: 0;
    padding: 0;
    list-style: none;
    width: 18rem;
    box-shadow: inset -1px 0 #e3e3e3;
  }
<section class="_sidebar">
  <div role="navigation" class="_list">

    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>
    <a href="#">link1</a>
    <br>

  </div>
</section>