CSS: Sticky element on the bottom of horizontally scrolling container

Viewed 1148

I am trying to make sticky footer inside of a horizontally scrolling element. I want that sticky footer to be aligned to the bottom, have auto height based on it's content and be as wide as scroll parent (in this example 200px), so when I scroll to the right, footer is on the same place and it's content is horizontally centered (content should be some element, not only a simple text). I would appreciate help.

Wanted solution: Wanted solution

My current code:

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.example {
  width: 200px;
  overflow-x: auto;
}

.container {
  position: relative;
  display: flex;
  width: 400px;
  height: 100px;
  background-color: moccasin;
}

.footer {
  position: sticky;
  bottom: 0;
  background-color: palegreen;
  justify-self: center;
}
<div class="example">
  <div class="container">
    <div class="footer">
      Footer
    </div>
  </div>
</div>

Thanks a lot.

2 Answers

Hope this solves your issue: (Added Lorem Ipsum to show the scroll effect and footer being fixed)

* {
  box-sizing: border-box;
  outline: 1px solid red;
}

body {
  margin: 0;
  padding: 0;
}

.example {
  width: 200px;
  height: 300px;
  overflow-x: auto;
}

.container {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 400px;
  height: 100%;
  background-color: moccasin;
}

.footer {
  position: sticky;
  width: 200px;
  height: 20px;
  top: 100%;
  background-color: palegreen;
  text-align: center;
  left: 0;
}
<div class="example">
  <div class="container">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
    software like Aldus PageMaker including versions of Lorem Ipsum
    <div class="footer">
      Footer
    </div>
  </div>
</div>

Please see the following changes. I changed some of your CSS on your .footer class to give it a 100% width and position: relative then I added align-self: flex-end; and some scroll bar styles using ::-webkit-scrollbar feel free to change it around as desired.

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.example {
  width: 200px;
  overflow-x: auto;
}

.container {
  position: relative;
  display: flex;
  align-self: flex-end;
  width: 400px;
  height: 100px;
  background-color: moccasin;
}

.footer {
  position: relative;
  height: 20px;
  background-color: palegreen;
  align-self: flex-end;
  width: 100%;
  text-align: center;
}

::-webkit-scrollbar {
  width: 4px;
  height: 8px;
}

::-webkit-scrollbar-thumb {
  border-radius: 4px;
  background-color: rgba(0, 0, 0, .5);
  box-shadow: 0 0 1px rgba(255, 255, 255, .5);
}

p {
  margin: 0;
  padding: 0;
  position: fixed;
  margin-left: 5rem;
}
<div class="example">
  <div class="container">
    <div class="footer"><p>Footer</p></div>
  </div>
</div>

EDIT: You were using justify-self: center;. With a position: relative; you can use align-self: flex-end; to get the footer to display at the bottom. Also added position: fixed; with some margin to get your text to stay when scrolling.

Related