How to get the buttons side by side on the same row? - html

I was getting the data from an API and displaying it using HTML.
#dat {
position: absolute;
width: 100px;
height: 100px;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
button {
margin-left: 20px;
display: inline;
}
<h1><u>Weather App</u></h1>
<div id="dat">
<p id="data"></p>
<p id="temp"></p>
<p id="time"></p>
<button onclick="convertc()">imperial</button>
<button onclick="convertf()">metric</button>
</div>
The problem is that both the buttons are one above another.I want to bring it on the same line.
Thanks.

You didn't allow enough width for both buttons to fit.
I changed the width to 200px;
#dat{
position:absolute;
width:200px;
height:100px;
left:50%;
top:50%;
margin-left:-50px;
margin-top:-50px;
}
button{
margin-left:20px;
display:inline;
}
<h1><u>Weather App</u></h1>
<div id="dat">
<p id="data">
</p>
<p id="temp">
</p>
<p id="time">
</p>
<button onclick="convertc()">imperial</button>
<button onclick="convertf()">metric</button>
</div>

The problem is the buttons are inside the #dat div which is set to be only 100px wide. If you want them to be centered on the same line, add a new div outside the #dat div with width: 100% and text-align: center.
#dat {
position: absolute;
width: 100px;
height: 100px;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
button {
margin-left: 20px;
display: inline;
}
#buttondiv {
width: 100%;
text-align: center;
}
<h1><u>Weather App</u></h1>
<div id="dat">
<p id="data">
</p>
<p id="temp">
</p>
<p id="time">
</p>
</div>
<div id="buttondiv">
<button onclick="convertc()">imperial</button>
<button onclick="convertf()">metric</button>
</div>

Slightly different approach then above, using floatleft; and auto width.
Created CSS classes as re-useables.
#dat {
position: absolute;
height: 100px;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
width: auto;
}
.pull-left {
float:left;
}
.ml-10 {
margin-left: 10px;
}
<h1><u>Weather App</u></h1>
<div id="dat">
<p id="data"></p>
<p id="temp"></p>
<p id="time"></p>
<button onclick="convertc()" class="pull-left">imperial</button>
<button onclick="convertf()" class="pull-left ml-10">metric</button>
</div>

You can separate your two buttons in different div and apply CSS in each div to adjust width.
<div>
<button onclick="convertc()">imperial</button>
</div>
<div>
<button onclick="convertf()">metric</button>
</div>
Beside, you can use Bootstrap to keep them inline for every devices.

My suggestions would be as follows:
Do not use the <u> tag for underlining unlinked text. This visual mechanism signifies something clickable. Using it for non-actionable text is bad UX.
Use an <ul> tag for the data items rather than <p> tags. Semantically the information is a list and not paragraphs of textual information.
For the <div> tags, use 100% width so that your UI is scalable with the visitors viewport. The CSS will continue to center the buttons as the viewport size changes.
See the following code:
#dat {
width: 100%;
margin: 0;
}
#dat ul
{
list-style:none;
margin:10px 0;
padding:0;
}
#dat .btns
{
width:100%;
text-align:center;
}
#dat .btns button {
display: inline;
}
<h1>Weather App</h1>
<div id="dat">
<ul>
<li id="data"></li>
<li id="temp"></li>
<li id="time"></li>
</ul>
<div class="btns">
<button onclick="convertc()">imperial</button>
<button onclick="convertf()">metric</button>
</div>
</div>

Related

Center all elements in a div but the text are justified in css

This is my attempt but the div wrapper doesn't center the content div.
I have a div containing ang box shape with another div with texts.
.wrapper {
position:absolute;
left: 50%;
}
.content {
width:90%;
text-align:justify;
left: -50%;
display: inline-block;
}
.button {
text-align:center;
}
<div class="wrapper">
<div name="box" style="background-color:#FF6201;width:220px;height:220px;:30px">
<div class="content">Content and Creative Writing, Social Media Management, SEO, Outbound Sales, Outbound Sales Appointment Setting,<button class="button">
weee</button>
</div>
</div>
</div>
I can actually do on html by adding tags but i am hoping to make this done in css. pls help.
I found this nifty trick a while back:
margin-left: 50%;
transform: translate(-50%)
This will center any statically positioned Div. I am assuming that you want the box centered; if so I would move the class="content" to that element and then revise your css to the following:
.content {
width:90%;
margin-left: 50%;
text-align:justify;
transform: translate(-50%)
}
Here is a fiddle link to see if this is what you were going for: https://jsfiddle.net/e3tsj8qf/5/
Edit:
Code from fiddle referenced below (accepted as answer):
.box {
background-color:#FF6201;
width:220px;
height:220px;
}
.content {
width:90%;
text-align:justify;
margin-left: 5%;
}
.centered {
text-align:center;
}
<div class="box" name="box">
<div class="content">
Content and Creative Writing, Social Media Management, SEO, Outbound Sales, Outbound Sales Appointment Setting,
</div>
<div class="centered">
<button class="button">weee</button>
</div>
</div>
Try this code markup and style.....
*{box-sizing:border-box};
*{margin:0;padding:0}
.wrapper {
position: absolute;
left: 50%;
transform:translate(-50%);
}
.content {
width: 85%;
text-align: center;
display: block;
margin:0 auto;
}
.button {
text-align: center;
}
<div class="wrapper">
<div name="box" style="background-color:#FF6201;width:220px;height:220px;">
<div class="content">Content and Creative Writing, Social Media Management, SEO, Outbound Sales, Outbound Sales Appointment Setting,<button class="button">
weee</button>
</div>
</div>
</div>

Keeping wrapper container a certain percentage of body

I'm having a tough time keeping my content centered within a certain width on my personal website. I have tried many methods such as setting body to a fix width and my wrapper container to a percentage of that. I have attached a picture of my website here and highlighted where I want my content to be contained in the picture shown
.
I want my content of my website centered within that highlighted area, while at the same time keeping the background to be the full size of the screen.
I realize this may be a simple question for many, but I have spent all day looking for and trying out different methods to do this with no avail.
body {
background-color: #F0f0f0;
text-align: center;
margin-right: 0px;
margin-left: 0px;
}
.wrapper {
text-align: center;
}
.topSection {
height: 300px;
border: solid 5px;
}
.mainAbout {
padding-left: 50px;
padding-right: 50px;
vertical-align: middle;
}
.mainAbout h1 {
font-size: 60px;
font-family: arvo, sans-serif;
margin-bottom: 5px;
}
#leftBrace {
vertical-align: middle;
}
#rightBrace {
vertical-align: middle;
}
.projects {
height: 864px;
border: solid 5px;
margin-top: 2px;
background: #0F1217;
}
.projects h2 {
color: #e6e6e6;
font-family: arvo, sans-serif;
font-size: 50px;
}
<link href="https://fonts.googleapis.com/css?family=Arvo" rel="stylesheet">
<div class="wrapper">
<!---- Wrapper Starts Here --->
<div class="topSection" style="display:block" ;>
<!---- Name Section Starts Here --->
<div id="leftBrace" style="display: inline-block" ;>
<img src="leftbrace.png">
</div>
<div class="mainAbout" style="display: inline-block" ;>
<!--- Main Name and About me Section ---->
<h1> Benjamin Yan </h1>
<p> I am a Senior Year Computer Science student at Sacramento State <br> University, California. I strive to become a professional Web Developer. </p>
</div>
<!--- End mainAbout --->
<div id="rightBrace" style="display: inline-block" ;>
<img src="rightbrace.png">
</div>
</div>
<!--- Wrapper Ends Here --->
<div class="projects">
<h2> Projects </h2>
</div>
<div class="contact">
</div>
</div>
<!--- Wrapper Ends Here --->
<footer>
</footer>
Instead of using background you could style curly-braces using pseudo selector :before and :after, thus it works like font styling, you could use transform:translate to center your intro text container, check below codes.
#box {
width: 100%;
height: 300px;
overflow: hidden;
background: #ccc;
}
#box > .cnt {
width:50%;
text-align: center;
top: 50%;
left: 50%;
position: relative;
transform: translate(-50%, -50%);
}
#box:before {
content:"{";
font-size: 250px;
position: absolute;
top: 0;
left:10%;
}
#box:after {
content: "}";
font-size: 250px;
position: absolute;
top: 0;
right:10%;
}
<div id="box">
<div class="cnt">
<h1> Benjamin Yan </h1>
<p> I am a Senior Year Computer Science student at Sacramento State <br> University, California. I strive to become a professional Web Developer. </p>
</div>
</div>
Apply margin: 0 auto; to your content class. This will work.
You need to make sure add an inner class inside each wrapper and define your desired width. And need to apply margin: 0 auto to the inner. I added demo snippet.If u want specific wrapper full width just remove innerclass that's enough you will get full width. I hope it will help you.
.wrapper {
height: 300px;
width: 100%;
background: orange;
margin-bottom: 20px;
}
.inner {
width: 70%;
margin: 0 auto;
background: pink;
height: 100%;
}
<div class="wrapper">
<div class="inner"></div>
</div>
<div class="wrapper">
<div class="inner"></div>
</div>
<div class="wrapper">
<div class="inner"></div>
</div>
<div class="wrapper">
<div class="inner"></div>
</div>

How can I align the thee spans with the width of the parent container?

Look at the picture, I have three span on the top of the "health issue", both width will be 89%, it is responsive. The challenge is that I cannot make 'ask,question,answer' width align with the health issue, especially when it's responsive.
#wrap {
width: 89%;
margin-bottom: 9px;
margin: 24px auto;
}
#wrap span {
float: left;
padding: 19px;
}
#health_issue {
width: 89%;
}
<div id="wrap">
<span id="wrap_a">ASK</span>
<span id="wrap_q">QUESTION</span>
<span id="wrap_an">ANSWER</span>
</div>
<!--wrap-->
<div style="clear:both;">
<div id="health_issue">
<h1>My Health Issue</h1>
<p>Describe your health problem</p>
</div>
<!--health_issue-->
</div>
Your elements are currently inline therefore they won't respond to width changes. You can change the style of #wrap span as follows
#wrap span {
display: inline-block;
width: 32%;
}
This will then put all of the elements at a 33% width depending on the parent.
This doesn't work when an element is inline as <span> tags are by default. Thus the change to display: inline-block
Full Snippet
#wrap {
width: 89%;
margin-bottom: 9px;
margin: 24px auto;
}
#wrap span {
display: inline-block;
width: 32%
}
#health_issue {
width: 89%;
}
<div id="wrap">
<span id="wrap_a">ASK</span>
<span id="wrap_q">QUESTION</span>
<span id="wrap_an">ANSWER</span>
</div>
<!--wrap-->
<div style="clear:both;">
<div id="health_issue">
<h1>My Health Issue</h1>
<p>Describe your health problem</p>
</div>
<!--health_issue-->
</div>
If you're able to support flex boxes then you can also use the following. Flex boxes are supported on all major browser versions. Internet explorer has some issues with modern specification of it. See can i use:
#wrap {
width: 89%;
margin-bottom: 9px;
margin: 24px auto;
display: flex;
}
#wrap span {
flex-grow:1;
}
#health_issue {
width: 89%;
}
<div id="wrap">
<span id="wrap_a">ASK</span>
<span id="wrap_q">QUESTION</span>
<span id="wrap_an">ANSWER</span>
</div>
<!--wrap-->
<div style="clear:both;">
<div id="health_issue">
<h1>My Health Issue</h1>
<p>Describe your health problem</p>
</div>
<!--health_issue-->
</div>

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

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.

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!