How can I center these varied number of elements within a div? - html

I have a <div> with a number of sub-elements (which happen to be custom-sized buttons). It can have between 1 and 3 buttons.
Example of HTML with 2 buttons:
<div id="head">
<div id="control-buttons-container">
<button class="control-button">some button text</button>
<button class="control-button">proceed with this button</button>
</div>
</div>
When there are 3 buttons, they fill the entire <div>, so it is not an issue. However, when there are 1 or 2 buttons, they need to be centered but I can't seem to accomplish this without introducing ridiculous conditional margins or something.
How can I modify this CSS so that <div> elements with 1 or 2 of these buttons show the buttons centered within the div?
Please refer to the Fiddle: https://jsfiddle.net/bf33wc6w/1/
Edit: With only 2 buttons, I don't want them to be spread out. I want them to be in the center with only ~2px between them similar to their spacing for when there are 3 buttons.

You could set inline block on the items, with container set to text align center.
.control-buttons-container {
text-align: center;
font-size: 0; /*fix inline block gap*/
}
.control-button {
display: inline-block;
vertical-align: top;
font-size: 12px; /*reset font size*/
}
JSFIDDLE DEMO
.control-buttons-container {
text-align: center;
font-size: 0; /*fix inline block gap*/
}
.control-button {
background-color: #0E80B4;
color: white;
outline: none;
height: 73px;
width: 128px;
margin: 3px 1.5px;
display: inline-block;
vertical-align: top;
font-size: 12px; /*reset font size*/
}
.control-button:hover {
background-color: #3FA9DB;
}
#head, #body, #foot {
border: 1px solid red;
position: absolute;
width: 396px;
height: 80px;
left: 0;
}
#head {
top: 0;
}
#body {
bottom: 50%;
-ms-transform: translateY(50%);
-webkit-transform: translateY(50%);
transform: translateY(50%);
}
#foot {
bottom: 0;
}
<div id="head">
<div class="control-buttons-container">
<button class="control-button">some button text</button>
<button class="control-button">proceed with this button</button>
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
<div id="body">
<div class="control-buttons-container">
<button class="control-button">proceed with this button</button>
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
<div id="foot">
<div class="control-buttons-container">
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
Updates:
Fixed same id being used multiple times on a single page, which is in valid HTML - changed it to class.
Improved the position of middle block, make it to always stay in the middle more accurately - by using CSS transform.
Merged some duplicated CSS rules.

Like this:https://jsfiddle.net/bf33wc6w/7/
All I did was change your float to none and the margin to auto for the left and right margin?
.control-button {
background-color: #0E80B4;
color: white;
outline: none;
border: none;
height: 73px;
width: 128px;
margin: 3px auto;
}

Add these style rules:
#head, #body, #foot { text-align: center; }
#control-buttons-container { display: inline-block; }
As an aside, you shouldn't use the same id (control-buttons-container) multiple times in one document. You should use a classname instead.
Updated fiddle: https://jsfiddle.net/mr8e7kyt/

Try something like this:
<div id="control-buttons-container">
<div class="col-1">
<button class="control-button">some button text</button>
</div>
<div class="col-2">
<button class="control-button">proceed with this button</button>
</div>
<div class="col-3">
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
<div id="control-buttons-container">
<div class="col-1">
</div>
<div class="col-2">
<button class="control-button">proceed with this button</button>
</div>
<div class="col-3">
</div>
</div>
.control-button {
background-color: #0E80B4;
color: white;
outline: none;
border: none;
float: left;
height: 73px;
width: 100%;
}
.control-button:hover {
background-color: #3FA9DB;
}
#control-buttons-container {
max-width: 400px;
padding: 1px;
border: 1px solid red;
}
.col-1, .col-2, .col-3 {
width: 32.6%;
display: inline-block;
margin: auto
}
Isn't flawless, but it was made in a couple of minutes and gets the job done: JSFiddle

For the containers without 3 items you should remove the float: left; for the buttons inside it. Leave it for the one with 3 items. Then you can just set text-align: center; on the container.
You can add a class like no-float on the containers you want to control whether its children should be floated or not.
https://jsfiddle.net/bf33wc6w/10/

This answer will probably help you out. Wrap your buttons in a container, give it a fixed width, and change margin to auto. Be sure to remove float: left.

Related

Text in div being bumped down - how to center?

I have a series of CSS-styled boxes. Each box has a heading, followed by a number in a paragraph tag. Some of the headings are 2 lines, and in this case, the number shows up right where I want it: centered in the bottom part of the box. Where the heading is only 1 line, the number floats up higher than I'd like. How can I get the number to be in the center of the white space? What's going on here?
Code here: https://jsfiddle.net/snp3gvke/
<div class="sm red left-margin"><h2>Website<br/>Visitors</h2><p>120,363</p> </div>
Try adding vertical-align:middle; and line-height
This is the hacky solution, but you can solve your problem by adding two line breaks to your headings. That's essentially the problem - when your heading only takes one line, it doesn't push the number down as far into the white part of the div.
p {
font-weight: bold;
font-size: 2em;
text-align: center;
position:absolute;
bottom: 5%;
width: 100%;
}
I was able to do it by using flexbox. I had to make some changes to your CSS to override the colors coming from langsdale-dashboard.css file.
I went ahead and made your CSS a little bit more efficient also. I made changes only on the CSS to make things work. Here's what I did:
Applied the colors to the h2 instead of the parent container.
Removed the height from the parent containers and set the heights to the h2 and p instead.
Applied display:flex; justify-content:center; and align-items:center to both the h2 and the p.
I'm including the code below. You can also view it on JSFiddle: https://jsfiddle.net/m0nk3y/snp3gvke/11/
Let me know if you have any questions.
.lg,.med,.sm {
border-radius: 15px;
border-style: solid;
border-width: 1px;
position: relative;
}
.lg {
width: 700px;
}
.med {
width: 500px;
display: inline-block;
}
.sm {
width: 175px;
display: inline-block
}
.sm, .med, .lg {
vertical-align: top;
}
.left-margin {
margin-left: 15px;
}
.row {
margin-bottom: 10px;
}
h2,
p {
display: flex;
margin: 0;
text-align: center;
justify-content: center;
align-items: center;
}
h2 {
height: 75px;
border-radius: 12px 12px 0 0;
}
p {
height: 100px;
font-weight: bold;
font-size: 2em;
}
.blue, .red, .green, .orange {
background: transparent;
}
.blue {
border-color: #41B6E6;
}
.blue h2 {
background: #41B6E6;
}
.red {
border-color: #ce2029;
}
.red h2 {
background: #ce2029;
}
.green {
border-color: #C4D600;
}
.green h2 {
background: #C4D600;
}
.orange {
border-color: #E35205;
}
.orange h2 {
background: #E35205;
}
<link href="https://langsdale.ubalt.edu/zz-test/langsdale-dashboard.css" rel="stylesheet"/>
<body>
<div class="row">
<div class="lg blue">
<h2>Walk-in Visitors</h2>
<p>109,328</p>
</div>
</div>
<div class="row">
<div class="med red">
<h2>Special Collections<br/>Flickr Views</h2>
<p>75,985</p>
</div>
<div class="sm green left-margin">
<h2>Questions<br/>Answered</h2>
<p>19,570</p>
</div>
</div>
<div class="row">
<div class="sm blue">
<h2>Materials<br/>Circulated</h2>
<p>375,985</p>
</div>
<div class="med orange left-margin">
<h2>Instruction Session<br/>Attendees</h2>
<p>2,045</p>
</div>
</div>
<div class="row">
<div class="med green">
<h2>Database Searches</h2>
<p>330,479</p>
</div>
<div class="sm red left-margin">
<h2>Website<br/>Visitors</h2>
<p>120,363</p>
</div>
</div>
<div class="row">
<div class="lg orange">
<h2>Titles Borrowed via ILL</h2>
<p>5,773</p>
</div>
</div>
</body>

Buttons not aligning with div

So I am trying to make this admin page responsive and there are some problems when I resize the page. I want the div with the Inventory to be aligned with the 3 buttons.
This is my container which needs to properly adapt when resizing the viewport.
.reports{
border: 1px solid red;
overflow-y: scroll;
/*overflow-x: hidden; */
height: 100%;
}
.row{
height: 4.5em;
width: 100%;
margin-left: 0;
}
.actions{
float: right;
width: 30%;
}
.entry-group{
border: 1px solid red;
float: left;
width: 70%;
}
.title{
border: 1px solid red;
float: right;
width: 72.5%;
height: 3.7em;
word-wrap: break-word;
margin-left: 2em;
}
<div class="reports">
<div class="list-group-item row">
<div class="entry-group">
<button class="btn btn-primary date" >2016-09-19</button>
<div class="title">
<h4 class="list-group-item-heading">2080136 - Inventory</h4>
<p class="list-group-item-text">2 Napier Court West Southend On Sea SS1 1JU</p>
</div>
</div>
<div class="actions">
<button class="btn btn-primary download">Download</button>
<button class="btn btn-primary edit">Edit</button>
</div>
</div>
</div>
I think there is a few issues with the code here. Firstly, I believe it odd to have DIVs between your 'tr' and td', and not having a 'table' element.
I think you are trying to mix different ways of doing things: bootstrap and HTML tables.
My suggestion is use something like layoutit.com to build a layout with divs only in bootstrap. You can easily still integrate it into your back end technology, and get the divs responsive by editing the class names, e.g. col-sm-1.
I understand that you want to use tables for reports, having said that, it should be carried through completely, without divs inbetween.
Add this:
.list-group-item-heading {
margin-top: 0;
}
https://jsfiddle.net/kefhvc7y/
Add:
.title h4 {
margin-top: 0px;
}
.date {
float: left;
}
and remove margin-left: 2em; from .title.
Here is an updated version of your fiddle: https://jsfiddle.net/r11p5n4d/

Responsive text fixed positioning with Button

http://i.imgur.com/Veauoig.png
I am currently trying to work out how to make the 'From £' text to keep in the same position as the buttons above. The page is responsive so I have been unable to keep the text in one position.
The CSS I have used so far -
element.style {position: absolute; width: 97%;}
I put each of the 'From £' parts in their own class. Not sure if there is an easier way?
<div class="price2">From £300</div>
Any help would be great. Thanks!
Add a container for the element for the price and button so that they remain in context with each other.
http://jsfiddle.net/05orkj1a/
.prices{
width: 100%;
}
.price-column{
display: table-cell;
width: 33%;
text-align: center;
margin: 0 auto;
padding: 0 5px;
}
<div class="prices">
<div class="price-column">
<button>Bass</button>
<div class="price2">From £65</div>
</div>
<div class="price-column">
<button>Mid</button>
<div class="price2">From £300</div>
</div>
<div class="price-column">
<button>Treble</button>
<div class="price2">From £715</div>
</div>
</div>
You could also Float the columns left to cause them to collapse vertically as the screen shrinks with the same html. Just change the margin or padding depending on how far apart you want them spaced
http://jsfiddle.net/z6agt11e/
.prices{
width: 100%;
overflow: hidden;
}
.price-column{
display: block;
float: left;
text-align: center;
padding: 5px 5px;
}
You can also add an outer container and then create a inner container for each button-price set.
Here is the HTML code:
<div class="outter">
<div class="block">
<div class="button">button1</div>
<div class="price2">From £65</div>
</div>
<div class="block">
<div class="button">button2</div>
<div class="price2">From £300</div>
</div>
<div class="block">
<div class="button">button3</div>
<div class="price2">From £715</div>
</div>
</div>
Here the CSS:
.outter{
width:100%;
}
.block{
width:33%;
background-color: yellow;
float:left;
text-align: center;
}
And here a jsfiddle:
http://jsfiddle.net/SoniaGM/ej4mdwx9/1/
Hope it helps.
You can use the CSS3 ::after pseudo-selector.
Give at button class:
position: relative;
Then you have to write something lime this:
.button-class::after {
content: 'From £300';
background: transparent;
height: 1%;
width: 3%;
position: absolute;
top: 20px;
left: 0px;
}
Obviously, you have to change height: 1%; width: 3%; and top: 20px; left: 0px;with whatever you want!

<button> has excess margin after setting padding, margin, border to 0

The example below shows a <button> element whose parent's height is not the height of the button. What's the best way to remove this excess height while remaining semantic and also keeping the <button> inline?
If I set the button to display:block then the excess height is removed. If I set the parent's font-size to 0, then it is also removed. If I change the <button> to a <div> element, then it is fixed as well. Should I just not be semantic?
I have tested this under the stable version of Google Chrome.
.box {
height: 30px;
width: 30px;
background-color: green;
}
.outer {
background-color: blue;
}
button {
border: 0;
margin: 0;
padding: 0;
}
<div class='outer'>
<button class='box'></button>
</div>
<button> is an inline-replaced element, so you just need to set the line-height property in CSS.
.box {
height: 30px;
width: 30px;
background-color: green;
}
.outer {
background-color: blue;
line-height: 0;
}
button {
border: 0;
margin: 0;
padding: 0;
}
<div class='outer'>
<button class='box'></button>
</div>
I hope this helps. If you need any additional help, please comment below.
This issue happened, because empty text don't rendered and baseline makes margin.
You can just add the text to your button, then it will correct rendered.
Also hardcoded vertical-align should make the trick.
.box {
height: 30px;
width: 30px;
background-color: green;
}
.fix {
vertical-align: sub;
}
.outer {
background-color: blue;
}
button {
border: 0;
margin: 0;
padding: 0;
}
<h5>Initial issue</h5>
<div class='outer'>
<button class='box'></button>
</div>
<hr/>
<h5>Add a text</h5>
<div class='outer'>
<button class='box'>+</button>
</div>
<hr/>
<h5>Fix by vertical-align</h5>
<div class='outer'>
<button class='box fix'></button>
</div>

If I apply "float: left;" to a inner div, why does it affect the background color of it's parent div?

I have a very simple design where I have 4 small boxes lined up on top of one another each with the same dimensions. However, when I try to apply "float: left" to the boxes, the background color of it's parent div goes away. Why is this? What does it have to do with the background color? I would just like my background color to remain the same.
See jsfiddle: http://jsfiddle.net/mush5ecc/
My html code:
<div id="careers">
<div class="container">
<h2 id="careers_title">Careers</h2>
<div id="four_grids">
<div id="top_left" class="grid"></div>
<div id="top_right" class="grid"></div>
<div id="bottom_left" class="grid"></div>
<div id="bottom_right" class="grid"></div>
</div>
</div>
</div>
My CSS code:
#careers {
background-color: orange;
}
.container {
width: 1026px;
margin: auto;
}
#careers_title {
text-align: center;
padding-top: 67px;
padding-bottom: 60px;
}
.grid {
width: 50px;
height: 50px;
float: left; /* COMMENT FLOAT TO SEE WHAT HAPPENS */
}
#top_left {
background-color: blue;
}
#top_right {
background-color: green;
}
#bottom_left {
background-color: red;
}
#bottom_right {
background-color: yellow;
}
Apply overflow: hidden to <div id="four_grids">.
See here for further details on this behaviour.
I'm a bit unsure of what your goal is, but I added the following css and I think this may be what you are looking for.
#four_grids {
position: absolute;
}