I have four divs inside a container like this:
<div class="container">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
I would like to keep the first and last div aligned to the edges of the screen and only center the two divs in the middle. I tried display inline block and adjusting the margins, but i just can't figure it out. Please try and enlighten me!
Kind Regards
Use this HTML
<div class="container">
<div class="block left">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block right">4</div>
</div>
and then this CSS
.container {
display:block;
text-align:center
}
.block {
display:inline-block;
border:1px solid #000;
padding:10px;
margin:auto
}
.left {
float:left
}
.right {
float:right;
}
you can also use first-child and last-child, but it's easier to add a class to first and last div
See fiddle here
Try using :nth-of-type() pseudo-class allows you to select elements with a formula,like this: http://jsfiddle.net/csdtesting/11fh7suy/
//second div
div.container div:nth-of-type(2) {
background-color: #9999ff;
text-align:center;
}
//third div
div.container div:nth-of-type(3) {
background-color: #9999ff;
text-align:center;
}
Hope it helps!
Adding an extra container will give you more control:
<div class="container">
<div class="block">1</div>
<div class="wrap">
<div class="block">2</div>
<div class="block">3</div>
</div>
<div class="block right">4</div>
</div>
.wrap{width:50px;margin:0 auto}
.block{float:left;width:25px;text-align:center}
.right{float:right}
Related
I have the following div arrangement, with the below css. Everywhere online seems to say you need margin:0 auto; for centering, but the text still goes to the top right,
Any help appreciated.
.menu_item {
margin: 0 auto;
}
<div style='position:absolute;width:110%;height:110%;left:-5%;top:-5%;background-color:white;z-index:7;overflow:none;'>
<div id='menu_but' style='width:36px;height:36px;'>
<div class='menu_line' />
<div class='menu_line' />
<div class='menu_line' />
</div>
<div id='menu'>
<div class='menu_item'>HOME</div>
<div class='menu_item'>ABOUT US</div>
<div class='menu_item'>CONTACT US</div>
<div class='menu_item'>PORTFOLIO</div>
</div>
</div>
First of all, <div> is not a self-closing tag so you have to write </div> instead of <div class='menu_line'/>
Then, you could simply do:
<style>
.menu_item{
display: inline-block;
}
#menu {
position: relative;
text-align: center;
}
</style>
<div id="menu">
<div class="menu_item">HOME</div>
<div class="menu_item">ABOUT US</div>
<div class="menu_item">CONTACT US</div>
<div class="menu_item">PORTFOLIO</div>
</div>
maybe use flexbox ?
.parent {
display:flex;
justify-content:center;
align-items:center;
}
All of the child elements will be centered
This code
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
displays the divs like this.
div1
div2
div3
div3
I want to display these like
div1 div2 div3 div4
The number of divs placed horizontally is not fixed and varies, so I cannot write the css classes with left-padding, width etc defined.
Whats the best way to do it?
Use flexbox:
.container {
display:flex;
flex-direction:row;
}
.container > div {
border:5px dotted black;
background:red;
flex-grow:1;
padding:2em;
text-align:center;
}
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
Some prefixing may be required depending on your target audience.
Learn about all the possibilities here.
Set the display style to inline-block
<div id="div1" style="display:inline-block"></div>
<div id="div2" style="display:inline-block"></div>
<div id="div3" style="display:inline-block"></div>
<div id="div4" style="display:inline-block"></div>
or better yet
<style>
#div1, #div2, #div3, #div4 {
display:inline-block
}
</style>
Just add a class like
<div id="div1" class="inline">lorem</div>
<div id="div2" class="inline">ipsum</div>
and in your CSS :
.inline {
float:left;
}
Additionaly, you can add some padding...
Here is a fiddle to essentially what I want to do - reorder divs using floating to float the middle div to the left on smaller screen sizes, and the outer two to the right.
Here's the current order:
<div id='one'></div>
<div id='two'></div>
<div id='three'></div>
In addition to what's already there, I want the outer two divs to sit underneath the middle div on the left. Essentially I want it to look like:
<div id='two'></div>
<div id='one'></div>
<div id='three'></div>
Where div two has display:block, and display one/three have display:inline-block - without changing the html.
Is this possible with just CSS?
This can be done:
<div class="boxes">
<div class="one box">Box One</div>
<div class="two box">Box Two</div>
<div class="three box">Box Three</div>
<div class="four box">Box Four</div>
</div>
CSS:
#media only screen and (max-width:768px) {
.boxes {
display:table;
width:100%;
}
.box {
display:block;
width:100%;
}
.three { display:table-caption; }
.four { display:table-header-group; }
.one { display:table-footer-group; }
}
Source: http://www.iandevlin.com/blog/2013/06/css/css-stacking-with-display-table (check the demo linked)
I know this question has been asked before. However, I do not seem to find my mistake.
I set up a plnkr. I try to align some text vertically, whereas the the stuff that needs to be aligned is nested into multiple divs.
<div class="news col-md-12" id="detailsView" >
<div class="col-md-8 ">
<div class="row">
<div class="col-md-5 widthInDetails">
<div class="row">
<div class="col-md-8" id="newsDetails">
Hello I am fine and how are your (I am good too)
</div>
</div>
</div>
</div>
</div>
</div>
and in my CSS I have this.
.news{
background-color:black;
line-height:200px;
width:200px;
height:200px;
display:table;
}
#newsDetails{
display:table-cell;
vertical-align:middle;
}
So basically I have I lineheight given from the outer div with class="news". I want the the nested div to be aligned vertically. Is there any way to do this?
http://plnkr.co/edit/cMaCrG1Y8Ky48HwtgNPk?p=preview
If you want to use the display: table property, you should set it in the parent node, like:
<div class="news col-md-12" id="detailsView" >
<div class="col-md-8 ">
<div class="row">
<div class="col-md-5 widthInDetails">
<div id="newsParent" class="row">
<div class="col-md-8" id="newsDetails">
Hello I am fine and how are your (I am good too)
</div>
</div>
</div>
</div>
</div>
</div>
.news{
background-color:black;
width:200px;
height:200px;
}
#newsParent {
display:table;
}
#newsDetails{
height: 200px;
display:table-cell;
vertical-align:middle;
}
Give it a try and let me know if it helps!
You can make wrapper display: table and child div display: table-cell; vertical-align: middle;. find the below jsfiddle demo.
HTML
<div class="news col-md-12" id="detailsView" >
<div class="col-md-8 ">
<div class="row">
<div class="col-md-5 widthInDetails">
<div class="row newsDetailswrap">
<div class="col-md-8" id="newsDetails">
Hello I am fine and how are you?</div>
</div>
</div>
</div>
</div>
</div>
CSS
.newsDetailswrap{
background: red;
height: 100px;
display:table;
}
#newsDetails{
display: table-cell;
vertical-align: middle;
}
http://jsfiddle.net/hhbnywq1/
I'm trying to apply some CSS to the last .topic-wrapper div (which is inside the entry-content div):
<div class="entry-content">
<div class="bbp-pagination">
<h2 class="dark-title">Top Topics</h2>
<div class="topic-wrapper">
<h4></h4>
<div class="topic-wrapper">
<h4></h4>
<div class="topic-wrapper"> <!-- I'm trying to get this one -->
<h4></h4>
<div class="mainbar">
<div class="bbp-pagination">
with this code: .entry-content div:last-child
But it seems like the CSS is being applied to some divs inside .topic-wrapper.
Like:
<div class="topic-wrapper">
<div class="topic-left">
<h2>
<span>
<span class="bbp-topic-started-in">
<div class="bbp-topic-tags"> <!-- this one -->
<p>
Any suggestions to solve this?
</div>
Unless I'm misreading your nesting you can't. :last-child finds elements that are the last child of their parents, but the last child of entry-content is bbp-pagination.
Additionally .entry-content div:last-child finds ALL divs that are descendants of .entry-content - NOT just the direct child.
You probably want .entry-content>div:last-child which looks only at immediate descendants. (i.e. children, but not grandchildren)
This should do it, using ">" gets you just the direct descendents:
.entry-content > div:last-child
Here is an example:
<div class="container">
<div class="outer">
<div class="inner"></div>
</div>
<div class="outer">
</div>
<div class="outer">
</div>
<div class="outer">
</div>
</div>
.container
{
width:300px;
background-color:yellow;
}
.outer
{
width:100px;
height:100px;
background-color:red;
margin:20px;
}
.inner
{
width:50px;
height:50px;
background-color:pink;
margin:5px;
}
.container > div:last-child
{
background-color:blue;
}
/* uncomment this statement to see the alternative broken version*/
/*
.container div:last-child
{
background-color:green;
}
*/
You can view the example on jsfiddle