I can't make <div> to sit next to each other [duplicate] - html

This question already has answers here:
How to place two divs next to each other? [duplicate]
(13 answers)
Closed 1 year ago.
I want to make some div blocks to go right after the previous one, but here's what I get: instead of staying next to each other, each block starts from a new line.
div.change {
width: 200px;
height: 200px;
background-color: green;
display: inline-block
}
div.change.one {
opacity: 0.7;
}
div.change.two {
opacity: 0.5;
}
div.change.three {
opacity: 0.3;
}
div p {
text-align: center;
padding: 80px 5px
}
<div class="change">
<div class="change one">
<p>here</p>
</div>
<div class="change two">
<p>here</p>
</div>
<div class="change three">
<p>here</p>
</div>
</div>

You've set the size of the parent element and you've set it to to inline-block, but you need to set those properties on the children only. This is one of the hazards of using the same class for nested elements. You can clear things up with an explicit child selector.
div.change div.change {
width: 150px;
height: 150px;
background-color: green;
display: inline-block
}
div.change.one {
opacity: 0.7;
}
div.change.two {
opacity: 0.5;
}
div.change.three {
opacity: 0.3;
}
div p {
text-align: center;
padding: 80px 5px
}
<div class="change">
<div class="change one">
<p>here</p>
</div>
<div class="change two">
<p>here</p>
</div>
<div class="change three">
<p>here</p>
</div>
</div>
You could simplify things by removing the change class from the inner elements:
div.change > div {
width: 150px;
height: 150px;
background-color: green;
display: inline-block
}
div.change .one {
opacity: 0.7;
}
div.change .two {
opacity: 0.5;
}
div.change .three {
opacity: 0.3;
}
div p {
text-align: center;
padding: 80px 5px
}
<div class="change">
<div class="one">
<p>here</p>
</div>
<div class="two">
<p>here</p>
</div>
<div class="three">
<p>here</p>
</div>
</div>

Because you are using display inline change the display to flex and they will be next to each other:
display: flex;
you can use this also:
flex-direction: row;
or
flex-direction: row-reverse;
and remove change class from childrens keep it only on the parent element. i'd suggest to check a flexbox css course or tutorial it's a very useful property in css

I think that your problem is that you are set 200px width to both container and the inner divs via the 'change' class.
Try to set proper wodth to the container element, or use flexbox:
Setting display: flex; in the container div should work (but give it a new class name, don't use 'change' since you use it for the inner divs as well, and this settings is relevant just for the container).
Try to set some different and suitable width to the container in a new class name or other unique selcetor (600px should work).
If you want to know more about flexbox use this:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Related

How to make flex-wrap work with flex-basis?

I have two divs present inside a parent div. The first div has an absolutely positioned image, whereas the other has some text content. I want the content of the parent div to wrap. So this is what I set:
.item-content {
display: flex;
flex-wrap: wrap;
}
.image-container {
flex-basis: 48%;
position: relative;
border: 2px solid red;
}
.image {
display: block;
position: absolute;
left: -100px;
}
.text-content {
margin: 0px auto;
flex-basis: 43%;
}
<div className="item-content">
<div className="image-container">
<img src={desktopImage} alt="" className="image " />
</div>
<div className="text-content">
<h2>{title}</h2>
<p>{subtitleText}</p>
<div className="divider" />
<h5>The challenge</h5>
<p className="description">{description}</p>
</div>
</div>
But, this doesn't make the content wrap when the screen size changes. How can I make the 'text-content' class wrap?
If you want all the content inside <div class="text-content"> to wrap then you just need to add the flex-wrap property to all the elements inside.
.text-content *{
display:flex;
flex-wrap:wrap;
}

Align all the fields in a line after applying word-break prop in css

I have 2 spans inside a div:
html:
<div class="main">
<span class="title"> Name</span>
<span class="values">/sys/fs/cgroup/sys/fs/</span>
</div>
here is how it looks:
http://jsfiddle.net/tdrgz1yj/
as you can see the size and volume title getting pushed down when I have long text for Name field. Also I cannot increase the width any more cause in actual app, the Volume field goes out of focus.
Is there a way I can align the Name, Size and Volume fields all in one line even after applying the word-break prop?
Can I make it look something like with css?
Thanks!
Yes. Just add display: flex to the .parent
Example
If you want to move the long text to the same line with the "Name" title, set display: flex to the .main as well.
Example 2
I suggest using span only to wrap inline elements. Instead, use divs :
.parent {
padding: 20px 10px;
margin: 0 0 20px;
display: flex;
width: 100%;
box-sizing: border-box;
}
.parent div {
margin-left: 20px;
}
.main {
flex-basis: 200px;
flex-grow: 1;
flex-shrink: 0;
display: flex;
}
.title {
margin-right: 20px;
display: inline-block;
}
.values {
word-break: break-word;
}
<div class="parent">
<div class="main">
<div class="title">Name:</div>
<div class="values">/sys/fs/cgroup/sys/fs//sys/fs/cgroup/sys/fs//sys/fs/cgroup/sys/fs/</div>
</div>
<div class="main">
<div class="title">Size:</div>
<div class="values">223k</div>
</div>
<div class="main">
<div class="title">Volume:</div>
<div class="values">111</div>
</div>
</div>

Why does div with "display: flex" and "position: fixed" doesn't occupy the full available width?

I noticed that when div has position: fixed and display: flex, it doesn't occupy the full available width like normal divs do.
.container {
display: flex;
background-color: #ddd;
margin-top: 50px;
}
.fixed {
position: fixed;
}
.content {
background-color: #bbb;
flex-grow: 1;
}
<div class="container">
<div>Title</div>
<div class="content">Content</div>
</div>
<div class="container fixed">
<div>Title</div>
<div class="content">Content</div>
</div>
How could I change my CSS so that the second container occupy the full available width like that first one does?
Why? is already answered by Michael_B
... it is out-of-flow ...
What you can also do is to size the fixed element from left and right coordonates instead width:100%; that is more often a trouble maker than helpfull.
If it is a direct-child of body, it can also inherits margins .
.container {
display: flex;
background-color: #ddd;
margin-top: 50px;
}
.fixed {
position: fixed;
left:0;
right:0;
margin-left:inherit;
margin-right:inherit;
}
.content {
background-color: #bbb;
flex-grow: 1;
}
<div class="container">
<div>Title</div>
<div class="content">Content</div>
</div>
<div class="container fixed">
<div>Title</div>
<div class="content">Content</div>
</div>
The first container represents an in-flow block-level element. Such elements are designed to stack vertically. This means they occupy the full width of their parent container.
The second container represents an absolutely-positioned element (fixed positioning is a form of absolute positioning). This means the element is out-of-flow and doesn't take up any space.
Unlike in-flow block-level elements, absolutely-positioned elements are not designed to stack vertically. So there's no automatic full width. The default width and height of an absolutely-positioned element is based on its content. To override default sizing, set your own lengths.
.container {
display: flex;
background-color: #ddd;
margin-top: 50px;
}
.fixed {
position: fixed;
width: 100%; /* new */
/* alternatively, you can use left: 0 and right: 0 */
}
.content {
background-color: #bbb;
flex-grow: 1;
}
<div class="container">
<div>Title</div>
<div class="content">Content</div>
</div>
<div class="container fixed">
<div>Title</div>
<div class="content">Content</div>
</div>
You have to add width: 100%; , otherwise the width will be just as wide as the contents of the element.
Also, you should add html, body { margin: 0; } to avoid the default margins.
BTW: That has nothing to do with display: flex, but only with position: fixed...
html, body {
margin: 0;
}
.container {
display: flex;
background-color: #ddd;
margin-top: 50px;
}
.fixed {
position: fixed;
width: 100%;
}
.content {
background-color: #bbb;
flex-grow: 1;
}
<div class="container">
<div>Title</div>
<div class="content">Content</div>
</div>
<div class="container fixed">
<div>Title</div>
<div class="content">Content</div>
</div>

Five div boxes, two on a row and the fifth one on top of the four - how do I do this in css?

This is what I have to do and I have no idea how to do it, i can create the four divs, but the fifth one is giving me trouble.
Here is what I need to do: create five divs, four of them need to be grouped in two, and the fifth one needs to be placed on top of the four divs. I have attached an image so you can better understand.
click here to see image
You can do this a bunch of ways. I would create a parent element with a flex row layout set to wrap and position: relative, then let the 4 children occupy the parent, the absolutely position the 5th over the 4.
body {
margin: 0;
}
.flex {
display: flex;
flex-wrap: wrap;
height: 100vh;
position: relative;
}
div > div {
flex-basis: 50%;
height: 50vh;
background: red;
}
div > div:nth-child(2) {
background: blue;
}
div > div:nth-child(3) {
background: green;
}
div > div:nth-child(4) {
background: yellow;
}
div > div:last-child {
background: pink;
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
width: 50vh;;
}
<div class="flex">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
There are different ways to get that done, one such is here:
<div class="parent">
<div class="block a"></div>
<div class="block b"></div>
<div class="block c"></div>
<div class="block d"></div>
<div class="upperBlock"></div>
</div>
<style>
.block{display:inline-block;width:50%;float:left;height:80px}
.a{background:red;}
.b{background:blue;}
.c{background:black;}
.d{background:gold;}
.parent{position:relative;height:160px;}
.upperBlock{height:80px;width:80px;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);background:white;}
</style>
https://jsfiddle.net/vy7th0es/

Position flex children next to each other with align-self

I am trying to make a simple nav with next and previous buttons. Previous is at the far left of the container, and next at the far right. I know what you're thinking: this is the ideal time to use justify-content: space-between. However, when a user is on the first page or the last page, the backend doesn't output the previous and next buttons respectively. This causes the next button to be aligned left on the first page, and that's not what I want.
I thought I could use align-self on the children. The problem I encounter now is that the first child pushes the second one down and I don't know how to fix this.
Here's a snippet
.container {
display: flex;
}
.container * {
width: 10%;
height: 80px;
}
#d1 {
background: green;
align-self: flex-start;
}
#d2 {
background: red;
align-self: flex-end;
}
<div class="container">
<div id="d1">
div 1
</div>
<div id="d2">
div 2
</div>
</div>
I completely forgot about magic margins in flex. Here goes:
.container {
display: flex;
}
.container * {
width: 10%;
height: 80px;
}
#d1 {
margin-right: auto;
background: red;
}
#d2 {
margin-left: auto;
background: green;
}
<div class="container">
<div id="d1">
div 1
</div>
<div id="d2">
div 2
</div>
</div>