Item with position fixed act as position absolute - css

.second{
width: 150px;
height: 150px;
border: 2px solid red;
}
.first{
position:fixed ;
top: 0;
}
<div class="container">
<div class="first">
<li>fff</li>
<li>fff</li>
<li>ff</li>
<li>fff</li></div>
<div class="second"></div>
</div>
How to avoid the positioning of "second" over "first" container? I want to position both containers side by sie (first->left;second->right). With position fixed i want to apply the sticky property.

You can use CSS flex-box to build what you want ;)
.container {
display: flex;
}
.first {
width: 150px;
height: 150px;
border: 2px solid blue;
}
.second {
width: 150px;
height: 150px;
border: 2px solid red;
}
<div class="container">
<div class="first">
<ul>
<li>fff</li>
<li>fff</li>
<li>ff</li>
<li>fff</li>
</ul>
</div>
<div class="second"></div>
</div>

Make use of
display: inline-block;

I don't know what you want to do with position: fixed but you can achieve your goal of side by side div using the following code.
.second{
width: 150px;
height: 150px;
border: 2px solid red;
float:left
}
.first{
width:150px;
height:150px;
border: 2px solid blue;
top: 0;
float:left
}
<div class="container">
<div class="first">
<li>fff</li>
<li>fff</li>
<li>ff</li>
<li>fff</li></div>
<div class="second"></div>
</div>

Building on the answer from masoudmanson if you want the red box to align right, you can do the following;
.container {
display: flex;
flex-wrap:no-wrap;
}
.first {
flex-grow:1;
border: 2px solid blue;
}
.second {
width: 150px;
height: 150px;
border: 2px solid red;
}
<div class="container">
<div class="first">
<ul>
<li>fff</li>
<li>fff</li>
<li>ff</li>
<li>fff</li>
</ul>
</div>
<div class="second"></div>
</div>

Related

CSS How to prevent background showing through when scrollbar is active

Does anyone know how to remove the thin gap to the left of the scrollbar that is letting the background through (showing green on this example)?
.menu {
min-width: 195px;
max-height: 200px;
overflow: auto;
position: fixed;
background-color: #F9F9F9;
border: solid 1pt yellow;
}
.button {
border: solid 1px white;
min-width: 200px;
}
body {
background-color: green;
}
<div id="x" class="menu" style="top: 100px; left: 100px;">
<div class="button">a</div>
<div class="button">b</div>
<div class="button">c</div>
<div class="button">d</div>
<div class="button">e</div>
<div class="button">f</div>
<div class="button">g</div>
<div class="button">h</div>
<div class="button">i</div>
<div class="button">j</div>
<div class="button">k</div>
<div class="button">l</div>
<div class="button">m</div>
<div class="button">n</div>
<div class="button">o</div>
<div class="button">p</div>
A running example of this can be found on https://jsfiddle.net/Abeeee/f9jtsrw0/29/
It seems like a bug to me but I replaced:
border: solid 1pt yellow;
from your .menu with:
border: solid 1px yellow;
The pt seems to make rounding errors occur.
I've added border-right: 0pt; to the menu css which removes the right border.
.menu {
width: 200px;
height: 195px;
overflow-y: scroll;
overflow-x: hidden;
position: fixed;
background-color: #F9F9F9;
border: solid 1pt yellow;
border-right: 0pt;
}
.button {
border: solid 1px white;
width: 200px;
}
body {
background-color: green;
}
.background {
background-color: white;
z-index: -1;
}
<div id="x" class="menu" style="top: 100px; left: 100px;">
<div class="background">
<div class="button">a</div>
<div class="button">b</div>
<div class="button">c</div>
<div class="button">d</div>
<div class="button">e</div>
<div class="button">f</div>
<div class="button">g</div>
<div class="button">h</div>
<div class="button">i</div>
<div class="button">j</div>
<div class="button">k</div>
<div class="button">l</div>
<div class="button">m</div>
<div class="button">n</div>
<div class="button">o</div>
<div class="button">p</div>
</div>
</div>

How to make div overflow instead of going on new line?

Want to create a slider containing 3 image containers and need all of them in the same row. All image containers are same size, but the size of slider is around 2.5 images, so the third one should overflow instead of going to new row. Example:
How it should look
What i have for now
html
<div class="image-slide-wrapper">
<div class="images-section">
<div class="image-slide">
<div class="slide-img">
</div>
</div>
<div class="image-slide">
<div class="slide-img">
</div>
</div>
<div class="image-slide">
<div class="slide-img">
</div>
</div>
</div>
</div>
scss
.image-slide-wrapper {
float: left;
height: 55%;
width: 100%;
.images-section {
float: left;
height: 85%;
width: 100%;
border: 1px solid grey;
border-radius: 8px;
overflow: hidden;
padding: 0.125rem;
}
.image-slide {
float: left;
height: 100%;
width: 100px;
margin-right: 0.25rem;
.slide-img {
float: left;
height: 100%;
width: 100%;
border-radius: 0.35rem;
background-color: #e9e9e9;
in the text slider is images-section class and image container is image-slide class. Forgot to mention that I have to go with float to make consistent with the rest of the code.
<div style="overflow: hidden; width: 250px;">
<div style="width: max-content">
<img style="width: 100px;height: 100px;border: 1px solid black;" src="...">
<img style="width: 100px;height: 100px;border: 1px solid black;" src="...">
<img style="width: 100px;height: 100px;border: 1px solid black;" src="...">
</div>
</div>
You could remove float from the thumb elements and use display: flex and flex-flow: row nowrap on the container
.container {
height: 100px;
width: 250px;
border: 1px solid grey;
border-radius: 8px;
overflow: hidden;
padding: 0.125rem;
display: flex;
flex-flow: row nowrap;
}
.image-slide {
height: calc(100% - .25rem);
flex: 1 0 100px;
margin-right: 0.25rem;
border: 1px #ccc solid;
}
<div class="container">
<div class="image-slide"></div>
<div class="image-slide"></div>
<div class="image-slide"></div>
<div class="image-slide"></div>
</div>
This works. You can add display:flex; and flex-wrap:nowrap; to .image-slide-wrapper class.
.main {
float: left;
height: 85%;
width: 100%;
border: 1px solid grey;
border-radius: 8px;
overflow: hidden;
padding: 0.125rem;
display:flex;
flex-wrap:nowrap;
}
.slider {
float: left;
height: 100%;
width: 300px;
margin-right: 0.25rem;
display: block;
}
<div class="main">
<div class="slider">
<img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTBYCs940TLL3tx_nyn7fVeidXWZyfOKV5QrQ&usqp=CAU">
</div>
<div class="slider">
<img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTBYCs940TLL3tx_nyn7fVeidXWZyfOKV5QrQ&usqp=CAU">
</div>
<div class="slider">
<img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTBYCs940TLL3tx_nyn7fVeidXWZyfOKV5QrQ&usqp=CAU">
</div>
</div>

How to vertical middle align div with uncertain height img?

UI told me to make a list, the list item height is not fixed, it depends on the images uploaded by user, and the user may upload a 10x1000 image or 1000x10 image, whatsoever, the image width is fixed to 100px, but height is auto. At right side of the image, there are some text written by user, which is not a one line text, it is multiline text which we don't know how many lines there will be.
html like below:
<ul>
<li class="container">
<img src="https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
<div class="aaa">
aaa<br>bbb<br>ccc
</div>
</li>
<li class="container">
<img src="https://gss0.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/9358d109b3de9c822f4d68126981800a19d84307.jpg">
<div class="aaa">
ddd<br>eee<br>ffff
</div>
</li>
</ul>
css as below:
.container {
border: 1px solid green;
padding: 1px;
position:relative;
}
.container img {
width: 300px;
vertical-align: middle;
}
.aaa {
display: inline-block;
font-size: 16px;
border: 1px solid red;
box-sizing: border-box;
}
.bbb {
border: 1px solid blue;
}
But the result is as below:
How can I make the text box vertical align middle to the image at left side?
All code is at Codepen, you can try it there.
Thank you!
Declare vertical-align: middle on the sibling element (.aaa) as well.
To display x2 sibling inline block-level elements vertically center to each other, both elements should have the property vertical-align: middle.
Code Snippet Demonstration:
.container {
border: 1px solid green;
padding: 1px;
position:relative;
}
.container img {
width: 300px;
vertical-align: middle;
}
.aaa {
display: inline-block;
font-size: 16px;
border: 1px solid red;
box-sizing: border-box;
vertical-align: middle; /* additional */
}
.bbb {
border: 1px solid blue;
}
<ul>
<li class="container">
<img src="https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
<div class="aaa">
aaa<br>bbb<br>ccc
</div>
</li>
<li class="container">
<img src="https://gss0.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/9358d109b3de9c822f4d68126981800a19d84307.jpg">
<div class="aaa">
ddd<br>eee<br>ffff
</div>
</li>
</ul>
You can use the power of flex.
Use align-items: center to do the trick.
Check it out: https://codepen.io/anon/pen/dJmrvB
Solution1: Just apply vertical-align: middle; to .aaa class
.container {
border: 1px solid green;
padding: 1px;
position: relative;
}
.container img {
width: 300px;
vertical-align: middle;
}
.aaa {
display: inline-block;
font-size: 16px;
border: 1px solid red;
box-sizing: border-box;
vertical-align: middle;
}
.bbb {
border: 1px solid blue;
}
<ul>
<li class="container">
<img src="https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
<div class="aaa">
aaa<br>bbb<br>ccc
</div>
</li>
<li class="container">
<img src="https://gss0.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/9358d109b3de9c822f4d68126981800a19d84307.jpg">
<div class="aaa">
ddd<br>eee<br>ffff
</div>
</li>
</ul>
Solution2: Try to make use of flex CSS
.container {
border: 1px solid green;
padding: 1px;
position: relative;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.container img {
width: 300px;
}
.aaa {
font-size: 16px;
border: 1px solid red;
box-sizing: border-box;
}
.bbb {
border: 1px solid blue;
}
<ul>
<li class="container">
<img src="https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
<div class="aaa">
aaa<br>bbb<br>ccc
</div>
</li>
<li class="container">
<img src="https://gss0.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/9358d109b3de9c822f4d68126981800a19d84307.jpg">
<div class="aaa">
ddd<br>eee<br>ffff
</div>
</li>
</ul>

How to set CSS to grow parent height only from one of two children?

I would like to have the parent (orange border) only grow to the size of the first child (grey background) and have the second child overflow vertically.
This is what I have:
This is what I want:
Codepen: https://codepen.io/gbucher/pen/wPGBpN
HTML:
<div class='parent'>
<div class='child1'>
</div>
<div class='child2'>
<div class='elem'>
A
</div>
<div class='elem'>
B
</div>
<div class='elem'>
C
</div>
<div class='elem'>
D
</div>
<div class='elem'>
E
</div>
</div>
</div>
CSS:
.parent {
border: 2px solid orange;
display: flex;
/* How to make it work without a fixed
height ?
height:60px;
*/
}
.child1 {
height: 60px;
width: 300px;
background: #888;
}
.child2 {
display: flex;
flex-direction: column;
overflow-y: auto;
}
.elem {
border: 1px solid black;
margin-top: -1px;
padding: 3px;
width: 10rem;
}
When computing the size of a parent, absolute positioned children are ignored.
The solution is thus to use a scroll wrapper around the second child. The wrapper should have position: relative and the scrolling child should have position: absolute.
.parent {
border: 2px solid orange;
display: flex;
}
.child1 {
height: 70px;
flex-grow: 1;
background: #888;
}
.child2 {
display: flex;
flex-direction: column;
position: absolute;
background: orange;
}
.scroll {
position: relative;
overflow-y: scroll;
overflow-x: hidden;
width: 323px;
background: green;
}
.elem {
border: 1px solid black;
margin-top: -1px;
padding: 3px;
width: 300px;
}
<div class='parent'>
<div class='child1'>
one
</div>
<div class='scroll'>
<div class='child2'>
<div class='elem'>
A
</div>
<div class='elem'>
B
</div>
<div class='elem'>
C
</div>
<div class='elem'>
D
</div>
<div class='elem'>
E
</div>
</div>
</div>
</div>

Hoping for guidance with “display: table-cell;” and padding

I would like the content in the second column to have 10px of space above it. But when I add "padding-top: 10px;" the content of both columns comes down 10px. Can anyone suggest a solution?
CSS:
#div1 { display: table;}
#div1-1 { display: table-cell;
width: 200px;
border: 1px solid #aaa;
}
#div1-2 { display: table-cell;
width: 200px;
border: 1px solid #aaa;
padding-top: 10px;
}
HTML:
<div id="div1">
<div id="div1-1">Apples</div>
<div id="div1-2">Oranges</div>
</div>
If you add float:left; to #div1-1, the first column will remain as it is.
My solution is to try to add a box in each cell
#myDiv {
display: table;
}
#div1 { display: table-row;}
#div1-1{
display: table-cell;
width: 200px;
border: 1px solid #aaa;
}
#div1-2 {
display: table-cell;
width: 200px;
border: 1px solid #aaa;
}
.space{
height:10px;
width:100%;
display:block;
}
.content{
width:100%;
}
<div id="myDiv">
<div id="div1">
<div id="div1-1">
<div class="content">Apples</div>
<div class="space"> </div>
</div>
<div id="div1-2">
<div class="space"> </div>
<div class="content">Oranges</div>
</div>
</div>
</div>

Resources