I've got three div's going here. One's a container then the other 2 are in place for some buttons! But it just doesn't look right. I'm not quite sure how to fix it
.button {
display: inline-block;
padding: 20px 30px;
font-size: 12px;
cursor: pointed;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #AB0002;
border: none;
border-radius: 15px;
}
.divButton {
height: 100%;
width: 100%;
background-color: #e4e4e4;
display: inline-block;
padding-bottom: 5px;
}
HTML:
<div class="divButton">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<!--2 buttons "centered"-->
<div style="float: left; padding-left: 325px;">
<p style="padding-left: 72px;">Centered text above button</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<!--add spacing to move away from 2 buttons-->
<div style="float: left; padding-left: 125px;">
<p style="padding-left: 40px;">TEXT</p>
<button class="button" style="float: right;">TEXT</button>
</div>
</div>
jsfiddle: https://jsfiddle.net/3ar1L0zy/1/
And what i'm trying to achieve in paint form!
I would move away from using floats - css has moved on sufficiently so you shouldn't need to use them anymore.
Use flex instead:
.button {
display: inline-block;
padding: 20px 30px;
font-size: 12px;
cursor: pointed;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #AB0002;
border: none;
border-radius: 15px;
}
.divButton {
width: 100%; /* you don't really need this - divs are block elements which are 100% by default */
background-color: #e4e4e4;
padding: 0 20px 5px 20px;
box-sizing: border-box; /* as you have set the width, you need this to stop the div being 100% + 40px wide */
display:flex; /* this will align items in a row by default */
flex-wrap:wrap; /* this allows the content to wrap to multiple rows */
justify-content:space-between; /* this will push any content to either side of the row */
}
.divButton > p {
width:100%; /* make this take up full row */
}
.divButton > div {
text-align:center; /* use this to centre text - not padding */
}
<div class="divButton">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<!--2 buttons "centered"-->
<div>
<p>Centered text above button</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<!--add spacing to move away from 2 buttons-->
<div>
<p>TEXT</p>
<button class="button">TEXT</button>
</div>
</div>
One other tip I would give you is try not to use inline styles - they become very hard to maintain and make it harder to debug too (and cause a lot larger files as you have to repeat code for styles instead of just using a class that can be used multiple times but programmed once)
Instead of use float: left on both div, you can use float right on the right one and remove the padding-left you set:
<div class="divButton">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<!--2 buttons "centered"-->
<div style="float: left;">
<p style="padding-left: 72px;">TEXT</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<!--add spacing to move away from 2 buttons-->
<div style="float: right;">
<p style="padding-left: 40px;">TEXT</p>
<button class="button" style="float: right;">TEXT</button>
</div>
</div>
https://jsfiddle.net/3ar1L0zy/13/
you can achieve this too by using flexbox too. (better solution in my opinon)
HTML code:
<div class="divButton">
<h3 style="text-align: center; padding-top: 15px; color: black;">TEXT!</h3>
<!--2 buttons "centered"-->
<div class="divText">
<p style="padding-left: 72px;">TEXT</p>
<p style="padding-left: 40px;">TEXT</p>
</div>
<!--add spacing to move away from 2 buttons-->
<div>
<div class="divButtons">
<div>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<button class="button">TEXT</button>
</div>
</div>
</div>
CSS code:
.button {
display: inline-block;
padding: 20px 30px;
font-size: 12px;
cursor: pointed;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #AB0002;
border: none;
border-radius: 15px;
}
.button:hover {
background-color: #880002;
}
.divButton {
height: 100%;
width: 100%;
background-color: #e4e4e4;
display: inline-block;
padding-bottom: 5px;
}
.divText {
display: flex;
justify-content: space-around;
}
.divButtons {
display: flex;
justify-content: space-around;
}
<div class="divButton" style="text-align: center;">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<div style="display: inline-block;">
<!--add spacing to move away from 2 buttons-->
<div style="float: right; display: inline-block; padding-left: 125px;">
<p style="text-align: center;">TEXT</p>
<button class="button" style="float: right;">TEXT</button>
</div>
<!--2 buttons "centered"-->
<div style="display: inline-block;">
<p style="text-align: center;">TEXT</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
</div>
</div>
https://jsfiddle.net/3ar1L0zy/85/
I would recommend flexbox
.parent{
background: tomato;
width: 400px;
padding: 30px;
display: flex;
flex-direction: column;
}
.header{
background: yellow;
text-align: center
}
.body{
background: green;
width: 100%;
display: flex;
}
.body-item{
background: pink;
width: 50%;
text-align: center;
padding: 10px;
}
.blue{
background: blue; /* to make it easier to see */
}
.buttons{
display: flex;
flex-wrap: wrap; /* wrap items onto multiple lines if needed, from top to bottom*/
justify-content: space-evenly; /* items are distributed so that the spacing between any two items (and the space to the edges) is equal */
}
<div class="parent">
<h3 class="header">HEADER TEXT</h3>
<section class="body">
<div class="body-item">
<div class="text">Text</div>
<div class="buttons">
<button>Button</button>
<button>Button</button>
</div>
</div>
<div class="body-item blue">
<div class="text">Text</div>
<div class="buttons">
<button>Button</button>
</div>
</div>
</section>
</div>
Related
I'm currently attempting to build a basic website using HTML, CSS and Javascript. However there's a specific part of this website that I'm having difficulty building. Here's what the section is meant to look like, with the part on the left being the part that's tripping me up the most. It's a section which is meant to contain four basic squares next to each other; Image 1 (linked earlier) provides a visual.
Here's an image of what I've currently managed to get: All four squares are present, but they're stacked on top of one another instead. I haven't been able to get much closer to it.
Here's the html code behind it:
<div class="top-large-left">
<h3 id="hot">Interaction</h3>
</div>
<div class="small-top-right">
<h3 id="item">Location</h3>
</div>
<div class="large-left-section">
<div>
<section class="left-section">
<div class="btn-group button">
<div class="button1">
<button type="button1">Display X, Y</button>
</div>
<div class="button2">
<button type="button2">Theme</button>
</div>
<div class="button3">
<button type="button3">Modal</button>
</div>
<div class="button4">
<button type="button4">Swap Image</button>
</div>
</section>
</div>
As well as the CSS:
.btn-group button {
background-color: #428bca;
border-radius: 5px;
font-weight: bold;
font-size: 15px;
color: white;
padding: 32px 19px;
cursor: pointer;
display: inline-block;
margin: 2px 2px;
border: 1px;
text-align: center;
line-height: 25px;
}
.button1 {
width: 130px;
}
.button2 {
width: 130px;
}
.button3 {
width: 130px;
}
.button4 {
width: 130px;
}
How would I be able to achieve what is being shown in the first image? Is the css-grid function what I need to use, or something else? Feel free to say if I need to provide any more information, thanks.
Flex is your solution here. Flex makes your life easier.
Run the snippet on full window. (Full page)
.top-large-left {
background-color: #428bca;
padding: 0 1rem;
}
.top-large-left h3 {
margin:0;
}
.wrapper {
display:flex;
flex-direction:row;
}
.large-left-section,
.large-right-section{
flex: 1 1 50%;
border: 10px solid lightgrey;
text-align: center;
margin:5px;
}
.btn-group button {
background-color: #428bca;
border-radius: 5px;
font-weight: bold;
font-size: 15px;
color: white;
padding: 32px 19px;
cursor: pointer;
display: inline-block;
margin: 2px 2px;
border: 1px;
text-align: center;
line-height: 25px;
}
.large-left-section .left-section .btn-group.button{
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.large-left-section .left-section .btn-group.button .btn {
flex: 0 0 45%;
margin:5px;
}
.large-left-section .left-section .btn-group.button .btn button {
width:100%;
}
<div class="top-large-left">
<h3 id="hot">Interaction</h3>
</div>
<div class="wrapper">
<div class="large-left-section">
<section class="left-section">
<div class="btn-group button">
<div class="btn button1">
<button type="button1">Display X, Y</button>
</div>
<div class="btn button2">
<button type="button2">Theme</button>
</div>
<div class="btn button3">
<button type="button3">Modal</button>
</div>
<div class="btn button4">
<button type="button4">Swap Image</button>
</div>
</div>
</section>
</div>
<div class="large-right-section">
</div>
</div>
I want to remove this space to make <span>-s closer, but there is no any margin or padding that could add more space between them.
<!-- HTML -->
<div class="service-container" id="logo-title-service-container">
<span class="logo-text" id="logo-title">company name</span><br>
<span class="logo-text" id="logo-subtitle">This is a subtitle</span>
</div>
Sass
// Sass
#logo-title-service-container
text-align: left
padding-left: 10px
#logo-title
font-size: 18px
#logo-subtitle
padding-top: -5px
font-size: 12px
How can I remove the space between them?
You should remove the 'br' element first.
then make the display of title and subtitle block or as i did here make the parent display flex and flex-direction to column to make it work.
#logo-service-container {
width: 100%;
text-align: center;
padding-top: 15px;
display: flex;
align-items: center;
justify-content: center;
}
#logo-main {
width: 26px;
height: 26px;
}
#logo-title-service-container {
display: flex;
flex-direction: column;
text-align: left;
padding-left: 10px;
}
#logo-title {
font-size: 18px;
}
#logo-subtitle {
font-size: 12px;
}
<div class="sidebar" id="left-sidebar">
<div class="service-container" id="logo-service-container">
<img id="logo-main" src="resources/images/icons/logo.svg" alt="logo">
<div class="service-container" id="logo-title-service-container">
<span class="logo-text" id="logo-title">company name</span>
<span class="logo-text" id="logo-subtitle">This is a subtitle</span>
</div>
</div>
</div>
You should try to avoid <br>
replace the span with div
<div class="sidebar" id="left-sidebar">
<div class="service-container" id="logo-service-container">
<img id="logo-main" src="resources/images/icons/logo.svg" alt="logo">
<div class="service-container" id="logo-title-service-container">
<div class="logo-text" id="logo-title">company name</div>
<div class="logo-text" id="logo-subtitle">This is a subtitle</div>
</div>
</div>
</div>
SASS:
#logo-service-container
width: 100%
text-align: center
padding-top: 15px
display: flex
align-items: center
justify-content: center
#logo-main
$size: 26px
width: $size
height: $size
#logo-title-service-container
text-align: left
padding-left: 10px
#logo-title
font-size: 18px
#logo-subtitle
padding-top: -5px
font-size: 12px
Is there a way to centralize text inside a bordered element?
When I do border at the element, maybe because its content size is reduced, it gets decentralized.
.circle {
width: 22px;
height: 22px;
border-radius: 50%;
border: solid #00b0ff 1px;
color: #00b0ff;
}
.button-styling {
color: #00b0ff;
font-size: x-large;
}
.outline-h {
outline: thin;
outline-color: blue;
outline-style: solid;
}
<div style="max-width: 15px;">
<h3> With border </h1>
<div class="circle">
<mat-icon class="button-styling">+</mat-icon>
</div>
</div>
<div style="max-width: 15px;">
<h3> Without border </h1>
<div class="outline-h">
<mat-icon class="button-styling">+</mat-icon>
</div>
</div>
You can use flexbox to center the text element:
.circle {
width: 22px;
height: 22px;
border-radius: 50%;
border: solid #00b0ff 1px;
color: #00b0ff;
/*Add*/
display: flex;
justify-content: center;
align-items: center;
}
<div style="max-width: 15px;">
<h3>With border</h3>
<div class="circle">
<mat-icon class="button-styling">+</mat-icon>
</div>
</div>
You could try this:
.circle {
text-align: center;
line-height: 20px;
}
I was able to center it using display: flex.
Check it's browser support though: https://caniuse.com/#feat=flexbox
.circle {
width: 22px;
height: 22px;
border-radius: 50%;
border: solid #00b0ff 1px;
color: #00b0ff;
display: flex;
justify-content: center;
align-items: center;
}
.button-styling {
color: #00b0ff;
font-size: x-large;
}
.outline-h {
outline: thin;
outline-color: blue;
outline-style: solid;
}
<div style="max-width: 15px;">
<h3> With border </h1>
<div class="circle">
<mat-icon class="button-styling">+</mat-icon>
</div>
</div>
<div style="max-width: 15px;">
<h3> Without border </h1>
<div class="outline-h">
<mat-icon class="button-styling">+</mat-icon>
</div>
</div>
I have one div containing 3 divs.
original
HTML code
.state {
background-color: rgba(233, 234, 237, 0.9);
height: 7vh;
width: 80%;
border-radius: 14px;
margin: 10px 0 15px 80px;
display: flex;
align-items: stretch;
}
.state-main {
text-align: center;
padding-top: 10px;
font-weight: 900;
font-size: 14px;
}
.options {
text-align: right;
margin-bottom: 10px;
}
.owner-image {
border-top-left-radius: 14px;
border-bottom-left-radius: 14px;
}
<div class="state">
<div class="owner">
<img class="owner-image" src="img/uk.jpg">
</div>
<div class="state-main">
<p class="state-name">PENNSYLVANIA</p>
</div>
<div class="options">
<p id="time"></p>
<button>SEND TROOPS</button>
<button>ATTACK</button>
</div>
</div>
Use flexbox (browser support).
.state {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 80px;
background-color: lightgray;
}
.state,
.btns button {
text-transform: uppercase;
}
<div class="state">
<img src="http://placehold.it/150x80/fc0">
<p>
Pennsylvania
</p>
<div class="btns">
<button>Send Troops</button>
<button>Attack</button>
</div>
</div>
**For IE9 and older you'll need to provide a fallback. Whether or not you need to do this depends on target audience.
.State is the div that contains all 3. .state-main is yellow div and should go at the center. .options is green div should go far right. .owner-image is the red div, and should stay at the same place.
Using flex to put the layout into place.
.state {
display: flex;
justify-content: space-between;
}
.state-mail {
text-align: center;
}
<div class="state">
<div class="owner-image">
<img src="http://placehold.it/100x50" />
</div>
<div class="state-main">PENNSYLVANIA</div>
<div class="options"><button>SEND TROOPS</button><button>ATTACK</button></div>
</div>
I am trying to align two buttons in the middle of the screen in HTML. When I try using text-align: center; it is not working. Below is my code for the button.
<style>
.button {
margin-top: 15px;
background-color: #0066FF;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
}
<body>
<div class="floated run">
<form action="aboutus.html">
<button class="button">See Projects</button>
</form>
</div>
<div class="floated run">
<form action="end.html">
<button class="button">About Us</button>
</form>
</div>
</body>
I am aware there are multiple answers to this question, but none of them have helped me so far.
You can just use Flexbox to get what you what and slightly change your markup.
So you'll need a wrapper with .flex class, and children with .flex-item classes.
.flex {
display: flex;
justify-content: center;
}
.flex-item + .flex-item {
margin-left: 10px;
}
.button {
margin-top: 15px;
background-color: #0066FF;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
}
<div class="flex">
<form action="aboutus.html" class="flex-item">
<button class="button">See Projects</button>
</form>
<form action="end.html" class="flex-item">
<button class="button">About Us</button>
</form>
</div>
The solution here is that because you have the buttons inside divs and forms, any CSS you apply directly to the buttons will not work - you will need to position the divs first:
https://jsfiddle.net/o2gxgz9r/6967/
The essence of this example is this CSS:
.floated {
width: 48%;
float: left;
padding: 1%;
}
.first {
text-align: right;
}
With this HTML:
<div class="floated run first">
<form action="aboutus.html">
<button class="button">See Projects</button>
</form>
</div>
<div class="floated run">
<form action="end.html">
<button class="button">About Us</button>
</form>
</div>
Note that I have added a class .first to the first div.
You can float your button to left, add a little left margin, wrap it with one div, and than position it to center:
<style>
.button {
margin-top: 15px;
margin-left: 10px;
background-color: #0066FF;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
float: left;
font-size: 16px;
cursor: pointer;
}
.button-wrapper{
width: 55%;
margin: 0 auto;
}
</style>
<body>
<div class="button-wrapper">
<div class="floated run">
<form action="aboutus.html">
<button class="button">See Projects</button>
</form>
</div>
<div class="floated run">
<form action="end.html">
<button class="button">About Us</button>
</form>
</div>
</div>
</body>
Easy
Give a div.floated width:100% and text-align:center
check -
div.floated {
width:100%;
text-align:center;
}