Centering the link as the button - css

.button {
display: block;
text-decoration: none;
color: #103d82;
background: #fff;
border: 1px solid #d2cfcd;
font-size: 1.4rem;
line-height: 1;
padding: 10px;
text-align: center;
}
div .button {
margin: 0 auto;
}
<div>
Link
</div>
<div>
<button type="button" class="button">Button</button>
</div>
My two elements have the same CSS, however button is centered and not my link.
How to do the width of the link is calculated in the same way as the button?
Is it possible without added properties to the container?
Thanks

You can add max-width/width properties and box-sizing:border-box to make them behave the same :
.button {
display: block;
text-decoration: none;
color: #103d82;
background: #fff;
border: 1px solid #d2cfcd;
font-size: 1.4rem;
line-height: 1;
padding: 10px;
text-align: center;
max-width: 200px;
width: 100%;
box-sizing: border-box;
margin: 0 auto;
}
<div>
Link
</div>
<div>
<button type="button" class="button">Button</button>
</div>
You can also try fit-content value of width. Simply pay attention to browser support: https://caniuse.com/#search=fit-content
.button {
display: block;
text-decoration: none;
color: #103d82;
background: #fff;
border: 1px solid #d2cfcd;
font-size: 1.4rem;
line-height: 1;
padding: 10px;
text-align: center;
width: fit-content;
box-sizing: border-box;
margin: 0 auto;
}
<div>
Link
</div>
<div>
<button type="button" class="button">Button</button>
</div>
Another idea is to change the display:block to display:table and both links and buttons will behave the same :
.button {
display: table;
text-decoration: none;
color: #103d82;
background: #fff;
border: 1px solid #d2cfcd;
font-size: 1.4rem;
line-height: 1;
padding: 10px;
text-align: center;
box-sizing: border-box;
margin: 0 auto;
}
<div>
Link
</div>
<div>
<button type="button" class="button">Button</button>
</div>

What you can do is to change display: block to display: inline-block instead
then add text-align: center to their parents instead of margin: 0 auto as follow:
.button {
display: inline-block;
text-decoration: none;
color: #103d82;
background: #fff;
border: 1px solid #d2cfcd;
font-size: 1.4rem;
line-height: 1;
padding: 10px;
text-align: center;
}
div {
text-align: center;
}
<div>
Link
</div>
<div>
<button type="button" class="button">Button</button>
</div>

Try This:
* {
box-sizing: border-box;
}
div {
width: 100px;
position: relative;
margin: 0 auto;
}
.button {
width: 100%;
display: block;
text-decoration: none;
color: #103d82;
background: #fff;
border: 1px solid #d2cfcd;
font-size: 1.4rem;
text-align: center;
padding: 10px;
}
<div>
Link
</div>
<div>
<button type="button" class="button">Button</button>
</div>

Related

How to remove box-shadow when side bar collapse (with toggle button)?

I made this sidebar and I cannot remove box-shadow when it collapse. I tried box-shadow: none; also box-shadow: none !important; box-shadow: transparent; even tried to change color: box-shadow: yellow; and it does not work. What should I do to remove shadow when it collapsing? It is all about React components:
<div>
<div className="sidebar-container container-flex navbar-expand-sm navbar-light">
<div className="navbar-header bg-white">
<button
className="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
</div>
<div
className="collapse navbar-collapse bg-white .d-none .d-sm-block .d-md-none"
id="navbarSupportedContent"
>
<div className="title1">Title</div>
<div className="title2">Title 2</div>
<ul className="mr-auto mb-2 mb-lg-0">
{<SidebarElement props={props} />}
</ul>
</div>
</div>
</div>
This is Sidebar CSS:
li {
list-style-type: none;
}
.title1 {
font-family: "Roboto";
font-style: normal;
font-weight: 700;
font-size: 26px;
letter-spacing: 0.5px;
padding-top: 24px;
padding-left: 24px;
padding-bottom: 28px;
color: #336cfb;
}
.title2 {
font-family: "Roboto";
font-style: normal;
font-weight: 400;
font-size: 14px;
letter-spacing: 0.5px;
padding-top: 24px;
padding-left: 24px;
padding-bottom: 20px;
color: #52575c;
}
.active-link {
color: #336cfb;
}
.sidebar-container {
position: fixed;
width: 242px;
left: 0;
overflow-x: hidden;
overflow-y: auto;
height: 100%;
box-shadow: 5px 0 5px -5px #333; /* Normal box shadow */
}
.navbar-collapse {
background: transparent;
height: inherit;
/* box-shadow: none; */ /* override box shadow */
box-shadow: yellow;
}
.collapse {
box-shadow: none; /* override box shadow */
}
#navbarSupportedContent {
flex-direction: column;
align-items: flex-start;
}
This is SidebarElement css:
.nav-link {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
padding-top: 8px;
padding-bottom: 8px;
font-family: "Roboto";
font-style: normal;
font-weight: 700;
font-size: 14px;
letter-spacing: 0.5px;
color: #52575c;
}
.customIcon {
width: 18px;
padding-right: 10px;
color: #dbdde0;
}
li {
list-style-type: none;
padding: 14px 10px;
}
.active-link {
color: #336cfb;
}
1: You're not resetting the box shadow on the same element
2: CSS is interpreted top down - box-shadow of none is being immediately overriden.
.sidebar-container {
position: fixed;
width: 242px;
left: 0;
overflow-x: hidden;
overflow-y: auto;
height: 100%;
box-shadow: 5px 0 5px -5px #333; /* Normal box shadow */
}
.navbar-collapse {
background: transparent;
height: inherit;
box-shadow: none; /* override box shadow */
}
.collapse {
box-shadow: none; /* override box shadow */
}
#navbarSupportedContent {
flex-direction: column;
align-items: flex-start;
}

link not inline because in it's own div?

The links were all horizontally aligned until i put one of them in it's own div to change it's color when i'm on the page it is linking to.
Now i can't get him to go back in line.
<div class="navigation">
Mes productions
DJ
<a target="_blank" href="./CV.pdf">Mon CV</a>
<div id="contact">
Me contacter
</div>
</div>
.navigation {
padding: 40px 0px;
position: relative;
text-align: center;
width: 100%;
font-size: 30px;
}
.navigation a {
background: black;
border: 1px solid grey;
border-radius: 7px;
color: white;
display: inline-block;
margin: 100px 35px;
padding: 14px;
text-decoration: none;
opacity: 0.75;
font-family: impact;
}
.navigation a:hover {
background: white;
border: 1px solid black;
color: black;
}
#contact a {
background: white !important;
color: black !important;
display: inline-block !important;
}
You need to set display: inline-block on #contact, not #contact a.

Change text hover

How change text when I push cursor in button? If I push cursor in button with price, will show text: "Add to basket".
This is Code:
body {
font-family: 'Roboto', sans-serif;
}
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
}
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<div class="price">$50</div>
<div class="info">Add to basket</div>
How solve this problem?
You can use a wrapper div to catch the over, I think is more maintainable if you change the html in the future:
body {
font-family: 'Roboto', sans-serif;
}
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
width: 20%;
margin: 20px auto;
cursor: pointer;
}
.wrapper:hover .info {
display: block;
}
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<div class="wrapper">
<div class="price">$50</div>
<div class="info">Add to basket</div>
</div>
you can use :active to show text when mouse is clicking down on the price like so:
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
}
.price:active + .info {
display: block;
}
<div class="price">$50</div>
<div class="info">Add to basket</div>
if you are trying to show the text on :hover like the title of the question says the unser :hover like so:
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
}
.price:hover + .info {
display: block;
}
<div class="price">$50</div>
<div class="info">Add to basket</div>
Update 3:
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info,
.price:hover .amount {
display: none;
}
.price:hover .info {
display: block;
}
<div class="price">
<div class="amount">$50</div>
<div class="info">Add to basket</div>
</div>

vertical alignment - floated element in nav bar

I'm trying to vertically align two left and right floated elements in a nav bar. (Where the 'collapsed' nav bar has had the clearfix hack applied). The height of the navbar is determined by the left floated title (h2, 2em by default). However the right floated element, a form with input and button, doesn't sit centrally even when trying the transform approach to vertically centering items?
If I uncomment the transform vertical alignment approach it just sends form further up (not down and centred).
CodePen(https://codepen.io/yunti/pen/xdvpQK)
https://codepen.io/yunti/pen/xdvpQK
.header {
background-color: darkorange;
}
.header-title {
float: left;
padding-left: 10px;
color: white;
font-weight: 400;
}
.form-header {
float: right;
padding-right: 10px;
/*position: relative;*/
/*top: 50%;*/
/*transform: translateY(-50%);*/
}
.clearfix:after {
content: "";
clear: both;
display: table;
}
input,
button {
vertical-align: middle;
}
.form-control {
margin: 10px;
height: 34px;
width: 180px;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 18px;
color: #555;
}
.form-control::placeholder {
color: grey;
}
.btn {
margin-left: 10px;
height: 34px;
padding-left: 12px;
padding-right: 12px;
line-height: 1.42857143;
white-space: nowrap;
border: 1px solid transparent;
border-radius: 4px;
background-color: forestgreen;
font-size: 14px;
font-weight: 400;
color: white;
cursor: pointer;
}
<div class="header clearfix">
<h1 class="header-title">Weather App</h1>
<div class="form-header">
<form class="form-group">
<input class="form-control" type="text" placeholder="St. George, Utah" />
<button class="btn">Get Weather</button>
</form>
</div>
</div>
Flexbox makes this really easy. Just add display: flex; justify-content: space-between; align-items: center; to the parent, and that will separate the elements in the parent and align them vertically.
.header {
background-color: darkorange;
display: flex;
align-items: center;
justify-content: space-between;
}
.header-title {
padding-left: 10px;
color: white;
font-weight: 400;
}
.form-header {
padding-right: 10px;
}
.form-control {
margin: 10px;
height: 34px;
width: 180px;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 18px;
color: #555;
}
.form-control::placeholder {
color: grey;
}
.btn {
margin-left: 10px;
height: 34px;
padding-left: 12px;
padding-right: 12px;
line-height: 1.42857143;
white-space: nowrap;
border: 1px solid transparent;
border-radius: 4px;
background-color: forestgreen;
font-size: 14px;
font-weight: 400;
color: white;
cursor: pointer;
}
<div class="header">
<h1 class="header-title">Weather App</h1>
<div class="form-header">
<form class="form-group">
<input class="form-control" type="text" placeholder="St. George, Utah" />
<button class="btn">Get Weather</button>
</form>
</div>
</div>
Or if you don't want to use flexbox, you can use display: table on the parent, display: table-cell on the children in combination with vertical-align: middle
.header {
background-color: darkorange;
display: table;
width: 100%;
}
.header-title {
padding-left: 10px;
color: white;
font-weight: 400;
}
.header-title, .form-header {
vertical-align: middle;
display: table-cell;
}
.form-header {
padding-right: 10px;
text-align: right;
}
.form-control {
margin: 10px;
height: 34px;
width: 180px;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 18px;
color: #555;
vertical-align: middle;
}
.form-control::placeholder {
color: grey;
}
.btn {
margin-left: 10px;
height: 34px;
padding-left: 12px;
padding-right: 12px;
line-height: 1.42857143;
white-space: nowrap;
border: 1px solid transparent;
border-radius: 4px;
background-color: forestgreen;
font-size: 14px;
font-weight: 400;
color: white;
cursor: pointer;
vertical-align: middle;
}
<div class="header">
<h1 class="header-title">Weather App</h1>
<div class="form-header">
<form class="form-group">
<input class="form-control" type="text" placeholder="St. George, Utah" />
<button class="btn">Get Weather</button>
</form>
</div>
</div>
You have different amounts of margin on your H1 element ("weather app" -- 22px) and your input/button (10px). Make your margin-top the same on all of these elements.

Padded links are outside of div

I have this JsFiddle, and I can not figure out why the buttons extend outside of the divs.
How can I get them to be inside of the divs?
Here is the CSS
.btn-link {
font-variant: small-caps;
text-decoration: none;
font-size: 1.2em;
padding: 8px 15px 12px 15px;
border-radius: 3px;
background-color: #7dc36b;
color: #ffffff;
}
div.left-nav {
float: left;
width: 200px;
}
div.left-nav > div {
clear: both;
width: 100%;
border: solid 1px red;
}
Here is the HTML:
<div class="left-nav">
<div>new story
</div>
<div>My Stories
</div>
</div>
You can specify display:inline-block; on the buttons, so the div expands to hold the anchors completely.
.btn-link {
font-variant: small-caps;
text-decoration: none;
font-size: 1.2em;
padding: 8px 15px 12px 15px;
border-radius: 3px;
background-color: #7dc36b;
color: #ffffff;
display:inline-block;
}
Fiddle
div.left-nav > div {
overflow: auto; /* removed clear: both; */
width: 100%;
border: solid 1px red;
}
div.left-nav > div > a {
display: inline-block;
}
http://jsfiddle.net/ZjvZu/10/
Hey hope this works for you : -
demo
<div class="left-nav">
<div class="btn">new story
</div>
<div class="btn">My Stories
</div>
CSS:-
.btn-link {
font-variant: small-caps;
text-decoration: none;
font-size: 1.2em;
padding: 8px 15px 8px 15px;
border-radius: 3px;
background-color: #7dc36b;
color: #ffffff;
}
div.left-nav {
float: left;
width: 200px;
}
div.left-nav > div {
clear: both;
width: 100%;
border: solid 1px red;
}
.btn{
padding:8px 15px 8px 15px;
}

Resources