I was trying to make my design responsive by making it so that when the screen gets smaller in width, the text should be 'cut off' by three dots. However, this does not seem to function in my situation, as the .container element just seems to overflow into the main element, instead of the text getting smaller. How could I fix this? Thanks in advance!
<main>
<div class="container">
<section class="account-section">
<h5 class="username">WWWWWWWWWWWWWWWWW</h5>
</section>
</div>
</main>
<style>
main {
height: 200px;
display: grid;
place-items: center;
padding: 50px;
}
.container {
position: relative;
display: grid;
grid-template: fit-content(100%) / 100%;
max-width: 1240px;
width: 100%;
position: relative;
}
.account-section {
height: fit-content;
background: var(--white);
box-shadow: 0 0 50px rgb(0, 0, 0, 0.15);
padding: 25px;
position: relative;
flex-shrink: 1;
}
.username {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
</style>
overflow in this context means, that the content cannot fit in the container and so overflows. Here the container is the <h5>, while the content is the text. The <h5> will just grow according to the text content by default, because it's not constrained, neither by rules set to itself nor by rules for the parent.
If you put a width: 20% on the <h5> as demonstrated below for example, the text would overflow, and the text-overflow: ellipsis kicks in.
Another option would be to add overflow: hidden to the parent with class .account-section
Just click on the Run code snippet button, and you'll see.
main {
height: 200px;
display: grid;
place-items: center;
padding: 50px;
}
.container {
position: relative;
display: grid;
grid-template: fit-content(100%) / 100%;
max-width: 1240px;
width: 100%;
position: relative;
}
.account-section {
height: fit-content;
background: var(--white);
box-shadow: 0 0 50px rgb(0, 0, 0, 0.15);
padding: 25px;
position: relative;
flex-shrink: 1;
overflow: hidden;
}
.username {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
width: 20%;
}
.username-fullwidth {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
<main>
<div class="container">
<section class="account-section">
<h5 class="username">WWWWWWWWWWWWWWWWW</h5>
</section>
</div>
<div class="container">
<section class="account-section">
<h5 class="username-fullwidth">WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWw</h5>
</section>
</div>
</main>
Related
I have a page that overflows the viewport both horizontally and vertically, and I'd like to sticky a nav so that it is always at the top and horizontally centered.
Right now, I can get sticky top working, but the centering does not work. Can anyone help?
body {
text-align: center;
}
#header {
background-color: yellow;
width: max-content;
position: sticky;
top: 0;
left: 50%;
translate: -50%
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
<div id="header">
I should always be at the top and centered
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
CodePen: https://codepen.io/hbchin/pen/bGjpQLJ
After doing some digging I found this:
Why is my element not sticking to the left when using position sticky in css?
Essentially, it's not sticking because the body is automatically expanding to the width of the size of the very big box.
Putting it in an inline-block container will make the width not auto-expand to children, and thus allow sticking behavior.
So this works:
body {
text-align: center;
}
#header {
background-color: yellow;
width: max-content;
position: sticky;
top: 0;
left: 50%;
translate: -50%
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
#whole-thing {
display: inline-block;
}
<div id="whole-thing">
<div id="header">
I should always be at the top and centered
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
</div>
Unlike position: sticky and vertical positioning, left: 50% isn't a dynamic positioning option; it just sets the initial position. A horizontal scrollbar will still cause it to move, so that it remains "50% from the left edge".
To achieve a fixed left-right position, add a header container with position: fixed around your header, and within that, your header div can get auto margins:
body {
text-align: center;
max-width:100vw;
overflow:scroll;
}
/*added*/
#headercontainer{
position:fixed;
width:100vw;
left:0;
top:0;
}
#header {
background-color: yellow;
width: max-content;
/*left: 50%;*/ /*Removed*/
margin:auto;/*added*/
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
<div id="headercontainer"> <!-- added -->
<div id="header">
I should always be at the top and centered
</div>
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
You mean something like this?:
<div id="header-container">
<div id="header">
I should always be at the top and centered
</div>
</div>
<div id="container">
<span>
I am extremely large and wide
</span>
</div>
body {
text-align: center;
}
#header-container {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 20px;
overflow: auto;
background-color: yellow;
}
#header {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
width: max-content;
}
#container {
background-color: black;
color: white;
width: 200vw;
height: 200vh;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
I'm pretty new to coding, and I'm trying to use a grid.
But I got stuck a little bit.
I have the following codePan project:
* {
box-sizing:border-box
}
body {
height:100vh;
width: 100%;
background-color: red;
padding:0;
margin:0;
}
#wrapper {
background-color:blue;
height:100%;
width:100%;
margin:0;
padding:0;
display: grid;
grid-template-areas:
"nav"
"content"
"footer" ;
grid-template-columns: 1fr;
grid-template-rows: .8fr 1fr .8fr ;
}
#nav {
background-color: yellow;
grid-area: "nav";
position: sticky;
top: 0;
}
#content {
background-color: pink;
grid-area: "nav";
display:flex;
flex-direction: column;
}
footer {
background-color: brown;
grid-area: "nav";
}
#box1 {
background-color:purple;
height:50%;
}
#box2 {
background-color: blue;
height:60%;
display:flex;
flex-direction: column;
}
.image {
height: auto;
width: 400px;
position: relative;
top:-150px;
}
button {
height: 90px;
width: 400px;
background-color: #262A58;
color: white;
font-size: 1.5em;
font-family: 'EightOne';
border-radius: 10px;
border-style: none;
cursor: pointer;
}
<div id="wrapper">
<div id="nav"> nav </div>
<div id="content">
<div id="box1">
<h1>flower</h1>
</div>
<div id="box2">
<img class="image" src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.goldpetal.net%2Fmedia%2Fimages%2Fproduct_gallery%2FGerbera_Flower_12.jpg&f=1&nofb=1" alt="">
<button> buy flower </button>
</div>
</div>
<footer>
footer
</footer>
</div>
what i want is the similar like on this picture:
picture
unfortunatly i can not postitioning, the contents in the Box2, becuase the button goes out completly out from grid.
I tried several options, but at the end it was always some issue with the content in the Box2.
if someone could give me an advice. Probably I'm using wrong way Grid...
Thank you!
Let your grid define the size of the areas, and use Flexbox to lay out your content.
You can absolutely achieve this layout with CSS Grid. The button was overflowing its content area because #box2 has a fixed height of 60% of the content grid area, but your .image and the button are taller than that. Basically your image pushes down the button.
You don’t need to set explicit heights on your content, because that’s taken care of the grid.
The white-blue background is a bit tricky, because it doesn’t align exactly with the edges of your image. But you can achieve this affect with a background gradient on your content container.
Check out this working solution as inspiration:
body {
height: 100vh;
padding: 0;
margin: 0;
}
#wrapper {
height: 100%;
width: 100%;
display: grid;
grid-template-areas:
'nav'
'content'
'footer';
grid-template-columns: 1fr;
grid-template-rows: 64px 1fr 96px;
}
#nav {
grid-area: nav;
position: sticky;
top: 0;
background-color: white;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}
#content {
grid-area: content;
display: flex;
flex-direction: column;
justify-content: center;
background: linear-gradient(to bottom, transparent 60%, #aad 60%);
align-items: center;
padding: 32px;
}
#content .wrapper {
display: flex;
flex-direction: column;
width: 400px;
}
footer {
grid-area: footer;
}
.image {
border: solid 16px #aad;
margin: 16px;
}
button {
padding: 8px 16px;
background-color: #262a58;
color: white;
font-size: 1.5em;
font-family: 'EightOne';
border-radius: 10px;
border-style: none;
cursor: pointer;
margin-top: 32px;
align-self: center;
}
<div id="wrapper">
<div id="nav">nav</div>
<div id="content">
<div class="wrapper">
<h1>flower</h1>
<img
class="image"
src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.goldpetal.net%2Fmedia%2Fimages%2Fproduct_gallery%2FGerbera_Flower_12.jpg&f=1&nofb=1"
alt="Flower"
/>
<button>buy flower</button>
</div>
</div>
<footer>footer</footer>
</div>
Grid is not really used to make a full webpage. Everything is automatically stacked on top of eachother and should not have a fixed height. The height should changed based on the content.
I have a pop-up modal which works overall, however the one annoyance is it has a hardcoded max-height which I'd like to eliminate.
Option #1:
Initially I explored using height: auto on the modal, which does keep the modal height to the natural height of the contents. However this effects the collapsing of the modal when you scale the browser viewport to a short height. The modal overflows out of the viewport, instead of only the green image area overflowing.
Option #2: I'm aware of the possibility of max-content (for height... or even max-height ?) but I haven't been able to get it to work anywhere, and anyhow it has spotty browser support.
Option #3 (current): Setting the modal to height: 100% and max-height: 500px is good enough, however obviously the content needs to be shorter than that.
Overall, requirements are:
A - In small screens, the modal should collapse with the green image area overflowing, thereby maintaining modal title and buttons in view.
B - In large screens, the modal height should only be as big as the contents.
C - Whatever happens, the modal should never visibly go past the global padding (2em).
See #modal in CSS below:
Demo and code here (Codepen)
html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
#app {
background-color: gray;
width: 75%;
height: 75%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
padding: 2em;
}
#container {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#modal {
/* OPTION #1 */
/* FAILS in small screen: overflow of green image not invoked */
/* height: auto; */
/* OPTION #2 */
/* Not working? */
/* height: max-content; */
/* OPTION #3 */
/* WORKS but specifying a max-height is not ideal */
height: 100%;
max-height: 500px;
width: auto;
position: relative;
background-color: pink;
overflow: hidden;
}
#modal_inner {
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
padding: 2em;
box-sizing: border-box;
}
#image {
overflow-y: auto;
overflow-x: hidden;
flex: 1;
}
#image .inner {
background-color: lime;
padding: 1em;
width: 400px;
height: 200px;
}
#controls {
background-color: yellow;
display: flex;
justify-content: center;
max-width: 20em;
width: 100%;
padding: 1em;
margin-top: 1em;
}
#cta {
background-color: white;
display: flex;
justify-content: center;
max-width: 10em;
padding: 1em;
margin-top: 1em;
}
<div id="app">
<div id="container">
<div id="modal">
<div id="modal_inner">
<div id="title">TITLE</div>
<div id="image">
<div class="inner">image</div>
</div>
<div id="controls">controls</div>
<div id="cta">submit</div>
</div>
</div>
</div>
</div>
You are almost good, use max-height:100% and also add display:flex that will give the height:100% effect you are trying to achieve on the modal_inner
html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
#app {
background-color: gray;
width: 75%;
height: 75%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
padding: 2em;
}
#container {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#modal {
max-height: 100%;
display:flex;
position: relative;
background-color: pink;
overflow: hidden;
}
#modal_inner {
display: flex;
flex-direction: column;
align-items: center;
/*height: 100%; remove this*/
padding: 2em;
box-sizing: border-box;
}
#image {
overflow-y: auto;
overflow-x: hidden;
flex: 1;
}
#image .inner {
background-color: lime;
padding: 1em;
width: 400px;
height: 200px;
}
#controls {
background-color: yellow;
display: flex;
justify-content: center;
max-width: 20em;
width: 100%;
padding: 1em;
margin-top: 1em;
}
#cta {
background-color: white;
display: flex;
justify-content: center;
max-width: 10em;
padding: 1em;
margin-top: 1em;
}
<div id="app">
<div id="container">
<div id="modal">
<div id="modal_inner">
<div id="title">TITLE</div>
<div id="image">
<div class="inner">image</div>
</div>
<div id="controls">controls</div>
<div id="cta">submit</div>
</div>
</div>
</div>
</div>
This question already has answers here:
Flexbox column-reverse in Firefox, Edge and IE
(4 answers)
Closed 4 years ago.
This code is designed to show the numbers in column reverse order from 1 to 4 so I suddenly realize I did not like how display flex was setting the .numbers height and it was ignoring my height in 200px in the numbers class name so I added
flex-shrink: 0;
and it prevented display flex from setting it's own height and it suddenly showed the .numbers original height so I was happy :)
so this is how it looks in Chrome
but sadly flex-shrink: 0 gave strange results in Edge, IE and Fire fox I notice in those browsers it removed the scrollbar and it ignored the overflow-y: auto; mentioned in the #numbers-container.
How can I get it to work like the chrome browser in those other browsers that it did not work in ? :(
Code
#container{
background-color: #d6b68d;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
}
#numbers-container{
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
display: flex;
flex-direction: column-reverse;
}
.numbers{
background-color: forestgreen;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
flex-shrink: 0;
}
.numbers h1{
text-align: center;
color: white;
}
<div id='container'>
<div id='numbers-container'>
<div class='numbers'>
<h1>1</h1>
</div><!--</numbers>-->
<div class='numbers'>
<h1>2</h1>
</div><!--</numbers>-->
<div class='numbers'>
<h1>3</h1>
</div><!--</numbers>-->
<div class='numbers'>
<h1>4</h1>
</div><!--</numbers>-->
</div><!--</numbers-container>-->
</div><!--</container>-->
You could move the scrolling onto it's own container:
/* CSS used here will be applied after bootstrap.css */
#container {
background-color: #d6b68d;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
}
#scroll {
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
}
#numbers-container {
background-color: orange;
overflow-x: hidden;
display: flex;
flex-direction: column-reverse;
}
.numbers {
background-color: forestgreen;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
flex-shrink: 0;
}
.numbers h1 {
text-align: center;
color: white;
}
<div id="container">
<div id="scroll">
<div id="numbers-container">
<div class="numbers">
<h1>1</h1>
</div>
<!--</numbers>-->
<div class="numbers">
<h1>2</h1>
</div>
<!--</numbers>-->
<div class="numbers">
<h1>3</h1>
</div>
<!--</numbers>-->
<div class="numbers">
<h1>4</h1>
</div>
<!--</numbers>-->
</div>
<!--</numbers-container>-->
</div>
<!--</scroll-container>-->
</div>
<!--</container>-->
However, if you want to start your scroll from the bottom, you would probably need to use js
I have a layout where I want to make the breadcrumb trail use up all of the available space in the row. In the same row I also have a div (.tools) which needs to be of a dynamic width as it's contents may vary.
At the moment I have to set the breadcrumb trail to a % width to get it to work roughly correctly, but this isn't ideal... I'd like it to just expand to fill the available space.
Any ideas?
header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 20px;
background-color: lightgreen;
width: 100%;
box-sizing: border-box;
}
.breadcrumb {
display: flex;
align-items: center;
justify-content: flex-start;
white-space: nowrap;
width: 55%;
background-color: lightblue;
}
.breadcrumbItem {
display: flex;
flex: 0 10 auto;
min-width: 45px;
align-items: center;
overflow: hidden;
text-overflow: ellipsis;
}
.breadcrumbItem button {
border: 0;
background: none;
padding: 0 5px;
flex: 0 1 auto;
white-space: nowrap;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
}
.tools {
display: flex;
justify-content: flex-end;
background-color: lightyellow;
}
.tools span, .tools button {
white-space: nowrap;
}
<header>
<div class="breadcrumb">
<div class="breadcrumbItem">
<button>Really long breadcrumb item here</button>
<span>
<span>></span>
</span>
</div>
<div class="breadcrumbItem">
<button>Another really long item that should ellipse when it gets too long, oidsf jsodifj dsoifj sdoifj sdoifj sdoifj sdoifj sdoifj sdoifj dsoifj sdoifj sd</button>
</div>
</div>
<div class="tools">
<span>0 photos selected</span>
<button>Button</button>
</div>
</header>
https://codepen.io/anon/pen/NYaQyV
You can use flex-grow: 1; or the shorthand flex: 1; along with min-width or overflow.
.breadcrumb {
...
flex: 1;
min-width: 0;
}
Or
.breadcrumb {
...
flex: 1;
overflow: hidden;
}
CodePen
you can achieve what you want with by adding flex: auto to the .breadcrumb selctor block. a lot of your css declarations are redundant. you need to look into them and group selectors having much declarations in common
header {
-webkit-display: flex;
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 20px;
background-color: lightgreen;
width: 100%;
box-sizing: border-box;
}
.breadcrumb {
flex: auto; //note this line .you can change it to auto as well
display: flex;
align-items: center;
justify-content: flex-start;
white-space: nowrap;
background-color: lightblue;
}
.breadcrumbItem {
flex: auto;
min-width: 45px;
overflow: hidden;
text-overflow: ellipsis;
}
There's a lot of redundancy in your code. Here's a simplified version that may achieve your goals.
header {
display: flex;
padding: 16px 20px;
background-color: lightgreen;
}
.breadcrumb {
flex-grow: 1;
display: flex;
overflow: hidden;
background-color: lightblue;
}
.breadcrumbItem {
display: flex;
}
button {
border: 0;
background: none;
padding: 0 5px;
}
.breadcrumbItem:last-child {
overflow: hidden;
}
.breadcrumbItem:last-child button { /* ellipsis on second button only */
overflow: hidden;
text-overflow: ellipsis;
}
.tools {
margin-left: auto;
background-color: lightyellow;
}
* {
box-sizing: border-box;
white-space: nowrap;
}
<header>
<div class="breadcrumb">
<div class="breadcrumbItem">
<button>Really long breadcrumb item here</button>
<span>
<span>></span>
</span>
</div>
<div class="breadcrumbItem">
<button>Another really long item that should ellipse when it gets too long, oidsf jsodifj dsoifj sdoifj sdoifj sdoifj sdoifj sdoifj sdoifj dsoifj sdoifj sd</button>
</div>
</div>
<div class="tools">
<span>0 photos selected</span>
<button>Button</button>
</div>
</header>
revised codepen