100% Width Minus Margin and Padding

How to make an element width: 100% minus padding?

box-sizing: border-box is a quick, easy way to fix it:

This will work in all modern browsers, and IE8+.

Here's a demo: http://jsfiddle.net/thirtydot/QkmSk/301/

.content {
width: 100%;
box-sizing: border-box;
}

The browser prefixed versions (-webkit-box-sizing, etc.) are not needed in modern browsers.

CSS Table width - 100% + minus margin

You could absolutely position your table, so that it's aligned at the right.

position: absolute;
right: 0;

There's no way to add a left margin without moving the table, because the table offset is calculated in this way: margin + padding + width = offset width.

When you set the width to be 100%, the margin and padding cause the element to expand.

  • Padding (left) X, (right) y + width = over 100%
  • over 100% + negative width (X+y) = 100%.

The first definition adds some padding at each side of the table. The second definition shifts the table to the left, because it's a negative margin.

Align item to full parent's width minus margin

Use nested View. You can try here: https://snack.expo.io/@vasylnahuliak/stackoverflow-67989491

import React, { useState } from 'react';
import { Text, View, StyleSheet } from 'react-native';

const App = () => {
return (
<View style={styles.container}>
<View style={styles.child}>
<Text style={styles.childText}>child</Text>
</View>
</View>
);
};

const styles = StyleSheet.create({
container: {
padding: 24,
backgroundColor: 'tomato',
},
child: {
height: 150,
backgroundColor: 'navy',
},
childText: {
color: 'white',
},
});

export default App;

setting a margin or padding for a 100% height grid without scrollbars

Use padding instead of margin for selector .container.withMargin:

.container.withMargin {
padding: 5px;
}

And add box-sizing: border-box for the .container selector.

function toggle() {
document.querySelector(".container").classList.toggle("withMargin");
}
html,
body,
.container {
height: 100%;
margin: 0;
}

.container {
display: grid;
grid-template-rows: 50px 1fr 1fr;
grid-template-columns: 300px 1fr 1fr;
gap: 5px 5px;
grid-auto-flow: row;
grid-template-areas: "logo header header" "nav-one main main" "nav-two main main";
background-color: black;
box-sizing: border-box;
}

.main {
grid-area: main;
background-color: lightcoral;
}

.logo {
grid-area: logo;
background-color: lightcyan;
}

.header {
grid-area: header;
background-color: lightgoldenrodyellow;
}

.nav-one {
grid-area: nav-one;
background-color: lightgray;
}

.nav-two {
grid-area: nav-two;
background-color: lightgreen;
}

.container.withMargin {
padding: 5px;
}
<!DOCTYPE html>
<html>
<head> </head>
<body>
<div class="container">
<div class="main">
<br />
<button onclick="toggle()">toggle .container margin</button>
</div>
<div class="logo">logo</div>
<div class="header">header</div>
<div class="nav-one">nav one</div>
<div class="nav-two">nav two</div>
</div>
</body>
</html>

Div width 100% minus fixed amount of pixels

You can use nested elements and padding to get a left and right edge on the toolbar. The default width of a div element is auto, which means that it uses the available width. You can then add padding to the element and it still keeps within the available width.

Here is an example that you can use for putting images as left and right rounded corners, and a center image that repeats between them.

The HTML:

<div class="Header">
<div>
<div>This is the dynamic center area</div>
</div>
</div>

The CSS:

.Header {
background: url(left.gif) no-repeat;
padding-left: 30px;
}
.Header div {
background: url(right.gif) top right no-repeat;
padding-right: 30px;
}
.Header div div {
background: url(center.gif) repeat-x;
padding: 0;
height: 30px;
}

html, width 100% and padding

I would use another div to wrap the image and give:

padding:10px; 

insteed of margin.

here is a demo: http://jsfiddle.net/f6jju2mn/



Related Topics



Leave a reply



Submit