I have a floating layout in HTML and I want to place boxes both at the beginning, ending and also in the middle.
Here is my snippet:
html,
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
height: 80%;
width: 80%;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top,
.tray-bottom {
height: 48px;
line-height:48px;
clear: both;
}
.tray-left,
.tray-right {
width: 48px;
height: calc(100% - 96px);
float: left;
position: relative;
}
.tray-right {
float: right;
}
.button {
background-color: yellow;
width: 46px;
height: 46px;
}
.tray-top .button,
.tray-bottom .button {
max-width: 33%;
}
.tray-left .button,
.tray-right .button {
max-height: 33%;
}
.tray-top .button.begin,
.tray-bottom .button.begin {
float: left;
}
.tray-top .button.middle,
.tray-bottom .button.middle {
float: left;
}
.tray-top .button.end,
.tray-bottom .button.end {
float: right;
}
.button.middle {
}
.tray-left .button.end,
.tray-right .button.end {
position: absolute;
bottom: 0px;
}
<div id="root">
<div class="tray tray-top">
<div class="button begin">1</div>
<div class="button middle">2</div>
<div class="button end">3</div>
</div>
<div class="tray tray-left">
<div class="button begin">4</div>
<div class="button middle">5</div>
<div class="button end">6</div>
</div>
<div class="tray tray-right">
<div class="button begin">7</div>
<div class="button middle">8</div>
<div class="button end">9</div>
</div>
<div class="tray tray-bottom">
<div class="button begin">10</div>
<div class="button middle">11</div>
<div class="button end">12</div>
</div>
</div>
How can I make (please see it in Full page):
Box2 and Box11 to go into middle (while keeping box3 and box12 at the end)?
Box5 and Box8 to go into middle vertically?
Box6 and Box9 to go to the end vertically?
I don't want to stick to this layout but I would like to keep the widest support for all the browsers (including IE11 too).
Please note that #root has 80% width and height. It only means #root's size != body's size (so absolute positioning and left: 50% is not a way to go).
I would add some flexbox for this. It would be difficult to do it with only float and probably not good to use positionning:
html,
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
height: 85%;
width: 85%;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top,
.tray-bottom {
height: 48px;
line-height: 48px;
clear: both;
}
.tray-left,
.tray-right {
width: 48px;
height: calc(100% - 96px);
float: left;
display:flex;
flex-direction:column;
justify-content:space-between;
}
.tray-right {
float: right;
}
.button {
background-color: yellow;
width: 46px;
height: 46px;
display:inline-block;
}
.begin {
float:left;
}
.end {
float:right;
}
<div id="root">
<div class="tray tray-top">
<div class="button begin">1</div>
<div class="button middle">2</div>
<div class="button end">3</div>
</div>
<div class="tray tray-left">
<div class="button begin">4</div>
<div class="button middle">5</div>
<div class="button end">6</div>
</div>
<div class="tray tray-right">
<div class="button begin">7</div>
<div class="button middle">8</div>
<div class="button end">9</div>
</div>
<div class="tray tray-bottom">
<div class="button begin">10</div>
<div class="button middle">11</div>
<div class="button end">12</div>
</div>
</div>
This solves my issue but I'm not sure if this is the most sophisticated solution for this problem:
html,
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
height: 80%;
width: 80%;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top,
.tray-bottom {
height: 48px;
line-height:48px;
clear: both;
position: relative;
}
.tray-left,
.tray-right {
width: 48px;
height: calc(100% - 96px);
float: left;
position: relative;
}
.tray-right {
float: right;
}
.button {
background-color: yellow;
width: 46px;
height: 46px;
}
.tray-top .button,
.tray-bottom .button {
max-width: 33%;
}
.tray-left .button,
.tray-right .button {
max-height: 33%;
}
.tray-top .button.begin,
.tray-bottom .button.begin {
float: left;
}
.tray-top .button.middle,
.tray-bottom .button.middle {
float: left;
}
.tray-top .button.end,
.tray-bottom .button.end {
float: right;
}
.button.middle {
float: left;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.tray-left .button.end,
.tray-right .button.end {
position: absolute;
bottom: 0px;
}
<div id="root">
<div class="tray tray-top">
<div class="button begin">1</div>
<div class="button middle">2</div>
<div class="button end">3</div>
</div>
<div class="tray tray-left">
<div class="button begin">4</div>
<div class="button middle">5</div>
<div class="button end">6</div>
</div>
<div class="tray tray-right">
<div class="button begin">7</div>
<div class="button middle">8</div>
<div class="button end">9</div>
</div>
<div class="tray tray-bottom">
<div class="button begin">10</div>
<div class="button middle">11</div>
<div class="button end">12</div>
</div>
</div>
I had to make the middle boxes position: absolute and their parents to position: relative then I'm able to change their position within their parents.
Related
I have two queries in a single program.
query1:
I am trying to align two sub super sub divisions horizontally inside a sub div of a container div. Below is my code, could you please help me out with this. I have attached the desirable output.
query2:
and from the code you can see inside a circle there is a paragraph day, I wanted it to start from the center of the circle such as if the number of days is 1 it should be shown from the center and when there are 3 digit days it should be adjusted in the center. Hope you understand my queries.
.circle {
height: 50px;
width: 50px;
border-radius: 50%;
padding: 5%;
text-align: center;
border-color: black;
border-style: solid;
}
.container-meta {
position: relative;
}
.container-meta .left {
float: left;
}
.container-meta .right {
float: right;
}
.right p,
.left p {
padding: 0;
margin: 0;
}
<div class="container-meta">
<div class="left">
<div class="circle">
<p>days</p>
<p>hours</p>
</div>
<div class="date">
<p>today-date</p>
<p>tomorrow-date</p>
</div>
</div>
<div class="right">
<p>text1</p>
<p>text2</p>
<p>text3</p>
</div>
</div>
current output:
expected output:
You can do this by giving display:flex; to the left class and by giving some margin to one of divs.
.circle {
height: max-content;
width: max-content;
border-radius: 50%;
padding: 5%;
text-align: center;
border-color: black;
border-style: solid;
}
.container-meta {
position: relative;
}
.container-meta .left {
float: left;
display: flex;
}
.container-meta .right {
float: right;
}
.right p,
.left p {
padding: 0;
margin: 0;
width:max-content;
}
.date {
margin-left: 5px;
}
<div class="container-meta">
<div class="left">
<div class="circle">
<p>days</p>
<p>hours</p>
</div>
<div class="date">
<p>today-date</p>
<p>tomorrow-date</p>
</div>
</div>
<div class="right">
<p>text1</p>
<p>text2</p>
<p>text3</p>
</div>
</div>
Solution1.
I add display: flex at the .leftand margin-right: 10px at .circle.
.circle {
height: 50px;
width: 50px;
border-radius: 50%;
padding: 5%;
text-align: center;
border-color: black;
border-style: solid;
margin-right: 10px;
}
.container-meta {
position: relative;
}
.container-meta .left {
display: flex;
float: left;
}
.container-meta .right {
float: right;
}
.right p,
.left p {
padding: 0;
margin: 0;
}
<body>
<div class="container-meta">
<div class="left">
<div class="circle">
<p>days</p>
<p>hours</p>
</div>
<div class="date">
<p>today-date</p>
<p>tomorrow-date</p>
</div>
</div>
<div class="right">
<p>text1</p>
<p>text2</p>
<p>text3</p>
</div>
</div>
</body>
Solution2.
Using flex instead of float.
.circle {
height: 50px;
width: 50px;
border-radius: 50%;
padding: 5%;
text-align: center;
border-color: black;
border-style: solid;
margin-right: 10px;
}
.container-meta {
display: flex;
align-items: center;
justify-content: space-between;
}
.container-meta .left {
display: flex;
align-items: center;
flex-grow: 1;
}
.right p,
.left p {
padding: 0;
margin: 0;
}
<body>
<div class="container-meta">
<div class="left">
<div class="circle">
<p>days</p>
<p>hours</p>
</div>
<div class="date">
<p>today-date</p>
<p>tomorrow-date</p>
</div>
</div>
<div class="right">
<p>text1</p>
<p>text2</p>
<p>text3</p>
</div>
</div>
</body>
I have the following simple html code.
.body-content {
width: 100%;
padding-left: 15px;
padding-right: 15px;
}
.left-menu {
background-color: red;
float: left;
width: 50px;
}
.right-container {
background-color: blue;
width: 100%;
}
.middle-view {
width: 70%;
float: left;
background-color: blueviolet;
}
.right-view {
width: 30%;
float: left;
background-color: burlywood;
}
<div class="body-content">
<div class="left-menu">
abc
</div>
<div class="right-container">
<div class="middle-view">
def
</div>
<div class="right-view">
ghi
</div>
</div>
</div>
I am getting the following result:
But I would like to 'def' and 'ghi' side by side.
I don't have much experience using HTML and CSS but I thought middle-view and right-view together will fill right-container (70% + 30%). But as I see the width given to left-menu (50px) has impact on it.
Here is the solution..
.body-content {
width: 100%;
padding-left: 15px;
padding-right: 15px;
float:left;
}
.left-menu {
background-color: red;
float: left;
width: 50px;
}
.right-container {
background-color: blue;
width:calc(100% - 50px);
float:left;
}
.middle-view {
width: 70%;
float: left;
background-color: blueviolet;
}
.right-view {
width: 30%;
float: left;
background-color: burlywood;
}
<div class="body-content">
<div class="left-menu">
abc
</div>
<div class="right-container">
<div class="middle-view">
def
</div>
<div class="right-view">
ghi
</div>
</div>
</div>
.body-content {
display: flex; /* forces children to same row */
padding-left: 15px;
padding-right: 15px;
}
.left-menu {
width: 50px;
background-color: red;
}
.right-container {
display: flex; /* forces children to same row */
flex: 1; /* consume remaining space on the row */
background-color: blue;
}
.middle-view {
width: 70%;
background-color: blueviolet;
}
.right-view {
width: 30%;
background-color: burlywood;
}
<div class="body-content">
<div class="left-menu">abc</div>
<div class="right-container">
<div class="middle-view">def</div>
<div class="right-view">ghi</div>
</div>
</div>
Everything on my page centers horizontally, including my buttons. However - when I resize my window, the buttons move vertically and off the page. Here is my jsfiddle: http://jsfiddle.net/rjord00r/utjhyckt/
.buttons {
text-align: center;
margin-top: 380px;
}
.buttons button {
padding: 8px;
border: none;
background-color: lightgrey;
font-weight: 500;
font-size: 13px;
position: relative;
text-align: center;
}
Thanks for your help!
This for you:
.searchBox{
position: fixed;
top: 50%;
left: 50%;
margin-top: -92px;
margin-left: -225px;
width: 450px;
text-align:center;
}
.logo {
width: 272px;
}
.search {
width: 450px;
height: 28px;
margin:15px 0px;
}
.search-bar{
position:relative;
}
#icon img {
position: absolute;
height: 28px;
width: 30px;
right: 0px;
top: 18px;
}
input:focus {
border-color: blue;
}
.buttons {
text-align: center;
}
.buttons button {
padding: 8px;
border: none;
background-color: lightgrey;
font-weight: 500;
font-size: 13px;
position: relative;
text-align: center;
}
<div class="searchBox">
<div class="centered">
<img class="logo" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
</div>
<div class="search-bar">
<form>
<input type="text" class="search">
<div id="icon">
<img src="http://i.imgur.com/l5S4LZ2.png" />
</div>
</form>
</div>
<div class="buttons">
<button type="button">Google Search</button>
<button type="button">I'm Feeling Lucky</button>
</div>
</div>
There's a lot of unnecessary positioning, margins and elements in your example. To be able to achieve what I think you're after, I've had to change a few things.
See new EXAMPLE here.
Here's a summary of the changes : -
HTML
<div class="wrapper">
<div class="centered">
<img class="logo" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" />
<form class="search-bar">
<input type="text" class="search">
<div id="icon">
<img src="http://i.imgur.com/l5S4LZ2.png" />
</div>
</form>
<div class="buttons">
<button type="button">Google Search</button>
<button type="button">I'm Feeling Lucky</button>
</div>
</div>
</div>
CSS
.wrapper {
display: block;
position: relative;
width: 100%;
height: 750px;
}
.centered {
position: absolute;
top: 25%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
text-align: center;
}
.logo {
width: 272px;
}
.search {
width: 400px;
height: 28px;
}
.search-bar {
position: relative;
}
#icon img {
position: absolute;
height: 28px;
width: 30px;
right: 0px;
top: 3px;
}
.buttons {
text-align: center;
margin-top: 10px;
}
NOTE - I've removed anything from the example that I haven't changed.
I've got this short code:
#div1 div {
margin: 0% 0,5%;
display: inline-block;
color: white;
border: 1px dotted yellow;
align: center;
}
#div1 {
margin: 0px auto;
width: 620px;
height: 100px;
background-color: black;
overflow: hidden;
text-align: center;
}
#div2, #div10 {
width: 21px;
height: 100px;
}
#div3, #div9 {
width: 60px;
height: 60px;
}
#div4, #div8 {
width: 70px;
height: 70px;
}
#div5, #div7 {
width: 77px;
height: 77px;
}
#div6 {
width: 85px;
height: 85px;
}
<div id="div1">
<div id="div2">Content2</div>
<div id="div3">Content3</div>
<div id="div4">Content4</div>
<div id="div5">Content5</div>
<div id="div6">Content6</div>
<div id="div7">Content7</div>
<div id="div8">Content8</div>
<div id="div9">Content9</div>
<div id="div10">Content10</div>
</div>
I would like to be able to horizontally align these divs so they are not aligned to the top of my main div but to the center.
I tried it many different ways, such as padding, margin, but i wasn't able to figure out how to do it.
Do you have any idea?
Just add vertical-align:middle; on the rule above:
CSS
#div1 div {
margin: 0% 0,5%;
display: inline-block;
color: white;
border: 1px dotted yellow;
align: center;
vertical-align: middle;
}
DEMO HERE
Hey if you are having some confusion or problem of using vertical-align:middle you can go through below example
I have added a new div inside of div with id div2 to div10 and updated css
#div1 > div {
display: inline-block;
align: center;
margin: 0% 0, 5%;
position: relative;
top: 50%;
}
#div1 > div[id] > div {
transform: translateY(-50%);
color: white;
border: 1px dotted yellow;
}
#div1 {
margin: 0px auto;
width: 620px;
height: 100px;
background-color: black;
overflow: hidden;
text-align: center;
}
#div2 > div, #div10 > div {
width: 21px;
height: 100px;
}
#div3 > div, #div9 > div {
width: 60px;
height: 60px;
}
#div4 > div, #div8 > div {
width: 70px;
height: 70px;
}
#div5 > div, #div7 > div {
width: 77px;
height: 77px;
}
#div6 > div {
width: 85px;
height: 85px;
}
<div id="div1">
<div id="div2">
<div>
Content2
</div>
</div>
<div id="div3">
<div>
Content3
</div>
</div>
<div id="div4">
<div>
Content4
</div>
</div>
<div id="div5">
<div>
Content5
</div>
</div>
<div id="div6">
<div>
Content6
</div>
</div>
<div id="div7">
<div>
Content7
</div>
</div>
<div id="div8">
<div>
Content8
</div>
</div>
<div id="div9">
<div>
Content9
</div>
</div>
<div id="div10">
<div>
Content10
</div>
</div>
</div>
JSFIDDLE: https://jsfiddle.net/9tdzqvot/
I'm trying to create three buttons on either side of my main logo. These buttons should appear vertically center however when applying different display properties and/or margins and padding of 24px~ nothing changes. The attached JSFiddle shows the issue I have.
http://jsfiddle.net/LRaJy/
.header {
width: 100%;
background-color: #64767f;
background-position: bottom;
padding: 10px 0;
}
.header .logo {
background-image: url('../img/header-logo.png');
display:inline-block;
width: 415px;
height: 100px;
background: #fff;
}
.header-container {
width: 733px;
height: 100px;
margin: 0 auto;
}
.hover-buttons {
height: 44px;
display: inline-block;
padding-top: -5px;
}
.hover-buttons .button {
width: 44px;
height: 44px;
display: inline-block;
margin-right: 5px;
background: #fff;
}
with the HTML
<div class="header">
<div class="header-container">
<div class="hover-buttons">
<div class="button backups"></div>
<div class="button currency"></div>
<div class="button contact"></div>
</div>
<div class="logo">
</div>
<div class="hover-buttons">
<div class="button satisfaction"></div>
<div class="button security"></div>
<div class="button care"></div>
</div>
</div>
</div>
Is this what you're looking for?
jsFiddle Demo
.button, .logo {
vertical-align: middle;
}